Class: Ask::Providers::Cloudflare
- Inherits:
-
Ask::Provider
- Object
- Ask::Provider
- Ask::Providers::Cloudflare
- Defined in:
- lib/ask/provider/cloudflare.rb
Overview
Cloudflare Workers AI provider. Supports both direct Workers AI and AI Gateway.
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
- #headers ⇒ Object
-
#initialize(config = {}) ⇒ Cloudflare
constructor
A new instance of Cloudflare.
- #list_models ⇒ Object
- #parse_error(response) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Cloudflare
Returns a new instance of Cloudflare.
7 8 9 10 11 |
# File 'lib/ask/provider/cloudflare.rb', line 7 def initialize(config = {}) config = normalize_config(config) super(config) @http = build_http end |
Class Method Details
.capabilities ⇒ Object
54 55 56 |
# File 'lib/ask/provider/cloudflare.rb', line 54 def capabilities { chat: true, streaming: true, vision: true } end |
.configuration_options ⇒ Object
57 |
# File 'lib/ask/provider/cloudflare.rb', line 57 def ; %i[api_key account_id gateway_id]; end |
.configuration_requirements ⇒ Object
58 |
# File 'lib/ask/provider/cloudflare.rb', line 58 def configuration_requirements; %i[api_key account_id]; end |
.slug ⇒ Object
59 |
# File 'lib/ask/provider/cloudflare.rb', line 59 def slug; "cloudflare"; end |
Instance Method Details
#api_base ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/ask/provider/cloudflare.rb', line 13 def api_base if @config.gateway_id "https://gateway.ai.cloudflare.com/v1/#{@config.account_id}/#{@config.gateway_id}" else "https://api.cloudflare.com/client/v4/accounts/#{@config.account_id}/ai/v1" end end |
#chat(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ask/provider/cloudflare.rb', line 25 def chat(, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block) msgs = .is_a?(Ask::Conversation) ? .to_a : endpoint = @config.gateway_id ? "chat/completions" : "run/#{model}" payload = if @config.gateway_id { model: model, messages: msgs.map { |m| { role: (m[:role] || m["role"]).to_s, content: m[:content] || m["content"] } }, stream: stream || false } else { messages: msgs.map { |m| { role: (m[:role] || m["role"]).to_s, content: m[:content] || m["content"] } } } end payload[:temperature] = temperature if temperature payload.merge(params) if stream && @config.gateway_id chat_stream_gateway(endpoint, payload, model, &block) else chat_nonstream(endpoint, payload, model) end end |
#headers ⇒ Object
21 22 23 |
# File 'lib/ask/provider/cloudflare.rb', line 21 def headers { "Content-Type" => "application/json", "Authorization" => "Bearer #{@config.api_key}" }.compact end |
#list_models ⇒ Object
43 44 45 46 |
# File 'lib/ask/provider/cloudflare.rb', line 43 def list_models # Workers AI lists models differently — rely on model catalog [] end |
#parse_error(response) ⇒ Object
48 49 50 51 |
# File 'lib/ask/provider/cloudflare.rb', line 48 def parse_error(response) body = response.body rescue nil body&.dig("errors", 0, "message") || body&.dig("error", "message") end |