Module: Cloudflare::AI
- Defined in:
- lib/homura/runtime/ai.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_OPTIONS =
Default REST options forwarded to env.AI.run as the third argument.
{}.freeze
- DEFAULT_CHAT_MODEL =
"@cf/moonshotai/kimi-k2.6"- DEFAULT_TRANSCRIBE_MODEL =
"@cf/openai/whisper"- DEFAULT_SPEAK_MODEL =
"@cf/deepgram/aura-1"
Class Method Summary collapse
- .audio_input(audio) ⇒ Object
- .build_messages(prompt = nil, messages: nil, system: nil) ⇒ Object
- .chat_input_options(model, input_options) ⇒ Object
- .extract_text(out) ⇒ Object
- .message_content_text(value) ⇒ Object
- .message_hash_text(value) ⇒ Object
-
.ruby_to_js(val) ⇒ Object
Convert a Ruby value (Hash / Array / String / Numeric / true / false / nil) into a plain JS object suitable for env.AI.run inputs.
-
.run(model, inputs, binding: nil, options: nil, raw_response: false) ⇒ Object
Run a Workers AI model.
- .speak(model, inputs, binding: nil, options: nil) ⇒ Object
- .speak_data_url(model, inputs, binding: nil, options: nil) ⇒ Object
Class Method Details
.audio_input(audio) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/homura/runtime/ai.rb', line 314 def self.audio_input(audio) if audio.respond_to?(:to_uint8_array) uint8 = audio.to_uint8_array return `Array.from(#{uint8})` end return audio if audio.is_a?(Array) if `typeof #{audio} !== 'undefined' && #{audio} instanceof Uint8Array` return `Array.from(#{audio})` end audio end |
.build_messages(prompt = nil, messages: nil, system: nil) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/homura/runtime/ai.rb', line 300 def self.(prompt = nil, messages: nil, system: nil) out = [] out << { role: "system", content: system.to_s } if system if unless .is_a?(Array) raise ArgumentError, "messages must be an Array of chat messages" end out.concat() end out << { role: "user", content: prompt.to_s } unless prompt.nil? raise ArgumentError, "chat requires a prompt or messages" if out.empty? out end |
.chat_input_options(model, input_options) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/homura/runtime/ai.rb', line 326 def self.(model, ) return unless model.to_s == DEFAULT_CHAT_MODEL = .dup key = if .key?(:chat_template_kwargs) :chat_template_kwargs elsif .key?("chat_template_kwargs") "chat_template_kwargs" end template_kwargs = key ? [key] : nil if template_kwargs && !template_kwargs.is_a?(Hash) raise ArgumentError, "chat_template_kwargs must be a Hash" end merged = (template_kwargs || {}).dup unless merged.key?(:thinking) || merged.key?("thinking") merged[:thinking] = false end [key || :chat_template_kwargs] = merged end |
.extract_text(out) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/homura/runtime/ai.rb', line 350 def self.extract_text(out) return out.to_s unless out.is_a?(Hash) if out["choices"].is_a?(Array) && !out["choices"].empty? msg = out["choices"][0].is_a?(Hash) ? out["choices"][0]["message"] : nil text = (msg) return text unless text.empty? end if out["messages"].is_a?(Array) && !out["messages"].empty? msg = out["messages"].find do |entry| entry.is_a?(Hash) && entry["role"].to_s == "assistant" end || out["messages"][0] text = (msg) return text unless text.empty? end %w[text transcription response result output].each do |key| value = (out[key]) return value unless value.empty? end nested = out["result"] return extract_text(nested) if nested.is_a?(Hash) "" end |
.message_content_text(value) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/homura/runtime/ai.rb', line 374 def self.(value) case value when String value when Array value .map { |part| part.is_a?(Hash) ? part["text"].to_s : part.to_s } .join else "" end end |
.message_hash_text(value) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/homura/runtime/ai.rb', line 387 def self.(value) return "" unless value.is_a?(Hash) %w[ content reasoning reasoning_content reasoningContent text ].each do |key| text = (value[key]) return text unless text.empty? end "" end |
.ruby_to_js(val) ⇒ Object
Convert a Ruby value (Hash / Array / String / Numeric / true / false / nil) into a plain JS object suitable for env.AI.run inputs.
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/homura/runtime/ai.rb', line 404 def self.ruby_to_js(val) if `#{val} != null && typeof #{val} === 'object' && #{val}.$$class == null` return val end if val.is_a?(Hash) obj = `({})` val.each do |k, v| ks = k.to_s jv = ruby_to_js(v) `#{obj}[#{ks}] = #{jv}` end obj elsif val.is_a?(Array) arr = `([])` val.each do |v| jv = ruby_to_js(v) `#{arr}.push(#{jv})` end arr elsif val.is_a?(Symbol) val.to_s else val end end |
.run(model, inputs, binding: nil, options: nil, raw_response: false) ⇒ Object
Run a Workers AI model. Returns a JS Promise that resolves to a Ruby Hash for non-streaming calls, or to a Cloudflare::AI::Stream wrapping the JS ReadableStream for streaming calls.
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/homura/runtime/ai.rb', line 171 def self.run(model, inputs, binding: nil, options: nil, raw_response: false) binding = binding.js if defined?(Binding) && `(#{binding} != null && #{binding}.$$class === #{Binding})` # Use a JS-side null check because `binding` may be a raw JS object # (env.AI), which has no Ruby `#nil?` method on the prototype. bound = !`(#{binding} == null)` unless bound raise AIError.new("AI binding not bound (env.AI is null)", model: model) end js_inputs = ruby_to_js(inputs) = ? ruby_to_js() : `({})` ai_binding = binding err_klass = Cloudflare::AIError stream_klass = Cloudflare::AI::Stream # Streaming may be requested either via `inputs[:stream]` (the # newer Workers AI shape) or `options: { stream: true }` (the # 3rd-arg "options" contract). Accept both so callers can use # whichever idiom matches the model docs they're following. streaming = ( inputs.is_a?(Hash) && (inputs[:stream] == true || inputs["stream"] == true) ) || ( .is_a?(Hash) && ([:stream] == true || ["stream"] == true) ) cf = Cloudflare # NOTE: multi-line backtick β Promise works HERE because the # value is assigned to `js_promise` (Opal emits the statement AND # keeps the returned value alive through the local). Do NOT # refactor this so the backtick is the method's last expression # or the Promise will be silently dropped (same pitfall # documented in lib/homura/runtime/{cache,queue}.rb β # Phase 11B audit). js_promise = ` (async function() { var out; try { out = await #{ai_binding}.run(#{model}, #{js_inputs}, #{}); } catch (e) { #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'run' }))); } return out; })() ` js_result = js_promise.__await__ if raw_response RawResponse.new(js_result) elsif streaming # Workers AI returns a ReadableStream<Uint8Array> when stream:true. # Wrap it so the Sinatra route can return it as an SSE body. stream_klass.new(js_result) else cf.js_to_ruby(js_result) end end |
.speak(model, inputs, binding: nil, options: nil) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/homura/runtime/ai.rb', line 233 def self.speak(model, inputs, binding: nil, options: nil) binding = binding.js if defined?(Binding) && `(#{binding} != null && #{binding}.$$class === #{Binding})` bound = !`(#{binding} == null)` unless bound raise AIError.new("AI binding not bound (env.AI is null)", model: model) end js_inputs = ruby_to_js(inputs) = ruby_to_js(( || {}).merge(returnRawResponse: true)) ai_binding = binding err_klass = Cloudflare::AIError js_response = ` (async function() { try { return await #{ai_binding}.run(#{model}, #{js_inputs}, #{}); } catch (e) { #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'speak' }))); } })() `.__await__ content_type = `#{js_response}.headers.get("content-type") || "application/octet-stream"` cache_control = `#{js_response}.headers.get("cache-control")` BinaryBody.new(`#{js_response}.body`, content_type, cache_control) end |
.speak_data_url(model, inputs, binding: nil, options: nil) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/homura/runtime/ai.rb', line 263 def self.speak_data_url(model, inputs, binding: nil, options: nil) binding = binding.js if defined?(Binding) && `(#{binding} != null && #{binding}.$$class === #{Binding})` bound = !`(#{binding} == null)` unless bound raise AIError.new("AI binding not bound (env.AI is null)", model: model) end js_inputs = ruby_to_js(inputs) = ruby_to_js(( || {}).merge(returnRawResponse: true)) ai_binding = binding err_klass = Cloudflare::AIError js_response = ` (async function() { try { return await #{ai_binding}.run(#{model}, #{js_inputs}, #{}); } catch (e) { #{Kernel}.$raise(#{err_klass}.$new(e && e.message ? e.message : String(e), Opal.hash({ model: #{model}, operation: 'speak_data_url' }))); } })() `.__await__ content_type = `#{js_response}.headers.get("content-type") || "application/octet-stream"` ` (async function(resp, ct) { var buf = await resp.arrayBuffer(); var bytes = new Uint8Array(buf); var bin = ''; for (var i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]); return 'data:' + ct + ';base64,' + globalThis.btoa(bin); })(#{js_response}, #{content_type}) `.__await__ end |