Class: Luo::Agent

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/luo/agent.rb

Direct Known Subclasses

XinghuoFinalAgent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: nil, action_input: nil, client: nil) ⇒ Agent

Returns a new instance of Agent.



8
9
10
11
12
# File 'lib/luo/agent.rb', line 8

def initialize(context: nil, action_input: nil, client: nil)
  @context = context
  @action_input = action_input
  @client = client
end

Instance Attribute Details

#action_inputObject (readonly)

Returns the value of attribute action_input.



7
8
9
# File 'lib/luo/agent.rb', line 7

def action_input
  @action_input
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/luo/agent.rb', line 7

def client
  @client
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/luo/agent.rb', line 7

def context
  @context
end

Class Method Details

.create_parameter_method(method_name, not_provided = Object.new, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/luo/agent.rb', line 41

def self.create_parameter_method(method_name, not_provided = Object.new, &block)
  define_method(method_name.to_sym) do |content = not_provided|
    if content === not_provided
      class_variable_get("@@#{method_name}")
    else
      content = block.call(content) if block_given?
      class_variable_set("@@#{method_name}", content)
    end
  end
end

.on_call(&block) ⇒ Object

Agent 运行被调用时会调用 on_call 方法,注意如果 on_call 方法返回 nil,那么最终结果就会是 nil



21
22
23
# File 'lib/luo/agent.rb', line 21

def on_call(&block)
  define_method(:call, &block)
end

.on_call_with_final_result(&block) ⇒ Object Also known as: on_call_with_fallback

Agent 运行被调用时会调用 on_call_with_final_result 方法,注意如果 on_call_with_final_result 方法返回 nil,那么最终结果就会是调用一次大模型来获取结果



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/luo/agent.rb', line 27

def on_call_with_final_result(&block)
  define_method(:call) do
    result = instance_eval(&block)
    if result.nil?
      messages = context.messages.to_a[0...-1] + [{role: :user, content: context.user_input}]
      context.final_result = client&.chat(messages)
    else
      context.final_result = result
    end
  end
end

Instance Method Details

#callObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/luo/agent.rb', line 14

def call
  raise NotImplementedError, "call method must be implemented in subclass"
end