Class: Anthropic::Client

Inherits:
Object
  • Object
show all
Includes:
HTTP
Defined in:
lib/anthropic/client.rb

Constant Summary collapse

CONFIG_KEYS =
i[
  access_token
  anthropic_version
  api_version
  log_errors
  uri_base
  request_timeout
  extra_headers
].freeze

Instance Method Summary collapse

Methods included from HTTP

#delete, #get, #json_post, #multipart_post

Methods included from HTTPHeaders

#add_headers

Constructor Details

#initialize(config = {}, &faraday_middleware) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/anthropic/client.rb', line 19

def initialize(config = {}, &faraday_middleware)
  CONFIG_KEYS.each do |key|
    # Set instance variables like api_type & access_token. Fall back to global config
    # if not present.
    instance_variable_set(
      "@#{key}",
      config[key].nil? ? Anthropic.configuration.send(key) : config[key]
    )
  end
  @faraday_middleware = faraday_middleware
end

Instance Method Details

#beta(version) ⇒ Client

Adds Anthropic beta features to API requests. Can be used in two ways:

  1. Multiple betas in one call with comma-separated string: client.beta(“feature1,feature2”).messages

  2. Chaining multiple beta calls: client.beta(“feature1”).beta(“feature2”).messages

Parameters:

  • version (String)

    The beta version(s) to enable

Returns:

  • (Client)

    A new client instance with the beta header(s)



94
95
96
97
98
99
100
# File 'lib/anthropic/client.rb', line 94

def beta(version)
  dup.tap do |client|
    existing_beta = client.extra_headers["anthropic-beta"]
    combined_beta = [existing_beta, version].compact.join(",")
    client.add_headers("anthropic-beta" => combined_beta)
  end
end

#complete(parameters: {}) ⇒ Object

Deprecated.

(but still works while Anthropic API responds to it)



32
33
34
35
# File 'lib/anthropic/client.rb', line 32

def complete(parameters: {})
  parameters[:prompt] = wrap_prompt(prompt: parameters[:prompt])
  json_post(path: "/complete", parameters: parameters)
end

#messages(**args) ⇒ Object

Anthropic API Parameters as of 2024-05-07: When called without parameters, returns a Messages::Batches instance for batch operations. When called with parameters, creates a single message.

with parameters, or a Messages::Client instance when called without parameters

Examples:

Creating a message:

{
  "id" => "msg_013xVudG9xjSvLGwPKMeVXzG",
  "type" => "message",
  "role" => "assistant",
  "content" => [{"type" => "text", "text" => "The sky has no distinct"}],
  "model" => "claude-2.1",
  "stop_reason" => "max_tokens",
  "stop_sequence" => nil,
  "usage" => {"input_tokens" => 15, "output_tokens" => 5}
}

Accessing batches:

client.messages.batches.create(requests: [...])
client.messages.batches.get(id: "batch_123")

Parameters:

  • parameters (Hash)

See Also:



78
79
80
81
82
# File 'lib/anthropic/client.rb', line 78

def messages(**args)
  return @messages ||= Messages::Client.new(self) unless args && args[:parameters]

  json_post(path: "/messages", parameters: args[:parameters])
end