Class: RubyLLM::Agent

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/ruby_llm/agent.rb

Overview

Base class for simple, class-configured agents.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs) ⇒ Agent



312
313
314
315
316
317
# File 'lib/ruby_llm/agent.rb', line 312

def initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs)
  input_values, chat_options = self.class.send(:partition_inputs, kwargs)
  @chat = chat || RubyLLM.chat(**self.class.chat_kwargs, **chat_options)
  self.class.send(:apply_configuration, @chat, input_values: input_values.merge(inputs || {}),
                                               persist_instructions:)
end

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



319
320
321
# File 'lib/ruby_llm/agent.rb', line 319

def chat
  @chat
end

Class Method Details

.chat(**kwargs) ⇒ Object



102
103
104
105
106
107
# File 'lib/ruby_llm/agent.rb', line 102

def chat(**kwargs)
  input_values, chat_options = partition_inputs(kwargs)
  chat = RubyLLM.chat(**chat_kwargs, **chat_options)
  apply_configuration(chat, input_values:, persist_instructions: true)
  chat
end

.chat_kwargsObject



98
99
100
# File 'lib/ruby_llm/agent.rb', line 98

def chat_kwargs
  @chat_kwargs || {}
end

.chat_model(value = nil) ⇒ Object



85
86
87
88
89
90
# File 'lib/ruby_llm/agent.rb', line 85

def chat_model(value = nil)
  return @chat_model if value.nil?

  @chat_model = value
  remove_instance_variable(:@resolved_chat_model) if instance_variable_defined?(:@resolved_chat_model)
end

.context(value = nil) ⇒ Object



79
80
81
82
83
# File 'lib/ruby_llm/agent.rb', line 79

def context(value = nil)
  return @context if value.nil?

  @context = value
end

.create(**kwargs) ⇒ Object



109
110
111
# File 'lib/ruby_llm/agent.rb', line 109

def create(**kwargs)
  with_rails_chat_record(:create, **kwargs)
end

.create!(**kwargs) ⇒ Object



113
114
115
# File 'lib/ruby_llm/agent.rb', line 113

def create!(**kwargs)
  with_rails_chat_record(:create!, **kwargs)
end

.find(id, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
# File 'lib/ruby_llm/agent.rb', line 117

def find(id, **kwargs)
  raise ArgumentError, 'chat_model must be configured to use find' unless resolved_chat_model

  input_values, = partition_inputs(kwargs)
  record = resolved_chat_model.find(id)
  apply_configuration(record, input_values:, persist_instructions: false)
  record
end

.headers(**headers, &block) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_llm/agent.rb', line 67

def headers(**headers, &block)
  return @headers || {} if headers.empty? && !block_given?

  @headers = block_given? ? block : headers
end

.inherited(subclass) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/agent.rb', line 14

def inherited(subclass)
  super
  subclass.instance_variable_set(:@chat_kwargs, (@chat_kwargs || {}).dup)
  subclass.instance_variable_set(:@tools, (@tools || []).dup)
  subclass.instance_variable_set(:@instructions, @instructions)
  subclass.instance_variable_set(:@temperature, @temperature)
  subclass.instance_variable_set(:@thinking, @thinking)
  subclass.instance_variable_set(:@params, (@params || {}).dup)
  subclass.instance_variable_set(:@headers, (@headers || {}).dup)
  subclass.instance_variable_set(:@schema, @schema)
  subclass.instance_variable_set(:@context, @context)
  subclass.instance_variable_set(:@chat_model, @chat_model)
  subclass.instance_variable_set(:@input_names, (@input_names || []).dup)
end

.inputs(*names) ⇒ Object



92
93
94
95
96
# File 'lib/ruby_llm/agent.rb', line 92

def inputs(*names)
  return @input_names || [] if names.empty?

  @input_names = names.flatten.map(&:to_sym)
end

.instructions(text = nil, **prompt_locals, &block) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/agent.rb', line 40

def instructions(text = nil, **prompt_locals, &block)
  if text.nil? && prompt_locals.empty? && !block_given?
    @instructions ||= { prompt: 'instructions', locals: {} }
    return @instructions
  end

  @instructions = block || text || { prompt: 'instructions', locals: prompt_locals }
end

.model(model_id = nil, **options) ⇒ Object



29
30
31
32
# File 'lib/ruby_llm/agent.rb', line 29

def model(model_id = nil, **options)
  options[:model] = model_id unless model_id.nil?
  @chat_kwargs = options
end

.params(**params, &block) ⇒ Object



61
62
63
64
65
# File 'lib/ruby_llm/agent.rb', line 61

def params(**params, &block)
  return @params || {} if params.empty? && !block_given?

  @params = block_given? ? block : params
end

.render_prompt(name, chat:, inputs:, locals:) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/ruby_llm/agent.rb', line 139

def render_prompt(name, chat:, inputs:, locals:)
  path = prompt_path_for(name)
  return nil unless File.exist?(path)

  resolved_locals = resolve_prompt_locals(locals, runtime: runtime_context(chat:, inputs:), chat:, inputs:)
  ERB.new(File.read(path)).result_with_hash(resolved_locals)
end

.schema(value = nil, &block) ⇒ Object



73
74
75
76
77
# File 'lib/ruby_llm/agent.rb', line 73

def schema(value = nil, &block)
  return @schema if value.nil? && !block_given?

  @schema = block_given? ? block : value
end

.sync_instructions!(chat_or_id, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby_llm/agent.rb', line 126

def sync_instructions!(chat_or_id, **kwargs)
  raise ArgumentError, 'chat_model must be configured to use sync_instructions!' unless resolved_chat_model

  input_values, = partition_inputs(kwargs)
  record = chat_or_id.is_a?(resolved_chat_model) ? chat_or_id : resolved_chat_model.find(chat_or_id)
  runtime = runtime_context(chat: record, inputs: input_values)
  instructions_value = resolved_instructions_value(record, runtime, inputs: input_values)
  return record if instructions_value.nil?

  record.with_instructions(instructions_value)
  record
end

.temperature(value = nil) ⇒ Object



49
50
51
52
53
# File 'lib/ruby_llm/agent.rb', line 49

def temperature(value = nil)
  return @temperature if value.nil?

  @temperature = value
end

.thinking(effort: nil, budget: nil) ⇒ Object



55
56
57
58
59
# File 'lib/ruby_llm/agent.rb', line 55

def thinking(effort: nil, budget: nil)
  return @thinking if effort.nil? && budget.nil?

  @thinking = { effort: effort, budget: budget }
end

.tools(*tools, &block) ⇒ Object



34
35
36
37
38
# File 'lib/ruby_llm/agent.rb', line 34

def tools(*tools, &block)
  return @tools || [] if tools.empty? && !block_given?

  @tools = block_given? ? block : tools.flatten
end