Class: ComposableAgents::AiAgents::Agent
- Inherits:
-
PromptDrivenAgent
- Object
- ComposableAgents::Agent
- PromptDrivenAgent
- ComposableAgents::AiAgents::Agent
- Defined in:
- lib/composable_agents/ai_agents/agent.rb
Overview
Agent implementation that uses an ai-agent's AgentRunner. This agent publishes the following run information:
- usage [Hash] Cumulative usage (cost and tokens) of the LLM calls made during the run.
This information is published in realtime, as soon as the AgentRunner completes LLM calls.
Here are the properties it contains:
- cost [Float] Cumulative monetary cost of all LLM calls of the run. This cost is computed by RubyLLM from the model's pricing in its registry: it is best-effort, as models with incomplete pricing information can have their cost underestimated.
- input_tokens [Integer] Cumulative number of input tokens of all LLM calls of the run.
- output_tokens [Integer] Cumulative number of output tokens of all LLM calls of the run.
- cache_read_tokens [Integer] Cumulative number of tokens read from cache by all LLM calls of the run.
- cache_write_tokens [Integer] Cumulative number of tokens written to cache by all LLM calls of the run.
- context_tokens [Integer, nil] Number of context tokens consumed by the last LLM call of the run. This property is not cumulative, as each LLM call contains the full context.
- context_tokens_limit [Integer, nil] Maximum context window limit of the model used, or nil if unknown.
Instance Attribute Summary
Attributes inherited from PromptDrivenAgent
#constraints, #conversation, #objective, #role, #system_instructions
Attributes inherited from ComposableAgents::Agent
Public API collapse
-
#full_name ⇒ String
Return the full name of the agent.
-
#initialize(*args, model: Agents.configuration.default_model, params: {}, handoff_agents: [], **kwargs) ⇒ Agent
constructor
Initialize a new agent with a list of ai-agents' Agents to be used with an AgentRunner.
-
#run(user_instructions: nil, **input_artifacts) ⇒ Hash{Symbol => Object}
Execute the agent to generate some output artifacts based on some input artifacts.
Internal collapse
-
#export_state ⇒ Object
Export the agent state for persistence.
-
#import_state(state) ⇒ Object
Import the agent state from persistence.
Methods inherited from PromptDrivenAgent
#input_artifacts_contracts, #output_artifacts_contracts, #report_error_for_output_artifact, #save_output_artifact
Constructor Details
#initialize(*args, model: Agents.configuration.default_model, params: {}, handoff_agents: [], **kwargs) ⇒ Agent
Initialize a new agent with a list of ai-agents' Agents to be used with an AgentRunner
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/composable_agents/ai_agents/agent.rb', line 30 def initialize( *args, model: Agents.configuration.default_model, params: {}, handoff_agents: [], **kwargs ) super(*args, **kwargs) @model = model @params = params @handoff_agents = handoff_agents @context = {} end |
Instance Method Details
#export_state ⇒ Object
Export the agent state for persistence
70 71 72 |
# File 'lib/composable_agents/ai_agents/agent.rb', line 70 def export_state super.merge(deep_transform_keys(context: Base64.encode64(Marshal.dump(@context)), &:to_s)) end |
#full_name ⇒ String
Return the full name of the agent. This method is intended to be overridden by subclasses to give better full names, tailored to the kind of agent. The full name can be used in logs and traces to better identify the agent.
49 50 51 |
# File 'lib/composable_agents/ai_agents/agent.rb', line 49 def full_name "#{name || 'Unnamed'} (AiAgent #{@model})" end |
#import_state(state) ⇒ Object
Import the agent state from persistence
77 78 79 80 |
# File 'lib/composable_agents/ai_agents/agent.rb', line 77 def import_state(state) super @context = Marshal.load(Base64.decode64(deep_transform_keys(state, &:to_sym)[:context])) end |
#run(user_instructions: nil, **input_artifacts) ⇒ Hash{Symbol => Object}
Execute the agent to generate some output artifacts based on some input artifacts. Also reset the usage statistics accumulated for this run (published realtime in the run info).
60 61 62 63 |
# File 'lib/composable_agents/ai_agents/agent.rb', line 60 def run(user_instructions: nil, **input_artifacts) @llm_call_responses = [] super end |