Class: LittleGhost::Providers::Anthropic
- Defined in:
- lib/little_ghost/providers/anthropic.rb,
lib/little_ghost/providers/anthropic/catalog_source.rb
Overview
Zero-dependency Anthropic Messages API adapter.
Defined Under Namespace
Classes: CatalogSource, Normalizer
Constant Summary collapse
- DEFAULT_BASE_URL =
:nodoc:
"https://api.anthropic.com/v1/"
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Provider-owned model identifier.
Class Method Summary collapse
-
.request_options ⇒ Object
Request policy supported by the Anthropic HTTP client.
Instance Method Summary collapse
-
#capabilities(metadata: {}) ⇒ Object
Reports tool and structured-output support from model metadata.
-
#initialize(api_key:, model:, base_url: DEFAULT_BASE_URL, api_version: "2023-06-01", open_timeout: 10, read_timeout: 120, max_response_bytes: Support::HTTPClient::DEFAULT_MAX_RESPONSE_BYTES, transport: nil) ⇒ Anthropic
constructor
Creates an Anthropic Messages client for
model. -
#stream(request) ⇒ Object
Streams normalized events for
request.
Methods inherited from Base
Constructor Details
#initialize(api_key:, model:, base_url: DEFAULT_BASE_URL, api_version: "2023-06-01", open_timeout: 10, read_timeout: 120, max_response_bytes: Support::HTTPClient::DEFAULT_MAX_RESPONSE_BYTES, transport: nil) ⇒ Anthropic
Creates an Anthropic Messages client for model.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/little_ghost/providers/anthropic.rb', line 22 def initialize(api_key:, model:, base_url: DEFAULT_BASE_URL, api_version: "2023-06-01", open_timeout: 10, read_timeout: 120, max_response_bytes: Support::HTTPClient::DEFAULT_MAX_RESPONSE_BYTES, transport: nil, **) raise CredentialError, "Anthropic api_key is required" if api_key.to_s.empty? @api_key = api_key @api_version = api_version @model = model @transport = transport || Support::HTTPClient.new(base_url:, open_timeout:, read_timeout:, max_response_bytes:) end |
Instance Attribute Details
#model ⇒ Object (readonly)
Provider-owned model identifier.
19 20 21 |
# File 'lib/little_ghost/providers/anthropic.rb', line 19 def model @model end |
Class Method Details
.request_options ⇒ Object
Request policy supported by the Anthropic HTTP client.
14 |
# File 'lib/little_ghost/providers/anthropic.rb', line 14 def self. = %i[max_response_bytes open_timeout read_timeout].freeze |
Instance Method Details
#capabilities(metadata: {}) ⇒ Object
Reports tool and structured-output support from model metadata.
60 61 62 63 64 65 66 67 68 |
# File 'lib/little_ghost/providers/anthropic.rb', line 60 def capabilities(metadata: {}) parameters = [:supported_parameters] ModelCapabilities.new( native_structured_output: parameters&.include?("structured_outputs"), tools: true, tool_choice: true, supported_parameters: parameters ) end |
#stream(request) ⇒ Object
Streams normalized events for request.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/little_ghost/providers/anthropic.rb', line 34 def stream(request) return enum_for(__method__, request) unless block_given? parser = Support::SSEParser.new normalizer = Normalizer.new(model:) @transport.stream( path: "messages", headers: { "x-api-key" => @api_key, "anthropic-version" => @api_version, "content-type" => "application/json", "accept" => "text/event-stream" }, body: JSON.generate(request_body(request)), cancellation_token: request.cancellation_token, deadline: request.deadline ) do |chunk| parser.<<(chunk).each { |data| normalizer.consume(JSON.parse(data)).each { |event| yield event } } end parser.finish.each { |data| normalizer.consume(JSON.parse(data)).each { |event| yield event } } normalizer.finish.each { |event| yield event } rescue JSON::ParserError => error raise ProtocolError, "Anthropic returned invalid JSON: #{error.}" end |