Skip to content

Action

The Action class is a powerful component in YosrAI that orchestrates complex workflows between multiple agents, tools, and states. It provides a graph-based system for defining agent interactions, managing context, and controlling execution flow.

Class Overview

from yosrai.core import Action, Agent

# Create an action
action = Action(
    action_name="Customer Support",
    action_description="Handle customer inquiries with multiple agents"
)

# Set up context and add agents
action.Context(messages=[])
action.add_agent(support_agent)
action.add_agent(specialist_agent)

Constructor Parameters

  • action_name (str): Name of the action
  • action_description (str, optional): Description of what the action does
  • Additional configuration through kwargs

Key Components

Context Management

def Context(self, **kwargs)

Sets up or updates the action's context with the provided parameters. This must be called before adding agents or defining workflows.

Agent Management

def add_agent(self, agent: Agent)

Adds an agent to the action's workflow. The first agent added becomes the entry point.

def add_link(self, from_agent: Agent, to_agent: Any)

Creates a direct connection between two agents in the workflow.

def add_link_dynamic(self, from_agent: Agent, to_agents: list[Any], condition_agent: Agent)

Creates conditional connections between agents based on a condition agent's evaluation.

Tool Integration

def enable_agent_tools(self, agent)

Enables tool usage for a specific agent and sets up the necessary workflow connections.

def add_tool(self, tool)

Adds a tool to the action's available tools.

Usage Examples

Basic Sequential Workflow

from yosrai.core import Action, Agent, LLM

# Create agents
greeter = Agent(
    agent_code="greeter",
    agent_name="Greeting Agent",
    llm=LLM(provider="openai")
)

processor = Agent(
    agent_code="processor",
    agent_name="Processing Agent",
    llm=LLM(provider="openai")
)

# Create action
action = Action(
    action_name="Customer Greeting",
    action_description="Greet and process customer request"
)

# Set up workflow
action.Context(messages=[])
action.add_agent(greeter)
action.add_agent(processor)
action.add_link(greeter, processor)
action.add_link(processor, "END")

# Run the workflow
await action.run()

Workflow with Tools

# Create tools
calculator_tool = {
    "name": "calculator",
    "func": calculate
}

# Add tool to action
action.add_tool(calculator_tool)
action.enable_agent_tools(math_agent)

Dynamic Routing

# Create condition agent
router = Agent(
    agent_code="router",
    agent_name="Routing Agent",
    llm=LLM(provider="openai")
)

# Set up dynamic routing
action.add_link_dynamic(
    from_agent=greeter,
    to_agents=[support_agent, sales_agent],
    condition_agent=router
)

Visualization and Debugging

The Action class provides several methods for inspecting and visualizing workflows:

Mermaid Graph Generation

# Generate a Mermaid.js graph of the workflow
graph = action.graph_mermaid()

Context and Message Inspection

# Print action details
action.pretty_print()

# Print messages in context
action.print_messages()

# Print full context
action.print_context()

# Print specific context parameter
action.print_context_param("messages")

LLM Configuration

# Update LLM settings for all agents
action.update_llm(provider="anthropic", model="claude-2")

Advanced Features

State Management

The Action class uses StateGraph from langgraph for workflow management:

  • Automatic state tracking
  • Conditional branching
  • Memory persistence
  • Parallel execution support

Error Handling

The class includes built-in validation and error checking:

  • Context validation before operations
  • Agent configuration validation
  • Workflow integrity checks

Memory Management

# Custom session handling
await action.run(session_id="customer_123")

The action maintains state and memory across executions using the MemorySaver system.