Class: Ask::Providers::Cloudflare
- Inherits:
-
Ask::Provider
- Object
- Ask::Provider
- Ask::Providers::Cloudflare
- Includes:
- LLM::ProviderConfig, LLM::SSEBuffer
- 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
-
#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
- #format_message(msg) ⇒ Object
- #headers ⇒ Object
-
#initialize(config = {}) ⇒ Cloudflare
constructor
A new instance of Cloudflare.
- #list_models ⇒ Object
- #parse_error(response) ⇒ Object
- #parse_response(body, model) ⇒ Object
- #parse_stream(raw, stream, model, &block) ⇒ Object
Methods included from LLM::ProviderConfig
Methods included from LLM::SSEBuffer
#each_sse_event, #init_sse_buffer
Constructor Details
#initialize(config = {}) ⇒ Cloudflare
Returns a new instance of Cloudflare.
10 11 12 13 14 |
# File 'lib/ask/provider/cloudflare.rb', line 10 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/cloudflare.rb', line 51 def capabilities { chat: true, streaming: true, vision: true } end |
.configuration_options ⇒ Object
55 |
# File 'lib/ask/provider/cloudflare.rb', line 55 def ; %i[api_key account_id gateway_id]; end |
.configuration_requirements ⇒ Object
56 |
# File 'lib/ask/provider/cloudflare.rb', line 56 def configuration_requirements; %i[api_key account_id]; end |
.slug ⇒ Object
49 |
# File 'lib/ask/provider/cloudflare.rb', line 49 def slug; "cloudflare"; end |
Instance Method Details
#api_base ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/ask/provider/cloudflare.rb', line 16 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 |
#build_request(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) ⇒ Object
--- Config transformation contract ---
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ask/provider/cloudflare.rb', line 61 def build_request(, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params) if @config.gateway_id payload = { model:, messages: .map { |m| (m) }, stream: stream || false } else payload = { messages: .map { |m| (m) } } end 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 37 |
# File 'lib/ask/provider/cloudflare.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) endpoint = @config.gateway_id ? "chat/completions" : "run/#{model}" if stream && @config.gateway_id chat_stream_gateway(endpoint, payload, model, &block) else chat_nonstream(endpoint, payload, model) end end |
#format_message(msg) ⇒ Object
96 97 98 |
# File 'lib/ask/provider/cloudflare.rb', line 96 def (msg) { role: (msg[:role] || msg["role"]).to_s, content: msg[:content] || msg["content"] } end |
#headers ⇒ Object
24 25 26 |
# File 'lib/ask/provider/cloudflare.rb', line 24 def headers { "Content-Type" => "application/json", "Authorization" => "Bearer #{@config.api_key}" }.compact end |
#list_models ⇒ Object
39 40 41 |
# File 'lib/ask/provider/cloudflare.rb', line 39 def list_models [] end |
#parse_error(response) ⇒ Object
43 44 45 46 |
# File 'lib/ask/provider/cloudflare.rb', line 43 def parse_error(response) body = response.body rescue nil body&.dig("errors", 0, "message") || body&.dig("error", "message") end |
#parse_response(body, model) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/ask/provider/cloudflare.rb', line 77 def parse_response(body, model) if @config.gateway_id parse_openai_response(body, model) else result = body["result"] || {} Ask::Message.new(role: :assistant, content: result["response"], metadata: { model:, raw: body }) end end |
#parse_stream(raw, stream, model, &block) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/ask/provider/cloudflare.rb', line 86 def parse_stream(raw, stream, model, &block) each_sse_event(raw) do |data| parsed = JSON.parse(data) rescue next delta = parsed.dig("choices", 0, "delta") || {} chunk = Ask::Chunk.new(content: delta["content"]) stream.add(chunk) yield chunk if block_given? end end |