Class: LlmHub::Completion::Client

Inherits:
LlmHub::Common::ClientBase show all
Defined in:
lib/llm_hub/completion/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,
  anthropic: Providers::Anthropic,
  deepseek: Providers::Deepseek,
  google: Providers::Google
}.freeze

Instance Attribute Summary

Attributes inherited from LlmHub::Common::ClientBase

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

Instance Method Summary collapse

Methods included from LlmHub::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 completion client

Parameters:

  • api_key (String)

    API key for the provider (required)

  • provider (Symbol, String)

    Provider name (:openai, :anthropic, :deepseek, :google) (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:



23
24
25
26
# File 'lib/llm_hub/completion/client.rb', line 23

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

#ask_single_question(system_prompt:, content:, model_name:, option_params: {}) ⇒ Hash{Symbol => String, Integer}

Execute a single question prompt and return the response

Parameters:

  • system_prompt (String)

    System prompt for the LLM

  • content (String)

    User content/question

  • model_name (String)

    Model name to use

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

    Additional parameters for the provider

Returns:

  • (Hash{Symbol => String, Integer})

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/llm_hub/completion/client.rb', line 35

def ask_single_question(
  system_prompt:,
  content:,
  model_name:,
  option_params: {}
)
  with_retry do
    url = provider_url(model_name)
    request_body = @provider_client.request_body(system_prompt, content, 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