Class: LLM::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ LLM::EventHandler

Parameters:

  • parser (#parse!)


10
11
12
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb', line 10

def initialize(parser)
  @parser = parser
end

Instance Method Details

#bodyLLM::Object

Returns a fully constructed response body

Returns:



42
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb', line 42

def body = @parser.body

#on_chunk(event) ⇒ void

This method returns an undefined value.

Callback for when any of chunk of data is received, regardless of whether it has a field name or not. Primarily for ollama, which does emit Server-Sent Events (SSE).

Parameters:



32
33
34
35
36
37
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb', line 32

def on_chunk(event)
  return if event.end?
  chunk = JSON.parse(event.chunk)
  @parser.parse!(chunk)
rescue JSON::ParserError
end

#on_data(event) ⇒ void

This method returns an undefined value.

“data:” event callback

Parameters:



18
19
20
21
22
23
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb', line 18

def on_data(event)
  return if event.end?
  chunk = JSON.parse(event.value)
  @parser.parse!(chunk)
rescue JSON::ParserError
end