Module: Legion::Extensions::Apollo::Runners::Gas
- Extended by:
- Helpers::Lex
- Includes:
- Helpers::Lex
- Defined in:
- lib/legion/extensions/apollo/runners/gas.rb
Constant Summary collapse
- RELATION_TYPES =
%w[ similar_to contradicts depends_on causes part_of supersedes supports_by extends ].freeze
- RELATE_CONFIDENCE_GATE =
0.7- SYNTHESIS_CONFIDENCE_CAP =
0.7- MAX_ANTICIPATIONS =
3
Class Method Summary collapse
- .build_synthesis_entry(item, facts) ⇒ Object
- .classify_relation(fact, entry) ⇒ Object
- .fallback_confidence ⇒ Object
- .fallback_relation(fact, entry) ⇒ Object
- .fetch_similar_entries(facts) ⇒ Object
- .geometric_mean(values) ⇒ Object
- .json_load(str) ⇒ Object
- .llm_anticipate(facts) ⇒ Object
- .llm_available? ⇒ Boolean
- .llm_classify_relation(fact, entry) ⇒ Object
- .llm_comprehend(messages, response) ⇒ Object
- .llm_synthesize(facts) ⇒ Object
- .max_anticipations ⇒ Object
- .mechanical_comprehend(_messages, response) ⇒ Object
-
.phase_anticipate(facts, _synthesis) ⇒ Object
Phase 6: Anticipate - pre-cache likely follow-up questions.
-
.phase_comprehend(audit_event) ⇒ Object
Phase 1: Comprehend - extract typed facts from the exchange.
-
.phase_deposit(facts, _entities, _relations, _synthesis, audit_event) ⇒ Object
Phase 5: Deposit - atomic write to Apollo.
-
.phase_extract(audit_event, _facts) ⇒ Object
Phase 2: Extract - entity extraction (delegates to existing EntityExtractor).
-
.phase_relate(facts, _entities) ⇒ Object
Phase 3: Relate - classify relationships between new and existing entries.
-
.phase_synthesize(facts, _relations) ⇒ Object
Phase 4: Synthesize - generate derivative knowledge.
- .process(audit_event) ⇒ Object
- .processable?(event) ⇒ Boolean
- .promote_to_pattern_store(question:, facts:) ⇒ Object
- .relate_confidence_gate ⇒ Object
- .similar_entries_limit ⇒ Object
- .synthesis_confidence_cap ⇒ Object
Class Method Details
.build_synthesis_entry(item, facts) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 311 def build_synthesis_entry(item, facts) source_indices = item[:source_indices] || item['source_indices'] || [] source_confs = source_indices.filter_map { |i| facts[i]&.dig(:confidence) } fb = fallback_confidence geo_mean = source_confs.empty? ? fb : geometric_mean(source_confs) { content: item[:content] || item['content'], content_type: (item[:content_type] || item['content_type'] || 'inference').to_sym, status: :candidate, confidence: [geo_mean, synthesis_confidence_cap].min, source_indices: source_indices } end |
.classify_relation(fact, entry) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 199 def classify_relation(fact, entry) fb_conf = fallback_confidence if llm_available? llm_classify_relation(fact, entry) else { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fb_conf } end rescue StandardError => e log.warn("GAS classify_relation failed: #{e.}") { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence } end |
.fallback_confidence ⇒ Object
30 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 30 def fallback_confidence = Helpers::Confidence.apollo_setting(:gas, :fallback_confidence, default: 0.5) |
.fallback_relation(fact, entry) ⇒ Object
261 262 263 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 261 def fallback_relation(fact, entry) { from_content: fact[:content], to_id: entry[:id], relation_type: 'similar_to', confidence: fallback_confidence } end |
.fetch_similar_entries(facts) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 183 def fetch_similar_entries(facts) lim = similar_entries_limit min_conf = Helpers::GraphQuery.default_query_min_confidence entries = [] facts.each do |fact| result = Runners::Knowledge.retrieve_relevant(query: fact[:content], limit: lim, min_confidence: min_conf) entries.concat(result[:entries]) if result[:success] && result[:entries]&.any? rescue StandardError => e log.warn("GAS fetch_similar_entries failed for fact: #{e.}") next end unique = entries.uniq { |e| e[:id] } log.debug("GAS fetch_similar_entries facts=#{facts.size} entries=#{unique.size}") unique end |
.geometric_mean(values) ⇒ Object
326 327 328 329 330 331 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 326 def geometric_mean(values) return 0.0 if values.empty? product = values.reduce(1.0) { |acc, v| acc * v } product**(1.0 / values.length) end |
.json_load(str) ⇒ Object
22 23 24 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 22 def json_load(str) ::JSON.parse(str, symbolize_names: true) end |
.llm_anticipate(facts) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 333 def llm_anticipate(facts) facts_text = facts.map { |f| "(#{f[:content_type]}) #{f[:content]}" }.join("\n") prompt = <<~PROMPT Given these knowledge entries, generate 1-3 likely follow-up questions a user might ask. Knowledge: #{facts_text} Return JSON with a "questions" array of question strings. PROMPT result = Legion::LLM::Pipeline::GaiaCaller.structured( message: prompt.strip, schema: { type: :object, properties: { questions: { type: :array, items: { type: :string } } }, required: ['questions'] }, phase: 'gas_anticipate' ) content = result.respond_to?(:message) ? result.[:content] : result.to_s parsed = json_load(content) questions = parsed.is_a?(Hash) ? (parsed[:questions] || parsed['questions'] || []) : [] questions = questions.first(max_anticipations) questions.map do |q| promote_to_pattern_store(question: q, facts: facts) { question: q } end rescue StandardError => e log.warn("GAS llm_anticipate failed: #{e.}") [] end |
.llm_available? ⇒ Boolean
384 385 386 387 388 389 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 384 def llm_available? defined?(Legion::LLM::Pipeline::GaiaCaller) rescue StandardError => e log.warn("GAS llm_available? check failed: #{e.}") false end |
.llm_classify_relation(fact, entry) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 211 def llm_classify_relation(fact, entry) prompt = <<~PROMPT Classify the relationship between these two knowledge entries. Valid types: #{RELATION_TYPES.join(', ')} Entry A (new): #{fact[:content]} Entry B (existing): #{entry[:content]} Return JSON with relation_type and confidence (0.0-1.0). PROMPT result = Legion::LLM::Pipeline::GaiaCaller.structured( message: prompt.strip, schema: { type: :object, properties: { relations: { type: :array, items: { type: :object, properties: { relation_type: { type: :string }, confidence: { type: :number } }, required: %w[relation_type confidence] } } }, required: ['relations'] }, phase: 'gas_relate' ) content = result.respond_to?(:message) ? result.[:content] : result.to_s parsed = json_load(content) rels = parsed.is_a?(Hash) ? (parsed[:relations] || parsed['relations'] || []) : [] best = rels.max_by { |r| r[:confidence] || r['confidence'] || 0 } return fallback_relation(fact, entry) unless best conf = best[:confidence] || best['confidence'] || 0 rtype = best[:relation_type] || best['relation_type'] return fallback_relation(fact, entry) if conf < relate_confidence_gate || !RELATION_TYPES.include?(rtype) { from_content: fact[:content], to_id: entry[:id], relation_type: rtype, confidence: conf } rescue StandardError => e log.warn("GAS llm_classify_relation failed: #{e.}") fallback_relation(fact, entry) end |
.llm_comprehend(messages, response) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 395 def llm_comprehend(, response) prompt = <<~PROMPT Extract distinct facts from this exchange. Return JSON array of {content:, content_type:} where content_type is one of: fact, concept, procedure, association. User: #{.last&.dig(:content)} Assistant: #{response} PROMPT result = Legion::LLM::Pipeline::GaiaCaller.structured( message: prompt.strip, schema: { type: :object, properties: { facts: { type: :array, items: { type: :object, properties: { content: { type: :string }, content_type: { type: :string } }, required: %w[content content_type] } } }, required: ['facts'] }, phase: 'gas_comprehend' ) content = result.respond_to?(:message) ? result.[:content] : result.to_s parsed = json_load(content) facts_array = parsed.is_a?(Hash) ? (parsed[:facts] || parsed['facts'] || []) : Array(parsed) facts_array.map do |f| { content: f[:content] || f['content'], content_type: (f[:content_type] || f['content_type'] || 'fact').to_sym } end rescue StandardError => e log.warn("GAS llm_comprehend failed: #{e.}") mechanical_comprehend(, response) end |
.llm_synthesize(facts) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 265 def llm_synthesize(facts) facts_text = facts.each_with_index.map { |f, i| "[#{i}] (#{f[:content_type]}) #{f[:content]}" }.join("\n") prompt = <<~PROMPT Given these knowledge entries, generate derivative insights (inferences, implications, or connections). Each synthesis should combine information from multiple sources. Entries: #{facts_text} Return JSON with a "synthesis" array where each item has: content (string), content_type (inference/implication/connection), source_indices (array of entry indices used). PROMPT result = Legion::LLM::Pipeline::GaiaCaller.structured( message: prompt.strip, schema: { type: :object, properties: { synthesis: { type: :array, items: { type: :object, properties: { content: { type: :string }, content_type: { type: :string }, source_indices: { type: :array, items: { type: :integer } } }, required: %w[content content_type source_indices] } } }, required: ['synthesis'] }, phase: 'gas_synthesize' ) content = result.respond_to?(:message) ? result.[:content] : result.to_s parsed = json_load(content) items = parsed.is_a?(Hash) ? (parsed[:synthesis] || parsed['synthesis'] || []) : [] items.map { |item| build_synthesis_entry(item, facts) } rescue StandardError => e log.warn("GAS llm_synthesize failed: #{e.}") [] end |
.max_anticipations ⇒ Object
28 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 28 def max_anticipations = Helpers::Confidence.apollo_setting(:gas, :max_anticipations, default: MAX_ANTICIPATIONS) |
.mechanical_comprehend(_messages, response) ⇒ Object
391 392 393 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 391 def mechanical_comprehend(, response) [{ content: response, content_type: :observation }] end |
.phase_anticipate(facts, _synthesis) ⇒ Object
Phase 6: Anticipate - pre-cache likely follow-up questions
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 165 def phase_anticipate(facts, _synthesis) if facts.empty? log.debug('GAS phase_anticipate skipped reason=no_facts') return [] end unless llm_available? log.debug('GAS phase_anticipate skipped reason=llm_unavailable') return [] end anticipations = llm_anticipate(facts) log.debug("GAS phase_anticipate anticipations=#{anticipations.size}") anticipations rescue StandardError => e log.warn("GAS phase_anticipate failed: #{e.}") [] end |
.phase_comprehend(audit_event) ⇒ Object
Phase 1: Comprehend - extract typed facts from the exchange
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 68 def phase_comprehend(audit_event) = audit_event[:messages] response = audit_event[:response_content] mode = llm_available? ? :llm : :mechanical log.debug("GAS phase_comprehend mode=#{mode} messages=#{Array().size} response_length=#{response.to_s.length}") facts = mode == :llm ? llm_comprehend(, response) : mechanical_comprehend(, response) log.debug("GAS phase_comprehend facts=#{facts.size}") facts end |
.phase_deposit(facts, _entities, _relations, _synthesis, audit_event) ⇒ Object
Phase 5: Deposit - atomic write to Apollo
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 139 def phase_deposit(facts, _entities, _relations, _synthesis, audit_event) unless defined?(Runners::Knowledge) log.debug('GAS phase_deposit skipped reason=knowledge_runner_unavailable') return { deposited: 0 } end deposited = 0 facts.each do |fact| Runners::Knowledge.handle_ingest( content: fact[:content], content_type: fact[:content_type].to_s, tags: %w[gas auto_extracted], source_agent: 'gas_pipeline', source_provider: audit_event.dig(:routing, :provider)&.to_s, knowledge_domain: 'general', context: { source_request_id: audit_event[:request_id] } ) deposited += 1 rescue StandardError => e log.warn("GAS deposit error: #{e.}") end log.info("GAS phase_deposit deposited=#{deposited} facts=#{facts.size}") { deposited: deposited } end |
.phase_extract(audit_event, _facts) ⇒ Object
Phase 2: Extract - entity extraction (delegates to existing EntityExtractor)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 80 def phase_extract(audit_event, _facts) unless defined?(Runners::EntityExtractor) log.debug('GAS phase_extract skipped reason=entity_extractor_unavailable') return [] end result = Runners::EntityExtractor.extract_entities(text: audit_event[:response_content]) entities = result[:success] ? (result[:entities] || []) : [] log.debug("GAS phase_extract success=#{result[:success]} entities=#{entities.size}") entities rescue StandardError => e log.warn("GAS phase_extract failed: #{e.}") [] end |
.phase_relate(facts, _entities) ⇒ Object
Phase 3: Relate - classify relationships between new and existing entries
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 96 def phase_relate(facts, _entities) unless defined?(Runners::Knowledge) log.debug('GAS phase_relate skipped reason=knowledge_runner_unavailable') return [] end existing = fetch_similar_entries(facts) if existing.empty? log.debug("GAS phase_relate skipped reason=no_existing_entries facts=#{facts.size}") return [] end relations = [] facts.each do |fact| existing.each do |entry| relation = classify_relation(fact, entry) relations << relation if relation end end log.debug("GAS phase_relate facts=#{facts.size} existing=#{existing.size} relations=#{relations.size}") relations end |
.phase_synthesize(facts, _relations) ⇒ Object
Phase 4: Synthesize - generate derivative knowledge
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 120 def phase_synthesize(facts, _relations) if facts.length < 2 log.debug("GAS phase_synthesize skipped reason=insufficient_facts facts=#{facts.length}") return [] end unless llm_available? log.debug('GAS phase_synthesize skipped reason=llm_unavailable') return [] end synthesis = llm_synthesize(facts) log.debug("GAS phase_synthesize synthesis=#{synthesis.size}") synthesis rescue StandardError => e log.warn("GAS phase_synthesize failed: #{e.}") [] end |
.process(audit_event) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 32 def process(audit_event) unless processable?(audit_event) log.debug('GAS process skipped reason=no_content') return { phases_completed: 0, reason: 'no content' } end 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 facts = phase_comprehend(audit_event) entities = phase_extract(audit_event, facts) relations = phase_relate(facts, entities) synthesis = phase_synthesize(facts, relations) deposit_result = phase_deposit(facts, entities, relations, synthesis, audit_event) anticipations = phase_anticipate(facts, synthesis) result = { phases_completed: 6, facts: facts.length, entities: entities.length, relations: relations.length, synthesis: synthesis.length, deposited: deposit_result, anticipations: anticipations.length } log.info("GAS process complete facts=#{result[:facts]} entities=#{result[:entities]} relations=#{result[:relations]} synthesis=#{result[:synthesis]} anticipations=#{result[:anticipations]}") # rubocop:disable Layout/LineLength result rescue StandardError => e log.error("GAS pipeline error: #{e.}") { phases_completed: 0, error: e. } end |
.processable?(event) ⇒ Boolean
63 64 65 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 63 def processable?(event) event[:messages]&.any? == true && !event[:response_content].nil? end |
.promote_to_pattern_store(question:, facts:) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 371 def promote_to_pattern_store(question:, facts:) return unless defined?(Legion::Extensions::Agentic::TBI::PatternStore) Legion::Extensions::Agentic::TBI::PatternStore.promote_candidate( intent: question, resolution: { source: 'gas_anticipate', facts: facts.map { |f| f[:content] } }, confidence: fallback_confidence ) rescue StandardError => e log.warn("GAS promote_to_pattern_store failed: #{e.}") nil end |
.relate_confidence_gate ⇒ Object
26 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 26 def relate_confidence_gate = Helpers::Confidence.apollo_setting(:gas, :relate_confidence_gate, default: RELATE_CONFIDENCE_GATE) |
.similar_entries_limit ⇒ Object
29 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 29 def similar_entries_limit = Helpers::Confidence.apollo_setting(:gas, :similar_entries_limit, default: 3) |
.synthesis_confidence_cap ⇒ Object
27 |
# File 'lib/legion/extensions/apollo/runners/gas.rb', line 27 def synthesis_confidence_cap = Helpers::Confidence.apollo_setting(:gas, :synthesis_confidence_cap, default: SYNTHESIS_CONFIDENCE_CAP) |