Class: ComposableAgents::AiAgents::Agent

Inherits:
PromptDrivenAgent show all
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

#name, #runs_info

Public API collapse

Internal collapse

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

Parameters:

  • model (String) (defaults to: Agents.configuration.default_model)

    Model to be used

  • params (Hash{Symbol => Object}) (defaults to: {})

    Additional parameters to give to the ai-agents' agent

  • handoff_agents (Array<Agents::Agent>) (defaults to: [])

    The list of additional agents that can be used for handoffs.



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_stateObject

Export the agent state for persistence

Returns:

  • (Object)

    Serialized state that can be marshalled to JSON



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_nameString

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.

Returns:

  • (String)

    The agent's full name



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

Parameters:

  • state (Object)

    Serialized state



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).

Parameters:

  • user_instructions (Object, nil) (defaults to: nil)

    Instructions for the user prompt, that will be rendered. The kind of instructions that can be given are defined by the Instructions's constructor (see Instructions#initialize).

  • input_artifacts (Hash{Symbol => Object})

    The input artifacts content, per artifact name

Returns:

  • (Hash{Symbol => Object})

    The output artifacts



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