Module: ActiveAgent::Streaming

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_agent/concerns/streaming.rb

Overview

Provides streaming callback support for agent classes.

Callbacks can be registered for three points in the streaming lifecycle:

  • on_stream_open - invoked before the first chunk

  • on_stream - invoked for every chunk received

  • on_stream_close - invoked after the final chunk

Callbacks automatically receive a StreamChunk parameter if they accept arguments.

Examples:

Basic usage with chunk parameter

class MyPrompt < ActiveAgent::Base
  on_stream_open :setup_stream
  on_stream :log_chunk
  on_stream_close :cleanup_stream

  private

  def setup_stream(chunk)
    puts "Stream opening..."
    puts "First message: #{chunk.message}"
  end

  def log_chunk(chunk)
    print chunk.delta if chunk.delta
  end

  def cleanup_stream(chunk)
    puts "\nStream complete!"
  end
end

Usage without chunk parameter

class MyPrompt < ActiveAgent::Base
  on_stream_open :initialize_counter
  on_stream :increment_counter
  on_stream_close :log_total

  private

  def initialize_counter
    @count = 0
  end

  def increment_counter
    @count += 1
  end

  def log_total
    puts "Total chunks: #{@count}"
  end
end

Using blocks

class MyPrompt < ActiveAgent::Base
  on_stream do |chunk|
    Rails.logger.info("Received: #{chunk.delta}")
  end
end

With callback options

class MyPrompt < ActiveAgent::Base
  on_stream :log_chunk, if: :debug_mode?
  on_stream_close :save_response, unless: :test_environment?
end

Defined Under Namespace

Classes: StreamChunk