Class: Ask::Providers::Anthropic
- Inherits:
-
Ask::Provider
- Object
- Ask::Provider
- Ask::Providers::Anthropic
- 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
- #chat(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) ⇒ Object
- #embed(_texts, model: nil) ⇒ Object
- #headers ⇒ Object
-
#initialize(config = {}) ⇒ Anthropic
constructor
A new instance of Anthropic.
- #list_models ⇒ Object
- #parse_error(response) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Anthropic
Returns a new instance of Anthropic.
7 8 9 10 11 |
# File 'lib/ask/provider/anthropic.rb', line 7 def initialize(config = {}) config = normalize_config(config) super(config) @http = build_http end |
Class Method Details
.capabilities ⇒ Object
51 52 53 |
# File 'lib/ask/provider/anthropic.rb', line 51 def capabilities { chat: true, streaming: true, tool_calls: true, vision: true, thinking: true, prompt_caching: true, structured_output: true } end |
.configuration_options ⇒ Object
54 |
# File 'lib/ask/provider/anthropic.rb', line 54 def ; %i[api_key api_base]; end |
.configuration_requirements ⇒ Object
55 |
# File 'lib/ask/provider/anthropic.rb', line 55 def configuration_requirements; %i[api_key]; end |
.slug ⇒ Object
56 |
# File 'lib/ask/provider/anthropic.rb', line 56 def slug; "anthropic"; end |
Instance Method Details
#api_base ⇒ Object
13 14 15 |
# File 'lib/ask/provider/anthropic.rb', line 13 def api_base @config.api_base || "https://api.anthropic.com" end |
#chat(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/ask/provider/anthropic.rb', line 25 def chat(, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) msgs = .is_a?(Ask::Conversation) ? .to_a : payload = build_chat_payload(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
35 36 37 |
# File 'lib/ask/provider/anthropic.rb', line 35 def (_texts, model: nil) raise Ask::CapabilityNotSupported, "Anthropic does not support embeddings" end |
#headers ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/ask/provider/anthropic.rb', line 17 def headers { "x-api-key" => @config.api_key, "anthropic-version" => "2023-06-01", "Content-Type" => "application/json" } end |
#list_models ⇒ Object
39 40 41 42 43 |
# File 'lib/ask/provider/anthropic.rb', line 39 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
45 46 47 48 |
# File 'lib/ask/provider/anthropic.rb', line 45 def parse_error(response) body = response.body rescue nil body&.dig("error", "message") || body&.dig("error", "type") end |