class RubyLLM::Agent
Base class for simple, class-configured agents.
Attributes
Public Class Methods
Source
# File lib/ruby_llm/agent.rb, line 103 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
Source
# File lib/ruby_llm/agent.rb, line 86 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
Source
# File lib/ruby_llm/agent.rb, line 80 def context(value = nil) return @context if value.nil? @context = value end
Source
# File lib/ruby_llm/agent.rb, line 110 def create(**kwargs) with_rails_chat_record(:create, **kwargs) end
Source
# File lib/ruby_llm/agent.rb, line 114 def create!(**kwargs) with_rails_chat_record(:create!, **kwargs) end
Source
# File lib/ruby_llm/agent.rb, line 118 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
Source
# File lib/ruby_llm/agent.rb, line 68 def headers(**headers, &block) return @headers || {} if headers.empty? && !block_given? @headers = block_given? ? block : headers end
Source
# File lib/ruby_llm/agent.rb, line 15 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
Calls superclass method
Source
# File lib/ruby_llm/agent.rb, line 93 def inputs(*names) return @input_names || [] if names.empty? @input_names = names.flatten.map(&:to_sym) end
# File lib/ruby_llm/agent.rb, line 41 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
# File lib/ruby_llm/agent.rb, line 30 def model(model_id = nil, **options) options[:model] = model_id unless model_id.nil? @chat_kwargs = options end
# File lib/ruby_llm/agent.rb, line 352 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
Source
# File lib/ruby_llm/agent.rb, line 62 def params(**params, &block) return @params || {} if params.empty? && !block_given? @params = block_given? ? block : params end
# File lib/ruby_llm/agent.rb, line 143 def render_prompt(name, chat:, inputs:, locals:) path = prompt_path_for(name) unless File.exist?(path) raise RubyLLM::PromptNotFoundError, "Prompt file not found for #{self}: #{path}. Create the file or use inline instructions." end resolved_locals = resolve_prompt_locals(locals, runtime: runtime_context(chat:, inputs:), chat:, inputs:) ERB.new(File.read(path)).result_with_hash(resolved_locals) end
Source
# File lib/ruby_llm/agent.rb, line 74 def schema(value = nil, &block) return @schema if value.nil? && !block_given? @schema = block_given? ? block : value end
# File lib/ruby_llm/agent.rb, line 129 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) apply_assume_model_exists(record) 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
Mutates persisted instructions on the configured chat record.
Source
# File lib/ruby_llm/agent.rb, line 50 def temperature(value = nil) return @temperature if value.nil? @temperature = value end
# File lib/ruby_llm/agent.rb, line 56 def thinking(effort: nil, budget: nil) return @thinking if effort.nil? && budget.nil? @thinking = { effort: effort, budget: budget } end
Source
# File lib/ruby_llm/agent.rb, line 35 def tools(*tools, &block) return @tools || [] if tools.empty? && !block_given? @tools = block_given? ? block : tools.flatten end