Class: LLM::Intercept::Multi

Inherits:
LLM::Intercept show all
Defined in:
lib/llm/intercept/multi.rb

Overview

The LLM::Intercept::Multi class combines multiple intercepts into a single intercept.

The first intercept that returns an instance of LLM::Function::Return stops all other intercepts from running, and when an intercept returns nil the next one in the list is called. When all intercepts return nil, the tool is called.

See Also:

Instance Attribute Summary

Attributes inherited from LLM::Intercept

#llm

Instance Method Summary collapse

Methods inherited from LLM::Intercept

inherited, multi, #options

Constructor Details

#initialize(llm, *intercepts) ⇒ Multi

Returns a new instance of Multi.

Parameters:



23
24
25
26
# File 'lib/llm/intercept/multi.rb', line 23

def initialize(llm, *intercepts)
  @llm = llm
  @intercepts = intercepts
end

Instance Method Details

#call(function:, **opts) ⇒ LLM::Function::Return, Object

Returns When this method returns a LLM::Function::Return, object it stops a tool call in its tracks and the Return object is the answer we give to the model for the tool call that was stopped. Any other return value lets the tool call through.

Parameters:

  • function (LLM::Function) —

    A pending function

  • opts (Hash) —

    The contents of intercept_options:

Returns:

  • (LLM::Function::Return, Object) —

    When this method returns a LLM::Function::Return, object it stops a tool call in its tracks and the Return object is the answer we give to the model for the tool call that was stopped. Any other return value lets the tool call through.



31
32
33
34
35
36
37
38
# File 'lib/llm/intercept/multi.rb', line 31

def call(function:, **opts)
  @intercepts.each do |intercept|
    intercept = intercept.new(llm).tap { _1.instance_variable_set(:@context, context) }
    result = intercept.call(function:, **opts)
    return result if LLM::Function::Return === result
  end
  nil
end