Class: LlmOrchestrator::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_orchestrator/chain.rb

Overview

Chain represents a sequence of processing steps for LLM operations It allows for composing multiple LLM operations together with memory persistence

Instance Method Summary collapse

Constructor Details

#initialize(memory: nil) ⇒ Chain

Returns a new instance of Chain.



6
7
8
9
# File 'lib/llm_orchestrator/chain.rb', line 6

def initialize(memory: nil)
  @steps = []
  @memory = memory || Memory.new
end

Instance Method Details

#add_step(&block) ⇒ Object



11
12
13
14
# File 'lib/llm_orchestrator/chain.rb', line 11

def add_step(&block)
  @steps << block
  self
end

#clear_memoryObject



24
25
26
# File 'lib/llm_orchestrator/chain.rb', line 24

def clear_memory
  @memory.clear
end

#run(input) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/llm_orchestrator/chain.rb', line 16

def run(input)
  @steps.reduce(input) do |result, step|
    output = step.call(result, @memory)
    @memory.add_message("assistant", output) if output.is_a?(String)
    output
  end
end