Class: RailsLLM::Stream
- Inherits:
-
LLM::Stream
- Object
- LLM::Stream
- RailsLLM::Stream
- Defined in:
- lib/rails_llm/stream.rb
Overview
Stream enriches llm.rb’s streaming with structured events for the chat UI. Captures content, reasoning, tool calls, and tool returns as structured events.
Instance Attribute Summary collapse
-
#reasoning_content ⇒ Object
readonly
Returns the value of attribute reasoning_content.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Instance Method Summary collapse
- #finish ⇒ void
-
#initialize(stream) ⇒ Stream
constructor
A new instance of Stream.
- #on_content(content) ⇒ void
- #on_reasoning_content(content) ⇒ void
- #on_tool_call(tool, error) ⇒ void
- #on_tool_return(tool, result) ⇒ void
Constructor Details
#initialize(stream) ⇒ Stream
Returns a new instance of Stream.
13 14 15 16 17 18 |
# File 'lib/rails_llm/stream.rb', line 13 def initialize(stream) @stream = stream @reasoning_content = +"" @tool_calls = [] @buffer = +"" end |
Instance Attribute Details
#reasoning_content ⇒ Object (readonly)
Returns the value of attribute reasoning_content.
11 12 13 |
# File 'lib/rails_llm/stream.rb', line 11 def reasoning_content @reasoning_content end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
11 12 13 |
# File 'lib/rails_llm/stream.rb', line 11 def tool_calls @tool_calls end |
Instance Method Details
#finish ⇒ void
This method returns an undefined value.
59 60 61 |
# File 'lib/rails_llm/stream.rb', line 59 def finish write(type: "done") end |
#on_content(content) ⇒ void
This method returns an undefined value.
23 24 25 26 |
# File 'lib/rails_llm/stream.rb', line 23 def on_content(content) @buffer << content write(type: "content", content: RailsLLM.markdown(@buffer)) end |
#on_reasoning_content(content) ⇒ void
This method returns an undefined value.
31 32 33 |
# File 'lib/rails_llm/stream.rb', line 31 def on_reasoning_content(content) @reasoning_content << content end |
#on_tool_call(tool, error) ⇒ void
This method returns an undefined value.
39 40 41 42 43 44 45 46 47 |
# File 'lib/rails_llm/stream.rb', line 39 def on_tool_call(tool, error) if error queue << error else @tool_calls << {name: tool.name, arguments: tool.arguments} write(type: "tool_call", name: tool.name, arguments: tool.arguments) queue << ctx.spawn(tool, :call) end end |
#on_tool_return(tool, result) ⇒ void
This method returns an undefined value.
53 54 55 |
# File 'lib/rails_llm/stream.rb', line 53 def on_tool_return(tool, result) write(type: "tool_return", name: tool.name, result: result.value) end |