Module: SmartBrain::ModelProvider::Rerankable

Included in:
Ollama, OpenAI
Defined in:
lib/smart_brain/model_provider/base.rb

Overview

LLM-as-judge rerank:让 complete 模型对候选批量排序。混入有 #complete 的 provider。

Instance Method Summary collapse

Instance Method Details

#rerank(query:, documents:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/smart_brain/model_provider/base.rb', line 45

def rerank(query:, documents:)
  return documents.each_with_index.map { |_, i| { index: i, score: 1.0 } } if documents.empty?

  list = documents.each_with_index.map { |d, i| "[#{i}] #{truncate(d)}" }.join("\n")
  prompt = "Rank the following passages by relevance to the query. " \
           "Reply with ONLY a JSON array of integer indices, most relevant first, no prose.\n" \
           "Query: #{query}\nPassages:\n#{list}"
  result = complete(prompt: prompt, temperature: 0.0, max_tokens: 240)
  order = parse_ranking(result[:text], documents.size)
  n = [documents.size, 1].max
  order.map.with_index { |idx, pos| { index: idx, score: (n - pos).to_f / n } }
rescue StandardError
  documents.each_with_index.map { |_, i| { index: i, score: 1.0 } }
end