Class: Cloudflare::AI::Binding
- Inherits:
-
Object
- Object
- Cloudflare::AI::Binding
- Defined in:
- lib/homura/runtime/ai.rb
Instance Attribute Summary collapse
-
#js ⇒ Object
readonly
Returns the value of attribute js.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #chat(prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, **input_options) ⇒ Object
- #chat_text(prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, **input_options) ⇒ Object
-
#initialize(js) ⇒ Binding
constructor
A new instance of Binding.
- #run(model, inputs = nil, options: nil, **input_options) ⇒ Object
- #run_stream(model, inputs = nil, **input_options) ⇒ Object
- #speak(text, model: DEFAULT_SPEAK_MODEL, options: nil, **input_options) ⇒ Object
- #speak_data_url(text, model: DEFAULT_SPEAK_MODEL, options: nil, **input_options) ⇒ Object
- #transcribe(audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, **input_options) ⇒ Object
- #transcribe_text(audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, **input_options) ⇒ Object
Constructor Details
#initialize(js) ⇒ Binding
Returns a new instance of Binding.
48 49 50 |
# File 'lib/homura/runtime/ai.rb', line 48 def initialize(js) @js = js end |
Instance Attribute Details
#js ⇒ Object (readonly)
Returns the value of attribute js.
46 47 48 |
# File 'lib/homura/runtime/ai.rb', line 46 def js @js end |
Instance Method Details
#available? ⇒ Boolean
52 53 54 55 |
# File 'lib/homura/runtime/ai.rb', line 52 def available? js = @js !!`(#{js} !== null && #{js} !== undefined && #{js} !== Opal.nil)` end |
#chat(prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, **input_options) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/homura/runtime/ai.rb', line 71 def chat( prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, ** ) = Cloudflare::AI.(model, ) payload = { messages: Cloudflare::AI.( prompt, messages: , system: system ) }.merge() run(model, payload, options: ) end |
#chat_text(prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, **input_options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/homura/runtime/ai.rb', line 91 def chat_text( prompt = nil, messages: nil, system: nil, model: DEFAULT_CHAT_MODEL, options: nil, ** ) response = chat( prompt, messages: , system: system, model: model, options: , ** ) response = response.__await__ if Cloudflare.js_promise?(response) Cloudflare::AI.extract_text(response) end |
#run(model, inputs = nil, options: nil, **input_options) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/homura/runtime/ai.rb', line 57 def run(model, inputs = nil, options: nil, **) payload = inputs || payload = payload.merge() if inputs.is_a?(Hash) && !.empty? Cloudflare::AI.run(model, payload, binding: @js, options: ) end |
#run_stream(model, inputs = nil, **input_options) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/homura/runtime/ai.rb', line 64 def run_stream(model, inputs = nil, **) payload = inputs || payload = payload.merge() if inputs.is_a?(Hash) && !.empty? run(model, payload.merge(stream: true)) end |
#speak(text, model: DEFAULT_SPEAK_MODEL, options: nil, **input_options) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/homura/runtime/ai.rb', line 136 def speak(text, model: DEFAULT_SPEAK_MODEL, options: nil, **) payload = { text: text.to_s }.merge() response = Cloudflare::AI.speak(model, payload, binding: @js, options: ) response = response.__await__ if Cloudflare.js_promise?(response) response end |
#speak_data_url(text, model: DEFAULT_SPEAK_MODEL, options: nil, **input_options) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/homura/runtime/ai.rb', line 144 def speak_data_url( text, model: DEFAULT_SPEAK_MODEL, options: nil, ** ) payload = { text: text.to_s }.merge() response = Cloudflare::AI.speak_data_url( model, payload, binding: @js, options: ) response = response.__await__ if Cloudflare.js_promise?(response) response.to_s end |
#transcribe(audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, **input_options) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/homura/runtime/ai.rb', line 112 def transcribe( audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, ** ) payload = { audio: Cloudflare::AI.audio_input(audio) }.merge( ) run(model, payload, options: ) end |
#transcribe_text(audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, **input_options) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/homura/runtime/ai.rb', line 124 def transcribe_text( audio, model: DEFAULT_TRANSCRIBE_MODEL, options: nil, ** ) response = transcribe(audio, model: model, options: , **) response = response.__await__ if Cloudflare.js_promise?(response) Cloudflare::AI.extract_text(response) end |