Class: Mistri::Providers::Anthropic
- Inherits:
-
Object
- Object
- Mistri::Providers::Anthropic
- Defined in:
- lib/mistri/providers/anthropic.rb,
lib/mistri/providers/anthropic/assembler.rb,
lib/mistri/providers/anthropic/serializer.rb
Overview
The Anthropic Messages API, streamed. Defaults target the current model generation: adaptive thinking with summarized display (so thinking streams for the UI), prompt caching on, 32k output headroom.
Provider failures fold into the stream as an error turn rather than raising: the loop decides whether to retry, and the host always gets a message back.
Defined Under Namespace
Modules: Serializer Classes: Assembler
Constant Summary collapse
- DEFAULT_ORIGIN =
"https://api.anthropic.com"- VERSION_HEADER =
"2023-06-01"- DEFAULT_THINKING =
{ type: "adaptive", display: "summarized" }.freeze
- PASSTHROUGH =
Messages API parameters passed through verbatim from a stream override.
%i[temperature top_p top_k stop_sequences metadata tool_choice].freeze
- UNKNOWN_MODEL_MAX_TOKENS =
The ceiling for an uncatalogued model: high enough for headroom, low enough that every current model accepts it. Catalog a model to unlock its real output limit.
64_000
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(api_key:, model: "claude-opus-4-8", origin: DEFAULT_ORIGIN, max_tokens: nil, thinking: DEFAULT_THINKING, cache: true, service_tier: nil, catalog_pricing: nil, **transport_options) ⇒ Anthropic
constructor
A new instance of Anthropic.
- #native_output_schema(schema) ⇒ Object
- #prices_usage? ⇒ Boolean
- #stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit) ⇒ Object
Constructor Details
#initialize(api_key:, model: "claude-opus-4-8", origin: DEFAULT_ORIGIN, max_tokens: nil, thinking: DEFAULT_THINKING, cache: true, service_tier: nil, catalog_pricing: nil, **transport_options) ⇒ Anthropic
Returns a new instance of Anthropic.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mistri/providers/anthropic.rb', line 26 def initialize(api_key:, model: "claude-opus-4-8", origin: DEFAULT_ORIGIN, max_tokens: nil, thinking: DEFAULT_THINKING, cache: true, service_tier: nil, catalog_pricing: nil, **) @api_key = api_key @model = model @max_tokens = max_tokens @thinking = thinking @cache = cache @service_tier = service_tier @catalog_pricing = catalog_pricing.nil? ? official_origin?(origin) : catalog_pricing @transport = Transport.new(origin: origin, **) end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
40 41 42 |
# File 'lib/mistri/providers/anthropic.rb', line 40 def model @model end |
Instance Method Details
#close ⇒ Object
71 |
# File 'lib/mistri/providers/anthropic.rb', line 71 def close = @transport.close |
#native_output_schema(schema) ⇒ Object
46 47 48 49 50 |
# File 'lib/mistri/providers/anthropic.rb', line 46 def native_output_schema(schema) return unless Models.find(model)&.provider == :anthropic SchemaCapabilities.derive(schema, :anthropic) end |
#prices_usage? ⇒ Boolean
42 43 44 |
# File 'lib/mistri/providers/anthropic.rb', line 42 def prices_usage? @catalog_pricing && Models.priced?(model) && @service_tier.to_s == "standard_only" end |
#stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mistri/providers/anthropic.rb', line 52 def stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit) delivery = EventDelivery.wrap(emit) model = overrides.fetch(:model, @model) assembler = Anthropic::Assembler.new(model: model, catalog_pricing: @catalog_pricing) assembler.start(&delivery) body = build_body(model, , system, tools, overrides) outcome = @transport.stream_post("/v1/messages", body: body, headers: headers, signal: signal) do |record| assembler.feed( record, &delivery ) end outcome == :aborted ? assembler.abort(&delivery) : assembler.finish(&delivery) rescue EventDelivery::Failure => e raise EventDelivery.unwrap(e, delivery) rescue Error => e assembler.fail_stream(e, &emit) end |