Module: Legion::LLM::Inference::Steps::RagContext

Includes:
Logging, Legion::Logging::Helper
Included in:
Executor
Defined in:
lib/legion/llm/inference/steps/rag_context.rb

Instance Method Summary collapse

Instance Method Details

#step_rag_contextObject



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
# File 'lib/legion/llm/inference/steps/rag_context.rb', line 14

def step_rag_context
  unless rag_enabled?
    log_step_debug(:rag_context, :skipped, reason: :disabled)
    return
  end
  unless substantive_query?
    log_step_debug(:rag_context, :skipped, reason: :non_substantive_query)
    return
  end
  unless apollo_available_or_warn?
    log_step_debug(:rag_context, :skipped, reason: :apollo_unavailable)
    return
  end

  utilization = estimate_utilization
  strategy = select_context_strategy(utilization: utilization)
  if strategy == :none
    log_step_debug(:rag_context, :skipped, reason: :context_window_high, utilization: utilization.round(3))
    return
  end

  query = extract_query
  start_time = Time.now
  log_step_debug(:rag_context, :retrieve, strategy: strategy, query_chars: query.to_s.length)
  result = apollo_retrieve(query: query, strategy: strategy)
  record_rag_enrichment(result, strategy)
  record_rag_timeline(result, strategy, start_time)
rescue StandardError => e
  @warnings << "RAG context error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.rag_context')
end