Class: Durable::Llm::Providers::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/durable/llm/providers/anthropic.rb

Defined Under Namespace

Classes: AnthropicChoice, AnthropicMessage, AnthropicResponse, AnthropicStreamChoice, AnthropicStreamDelta, AnthropicStreamResponse

Constant Summary collapse

BASE_URL =
'https://api.anthropic.com'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#embedding, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Anthropic

Returns a new instance of Anthropic.



18
19
20
21
22
23
24
25
26
# File 'lib/durable/llm/providers/anthropic.rb', line 18

def initialize(api_key: nil)
  @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_keyObject

Returns the value of attribute api_key.



16
17
18
# File 'lib/durable/llm/providers/anthropic.rb', line 16

def api_key
  @api_key
end

Class Method Details

.modelsObject



43
44
45
# File 'lib/durable/llm/providers/anthropic.rb', line 43

def self.models
  ['claude-3-5-sonnet-20240620', 'claude-3-opus-20240229', 'claude-3-haiku-20240307']
end

.stream?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/durable/llm/providers/anthropic.rb', line 47

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/durable/llm/providers/anthropic.rb', line 28

def completion(options)
  options['max_tokens'] ||= 1024
  response = @conn.post('/v1/messages') do |req|
    req.headers['x-api-key'] = @api_key
    req.headers['anthropic-version'] = '2023-06-01'
    req.body = options
  end

  handle_response(response)
end

#default_api_keyObject



12
13
14
# File 'lib/durable/llm/providers/anthropic.rb', line 12

def default_api_key
  Durable::Llm.configuration.anthropic&.api_key || ENV['ANTHROPIC_API_KEY']
end

#modelsObject



39
40
41
# File 'lib/durable/llm/providers/anthropic.rb', line 39

def models
  self.class.models
end

#stream(options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/durable/llm/providers/anthropic.rb', line 51

def stream(options)
  options[:stream] = true
  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 = options
    req.options.on_data = proc do |chunk, _size, _total|
      next if chunk.strip.empty?

      yield AnthropicStreamResponse.new(chunk) if chunk.start_with?('data: ')
    end
  end

  handle_response(response)
end