Class: OmniAI::Anthropic::Client

Inherits:
Client
  • Object
show all
Defined in:
lib/omniai/anthropic/client.rb

Overview

An Anthropic client implementation. Usage:

w/ ‘api_key“:

client = OmniAI::Anthropic::Client.new(api_key: '...')

w/ ENV:

ENV['ANTHROPIC_API_KEY'] = '...'
client = OmniAI::Anthropic::Client.new

w/ config:

OmniAI::Anthropic.configure do |config|
  config.api_key = '...'
end

client = OmniAI::Anthropic::Client.new

Constant Summary collapse

VERSION =
'v1'

Instance Method Summary collapse

Constructor Details

#initialize(api_key: OmniAI::Anthropic.config.api_key, host: OmniAI::Anthropic.config.host, version: OmniAI::Anthropic.config.version, logger: OmniAI::Anthropic.config.logger, timeout: OmniAI::Anthropic.config.timeout) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String) (defaults to: OmniAI::Anthropic.config.api_key)

    optional - defaults to ‘OmniAI::Anthropic.config.api_key`

  • host (String) (defaults to: OmniAI::Anthropic.config.host)

    optional - defaults to ‘OmniAI::Anthropic.config.host`

  • version (String) (defaults to: OmniAI::Anthropic.config.version)

    optional - defaults to ‘OmniAI::Anthropic.config.version`

  • logger (Logger) (defaults to: OmniAI::Anthropic.config.logger)

    optional - defaults to ‘OmniAI::Anthropic.config.logger`

  • timeout (Integer) (defaults to: OmniAI::Anthropic.config.timeout)

    optional - defaults to ‘OmniAI::Anthropic.config.timeout`

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/omniai/anthropic/client.rb', line 30

def initialize(
  api_key: OmniAI::Anthropic.config.api_key,
  host: OmniAI::Anthropic.config.host,
  version: OmniAI::Anthropic.config.version,
  logger: OmniAI::Anthropic.config.logger,
  timeout: OmniAI::Anthropic.config.timeout
)
  raise(ArgumentError, %(ENV['ANTHROPIC_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?

  super(api_key:, host:, logger:, timeout:)

  @host = host
  @version = version
end

Instance Method Details

#chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil) {|prompt| ... } ⇒ OmniAI::Chat::Completion

Parameters:

  • messages (String) (defaults to: nil)

    optional

  • model (String) (defaults to: Chat::DEFAULT_MODEL)

    optional

  • format (Symbol) (defaults to: nil)

    optional :text or :json

  • temperature (Float, nil) (defaults to: nil)

    optional

  • stream (Proc, nil) (defaults to: nil)

    optional

  • tools (Array<OmniAI::Tool>, nil) (defaults to: nil)

    optional

Yields:

  • (prompt)

Yield Parameters:

  • prompt (OmniAI::Chat::Prompt)

Returns:

  • (OmniAI::Chat::Completion)

Raises:

  • (OmniAI::Error)


66
67
68
# File 'lib/omniai/anthropic/client.rb', line 66

def chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil, &)
  Chat.process!(messages, model:, temperature:, format:, stream:, tools:, client: self, &)
end

#connectionHTTP::Client

Returns:

  • (HTTP::Client)


46
47
48
49
50
51
# File 'lib/omniai/anthropic/client.rb', line 46

def connection
  @connection ||= super.headers({
    'x-api-key': @api_key,
    'anthropic-version': @version,
  }.compact)
end