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
  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.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/anthropic/client.rb', line 14

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

#complete(parameters: {}) ⇒ Object

Deprecated.

(but still works while Anthropic API responds to it)



27
28
29
30
# File 'lib/anthropic/client.rb', line 27

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

#messages(parameters: {}) ⇒ Object

Anthropic API Parameters as of 2024-05-07:

@see https://docs.anthropic.com/claude/reference/messages_post

"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" => => 15, "output_tokens" => 5

}

Parameters:

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

Options Hash (parameters:):

  • :messages (Array)
    • Required. An array of messages to send to the API. Each

    message should have a role and content. Single message example: [{ role: “user”, content: “Hello, Claude!” }]

  • :model (String)
  • :max_tokens (Integer)
  • :system (String)
  • :temperature (Float)
    • Optional, defaults to 1.0

  • :stream (Proc)
    • Optional, if present, must be a Proc that will receive the

    content fragments as they come in

  • :preprocess_stream (String)
    • If true, the streaming Proc will be pre-

    processed. Specifically, instead of being passed a raw Hash like: “index”=>0, “delta”=>{“type”=>“text_delta”, “text”=>“ of”} the Proc will instead be passed something nicer. If preprocess_stream is set to “json” or :json, then the Proc will only receive full json objects, one at a time. If preprocess_stream is set to “text” or :text then the Proc will receive two arguments: the first will be the text accrued so far, and the second will be the delta just received in the current chunk.



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

def messages(parameters: {})
  json_post(path: "/messages", parameters: parameters)
end