Class: Yorishiro::Provider::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/yorishiro/provider/anthropic.rb

Constant Summary collapse

API_URL =
"https://api.anthropic.com/v1/messages"
API_VERSION =
"2023-06-01"
SUPPORTED_MODELS =

Alias IDs (no date suffix) from Anthropic's model catalog. Aliases track the latest snapshot of each model, so this list goes stale far more slowly than the dated IDs it used to hold — but it is still a snapshot: add new entries here as models are released.

%w[
  claude-fable-5
  claude-opus-4-8
  claude-opus-4-7
  claude-opus-4-6
  claude-opus-4-5
  claude-sonnet-5
  claude-sonnet-4-6
  claude-sonnet-4-5
  claude-haiku-4-5
].freeze

Instance Attribute Summary

Attributes inherited from Base

#api_key, #model_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#context_budget_tokens, #debug?, #debug_log, #initialize

Constructor Details

This class inherits a constructor from Yorishiro::Provider::Base

Class Method Details

.supported_modelsObject



25
26
27
# File 'lib/yorishiro/provider/anthropic.rb', line 25

def self.supported_models
  SUPPORTED_MODELS
end

Instance Method Details

#chat(conversation, tools: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yorishiro/provider/anthropic.rb', line 29

def chat(conversation, tools: [], &)
  uri = URI(API_URL)
  messages = format_messages(conversation.to_api_messages)
  system_prompt = extract_system_prompt(conversation.to_api_messages)

  body = {
    model: @model_name,
    max_tokens: 8192,
    messages: messages,
    stream: true
  }
  body[:system] = system_prompt if system_prompt
  body[:tools] = format_tools(tools) unless tools.empty?

  headers = {
    "Content-Type" => "application/json",
    "x-api-key" => @api_key,
    "anthropic-version" => API_VERSION
  }

  @last_usage = {}
  result = post_stream(uri, headers: headers, body: body, &)
  result[:usage] = @last_usage
  result
end