Class: RailsLLM::Stream

Inherits:
LLM::Stream
  • Object
show all
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

Instance Method Summary collapse

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_contentObject (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_callsObject (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

#finishvoid

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.

Parameters:

  • content (String)


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.

Parameters:

  • content (String)


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.

Parameters:

  • tool (LLM::Function)
  • error (String, nil)


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.

Parameters:

  • tool (LLM::Function)
  • result (LLM::Function::Return)


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