lex-apollo 0.4.30 → 0.4.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -0
- data/README.md +113 -18
- data/lib/legion/extensions/apollo/actors/contradiction_scanner.rb +2 -38
- data/lib/legion/extensions/apollo/actors/corroboration_checker.rb +1 -1
- data/lib/legion/extensions/apollo/actors/decay.rb +1 -1
- data/lib/legion/extensions/apollo/actors/entity_watchdog.rb +2 -135
- data/lib/legion/extensions/apollo/actors/expertise_aggregator.rb +1 -1
- data/lib/legion/extensions/apollo/actors/gas_subscriber.rb +5 -19
- data/lib/legion/extensions/apollo/actors/ingest.rb +7 -7
- data/lib/legion/extensions/apollo/actors/query_responder.rb +25 -6
- data/lib/legion/extensions/apollo/actors/writeback_store.rb +10 -4
- data/lib/legion/extensions/apollo/actors/writeback_vectorize.rb +9 -35
- data/lib/legion/extensions/apollo/api.rb +116 -51
- data/lib/legion/extensions/apollo/client.rb +20 -9
- data/lib/legion/extensions/apollo/gaia_integration.rb +14 -12
- data/lib/legion/extensions/apollo/helpers/capability.rb +10 -63
- data/lib/legion/extensions/apollo/helpers/confidence.rb +9 -53
- data/lib/legion/extensions/apollo/helpers/data_models.rb +22 -38
- data/lib/legion/extensions/apollo/helpers/embedding.rb +118 -0
- data/lib/legion/extensions/apollo/helpers/graph_query.rb +107 -67
- data/lib/legion/extensions/apollo/helpers/similarity.rb +35 -26
- data/lib/legion/extensions/apollo/helpers/storage.rb +75 -0
- data/lib/legion/extensions/apollo/helpers/tag_normalizer.rb +4 -4
- data/lib/legion/extensions/apollo/helpers/writeback.rb +67 -109
- data/lib/legion/extensions/apollo/runners/contradictions.rb +126 -0
- data/lib/legion/extensions/apollo/runners/embedding.rb +60 -0
- data/lib/legion/extensions/apollo/runners/entity_extractor.rb +18 -10
- data/lib/legion/extensions/apollo/runners/entity_watchdog.rb +110 -0
- data/lib/legion/extensions/apollo/runners/expertise.rb +58 -29
- data/lib/legion/extensions/apollo/runners/gas.rb +165 -144
- data/lib/legion/extensions/apollo/runners/knowledge.rb +349 -371
- data/lib/legion/extensions/apollo/runners/maintenance.rb +242 -56
- data/lib/legion/extensions/apollo/runners/query_response.rb +108 -0
- data/lib/legion/extensions/apollo/runners/request.rb +51 -47
- data/lib/legion/extensions/apollo/runners/search.rb +88 -0
- data/lib/legion/extensions/apollo/runners/writeback.rb +57 -0
- data/lib/legion/extensions/apollo/transport/messages/ingest.rb +13 -9
- data/lib/legion/extensions/apollo/transport/messages/query.rb +4 -14
- data/lib/legion/extensions/apollo/transport/messages/writeback.rb +36 -14
- data/lib/legion/extensions/apollo/transport/subscription_delivery.rb +40 -0
- data/lib/legion/extensions/apollo/transport.rb +20 -0
- data/lib/legion/extensions/apollo/version.rb +1 -1
- data/lib/legion/extensions/apollo.rb +26 -24
- data/spec/legion/extensions/apollo/actors/ingest_spec.rb +18 -21
- data/spec/legion/extensions/apollo/actors/query_responder_spec.rb +59 -0
- data/spec/legion/extensions/apollo/api_spec.rb +250 -28
- data/spec/legion/extensions/apollo/contradiction_spec.rb +23 -54
- data/spec/legion/extensions/apollo/gaia_integration_spec.rb +10 -21
- data/spec/legion/extensions/apollo/helpers/capability_spec.rb +26 -29
- data/spec/legion/extensions/apollo/helpers/confidence_spec.rb +16 -101
- data/spec/legion/extensions/apollo/helpers/embedding_spec.rb +33 -0
- data/spec/legion/extensions/apollo/helpers/graph_query_spec.rb +88 -80
- data/spec/legion/extensions/apollo/helpers/similarity_spec.rb +29 -12
- data/spec/legion/extensions/apollo/helpers/storage_spec.rb +59 -0
- data/spec/legion/extensions/apollo/helpers/tag_normalizer_spec.rb +20 -18
- data/spec/legion/extensions/apollo/helpers/writeback_spec.rb +98 -71
- data/spec/legion/extensions/apollo/runners/corroboration_integration_spec.rb +220 -0
- data/spec/legion/extensions/apollo/runners/decay_cycle_spec.rb +17 -10
- data/spec/legion/extensions/apollo/runners/embedding_spec.rb +99 -0
- data/spec/legion/extensions/apollo/runners/entity_extractor_spec.rb +32 -99
- data/spec/legion/extensions/apollo/runners/entity_watchdog_spec.rb +124 -0
- data/spec/legion/extensions/apollo/runners/expertise_spec.rb +75 -79
- data/spec/legion/extensions/apollo/runners/gas_anticipate_spec.rb +15 -85
- data/spec/legion/extensions/apollo/runners/gas_deposit_integration_spec.rb +108 -0
- data/spec/legion/extensions/apollo/runners/gas_relate_spec.rb +45 -87
- data/spec/legion/extensions/apollo/runners/gas_spec.rb +51 -83
- data/spec/legion/extensions/apollo/runners/gas_synthesize_spec.rb +17 -84
- data/spec/legion/extensions/apollo/runners/ingest_transaction_integration_spec.rb +246 -0
- data/spec/legion/extensions/apollo/runners/knowledge_scope_integration_spec.rb +130 -0
- data/spec/legion/extensions/apollo/runners/knowledge_spec.rb +332 -683
- data/spec/legion/extensions/apollo/runners/lifecycle_integration_spec.rb +174 -0
- data/spec/legion/extensions/apollo/runners/maintenance_spec.rb +42 -160
- data/spec/legion/extensions/apollo/runners/query_response_spec.rb +161 -0
- data/spec/legion/extensions/apollo/runners/reembedding_integration_spec.rb +227 -0
- data/spec/legion/extensions/apollo/runners/request_spec.rb +146 -107
- data/spec/legion/extensions/apollo/runners/search_spec.rb +155 -0
- data/spec/legion/extensions/apollo/runners/writeback_spec.rb +93 -0
- data/spec/legion/extensions/apollo/transport/messages/ingest_spec.rb +26 -51
- data/spec/legion/extensions/apollo/transport/messages/query_spec.rb +20 -61
- data/spec/legion/extensions/apollo/transport/messages/writeback_spec.rb +126 -47
- data/spec/legion/extensions/apollo/transport/subscription_delivery_spec.rb +41 -0
- data/spec/spec_helper.rb +13 -15
- metadata +71 -13
- data/lib/legion/extensions/apollo/helpers/entity_watchdog.rb +0 -91
- data/spec/legion/extensions/apollo/actors/decay_spec.rb +0 -45
- data/spec/legion/extensions/apollo/actors/entity_watchdog_spec.rb +0 -134
- data/spec/legion/extensions/apollo/actors/expertise_aggregator_spec.rb +0 -41
- data/spec/legion/extensions/apollo/actors/gas_subscriber_spec.rb +0 -49
- data/spec/legion/extensions/apollo/actors/writeback_store_spec.rb +0 -42
- data/spec/legion/extensions/apollo/actors/writeback_vectorize_spec.rb +0 -102
- data/spec/legion/extensions/apollo/helpers/entity_watchdog_spec.rb +0 -64
|
@@ -1,54 +1,47 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'legion/llm/inference'
|
|
4
|
+
require 'legion/logging/helper'
|
|
5
|
+
require 'legion/settings/helper'
|
|
6
|
+
require 'legion/json'
|
|
7
|
+
require 'legion/extensions/apollo/helpers/data_models'
|
|
8
|
+
require 'legion/extensions/apollo/runners/entity_extractor'
|
|
9
|
+
require 'legion/extensions/apollo/runners/knowledge'
|
|
10
|
+
|
|
3
11
|
module Legion
|
|
4
12
|
module Extensions
|
|
5
13
|
module Apollo
|
|
6
14
|
module Runners
|
|
7
15
|
module Gas
|
|
8
|
-
include Legion::Extensions::Helpers::Lex if defined?(Legion::Extensions::Helpers::Lex)
|
|
9
16
|
include Legion::Logging::Helper
|
|
10
17
|
include Legion::JSON::Helper
|
|
11
|
-
|
|
12
|
-
extend Legion::
|
|
13
|
-
extend Legion::JSON::Helper
|
|
18
|
+
include Legion::Settings::Helper
|
|
19
|
+
extend Legion::Extensions::Apollo::Helpers::DataModels
|
|
14
20
|
|
|
15
21
|
RELATION_TYPES = %w[
|
|
16
22
|
similar_to contradicts depends_on causes
|
|
17
23
|
part_of supersedes supports_by extends
|
|
18
24
|
].freeze
|
|
19
25
|
|
|
20
|
-
|
|
21
|
-
SYNTHESIS_CONFIDENCE_CAP = 0.7
|
|
22
|
-
MAX_ANTICIPATIONS = 3
|
|
23
|
-
|
|
24
|
-
module_function
|
|
25
|
-
|
|
26
|
-
def json_load(str)
|
|
27
|
-
json_parse(str)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def relate_confidence_gate = Legion::Extensions::Apollo.settings[:gas][:relate_confidence_gate]
|
|
31
|
-
def synthesis_confidence_cap = Legion::Extensions::Apollo.settings[:gas][:synthesis_confidence_cap]
|
|
32
|
-
def max_anticipations = Legion::Extensions::Apollo.settings[:gas][:max_anticipations]
|
|
33
|
-
def similar_entries_limit = Legion::Extensions::Apollo.settings[:gas][:similar_entries_limit]
|
|
34
|
-
def fallback_confidence = Legion::Extensions::Apollo.settings[:gas][:fallback_confidence]
|
|
26
|
+
extend self
|
|
35
27
|
|
|
36
|
-
def process(audit_event)
|
|
37
|
-
unless
|
|
28
|
+
def process(**audit_event)
|
|
29
|
+
unless gas_processable?(audit_event)
|
|
38
30
|
log.debug('GAS process skipped reason=no_content')
|
|
39
|
-
return { phases_completed: 0, reason: 'no content' }
|
|
31
|
+
return { success: true, phases_completed: 0, reason: 'no content' }
|
|
40
32
|
end
|
|
41
33
|
|
|
42
34
|
log.debug("GAS process start request_id=#{audit_event[:request_id] || 'nil'} messages=#{Array(audit_event[:messages]).size} response_length=#{audit_event[:response_content].to_s.length}") # rubocop:disable Layout/LineLength
|
|
43
35
|
|
|
44
|
-
facts =
|
|
45
|
-
entities =
|
|
46
|
-
relations =
|
|
47
|
-
synthesis =
|
|
48
|
-
deposit_result =
|
|
49
|
-
anticipations =
|
|
36
|
+
facts = gas_phase_comprehend(audit_event)
|
|
37
|
+
entities = gas_phase_extract(audit_event, facts)
|
|
38
|
+
relations = gas_phase_relate(facts: facts)
|
|
39
|
+
synthesis = gas_phase_synthesize(facts, relations)
|
|
40
|
+
deposit_result = gas_phase_deposit(facts: facts, relations: relations, synthesis: synthesis, audit_event: audit_event)
|
|
41
|
+
anticipations = gas_phase_anticipate(facts, synthesis)
|
|
50
42
|
|
|
51
43
|
result = {
|
|
44
|
+
success: deposit_result[:failed].zero? && deposit_result[:relations_failed].zero?,
|
|
52
45
|
phases_completed: 6,
|
|
53
46
|
facts: facts.length,
|
|
54
47
|
entities: entities.length,
|
|
@@ -61,49 +54,40 @@ module Legion
|
|
|
61
54
|
result
|
|
62
55
|
rescue StandardError => e
|
|
63
56
|
handle_exception(e, level: :error, operation: 'apollo.gas.process')
|
|
64
|
-
{ phases_completed: 0, error: e.message }
|
|
57
|
+
{ success: false, phases_completed: 0, error: e.message }
|
|
65
58
|
end
|
|
66
59
|
|
|
67
|
-
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def gas_processable?(event)
|
|
68
63
|
event[:messages]&.any? == true && !event[:response_content].nil?
|
|
69
64
|
end
|
|
70
65
|
|
|
71
66
|
# Phase 1: Comprehend - extract typed facts from the exchange
|
|
72
|
-
def
|
|
67
|
+
def gas_phase_comprehend(audit_event)
|
|
73
68
|
messages = audit_event[:messages]
|
|
74
69
|
response = audit_event[:response_content]
|
|
75
70
|
|
|
76
|
-
mode =
|
|
71
|
+
mode = Legion::LLM.started? ? :llm : :mechanical
|
|
77
72
|
log.debug("GAS phase_comprehend mode=#{mode} messages=#{Array(messages).size} response_length=#{response.to_s.length}")
|
|
78
|
-
facts = mode == :llm ?
|
|
73
|
+
facts = mode == :llm ? gas_llm_comprehend(messages, response) : gas_mechanical_comprehend(messages, response)
|
|
79
74
|
log.debug("GAS phase_comprehend facts=#{facts.size}")
|
|
80
75
|
facts
|
|
81
76
|
end
|
|
82
77
|
|
|
83
78
|
# Phase 2: Extract - entity extraction (delegates to existing EntityExtractor)
|
|
84
|
-
def
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return []
|
|
88
|
-
end
|
|
79
|
+
def gas_phase_extract(audit_event, _facts)
|
|
80
|
+
result = Legion::Extensions::Apollo::Runners::EntityExtractor.extract_entities(text: audit_event[:response_content])
|
|
81
|
+
raise IOError, "Apollo entity extraction failed: #{result[:error]}" unless result[:success]
|
|
89
82
|
|
|
90
|
-
|
|
91
|
-
entities = result[:success] ? (result[:entities] || []) : []
|
|
83
|
+
entities = result[:entities]
|
|
92
84
|
log.debug("GAS phase_extract success=#{result[:success]} entities=#{entities.size}")
|
|
93
85
|
entities
|
|
94
|
-
rescue StandardError => e
|
|
95
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.phase_extract')
|
|
96
|
-
[]
|
|
97
86
|
end
|
|
98
87
|
|
|
99
88
|
# Phase 3: Relate - classify relationships between new and existing entries
|
|
100
|
-
def
|
|
101
|
-
|
|
102
|
-
log.debug('GAS phase_relate skipped reason=knowledge_runner_unavailable')
|
|
103
|
-
return []
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
existing = fetch_similar_entries(facts)
|
|
89
|
+
def gas_phase_relate(facts:, **)
|
|
90
|
+
existing = gas_fetch_similar_entries(facts: facts)
|
|
107
91
|
if existing.empty?
|
|
108
92
|
log.debug("GAS phase_relate skipped reason=no_existing_entries facts=#{facts.size}")
|
|
109
93
|
return []
|
|
@@ -112,7 +96,7 @@ module Legion
|
|
|
112
96
|
relations = []
|
|
113
97
|
facts.each do |fact|
|
|
114
98
|
existing.each do |entry|
|
|
115
|
-
relation =
|
|
99
|
+
relation = gas_classify_relation(fact, entry)
|
|
116
100
|
relations << relation if relation
|
|
117
101
|
end
|
|
118
102
|
end
|
|
@@ -121,98 +105,154 @@ module Legion
|
|
|
121
105
|
end
|
|
122
106
|
|
|
123
107
|
# Phase 4: Synthesize - generate derivative knowledge
|
|
124
|
-
def
|
|
108
|
+
def gas_phase_synthesize(facts, _relations)
|
|
125
109
|
if facts.length < 2
|
|
126
110
|
log.debug("GAS phase_synthesize skipped reason=insufficient_facts facts=#{facts.length}")
|
|
127
111
|
return []
|
|
128
112
|
end
|
|
129
|
-
unless
|
|
113
|
+
unless Legion::LLM.started?
|
|
130
114
|
log.debug('GAS phase_synthesize skipped reason=llm_unavailable')
|
|
131
115
|
return []
|
|
132
116
|
end
|
|
133
117
|
|
|
134
|
-
synthesis =
|
|
118
|
+
synthesis = gas_llm_synthesize(facts)
|
|
135
119
|
log.debug("GAS phase_synthesize synthesis=#{synthesis.size}")
|
|
136
120
|
synthesis
|
|
137
|
-
rescue StandardError => e
|
|
138
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.phase_synthesize')
|
|
139
|
-
[]
|
|
140
121
|
end
|
|
141
122
|
|
|
142
|
-
# Phase 5: Deposit
|
|
143
|
-
def
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
123
|
+
# Phase 5: Deposit entries, then link successfully stored facts.
|
|
124
|
+
def gas_phase_deposit(facts:, relations:, synthesis:, audit_event:, **)
|
|
125
|
+
counts = { deposited: 0, failed: 0, facts: 0, synthesis: 0 }
|
|
126
|
+
fact_ids = Array.new(facts.size)
|
|
127
|
+
source_ids = {}
|
|
128
|
+
{ facts: facts, synthesis: synthesis }.each do |kind, entries|
|
|
129
|
+
entries.each_with_index do |item, index|
|
|
130
|
+
context = { source_request_id: audit_event[:request_id] }
|
|
131
|
+
if kind == :synthesis
|
|
132
|
+
context[:source_indices] = Array(item[:source_indices])
|
|
133
|
+
context[:source_entry_ids] = context[:source_indices].filter_map do |source_index|
|
|
134
|
+
fact_ids[source_index] if source_index.is_a?(Integer) && source_index.between?(0, facts.size - 1)
|
|
135
|
+
end.uniq
|
|
136
|
+
end
|
|
137
|
+
result = Legion::Extensions::Apollo::Runners::Knowledge.handle_ingest(
|
|
138
|
+
content: item[:content], content_type: item[:content_type].to_s,
|
|
139
|
+
tags: %w[gas auto_extracted], source_agent: 'gas_pipeline',
|
|
140
|
+
source_provider: audit_event.dig(:routing, :provider),
|
|
141
|
+
knowledge_domain: 'general', context: context
|
|
142
|
+
)
|
|
143
|
+
unless result.is_a?(Hash) && result[:success] == true && result[:entry_id]
|
|
144
|
+
counts[:failed] += 1
|
|
145
|
+
next
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
counts[:deposited] += 1
|
|
149
|
+
counts[kind] += 1
|
|
150
|
+
next unless kind == :facts
|
|
151
|
+
|
|
152
|
+
fact_ids[index] = result[:entry_id]
|
|
153
|
+
ids = (source_ids[item[:content]] ||= [])
|
|
154
|
+
ids << result[:entry_id] unless ids.include?(result[:entry_id])
|
|
155
|
+
rescue StandardError => e
|
|
156
|
+
counts[:failed] += 1
|
|
157
|
+
handle_exception(e, level: :warn, operation: 'apollo.gas.phase_deposit_entry')
|
|
158
|
+
end
|
|
147
159
|
end
|
|
160
|
+
counts.merge!(gas_deposit_relations(relations: relations, source_ids: source_ids))
|
|
161
|
+
log.info("GAS phase_deposit deposited=#{counts[:deposited]} failed=#{counts[:failed]} relations=#{counts[:relations]} relations_failed=#{counts[:relations_failed]}") # rubocop:disable Layout/LineLength
|
|
162
|
+
counts
|
|
163
|
+
end
|
|
148
164
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
165
|
+
def gas_deposit_relations(relations:, source_ids:)
|
|
166
|
+
counts = { relations: 0, relations_failed: 0, relations_skipped: 0 }
|
|
167
|
+
return counts if relations.empty?
|
|
168
|
+
unless !data_model_entry.nil? &&
|
|
169
|
+
!data_model_relation.nil?
|
|
170
|
+
return counts.merge(relations_failed: relations.size)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
entry_model = data_model_entry
|
|
174
|
+
relation_model = data_model_relation
|
|
175
|
+
seen = {}
|
|
176
|
+
relations.each do |relation|
|
|
177
|
+
type = relation[:relation_type].to_s
|
|
178
|
+
weight = relation[:confidence]
|
|
179
|
+
unless gas_relation_accepted?(type: type, confidence: weight)
|
|
180
|
+
counts[:relations_skipped] += 1
|
|
181
|
+
next
|
|
182
|
+
end
|
|
183
|
+
ids = source_ids[relation[:from_content]]
|
|
184
|
+
source = entry_model[ids.first] if ids&.one?
|
|
185
|
+
target = entry_model[relation[:to_id]] if relation[:to_id]
|
|
186
|
+
unless source && target
|
|
187
|
+
counts[:relations_failed] += 1
|
|
188
|
+
next
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
key = [source.id, target.id, type]
|
|
192
|
+
if source.id == target.id || source[:access_scope] != target[:access_scope] || seen[key]
|
|
193
|
+
counts[:relations_skipped] += 1
|
|
194
|
+
next
|
|
195
|
+
end
|
|
196
|
+
seen[key] = true
|
|
197
|
+
relation_model.create(from_entry_id: source.id, to_entry_id: target.id, relation_type: type,
|
|
198
|
+
weight: weight, source_agent: 'gas_pipeline')
|
|
199
|
+
counts[:relations] += 1
|
|
161
200
|
rescue StandardError => e
|
|
162
|
-
|
|
201
|
+
counts[:relations_failed] += 1
|
|
202
|
+
handle_exception(e, level: :warn, operation: 'apollo.gas.phase_deposit_relation')
|
|
163
203
|
end
|
|
164
|
-
|
|
165
|
-
|
|
204
|
+
counts
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def gas_relation_accepted?(type:, confidence:)
|
|
208
|
+
Legion::Extensions::Apollo::Helpers::Confidence::RELATION_TYPES.include?(type) &&
|
|
209
|
+
confidence.is_a?(Numeric) && confidence.real? && confidence.finite? &&
|
|
210
|
+
confidence.between?(settings[:gas][:relate_confidence_gate], 1.0)
|
|
166
211
|
end
|
|
167
212
|
|
|
168
213
|
# Phase 6: Anticipate - pre-cache likely follow-up questions
|
|
169
|
-
def
|
|
214
|
+
def gas_phase_anticipate(facts, _synthesis)
|
|
170
215
|
if facts.empty?
|
|
171
216
|
log.debug('GAS phase_anticipate skipped reason=no_facts')
|
|
172
217
|
return []
|
|
173
218
|
end
|
|
174
|
-
unless
|
|
219
|
+
unless Legion::LLM.started?
|
|
175
220
|
log.debug('GAS phase_anticipate skipped reason=llm_unavailable')
|
|
176
221
|
return []
|
|
177
222
|
end
|
|
178
223
|
|
|
179
|
-
anticipations =
|
|
224
|
+
anticipations = gas_llm_anticipate(facts)
|
|
180
225
|
log.debug("GAS phase_anticipate anticipations=#{anticipations.size}")
|
|
181
226
|
anticipations
|
|
182
|
-
rescue StandardError => e
|
|
183
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.phase_anticipate')
|
|
184
|
-
[]
|
|
185
227
|
end
|
|
186
228
|
|
|
187
|
-
def
|
|
188
|
-
lim = similar_entries_limit
|
|
189
|
-
min_conf =
|
|
229
|
+
def gas_fetch_similar_entries(facts:, **)
|
|
230
|
+
lim = settings[:gas][:similar_entries_limit]
|
|
231
|
+
min_conf = settings[:query][:default_min_confidence]
|
|
190
232
|
entries = []
|
|
191
233
|
facts.each do |fact|
|
|
192
|
-
result = Runners::Knowledge.retrieve_relevant(
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
234
|
+
result = Legion::Extensions::Apollo::Runners::Knowledge.retrieve_relevant(
|
|
235
|
+
query: fact[:content], limit: lim, min_confidence: min_conf
|
|
236
|
+
)
|
|
237
|
+
raise IOError, "Apollo semantic retrieval failed: #{result[:error]}" unless result[:success]
|
|
238
|
+
|
|
239
|
+
entries.concat(result[:entries])
|
|
197
240
|
end
|
|
198
241
|
unique = entries.uniq { |e| e[:id] }
|
|
199
242
|
log.debug("GAS fetch_similar_entries facts=#{facts.size} entries=#{unique.size}")
|
|
200
243
|
unique
|
|
201
244
|
end
|
|
202
245
|
|
|
203
|
-
def
|
|
204
|
-
fb_conf = fallback_confidence
|
|
205
|
-
if
|
|
206
|
-
|
|
246
|
+
def gas_classify_relation(fact, entry)
|
|
247
|
+
fb_conf = settings[:gas][:fallback_confidence]
|
|
248
|
+
if Legion::LLM.started?
|
|
249
|
+
gas_llm_classify_relation(fact, entry)
|
|
207
250
|
else
|
|
208
251
|
{ from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fb_conf }
|
|
209
252
|
end
|
|
210
|
-
rescue StandardError => e
|
|
211
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.classify_relation')
|
|
212
|
-
{ from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence }
|
|
213
253
|
end
|
|
214
254
|
|
|
215
|
-
def
|
|
255
|
+
def gas_llm_classify_relation(fact, entry) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
216
256
|
prompt = <<~PROMPT
|
|
217
257
|
Classify the relationship between these two knowledge entries.
|
|
218
258
|
Valid types: #{RELATION_TYPES.join(', ')}
|
|
@@ -223,7 +263,7 @@ module Legion
|
|
|
223
263
|
Return JSON with relation_type and confidence (0.0-1.0).
|
|
224
264
|
PROMPT
|
|
225
265
|
|
|
226
|
-
result = Legion::LLM::
|
|
266
|
+
result = Legion::LLM::Inference::GaiaCaller.structured(
|
|
227
267
|
message: prompt.strip,
|
|
228
268
|
schema: {
|
|
229
269
|
type: :object,
|
|
@@ -249,28 +289,25 @@ module Legion
|
|
|
249
289
|
result[:data][:relations] || result.dig(:data, 'relations') || []
|
|
250
290
|
else
|
|
251
291
|
content = result.respond_to?(:message) ? result.message[:content] : result.to_s
|
|
252
|
-
parsed =
|
|
292
|
+
parsed = json_parse(content)
|
|
253
293
|
parsed.is_a?(Hash) ? (parsed[:relations] || parsed['relations'] || []) : []
|
|
254
294
|
end
|
|
255
295
|
best = rels.max_by { |r| r[:confidence] || r['confidence'] || 0 }
|
|
256
296
|
|
|
257
|
-
return
|
|
297
|
+
return gas_fallback_relation(fact, entry) unless best
|
|
258
298
|
|
|
259
299
|
conf = best[:confidence] || best['confidence'] || 0
|
|
260
300
|
rtype = best[:relation_type] || best['relation_type']
|
|
261
|
-
return
|
|
301
|
+
return gas_fallback_relation(fact, entry) if conf < settings[:gas][:relate_confidence_gate] || !RELATION_TYPES.include?(rtype)
|
|
262
302
|
|
|
263
303
|
{ from_content: fact[:content], to_id: entry[:id], relation_type: rtype, confidence: conf }
|
|
264
|
-
rescue StandardError => e
|
|
265
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.llm_classify_relation')
|
|
266
|
-
fallback_relation(fact, entry)
|
|
267
304
|
end
|
|
268
305
|
|
|
269
|
-
def
|
|
270
|
-
{ from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence }
|
|
306
|
+
def gas_fallback_relation(fact, entry)
|
|
307
|
+
{ from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: settings[:gas][:fallback_confidence] }
|
|
271
308
|
end
|
|
272
309
|
|
|
273
|
-
def
|
|
310
|
+
def gas_llm_synthesize(facts)
|
|
274
311
|
facts_text = facts.each_with_index.map { |f, i| "[#{i}] (#{f[:content_type]}) #{f[:content]}" }.join("\n")
|
|
275
312
|
|
|
276
313
|
prompt = <<~PROMPT
|
|
@@ -283,7 +320,7 @@ module Legion
|
|
|
283
320
|
Return JSON with a "synthesis" array where each item has: content (string), content_type (inference/implication/connection), source_indices (array of entry indices used).
|
|
284
321
|
PROMPT
|
|
285
322
|
|
|
286
|
-
result = Legion::LLM::
|
|
323
|
+
result = Legion::LLM::Inference::GaiaCaller.structured(
|
|
287
324
|
message: prompt.strip,
|
|
288
325
|
schema: {
|
|
289
326
|
type: :object,
|
|
@@ -310,39 +347,36 @@ module Legion
|
|
|
310
347
|
result[:data][:synthesis] || result.dig(:data, 'synthesis') || []
|
|
311
348
|
else
|
|
312
349
|
content = result.respond_to?(:message) ? result.message[:content] : result.to_s
|
|
313
|
-
parsed =
|
|
350
|
+
parsed = json_parse(content)
|
|
314
351
|
parsed.is_a?(Hash) ? (parsed[:synthesis] || parsed['synthesis'] || []) : []
|
|
315
352
|
end
|
|
316
353
|
|
|
317
|
-
items.map { |item|
|
|
318
|
-
rescue StandardError => e
|
|
319
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.llm_synthesize')
|
|
320
|
-
[]
|
|
354
|
+
items.map { |item| gas_build_synthesis_entry(item, facts) }
|
|
321
355
|
end
|
|
322
356
|
|
|
323
|
-
def
|
|
357
|
+
def gas_build_synthesis_entry(item, facts)
|
|
324
358
|
source_indices = item[:source_indices] || item['source_indices'] || []
|
|
325
359
|
source_confs = source_indices.filter_map { |i| facts[i]&.dig(:confidence) }
|
|
326
|
-
fb = fallback_confidence
|
|
327
|
-
geo_mean = source_confs.empty? ? fb :
|
|
360
|
+
fb = settings[:gas][:fallback_confidence]
|
|
361
|
+
geo_mean = source_confs.empty? ? fb : gas_geometric_mean(source_confs)
|
|
328
362
|
|
|
329
363
|
{
|
|
330
364
|
content: item[:content] || item['content'],
|
|
331
365
|
content_type: (item[:content_type] || item['content_type'] || 'inference').to_sym,
|
|
332
366
|
status: :candidate,
|
|
333
|
-
confidence: [geo_mean, synthesis_confidence_cap].min,
|
|
367
|
+
confidence: [geo_mean, settings[:gas][:synthesis_confidence_cap]].min,
|
|
334
368
|
source_indices: source_indices
|
|
335
369
|
}
|
|
336
370
|
end
|
|
337
371
|
|
|
338
|
-
def
|
|
372
|
+
def gas_geometric_mean(values)
|
|
339
373
|
return 0.0 if values.empty?
|
|
340
374
|
|
|
341
375
|
product = values.reduce(1.0) { |acc, v| acc * v }
|
|
342
376
|
product**(1.0 / values.length)
|
|
343
377
|
end
|
|
344
378
|
|
|
345
|
-
def
|
|
379
|
+
def gas_llm_anticipate(facts)
|
|
346
380
|
facts_text = facts.map { |f| "(#{f[:content_type]}) #{f[:content]}" }.join("\n")
|
|
347
381
|
|
|
348
382
|
prompt = <<~PROMPT
|
|
@@ -354,7 +388,7 @@ module Legion
|
|
|
354
388
|
Return JSON with a "questions" array of question strings.
|
|
355
389
|
PROMPT
|
|
356
390
|
|
|
357
|
-
result = Legion::LLM::
|
|
391
|
+
result = Legion::LLM::Inference::GaiaCaller.structured(
|
|
358
392
|
message: prompt.strip,
|
|
359
393
|
schema: {
|
|
360
394
|
type: :object,
|
|
@@ -370,45 +404,35 @@ module Legion
|
|
|
370
404
|
result[:data][:questions] || result.dig(:data, 'questions') || []
|
|
371
405
|
else
|
|
372
406
|
content = result.respond_to?(:message) ? result.message[:content] : result.to_s
|
|
373
|
-
parsed =
|
|
407
|
+
parsed = json_parse(content)
|
|
374
408
|
parsed.is_a?(Hash) ? (parsed[:questions] || parsed['questions'] || []) : []
|
|
375
409
|
end
|
|
376
|
-
questions = questions.first(max_anticipations)
|
|
410
|
+
questions = questions.first(settings[:gas][:max_anticipations])
|
|
377
411
|
|
|
378
412
|
questions.map do |q|
|
|
379
|
-
|
|
413
|
+
gas_promote_to_pattern_store(question: q, facts: facts)
|
|
380
414
|
{ question: q }
|
|
381
415
|
end
|
|
382
|
-
rescue StandardError => e
|
|
383
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.llm_anticipate')
|
|
384
|
-
[]
|
|
385
416
|
end
|
|
386
417
|
|
|
387
|
-
def
|
|
418
|
+
def gas_promote_to_pattern_store(question:, facts:)
|
|
388
419
|
return unless defined?(Legion::Extensions::Agentic::TBI::PatternStore)
|
|
389
420
|
|
|
390
421
|
Legion::Extensions::Agentic::TBI::PatternStore.promote_candidate(
|
|
391
422
|
intent: question,
|
|
392
423
|
resolution: { source: 'gas_anticipate', facts: facts.map { |f| f[:content] } },
|
|
393
|
-
confidence: fallback_confidence
|
|
424
|
+
confidence: settings[:gas][:fallback_confidence]
|
|
394
425
|
)
|
|
395
426
|
rescue StandardError => e
|
|
396
427
|
handle_exception(e, level: :warn, operation: 'apollo.gas.promote_to_pattern_store')
|
|
397
428
|
nil
|
|
398
429
|
end
|
|
399
430
|
|
|
400
|
-
def
|
|
401
|
-
defined?(Legion::LLM::Pipeline::GaiaCaller)
|
|
402
|
-
rescue StandardError => e
|
|
403
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.llm_available')
|
|
404
|
-
false
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
def mechanical_comprehend(_messages, response)
|
|
431
|
+
def gas_mechanical_comprehend(_messages, response)
|
|
408
432
|
[{ content: response, content_type: :observation }]
|
|
409
433
|
end
|
|
410
434
|
|
|
411
|
-
def
|
|
435
|
+
def gas_llm_comprehend(messages, response)
|
|
412
436
|
prompt = <<~PROMPT
|
|
413
437
|
Extract distinct facts from this exchange. Return JSON array of {content:, content_type:} where content_type is one of: fact, concept, procedure, association.
|
|
414
438
|
|
|
@@ -416,7 +440,7 @@ module Legion
|
|
|
416
440
|
Assistant: #{response}
|
|
417
441
|
PROMPT
|
|
418
442
|
|
|
419
|
-
result = Legion::LLM::
|
|
443
|
+
result = Legion::LLM::Inference::GaiaCaller.structured(
|
|
420
444
|
message: prompt.strip,
|
|
421
445
|
schema: {
|
|
422
446
|
type: :object,
|
|
@@ -442,7 +466,7 @@ module Legion
|
|
|
442
466
|
result[:data][:facts] || result.dig(:data, 'facts') || []
|
|
443
467
|
else
|
|
444
468
|
content = result.respond_to?(:message) ? result.message[:content] : result.to_s
|
|
445
|
-
parsed =
|
|
469
|
+
parsed = json_parse(content)
|
|
446
470
|
parsed.is_a?(Hash) ? (parsed[:facts] || parsed['facts'] || []) : Array(parsed)
|
|
447
471
|
end
|
|
448
472
|
facts_array.map do |f|
|
|
@@ -451,9 +475,6 @@ module Legion
|
|
|
451
475
|
content_type: (f[:content_type] || f['content_type'] || 'fact').to_sym
|
|
452
476
|
}
|
|
453
477
|
end
|
|
454
|
-
rescue StandardError => e
|
|
455
|
-
handle_exception(e, level: :warn, operation: 'apollo.gas.llm_comprehend')
|
|
456
|
-
mechanical_comprehend(messages, response)
|
|
457
478
|
end
|
|
458
479
|
end
|
|
459
480
|
end
|