Class: LlmHub::Embedding::Client

Inherits:
Common::ClientBase show all
Defined in:
lib/llm_hub/embedding/client.rb

Overview

Client for LLM providers (OpenAI, Anthropic, etc.)

Constant Summary collapse

PROVIDER_CLASSES =

Available provider mappings

Returns:

  • (Hash<Symbol, Class>)

    mapping of provider names to their classes

{
  openai: Providers::OpenAI
}.freeze

Instance Attribute Summary

Attributes inherited from Common::ClientBase

#api_key, #open_time_out, #provider, #read_time_out, #retry_count

Instance Method Summary collapse

Methods included from Common::HttpHelper

#http_client, #http_post

Constructor Details

#initialize(api_key:, provider:, open_time_out: nil, read_time_out: nil, retry_count: nil) ⇒ Client

Initialize a new embedding client

Parameters:

  • api_key (String)

    API key for the provider (required)

  • provider (Symbol, String)

    Provider name (:openai) (required)

  • open_time_out (Integer) (defaults to: nil)

    HTTP open timeout in seconds (optional, defaults to Config value)

  • read_time_out (Integer) (defaults to: nil)

    HTTP read timeout in seconds (optional, defaults to Config value)

  • retry_count (Integer) (defaults to: nil)

    Number of retries for failed requests (optional, defaults to Config value)

See Also:



20
21
22
23
# File 'lib/llm_hub/embedding/client.rb', line 20

def initialize(api_key:, provider:, open_time_out: nil, read_time_out: nil, retry_count: nil)
  super
  @provider_client = create_provider_client
end

Instance Method Details

#post_embedding(text:, model_name:, option_params: {}) ⇒ Hash{Symbol => Array<Float>, Hash}

Generate embeddings for the given text

Parameters:

  • text (String)

    The text to generate embeddings for

  • model_name (String)

    The model to use for embedding generation

  • option_params (Hash) (defaults to: {})

    Additional parameters to pass to the provider

Returns:

  • (Hash{Symbol => Array<Float>, Hash})

    Response with :embedding and :tokens keys on success or :error key on failure



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/llm_hub/embedding/client.rb', line 31

def post_embedding(
  text:,
  model_name:,
  option_params: {}
)
  with_retry do
    url = @provider_client.url
    request_body = @provider_client.request_body(text, model_name, option_params)
    headers = @provider_client.headers

    response_body = make_request(url, request_body, headers)
    formatted_response(response_body)
  end
rescue StandardError => e
  { error: e.message }.deep_symbolize_keys
end