Class: LLM::EventHandler
- Inherits:
-
Object
- Object
- LLM::EventHandler
- Defined in:
- lib/llm/eventhandler.rb
Instance Method Summary collapse
-
#body ⇒ LLM::Object
Returns a fully constructed response body.
-
#free ⇒ void
Frees parser state after streaming completes.
- #initialize(parser) ⇒ LLM::EventHandler constructor
-
#on_chunk(event, chunk = nil) ⇒ void
Callback for when any of chunk of data is received, regardless of whether it has a field name or not.
-
#on_data(event, chunk = nil) ⇒ void
“data:” event callback.
Constructor Details
#initialize(parser) ⇒ LLM::EventHandler
10 11 12 |
# File 'lib/llm/eventhandler.rb', line 10 def initialize(parser) @parser = parser end |
Instance Method Details
#body ⇒ LLM::Object
Returns a fully constructed response body
48 |
# File 'lib/llm/eventhandler.rb', line 48 def body = @parser.body |
#free ⇒ void
This method returns an undefined value.
Frees parser state after streaming completes.
53 54 55 |
# File 'lib/llm/eventhandler.rb', line 53 def free @parser.free end |
#on_chunk(event, chunk = nil) ⇒ 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).
36 37 38 39 40 41 42 43 |
# File 'lib/llm/eventhandler.rb', line 36 def on_chunk(event, chunk = nil) raw_chunk = chunk || event&.chunk || event return if raw_chunk == "[DONE]" payload = LLM.json.load(raw_chunk) return unless payload @parser.parse!(payload) rescue *LLM.json.parser_error end |
#on_data(event, chunk = nil) ⇒ void
This method returns an undefined value.
“data:” event callback
19 20 21 22 23 24 25 26 |
# File 'lib/llm/eventhandler.rb', line 19 def on_data(event, chunk = nil) value = chunk ? event : event.value return if value == "[DONE]" payload = LLM.json.load(value) return unless payload @parser.parse!(payload) rescue *LLM.json.parser_error end |