Class: OllamaAgent::Providers::Anthropic
- Defined in:
- lib/ollama_agent/providers/anthropic.rb
Overview
Anthropic provider — talks to the Claude Messages API.
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
Instance Method Summary collapse
- #available? ⇒ Boolean
- #chat(messages:, model: nil, tools: nil, stream_hooks: nil, max_tokens: DEFAULT_TOKENS, temperature: 0.2, system: nil, **_opts) ⇒ Response
- #estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL) ⇒ Object
-
#initialize(api_key: nil, beta_headers: nil, timeout: 120) ⇒ Anthropic
constructor
A new instance of Anthropic.
- #streaming_supported? ⇒ Boolean
Methods inherited from Base
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
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
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, = split_system(, system) body = build_body( 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
69 70 71 |
# File 'lib/ollama_agent/providers/anthropic.rb', line 69 def streaming_supported? true end |