Module: RubyLLM::Protocols::ChatCompletions::Rerank

Included in:
RubyLLM::Providers::GPUStack::ChatCompletions, RubyLLM::Providers::OpenRouter::ChatCompletions
Defined in:
lib/ruby_llm/protocols/chat_completions/rerank.rb

Overview

The Jina-style rerank endpoint several OpenAI-compatible providers serve: OpenRouter at /api/v1/rerank and GPUStack at /v1/rerank, with identical request and response shapes.

Class Method Summary collapse

Class Method Details

.parse_rerank_response(response, model:, documents: []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 25

def parse_rerank_response(response, model:, documents: [])
  data = response.body
  usage = data['usage'] || {}

  RubyLLM::Rerank.new(
    results: parse_rerank_results(data, documents),
    model: data['model'] || model,
    raw: data,
    input_tokens: usage['total_tokens'],
    reported_cost: usage['cost']
  )
end

.parse_rerank_results(data, documents = []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 38

def parse_rerank_results(data, documents = [])
  Array(data['results']).map do |result|
    index = result['index']
    unless valid_rerank_index?(index, documents)
      raise Error, 'Rerank endpoint returned an invalid document index'
    end

    RubyLLM::Rerank::Result.new(
      index: index,
      document: rerank_document(result['document']) || documents[index],
      score: result['relevance_score']
    )
  end
end

.render_rerank_payload(query, documents, model:, top_n: nil, provider_options: {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 16

def render_rerank_payload(query, documents, model:, top_n: nil, provider_options: {})
  {
    model: model,
    query: query,
    documents: documents,
    top_n: top_n
  }.compact.merge(provider_options)
end

.rerank_document(document) ⇒ Object



53
54
55
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 53

def rerank_document(document)
  document.is_a?(Hash) ? document['text'] : document
end

.rerank_urlObject



12
13
14
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 12

def rerank_url
  'rerank'
end

.valid_rerank_index?(index, documents) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_llm/protocols/chat_completions/rerank.rb', line 57

def valid_rerank_index?(index, documents)
  index.is_a?(Integer) && index.between?(0, documents.length - 1)
end