Class: Anthropic::Client
- Inherits:
-
Object
- Object
- Anthropic::Client
- 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
-
#beta(version) ⇒ Client
Adds Anthropic beta features to API requests.
-
#complete(parameters: {}) ⇒ Object
deprecated
Deprecated.
(but still works while Anthropic API responds to it)
-
#initialize(config = {}, &faraday_middleware) ⇒ Client
constructor
A new instance of Client.
-
#messages(**args) ⇒ Object
Anthropic API Parameters as of 2024-05-07: When called without parameters, returns a Messages::Batches instance for batch operations.
Methods included from HTTP
#delete, #get, #json_post, #multipart_post
Methods included from HTTPHeaders
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:
-
Multiple betas in one call with comma-separated string: client.beta(“feature1,feature2”).messages
-
Chaining multiple beta calls: client.beta(“feature1”).beta(“feature2”).messages
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
(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
78 79 80 81 82 |
# File 'lib/anthropic/client.rb', line 78 def (**args) return ||= Messages::Client.new(self) unless args && args[:parameters] json_post(path: "/messages", parameters: args[:parameters]) end |