Class: RubyLLM::Voyage::Reranking

Inherits:
Struct
  • Object
show all
Defined in:
lib/ruby_llm/voyage/results.rb

Overview

Typed result returned by rerank.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#input_tokensObject

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



9
10
11
# File 'lib/ruby_llm/voyage/results.rb', line 9

def input_tokens
  @input_tokens
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



9
10
11
# File 'lib/ruby_llm/voyage/results.rb', line 9

def model
  @model
end

#resultsObject

Returns the value of attribute results

Returns:

  • (Object)

    the current value of results



9
10
11
# File 'lib/ruby_llm/voyage/results.rb', line 9

def results
  @results
end

Instance Method Details

#reorder(collection) ⇒ Array

Reorders the original candidate collection by relevance.

Pass the same collection whose contents were sent as documents:. Returns the matching items in reranked order, truncated to top_k when one was requested.

Examples:

reranking = RubyLLM::Voyage.rerank(
  query: query, documents: articles.map(&:content), top_k: 5
)
reranking.reorder(articles) # => the five most relevant articles

Parameters:

  • collection (Array, #to_a)

    candidates in their original order

Returns:

  • (Array)

    the reranked items

Raises:

  • (IndexError)

    when the collection is smaller than the document list that was reranked



26
27
28
29
# File 'lib/ruby_llm/voyage/results.rb', line 26

def reorder(collection)
  items = collection.to_a
  results.map { |result| items.fetch(result.index) }
end