Class: Net::Llm::Anthropic

Inherits:
Object
  • Object
show all
Defined in:
lib/net/llm/anthropic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, model: "claude-3-5-sonnet-20241022", http: Net::Llm.http) ⇒ Anthropic

Returns a new instance of Anthropic.



8
9
10
11
12
# File 'lib/net/llm/anthropic.rb', line 8

def initialize(api_key:, model: "claude-3-5-sonnet-20241022", http: Net::Llm.http)
  @api_key = api_key
  @model = model
  @http = http
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/net/llm/anthropic.rb', line 6

def api_key
  @api_key
end

#httpObject (readonly)

Returns the value of attribute http.



6
7
8
# File 'lib/net/llm/anthropic.rb', line 6

def http
  @http
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/net/llm/anthropic.rb', line 6

def model
  @model
end

Instance Method Details

#messages(messages, system: nil, max_tokens: 1024, tools: nil, &block) ⇒ Object



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

def messages(messages, system: nil, max_tokens: 1024, tools: nil, &block)
  url = "https://api.anthropic.com/v1/messages"
  payload = build_payload(messages, system, max_tokens, tools, block_given?)

  if block_given?
    stream_request(url, payload, &block)
  else
    post_request(url, payload)
  end
end