Class: Llmemory::LLM::Anthropic

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

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.anthropic.com"

Instance Method Summary collapse

Methods inherited from Base

#invoke_with_json_schema

Constructor Details

#initialize(api_key: nil, model: nil, base_url: nil) ⇒ Anthropic

Returns a new instance of Anthropic.



12
13
14
15
16
# File 'lib/llmemory/llm/anthropic.rb', line 12

def initialize(api_key: nil, model: nil, base_url: nil)
  @api_key = api_key || config.llm_api_key || ENV["ANTHROPIC_API_KEY"]
  @model = model || config.llm_model || "claude-3-sonnet-20240229"
  @base_url = base_url || config.llm_base_url || DEFAULT_BASE_URL
end

Instance Method Details

#invoke(prompt) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llmemory/llm/anthropic.rb', line 18

def invoke(prompt)
  response = connection.post("v1/messages") do |req|
    req.body = {
      model: @model,
      max_tokens: 1024,
      messages: [{ role: "user", content: prompt }]
    }.to_json
    req.headers["Content-Type"] = "application/json"
    req.headers["x-api-key"] = @api_key
    req.headers["anthropic-version"] = "2023-06-01"
  end

  raise Llmemory::LLMError, "Anthropic API error: #{response.body}" unless response.success?

  body = response.body.is_a?(Hash) ? response.body : JSON.parse(response.body.to_s)
  content = body.dig("content", 0, "text")
  content&.strip || ""
end