Class: VoyageAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/voyageai/client.rb

Overview

A client for interacting with the VoyageAI API.

Defined Under Namespace

Classes: RequestError

Instance Method Summary collapse

Constructor Details

#initialize(api_key: VoyageAI.config.api_key, host: VoyageAI.config.host, version: VoyageAI.config.version, logger: VoyageAI.config.logger) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String) (defaults to: VoyageAI.config.api_key)

    optional if ENV otherwise required

  • host (String) (defaults to: VoyageAI.config.host)

    optional

  • version (String) (defaults to: VoyageAI.config.version)

    optional

  • logger (Logger) (defaults to: VoyageAI.config.logger)

    optional



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/voyageai/client.rb', line 21

def initialize(
  api_key: VoyageAI.config.api_key,
  host: VoyageAI.config.host,
  version: VoyageAI.config.version,
  logger: VoyageAI.config.logger
)
  @api_key = api_key || raise(ArgumentError, "api_key is required or ENV['VOYAGEAI_API_KEY'] must be present")
  @host = host
  @version = version
  @logger = logger
end

Instance Method Details

#embed(input, model: Model::VOYAGE, input_type: nil, truncation: nil) ⇒ Embedding

Parameters:

  • input (String, Array<String>)

    required (e.g. “Sample” or [“Sample 1”, “Sample 2”, …])

  • model (String) (defaults to: Model::VOYAGE)

    optional (e.g. VoyageAI::Model::VOYAGE or “voyage-3”)

  • input_type (Symbol) (defaults to: nil)

    optional (e.g. :query or :document)

  • truncation (Boolean) (defaults to: nil)

    optional

Returns:

  • (Embedding)

Raises:



46
47
48
49
50
51
52
53
# File 'lib/voyageai/client.rb', line 46

def embed(input, model: Model::VOYAGE, input_type: nil, truncation: nil)
  payload = { input: arrayify(input), model: model, truncation:, input_type: }.compact
  response = http.accept(:json).post("/#{@version}/embeddings", json: payload)

  raise RequestError.new(response:) unless response.status.ok?

  Embed.parse(data: response.parse)
end

#inspectString

Returns:

  • (String)


34
35
36
37
38
# File 'lib/voyageai/client.rb', line 34

def inspect
  masked_api_key = "#{@api_key[..4]}***"

  "#<#{self.class.name} api_key=#{masked_api_key.inspect} host=#{@host.inspect} version=#{@version.inspect}>"
end

#rerank(query:, documents:, model: Model::RERANK, top_k: nil, truncation: nil) ⇒ Object

Parameters:

  • query (String)

    required

  • documents (Array<String>)

    required

  • model (String) (defaults to: Model::RERANK)

    optional (e.g. VoyageAI::Model::RERANK or “rerank-2”)

  • top_k (Integer) (defaults to: nil)

    optional

  • truncation (Boolean) (defaults to: nil)

    optional

Raises:



60
61
62
63
64
65
66
67
# File 'lib/voyageai/client.rb', line 60

def rerank(query:, documents:, model: Model::RERANK, top_k: nil, truncation: nil)
  payload = { query:, documents:, model:, top_k:, truncation: }.compact
  response = http.accept(:json).post("/#{@version}/rerank", json: payload)

  raise RequestError.new(response:) unless response.status.ok?

  Rerank.parse(data: response.parse)
end