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 25 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 18 def <<(chunk) return if @closed text = extract_text(chunk) return self if text.nil? || text.empty? payload = { choices: [{ delta: { content: text } }] } write("data: #{JSON.generate(payload)}\n\n") self end |
#closed? ⇒ Boolean
38 39 40 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 38 def closed? @closed end |
#error(type, message) ⇒ Object
33 34 35 36 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 33 def error(type, ) return if @closed write("event: error\ndata: #{JSON.generate({ error: type, message: })}\n\n") end |
#finish ⇒ Object
27 28 29 30 31 |
# File 'lib/whoosh/streaming/llm_stream.rb', line 27 def finish return if @closed write("data: [DONE]\n\n") @closed = true end |