Class: Whoosh::Streaming::LlmStream
- Inherits:
-
Object
- Object
- Whoosh::Streaming::LlmStream
- Defined in:
- lib/whoosh/streaming/llm_stream.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<<(chunk) ⇒ Object
- #closed? ⇒ Boolean
- #error(type, message) ⇒ Object
- #finish ⇒ Object
-
#initialize(io) ⇒ LlmStream
constructor
A new instance of LlmStream.
Constructor Details
#initialize(io) ⇒ LlmStream
Returns a new instance of LlmStream.
13 14 15 16 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 13 def initialize(io) @io = io @closed = false end |
Class Method Details
Instance Method Details
#<<(chunk) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 18 def <<(chunk) return if @closed text = chunk.respond_to?(:text) ? chunk.text : chunk.to_s payload = { choices: [{ delta: { content: text } }] } write("data: #{JSON.generate(payload)}\n\n") self end |
#closed? ⇒ Boolean
37 38 39 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 37 def closed? @closed end |
#error(type, message) ⇒ Object
32 33 34 35 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 32 def error(type, ) return if @closed write("event: error\ndata: #{JSON.generate({ error: type, message: })}\n\n") end |
#finish ⇒ Object
26 27 28 29 30 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 26 def finish return if @closed write("data: [DONE]\n\n") @closed = true end |