Module: Legion::LLM::Inference::AuditPublisher
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/inference/audit_publisher.rb
Class Method Summary collapse
- .audit_max_messages ⇒ Object
- .build_event(request:, response:) ⇒ Object
- .build_message_context(response:) ⇒ Object
- .compact_enrichments(enrichments) ⇒ Object
- .compact_timeline(timeline) ⇒ Object
- .current_turn_messages(messages) ⇒ Object
- .extract_identity(caller) ⇒ Object
- .hash_value(hash, key) ⇒ Object
- .nested_value(hash, *keys) ⇒ Object
- .publish(request:, response:) ⇒ Object
- .serialize_tokens(tokens) ⇒ Object
- .without_provider_payload(audit_data) ⇒ Object
Class Method Details
.audit_max_messages ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 121 def max = Legion::LLM::Settings.value(:compliance, :audit_max_messages) max = max.to_i if max.respond_to?(:to_i) max.is_a?(Integer) && max.positive? ? max : 20 rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.audit_publisher.audit_max_messages') 20 end |
.build_event(request:, response:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 12 def build_event(request:, response:) log.debug("[audit_publisher][build_event] action=build request_id=#{response.request_id} conversation_id=#{response.conversation_id}") = response. msg_content = .is_a?(Types::Message) ? .text : hash_value(, :content) msg_id = .is_a?(Types::Message) ? .id : nil msg_task_id = .is_a?(Types::Message) ? .task_id : nil msg_conversation_id = .is_a?(Types::Message) ? .conversation_id : nil tools_data = Array(response.tools).map do |tc| tc.is_a?(Types::ToolCall) ? tc.to_audit_hash : tc end audit_data = response.audit || {} provider_payload = hash_value(audit_data, :provider_payload) || {} event = { request_id: response.request_id, conversation_id: response.conversation_id, caller: response.caller, identity: extract_identity(response.caller), routing: response.routing, tokens: serialize_tokens(response.tokens), cost: response.cost, system_prompt: hash_value(provider_payload, :system_prompt), injected_tools: hash_value(provider_payload, :injected_tools), enrichments: compact_enrichments(response.enrichments), audit: without_provider_payload(audit_data), timeline: compact_timeline(response.timeline), classification: response.classification, tracing: response.tracing, messages: (request.), response_content: msg_content, tools_used: tools_data, timestamp: Time.now, request_type: request.respond_to?(:request_type) ? request.request_type : 'chat', tier: hash_value(response.routing, :tier), message_context: (request: request, response: response) } event[:message_id] = msg_id if msg_id event[:task_id] = msg_task_id if msg_task_id event[:message_conversation_id] = msg_conversation_id if msg_conversation_id event end |
.build_message_context(response:) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 130 def (response:, **) { request_id: response.request_id, conversation_id: response.conversation_id }.compact end |
.compact_enrichments(enrichments) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 86 def compact_enrichments(enrichments) return {} unless enrichments.is_a?(Hash) enrichments.transform_values do |v| next v unless v.is_a?(Hash) summary = { content: hash_value(v, :content), timestamp: hash_value(v, :timestamp) } data = hash_value(v, :data) next summary unless data.is_a?(Hash) compacted = data.transform_values do |dv| dv.is_a?(Array) && dv.size > 1 ? dv.last : dv end summary.merge(data: compacted) end end |
.compact_timeline(timeline) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 103 def compact_timeline(timeline) return [] unless timeline.is_a?(Array) timeline.select do |event| key = (event[:key] || event['key']).to_s key.start_with?('provider:') || key.start_with?('escalation:') || key.start_with?('tool:execute:') end end |
.current_turn_messages(messages) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 112 def () return unless .is_a?(Array) max = return if .size <= max .last(max) end |
.extract_identity(caller) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 66 def extract_identity(caller) return nil unless caller.is_a?(Hash) rb = caller[:requested_by] || caller['requested_by'] return nil unless rb.is_a?(Hash) { identity: rb[:identity] || rb['identity'], type: rb[:type] || rb['type'], credential: rb[:credential] || rb['credential'] }.compact end |
.hash_value(hash, key) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 151 def hash_value(hash, key) return nil unless hash.respond_to?(:key?) string_key = key.to_s return hash[string_key] if hash.key?(string_key) hash[key] if hash.key?(key) end |
.nested_value(hash, *keys) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 143 def nested_value(hash, *keys) keys.reduce(hash) do |current, key| return nil unless current.respond_to?(:key?) hash_value(current, key) end end |
.publish(request:, response:) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 57 def publish(request:, response:) event = build_event(request: request, response: response) Legion::LLM::Audit.emit_prompt(event) event rescue StandardError => e handle_exception(e, level: :warn) nil end |
.serialize_tokens(tokens) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 79 def serialize_tokens(tokens) return tokens.to_h if tokens.respond_to?(:to_h) && !tokens.is_a?(Hash) return tokens if tokens.is_a?(Hash) {} end |
.without_provider_payload(audit_data) ⇒ Object
137 138 139 140 141 |
# File 'lib/legion/llm/inference/audit_publisher.rb', line 137 def without_provider_payload(audit_data) return {} unless audit_data.is_a?(Hash) audit_data.reject { |key, _| key.to_s == 'provider_payload' } end |