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
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

Returns:

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

#finishObject



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