Class: RubyLLM::Protocols::VertexAI::Ranking

Inherits:
RubyLLM::Protocol show all
Defined in:
lib/ruby_llm/protocols/vertexai/ranking.rb

Overview

Vertex AI Search's Discovery Engine ranking API.

Instance Attribute Summary

Attributes inherited from RubyLLM::Protocol

#config, #connection, #model, #provider

Instance Method Summary collapse

Methods inherited from RubyLLM::Protocol

abstract, #animate_later, #apply_compaction, #apply_compaction_headers, #apply_end_user, #cache_content, #compact, #complete, #count_tokens, #delete_cache, #embed, #extend_cache, #find_cache, #list_models, #moderate, #ocr, #paint, #parse_error, #parse_image_responses, #post_image, #post_video, #preprocess_message, #raise_transcription_streaming_unsupported, #refresh_video_job, #render, #render_embedding, #render_transcription_options, #render_video_extension_payload, #rerank, #server_tool_aliases, #speak, #stream_speech, #stream_speech_response, #stream_transcription, #supports_embedding_media?, #tokenize, #tool_approval_response, #transcribe, #video_extension_attachment, #video_extension_url, #video_request_url

Methods included from RubyLLM::Protocol::BinaryStreaming

#stream_binary

Methods included from RubyLLM::Protocol::Streaming

build_on_data_handler, build_stream_error_response, error_chunk?, failed_http_status, faraday_1?, handle_data, handle_error_chunk, handle_error_event, handle_failed_response, handle_json_error_chunk, handle_sse, handle_stream, json_error_payload?, parse_error_from_json, parse_streaming_error, process_stream_chunk, raise_stream_error, stream_events, stream_response, stream_state

Constructor Details

#initialize(provider, model = nil) ⇒ Ranking

Returns a new instance of Ranking.



8
9
10
11
# File 'lib/ruby_llm/protocols/vertexai/ranking.rb', line 8

def initialize(provider, model = nil)
  super
  @connection = provider.ranking_connection
end

Instance Method Details

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

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/protocols/vertexai/ranking.rb', line 23

def parse_rerank_response(response, model:, documents: [])
  records = response.body['records']
  raise Error.new('Vertex AI Search returned no ranking records', response:) unless records.is_a?(Array)

  seen = []
  results = records.map do |record|
    index = ranking_index(record, documents)
    raise Error.new('Vertex AI Search returned a duplicate document id', response:) if seen.include?(index)

    seen << index
    Rerank::Result.new(index: index, document: documents[index], score: record.fetch('score'))
  end
  Rerank.new(results: results, model: model, raw: response.body)
end

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



17
18
19
20
21
# File 'lib/ruby_llm/protocols/vertexai/ranking.rb', line 17

def render_rerank_payload(query, documents, model:, top_n: nil, provider_options: {})
  validate_ranking_input(query, documents, top_n)
  records = documents.each_with_index.map { |document, index| { id: index.to_s, content: document } }
  { model: model, query: query, records: records, topN: top_n }.compact.merge(provider_options)
end

#rerank_urlObject



13
14
15
# File 'lib/ruby_llm/protocols/vertexai/ranking.rb', line 13

def rerank_url
  "#{@provider.ranking_config}:rank"
end