Class: Whoosh::Streaming::LlmStream

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/streaming/llm_stream.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.headersObject



9
10
11
# File 'lib/whoosh/streaming/llm_stream.rb', line 9

def self.headers
  SSE.headers
end

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

Returns:

  • (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, message)
  return if @closed
  write("event: error\ndata: #{JSON.generate({ error: type, message: message })}\n\n")
end

#finishObject



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