Agent
The Agent
class is the central component of YosrAI that manages AI interactions, message handling, and tool usage. It provides a flexible interface for creating AI agents with different capabilities and behaviors.
Class Overview
from yosrai.core import Agent
agent = Agent(
agent_code="assistant",
agent_name="AI Assistant",
prompt_template="",
instructions_template="",
use_tools=False
)
Constructor Parameters
agent_code
(str): Unique identifier code for the agentagent_name
(str): Display name of the agentprompt_template
(str, optional): Template for generating promptsinstructions_template
(str, optional): Template for system instructionsoutputs
(dict, optional): Configuration for output handlingconfig
(Config, optional): Configuration settingsagent_type
(str, optional): Type of agent (default: 'llm')use_tools
(bool, optional): Enable/disable tool usagellm
(LLM, optional): Language model configuration
Key Methods
act()
async def act(
self,
input: Union[str, Any],
action_name: str = None,
streaming: bool = False,
message_type: MessageType = MessageType.AI,
streaming_callback: Callable = None,
messaging_console: Optional[MessagingConsoleBaseClass] = None,
tools: list = None,
context: Any = {}
)
The act()
method processes input and generates responses using the configured LLM.
interact()
Handles complex interactions including tool usage and template processing.
bind_tools()
Attaches a list of tools that the agent can use during interactions.
Usage Examples
Basic Usage
from yosrai.core import Agent, LLM
# Create an agent
agent = Agent(
agent_code="assistant",
agent_name="AI Assistant",
llm=LLM(provider="openai", model="gpt-3.5-turbo")
)
# Simple interaction
response = await agent.act("What is the weather today?")
Using Templates
agent = Agent(
agent_code="teacher",
agent_name="Math Teacher",
instructions_template="You are a helpful math teacher who explains concepts clearly.",
prompt_template="Please explain the concept of {{ topic }} in simple terms."
)
# Use with context
response = await agent.interact({"topic": "quadratic equations"})
Using Tools
agent = Agent(
agent_code="assistant",
agent_name="Tool Assistant",
use_tools=True
)
# Bind tools
agent.bind_tools([
{"name": "calculator", "func": calculate},
{"name": "weather", "func": get_weather}
])
# Interact with tools
response = await agent.act("What's 2+2?", tools=agent.tools)
Properties
UseTools
: Property to enable/disable tool usagechat_manager
: Cached property providing access to the ChatManager instance
Advanced Features
Output Configuration
The agent supports various output configurations through the outputs
parameter:
agent = Agent(
agent_code="assistant",
agent_name="AI Assistant",
outputs={
"messages": "APPEND_MESSAGES",
"content": "DEFAULT",
"json_data": "JSON"
}
)
State Management
The agent implements __getstate__
and __setstate__
for proper serialization and deserialization, making it suitable for distributed systems and persistence.