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, beta: OmniAI::Anthropic.config.beta, 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`

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

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

  • 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)


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

def initialize(
  api_key: OmniAI::Anthropic.config.api_key,
  host: OmniAI::Anthropic.config.host,
  version: OmniAI::Anthropic.config.version,
  beta: OmniAI::Anthropic.config.beta,
  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
  @beta = beta
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)


70
71
72
# File 'lib/omniai/anthropic/client.rb', line 70

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)


49
50
51
52
53
54
55
# File 'lib/omniai/anthropic/client.rb', line 49

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