Class: OllamaAgent::Providers::Anthropic

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

Overview

Anthropic provider — talks to the Claude Messages API.

Examples:

provider = OllamaAgent::Providers::Anthropic.new(api_key: ENV["ANTHROPIC_API_KEY"])
response = provider.chat(messages: [...], model: "claude-3-5-sonnet-20241022")

Constant Summary collapse

API_BASE =
"https://api.anthropic.com/v1"
API_VERSION =
"2023-06-01"
DEFAULT_MODEL =
"claude-3-5-haiku-20241022"
DEFAULT_TOKENS =
4096
PRICING =

Pricing per 1M tokens (USD)

{
  "claude-opus-4-5" => { input: 15.0, output: 75.0 },
  "claude-sonnet-4-5" => { input: 3.0, output: 15.0 },
  "claude-3-5-sonnet-20241022" => { input: 3.0, output: 15.0 },
  "claude-3-5-haiku-20241022" => { input: 0.8, output:  4.0 },
  "claude-3-haiku-20240307" => { input: 0.25, output: 1.25 }
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#to_s

Constructor Details

#initialize(api_key: nil, beta_headers: nil, timeout: 120) ⇒ Anthropic

Returns a new instance of Anthropic.



30
31
32
33
34
35
# File 'lib/ollama_agent/providers/anthropic.rb', line 30

def initialize(api_key: nil, beta_headers: nil, timeout: 120, **)
  super(name: "anthropic", **)
  @api_key      = api_key      || ENV.fetch("ANTHROPIC_API_KEY", nil)
  @beta_headers = beta_headers || []
  @timeout      = timeout
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ollama_agent/providers/anthropic.rb', line 65

def available?
  !@api_key.nil?
end

#chat(messages:, model: nil, tools: nil, stream_hooks: nil, max_tokens: DEFAULT_TOKENS, temperature: 0.2, system: nil, **_opts) ⇒ Response

Parameters:

  • messages (Array<Hash>)
  • model (String) (defaults to: nil)
  • tools (Array<Hash>) (defaults to: nil)

    Anthropic-format or OpenAI-format (auto-converted)

  • stream_hooks (Hash) (defaults to: nil)

    :on_token, :on_thinking lambdas

  • max_tokens (Integer) (defaults to: DEFAULT_TOKENS)
  • temperature (Float) (defaults to: 0.2)
  • system (String, nil) (defaults to: nil)

    system prompt (extracted from messages if not given)

Returns:

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ollama_agent/providers/anthropic.rb', line 45

def chat(messages:, model: nil, tools: nil, stream_hooks: nil,
         max_tokens: DEFAULT_TOKENS, temperature: 0.2, system: nil, **_opts)
  raise ConfigurationError, "Anthropic API key not set (ANTHROPIC_API_KEY)" unless @api_key

  model ||= DEFAULT_MODEL
  system_prompt, user_messages = split_system(messages, system)

  body = build_body(
    messages: user_messages, model: model, tools: tools,
    max_tokens: max_tokens, temperature: temperature,
    system: system_prompt, stream: !stream_hooks.nil?
  )

  if stream_hooks
    stream_chat(body, model, stream_hooks)
  else
    blocking_chat(body, model)
  end
end

#estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL) ⇒ Object



73
74
75
76
77
# File 'lib/ollama_agent/providers/anthropic.rb', line 73

def estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL)
  pricing = PRICING[model] || PRICING[DEFAULT_MODEL]
  (input_tokens / 1_000_000.0 * pricing[:input]) +
    (output_tokens / 1_000_000.0 * pricing[:output])
end

#streaming_supported?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/ollama_agent/providers/anthropic.rb', line 69

def streaming_supported?
  true
end