Class: Ask::Providers::Cloudflare

Inherits:
Ask::Provider
  • Object
show all
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

Instance Method Summary collapse

Methods included from LLM::ProviderConfig

#format_tools

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

.capabilitiesObject



51
52
53
# File 'lib/ask/provider/cloudflare.rb', line 51

def capabilities
  { chat: true, streaming: true, vision: true }
end

.configuration_optionsObject



55
# File 'lib/ask/provider/cloudflare.rb', line 55

def configuration_options; %i[api_key account_id gateway_id]; end

.configuration_requirementsObject



56
# File 'lib/ask/provider/cloudflare.rb', line 56

def configuration_requirements; %i[api_key account_id]; end

.slugObject



49
# File 'lib/ask/provider/cloudflare.rb', line 49

def slug; "cloudflare"; end

Instance Method Details

#api_baseObject



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.}/#{@config.gateway_id}"
  else
    "https://api.cloudflare.com/client/v4/accounts/#{@config.}/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(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params)
  if @config.gateway_id
    payload = {
      model:,
      messages: messages.map { |m| format_message(m) },
      stream: stream || false
    }
  else
    payload = {
      messages: messages.map { |m| format_message(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(messages, model:, tools: nil, temperature: nil, stream: nil, schema: nil, **params, &block)
  msgs = messages.is_a?(Ask::Conversation) ? messages.to_a : messages
  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 format_message(msg)
  { role: (msg[:role] || msg["role"]).to_s, content: msg[:content] || msg["content"] }
end

#headersObject



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_modelsObject



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