Module: Legion::Extensions::Knowledge::Runners::Query

Extended by:
JSON::Helper, Logging::Helper, Settings::Helper
Included in:
Client
Defined in:
lib/legion/extensions/knowledge/runners/query.rb

Overview

rubocop:disable Legion/Extension/RunnerIncludeHelpers

Class Method Summary collapse

Class Method Details

.query(question:, top_k: nil, synthesize: true, expand_neighbors: false, neighbor_radius: nil) ⇒ Object



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
# File 'lib/legion/extensions/knowledge/runners/query.rb', line 18

def query(question:, top_k: nil, synthesize: true, expand_neighbors: false, neighbor_radius: nil)
  started = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  resolved_k      = top_k || settings[:query][:top_k]
  resolved_radius = resolve_neighbor_radius(neighbor_radius)

  chunks = retrieve_chunks(
    question,
    resolved_k,
    expand_neighbors: expand_neighbors,
    neighbor_radius:  resolved_radius
  )

  answer = (synthesize_answer(question, chunks) if synthesize && llm_available?)

  latency_ms = ((::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - started) * 1000).round

  score = average_score(chunks)
  unless chunks.empty?
    record_feedback(
      question:        question,
      chunk_ids:       chunks.filter_map { |c| c[:id] },
      retrieval_score: score.to_f,
      synthesized:     synthesize && llm_available?
    )
  end

  {
    success:  true,
    answer:   answer,
    sources:  chunks.map { |c| format_source(c) },
    metadata: (chunks, score, latency_ms)
  }
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'knowledge.query.query')
  { success: false, error: e.message }
end

.record_feedback(question:, chunk_ids:, retrieval_score:, synthesized: true, rating: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/knowledge/runners/query.rb', line 75

def record_feedback(question:, chunk_ids:, retrieval_score:, synthesized: true, rating: nil)
  question_hash = ::Digest::SHA256.hexdigest(question.to_s)[0, 16]
  emit_feedback_event(
    question_hash:   question_hash,
    chunk_ids:       chunk_ids,
    retrieval_score: retrieval_score,
    synthesized:     synthesized,
    rating:          rating
  )
  { success: true, question_hash: question_hash, rating: rating }
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'knowledge.query.record_feedback')
  { success: false, error: e.message }
end

.retrieve(question:, top_k: nil, expand_neighbors: false, neighbor_radius: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/knowledge/runners/query.rb', line 55

def retrieve(question:, top_k: nil, expand_neighbors: false, neighbor_radius: nil)
  resolved_k      = top_k || settings[:query][:top_k]
  resolved_radius = resolve_neighbor_radius(neighbor_radius)
  chunks          = retrieve_chunks(
    question,
    resolved_k,
    expand_neighbors: expand_neighbors,
    neighbor_radius:  resolved_radius
  )

  {
    success:  true,
    sources:  chunks.map { |c| format_source(c) },
    metadata: (chunks, average_score(chunks))
  }
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'knowledge.query.retrieve')
  { success: false, error: e.message }
end