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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/homura/runtime/ai.rb', line 77 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/homura/runtime/ai.rb', line 96 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 63 64 65 |
# File 'lib/homura/runtime/ai.rb', line 57 def run(model, inputs = nil, options: nil, **) payload = inputs || if inputs.is_a?(Hash) && !.empty? payload = payload.merge() end Cloudflare::AI.run(model, payload, binding: @js, options: ) end |
#run_stream(model, inputs = nil, **input_options) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/homura/runtime/ai.rb', line 67 def run_stream(model, inputs = nil, **) payload = inputs || if inputs.is_a?(Hash) && !.empty? payload = payload.merge() end run(model, payload.merge(stream: true)) end |
#speak(text, model: DEFAULT_SPEAK_MODEL, options: nil, **input_options) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/homura/runtime/ai.rb', line 139 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/homura/runtime/ai.rb', line 146 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
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/homura/runtime/ai.rb', line 116 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
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/homura/runtime/ai.rb', line 128 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 |