Class: Kreator::Providers::Anthropic
- Defined in:
- lib/kreator/providers/anthropic.rb
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.anthropic.com/v1"- DEFAULT_VERSION =
"2023-06-01"
Constants inherited from Base
Base::DEFAULT_MAX_RETRIES, Base::TRANSIENT_HTTP_STATUSES
Instance Attribute Summary
Attributes inherited from Base
#api_key, #base_url, #max_retries, #name
Instance Method Summary collapse
- #capabilities(model) ⇒ Object
-
#initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), base_url: ENV.fetch("ANTHROPIC_BASE_URL", DEFAULT_BASE_URL), max_retries: DEFAULT_MAX_RETRIES) ⇒ Anthropic
constructor
A new instance of Anthropic.
- #stream(messages:, tools:, system_prompt:, model:, signal:) ⇒ Object
Constructor Details
#initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), base_url: ENV.fetch("ANTHROPIC_BASE_URL", DEFAULT_BASE_URL), max_retries: DEFAULT_MAX_RETRIES) ⇒ Anthropic
Returns a new instance of Anthropic.
9 10 11 12 13 |
# File 'lib/kreator/providers/anthropic.rb', line 9 def initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), base_url: ENV.fetch("ANTHROPIC_BASE_URL", DEFAULT_BASE_URL), max_retries: DEFAULT_MAX_RETRIES) raise Error, "ANTHROPIC_API_KEY is required for the anthropic provider" if api_key.to_s.empty? super(api_key: api_key, base_url: base_url, name: "anthropic", max_retries: max_retries) end |
Instance Method Details
#capabilities(model) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/kreator/providers/anthropic.rb', line 31 def capabilities(model) super.merge( "vision" => model.to_s.match?(/sonnet|opus/), "reasoning" => model.to_s.match?(/3-7|4/), "context_window" => 200_000 ) end |
#stream(messages:, tools:, system_prompt:, model:, signal:) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/kreator/providers/anthropic.rb', line 15 def stream(messages:, tools:, system_prompt:, model:, signal:, &) body = anthropic_stream_body(, tools, system_prompt, model) stream_state = { content_blocks: {}, emitted_model_output: false } begin stream_anthropic_events(body, signal, stream_state, &) rescue Providers::Error raise if stream_state.fetch(:emitted_model_output) complete_once(body.merge(stream: false), &) end end |