Class: Ask::Providers::Anthropic
- Inherits:
-
Ask::Provider
- Object
- Ask::Provider
- Ask::Providers::Anthropic
- Includes:
- LLM::ProviderConfig, LLM::SSEBuffer
- Defined in:
- lib/ask/provider/anthropic.rb
Overview
Anthropic Claude API provider.
Class Method Summary collapse
- .capabilities ⇒ Object
- .configuration_options ⇒ Object
- .configuration_requirements ⇒ Object
- .slug ⇒ Object
Instance Method Summary collapse
- #api_base ⇒ Object
-
#build_request(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) ⇒ Object
--- Config transformation contract ---.
- #chat(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) ⇒ Object
- #embed(_texts, model: nil) ⇒ Object
- #format_message(msg) ⇒ Object
- #format_tools(tools) ⇒ Object
- #headers ⇒ Object
-
#initialize(config = {}) ⇒ Anthropic
constructor
A new instance of Anthropic.
- #list_models ⇒ Object
- #parse_error(response) ⇒ Object
- #parse_response(body, model) ⇒ Object
- #parse_stream(raw, stream, model, &block) ⇒ Object
Methods included from LLM::SSEBuffer
#each_sse_event, #init_sse_buffer
Constructor Details
#initialize(config = {}) ⇒ Anthropic
Returns a new instance of Anthropic.
10 11 12 13 14 |
# File 'lib/ask/provider/anthropic.rb', line 10 def initialize(config = {}) config = normalize_config(config) super(config) @http = build_http end |
Class Method Details
.capabilities ⇒ Object
57 58 59 60 61 62 |
# File 'lib/ask/provider/anthropic.rb', line 57 def capabilities { chat: true, streaming: true, tool_calls: true, vision: true, thinking: true, prompt_caching: true, structured_output: true } end |
.configuration_options ⇒ Object
64 |
# File 'lib/ask/provider/anthropic.rb', line 64 def ; %i[api_key api_base]; end |
.configuration_requirements ⇒ Object
65 |
# File 'lib/ask/provider/anthropic.rb', line 65 def configuration_requirements; %i[api_key]; end |
.slug ⇒ Object
55 |
# File 'lib/ask/provider/anthropic.rb', line 55 def slug; "anthropic"; end |
Instance Method Details
#api_base ⇒ Object
16 17 18 |
# File 'lib/ask/provider/anthropic.rb', line 16 def api_base @config.api_base || "https://api.anthropic.com" end |
#build_request(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) ⇒ Object
--- Config transformation contract ---
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ask/provider/anthropic.rb', line 70 def build_request(, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) system_msgs, chat_msgs = .partition { |m| (m[:role] || m["role"]).to_s == "system" } prompt_caching = params.delete(:prompt_caching) || false payload = { model:, messages: chat_msgs.map { |m| (m) }, max_tokens: params.delete(:max_tokens) || 4096, stream: stream || false } if prompt_caching payload[:system] = format_system_with_caching(system_msgs, chat_msgs) # Mark the last user message for caching (required by Anthropic for conversation caching) if payload[:messages].any? last_user_idx = payload[:messages].rindex { |m| m[:role] == "user" } if last_user_idx content = payload[:messages][last_user_idx][:content] payload[:messages][last_user_idx][:content] = wrap_content_for_caching(content) end end else system_content = format_system_content(system_msgs) payload[:system] = system_content if system_content end tool_defs = format_tools(tools) if tools&.any? payload[:tools] = tool_defs if tool_defs payload[:temperature] = temperature if temperature payload.merge(params) end |
#chat(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/ask/provider/anthropic.rb', line 28 def chat(, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) msgs = .is_a?(Ask::Conversation) ? .to_a : payload = build_request(msgs, model:, tools:, temperature:, stream:, schema:, **params) if stream chat_stream(payload, model, &block) else chat_nonstream(payload, model) end end |
#embed(_texts, model: nil) ⇒ Object
38 39 40 |
# File 'lib/ask/provider/anthropic.rb', line 38 def (_texts, model: nil) raise Ask::CapabilityNotSupported, "Anthropic does not support embeddings" end |
#format_message(msg) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ask/provider/anthropic.rb', line 164 def (msg) role = (msg[:role] || msg["role"]).to_s content = msg[:content] || msg["content"] if msg[:tool_calls] || msg["tool_calls"] tc = msg[:tool_calls] || msg["tool_calls"] return { role:, content:, tool_calls: tc.map { |t| { type: "tool_use", id: t[:id] || t["id"], name: t.dig(:function, :name) || t.dig("function", "name") || t[:name], input: parse_json(t.dig(:function, :arguments) || t.dig("function", "arguments") || t[:arguments] || "{}") } }.compact }.compact end if msg[:tool_call_id] || msg["tool_call_id"] return { role: "user", content: [{ type: "tool_result", tool_use_id: msg[:tool_call_id] || msg["tool_call_id"], content: content || "" }] } end { role:, content: }.compact end |
#format_tools(tools) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/ask/provider/anthropic.rb', line 154 def format_tools(tools) tools.map do |t| { name: t.respond_to?(:name) ? t.name : t[:name], description: t.respond_to?(:description) ? t.description : t[:description], input_schema: t.respond_to?(:parameters) ? t.parameters : (t[:parameters] || { type: "object", properties: {} }) } end end |
#headers ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ask/provider/anthropic.rb', line 20 def headers { "x-api-key" => @config.api_key, "anthropic-version" => "2023-06-01", "Content-Type" => "application/json" } end |
#list_models ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ask/provider/anthropic.rb', line 42 def list_models response = @http.get("v1/models") return [] unless response.success? response.body["data"].map { |m| Ask::ModelInfo.new(id: m["id"], provider: slug) } end |
#parse_error(response) ⇒ Object
49 50 51 52 |
# File 'lib/ask/provider/anthropic.rb', line 49 def parse_error(response) body = response.body rescue nil body&.dig("error", "message") || body&.dig("error", "type") end |
#parse_response(body, model) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ask/provider/anthropic.rb', line 102 def parse_response(body, model) content_blocks = body["content"] || [] text_content = content_blocks.select { |c| c["type"] == "text" }.map { |c| c["text"] }.join tool_blocks = content_blocks.select { |c| c["type"] == "tool_use" } thinking_blocks = content_blocks.select { |c| %w[thinking redacted_thinking].include?(c["type"]) } usage = body["usage"] || {} tool_calls = tool_blocks.map do |tb| { id: tb["id"], type: "function", name: tb["name"], arguments: JSON.generate(tb["input"]) } end = { model: body["model"] || model, stop_reason: body["stop_reason"], stop_sequence: body["stop_sequence"], input_tokens: usage["input_tokens"], output_tokens: usage["output_tokens"], cache_creation_input_tokens: usage["cache_creation_input_tokens"], cache_read_input_tokens: usage["cache_read_input_tokens"], thinking: thinking_blocks.map { |b| b["thinking"] || b["text"] }.compact.join("\n"), raw: body }.compact text = text_content.empty? ? nil : text_content Ask::Message.new(role: :assistant, content: text, tool_calls: tool_calls.empty? ? nil : tool_calls, metadata:) end |
#parse_stream(raw, stream, model, &block) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ask/provider/anthropic.rb', line 129 def parse_stream(raw, stream, model, &block) each_sse_event(raw) do |data| parsed = JSON.parse(data) rescue next case parsed["type"] when "content_block_delta" delta = parsed.dig("delta") next unless delta chunk = Ask::Chunk.new(content: delta["text"]) stream.add(chunk) yield chunk if block_given? when "message_stop" usage = parsed["usage"] || parsed["message"]&.dig("usage") if usage chunk = Ask::Chunk.new(finish_reason: "stop", usage:) stream.add(chunk) yield chunk if block_given? end when "message_start" # Handled by content_block_start instead end end end |