Class: LLM::Stream::IO

Inherits:
LLM::Stream show all
Defined in:
lib/llm/stream/io.rb

Overview

An LLM::Stream::IO wraps an object that responds to #<< and forwards streamed content to it.

This enables any object that implements #<< - such as an IO, StringIO, or a custom logger - to be used as a stream target. try creates instances of this class automatically when given an IO-like object.

Examples:

Using an IO object as a stream

File.open("output.txt", "w") do |file|
  ctx.ask("Tell me a story", stream: file)
end

Instance Method Summary collapse

Methods inherited from LLM::Stream

#__find__, #ctx, #enabled?, #extra, #on_compaction, #on_compaction_finish, #on_reasoning_content, #on_tool_call, #on_tool_return, #on_transform, #on_transform_finish, #queue, try, #wait

Constructor Details

#initialize(io) ⇒ IO

Returns a new instance of IO.

Parameters:

  • io (#<<)

    Any object that implements #<<.



21
22
23
# File 'lib/llm/stream/io.rb', line 21

def initialize(io)
  @io = io
end

Instance Method Details

#<<(content) ⇒ void

This method returns an undefined value.

Forwards streamed content to the wrapped IO object. Redefined here because alias_method in the parent class captures the base implementation, not the overridden one.

Parameters:

  • content (String)


39
40
41
# File 'lib/llm/stream/io.rb', line 39

def <<(content)
  on_content(content)
end

#on_content(content) ⇒ void

This method returns an undefined value.

Writes a chunk of content to the wrapped IO object.

Parameters:

  • content (String)


29
30
31
# File 'lib/llm/stream/io.rb', line 29

def on_content(content)
  @io << content
end