Class: Legion::Extensions::Llm::Agent
- Inherits:
-
Object
- Object
- Legion::Extensions::Llm::Agent
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/legion/extensions/llm/agent.rb
Overview
Base class for simple, class-configured agents.
Instance Attribute Summary collapse
-
#chat ⇒ Object
readonly
Returns the value of attribute chat.
Class Method Summary collapse
- .chat(**kwargs) ⇒ Object
- .chat_kwargs ⇒ Object
- .chat_model(value = nil) ⇒ Object
- .context(value = nil) ⇒ Object
- .create ⇒ Object
- .create! ⇒ Object
- .find(id, **kwargs) ⇒ Object
- .headers(**headers, &block) ⇒ Object
- .inherited(subclass) ⇒ Object
- .inputs(*names) ⇒ Object
- .instructions(text = nil, **prompt_locals, &block) ⇒ Object
- .model(model_id = nil, **options) ⇒ Object
- .params(**params, &block) ⇒ Object
- .render_prompt(name, chat:, inputs:, locals:) ⇒ Object
- .schema(value = nil, &block) ⇒ Object
- .sync_instructions!(chat_or_id, **kwargs) ⇒ Object
- .temperature(value = nil) ⇒ Object
- .thinking(effort: nil, budget: nil) ⇒ Object
- .tools(*tools, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs) ⇒ Agent
constructor
A new instance of Agent.
Constructor Details
#initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs) ⇒ Agent
Returns a new instance of Agent.
349 350 351 352 353 354 |
# File 'lib/legion/extensions/llm/agent.rb', line 349 def initialize(chat: nil, inputs: nil, persist_instructions: true, **kwargs) input_values, = self.class.send(:partition_inputs, kwargs) @chat = chat || Legion::Extensions::Llm.chat(**self.class.chat_kwargs, **) self.class.send(:apply_configuration, @chat, input_values: input_values.merge(inputs || {}), persist_instructions:) end |
Instance Attribute Details
#chat ⇒ Object (readonly)
Returns the value of attribute chat.
356 357 358 |
# File 'lib/legion/extensions/llm/agent.rb', line 356 def chat @chat end |
Class Method Details
.chat(**kwargs) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/legion/extensions/llm/agent.rb', line 105 def chat(**kwargs) input_values, = partition_inputs(kwargs) chat = Legion::Extensions::Llm.chat(**chat_kwargs, **) apply_configuration(chat, input_values:, persist_instructions: true) chat end |
.chat_kwargs ⇒ Object
101 102 103 |
# File 'lib/legion/extensions/llm/agent.rb', line 101 def chat_kwargs @chat_kwargs || {} end |
.chat_model(value = nil) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/legion/extensions/llm/agent.rb', line 88 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
82 83 84 85 86 |
# File 'lib/legion/extensions/llm/agent.rb', line 82 def context(value = nil) return @context if value.nil? @context = value end |
.create ⇒ Object
112 113 114 |
# File 'lib/legion/extensions/llm/agent.rb', line 112 def create(**) with_chat_record(:create, **) end |
.create! ⇒ Object
116 117 118 |
# File 'lib/legion/extensions/llm/agent.rb', line 116 def create!(**) with_chat_record(:create!, **) end |
.find(id, **kwargs) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/legion/extensions/llm/agent.rb', line 120 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
70 71 72 73 74 |
# File 'lib/legion/extensions/llm/agent.rb', line 70 def headers(**headers, &block) return @headers || {} if headers.empty? && !block_given? @headers = block_given? ? block : headers end |
.inherited(subclass) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/legion/extensions/llm/agent.rb', line 17 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
95 96 97 98 99 |
# File 'lib/legion/extensions/llm/agent.rb', line 95 def inputs(*names) return @input_names || [] if names.empty? @input_names = names.flatten.map(&:to_sym) end |
.instructions(text = nil, **prompt_locals, &block) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/llm/agent.rb', line 43 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
32 33 34 35 |
# File 'lib/legion/extensions/llm/agent.rb', line 32 def model(model_id = nil, **) [:model] = model_id unless model_id.nil? @chat_kwargs = end |
.params(**params, &block) ⇒ Object
64 65 66 67 68 |
# File 'lib/legion/extensions/llm/agent.rb', line 64 def params(**params, &block) return @params || {} if params.empty? && !block_given? @params = block_given? ? block : params end |
.render_prompt(name, chat:, inputs:, locals:) ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/legion/extensions/llm/agent.rb', line 144 def render_prompt(name, chat:, inputs:, locals:) path = prompt_path_for(name) unless File.exist?(path) raise Legion::Extensions::Llm::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 |
.schema(value = nil, &block) ⇒ Object
76 77 78 79 80 |
# File 'lib/legion/extensions/llm/agent.rb', line 76 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
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/legion/extensions/llm/agent.rb', line 130 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 |
.temperature(value = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/legion/extensions/llm/agent.rb', line 52 def temperature(value = nil) return @temperature if value.nil? @temperature = value end |
.thinking(effort: nil, budget: nil) ⇒ Object
58 59 60 61 62 |
# File 'lib/legion/extensions/llm/agent.rb', line 58 def thinking(effort: nil, budget: nil) return @thinking if effort.nil? && budget.nil? @thinking = { effort: effort, budget: budget } end |
.tools(*tools, &block) ⇒ Object
37 38 39 40 41 |
# File 'lib/legion/extensions/llm/agent.rb', line 37 def tools(*tools, &block) return @tools || [] if tools.empty? && !block_given? @tools = block_given? ? block : tools.flatten end |