Class: Durable::Llm::Providers::Anthropic
- Defined in:
- lib/durable/llm/providers/anthropic.rb
Overview
Anthropic provider for accessing Claude language models through their API.
This provider implements the Durable::Llm::Providers::Base interface to provide completion and streaming capabilities for Anthropic’s Claude models including Claude 3.5 Sonnet, Claude 3 Opus, and Claude 3 Haiku. It handles authentication via API keys, supports system messages, and provides comprehensive error handling for various Anthropic API error conditions.
Key features:
-
Message-based chat completions with multi-turn conversations
-
Real-time streaming responses for interactive applications
-
System message support for setting context
-
Automatic model listing from predefined supported models
-
Comprehensive error handling with specific exception types
Defined Under Namespace
Classes: AnthropicChoice, AnthropicMessage, AnthropicResponse, AnthropicStreamChoice, AnthropicStreamDelta, AnthropicStreamResponse
Constant Summary collapse
- BASE_URL =
'https://api.anthropic.com'
Instance Attribute Summary collapse
-
#api_key ⇒ String?
The API key used for authentication with Anthropic.
Class Method Summary collapse
-
.models ⇒ Array<String>
Retrieves the list of supported Claude models.
-
.stream? ⇒ Boolean
True, indicating this provider supports streaming.
Instance Method Summary collapse
-
#completion(options) ⇒ AnthropicResponse
Performs a completion request to Anthropic’s messages API.
-
#default_api_key ⇒ String?
The default API key for Anthropic, or nil if not configured.
-
#embedding(model:, input:, **options) ⇒ Object
Performs an embedding request (not supported by Anthropic).
-
#initialize(api_key: nil) ⇒ Anthropic
constructor
Initializes a new Anthropic provider instance.
-
#models ⇒ Array<String>
Retrieves the list of available models for this provider instance.
-
#stream(options) {|AnthropicStreamResponse| ... } ⇒ Object
Performs a streaming completion request to Anthropic’s messages API.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil) ⇒ Anthropic
Initializes a new Anthropic provider instance.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/durable/llm/providers/anthropic.rb', line 68 def initialize(api_key: nil) super() @api_key = api_key || default_api_key @conn = Faraday.new(url: BASE_URL) do |faraday| faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#api_key ⇒ String?
Returns The API key used for authentication with Anthropic.
62 63 64 |
# File 'lib/durable/llm/providers/anthropic.rb', line 62 def api_key @api_key end |
Class Method Details
.models ⇒ Array<String>
Retrieves the list of supported Claude models.
129 130 131 |
# File 'lib/durable/llm/providers/anthropic.rb', line 129 def self.models ['claude-3-5-sonnet-20240620', 'claude-3-opus-20240229', 'claude-3-haiku-20240307'] end |
.stream? ⇒ Boolean
Returns True, indicating this provider supports streaming.
134 135 136 |
# File 'lib/durable/llm/providers/anthropic.rb', line 134 def self.stream? true end |
Instance Method Details
#completion(options) ⇒ AnthropicResponse
Performs a completion request to Anthropic’s messages API.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/durable/llm/providers/anthropic.rb', line 92 def completion() # Convert symbol keys to strings for consistency = .transform_keys(&:to_s) # Ensure max_tokens is set ['max_tokens'] ||= 1024 # Handle system message separately as Anthropic expects it as a top-level parameter = nil = ['messages']&.dup || [] if .first && (.first['role'] || .first[:role]) == 'system' = .first['content'] || .first[:content] = [1..] || [] end request_body = .merge('messages' => ) request_body['system'] = if response = @conn.post('/v1/messages') do |req| req.headers['x-api-key'] = @api_key req.headers['anthropic-version'] = '2023-06-01' req.body = request_body end handle_response(response) end |
#default_api_key ⇒ String?
Returns The default API key for Anthropic, or nil if not configured.
56 57 58 |
# File 'lib/durable/llm/providers/anthropic.rb', line 56 def default_api_key Durable::Llm.configuration.anthropic&.api_key || ENV['ANTHROPIC_API_KEY'] end |
#embedding(model:, input:, **options) ⇒ Object
Performs an embedding request (not supported by Anthropic).
144 145 146 |
# File 'lib/durable/llm/providers/anthropic.rb', line 144 def (model:, input:, **) raise NotImplementedError, 'Anthropic does not provide embedding APIs' end |
#models ⇒ Array<String>
Retrieves the list of available models for this provider instance.
122 123 124 |
# File 'lib/durable/llm/providers/anthropic.rb', line 122 def models self.class.models end |
#stream(options) {|AnthropicStreamResponse| ... } ⇒ Object
Performs a streaming completion request to Anthropic’s messages API.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/durable/llm/providers/anthropic.rb', line 157 def stream() = .transform_keys(&:to_s) ['stream'] = true # Handle system message separately = nil = ['messages']&.dup || [] if .first && (.first['role'] || .first[:role]) == 'system' = .first['content'] || .first[:content] = [1..] || [] end request_body = .merge('messages' => ) request_body['system'] = if response = @conn.post('/v1/messages') do |req| req.headers['x-api-key'] = @api_key req.headers['anthropic-version'] = '2023-06-01' req.headers['Accept'] = 'text/event-stream' req.body = request_body user_proc = proc do |chunk, _size, _total| yield AnthropicStreamResponse.new(chunk) end req..on_data = to_json_stream(user_proc: user_proc) end handle_response(response) end |