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 actionaction_description
(str, optional): Description of what the action does- Additional configuration through kwargs
Key Components
Context Management
Sets up or updates the action's context with the provided parameters. This must be called before adding agents or defining workflows.
Agent Management
Adds an agent to the action's workflow. The first agent added becomes the entry point.
Creates a direct connection between two agents in the workflow.
Creates conditional connections between agents based on a condition agent's evaluation.
Tool Integration
Enables tool usage for a specific agent and sets up the necessary workflow connections.
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
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
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
The action maintains state and memory across executions using the MemorySaver
system.