Skip to content

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 agent
  • agent_name (str): Display name of the agent
  • prompt_template (str, optional): Template for generating prompts
  • instructions_template (str, optional): Template for system instructions
  • outputs (dict, optional): Configuration for output handling
  • config (Config, optional): Configuration settings
  • agent_type (str, optional): Type of agent (default: 'llm')
  • use_tools (bool, optional): Enable/disable tool usage
  • llm (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()

async def interact(self, context: Any)

Handles complex interactions including tool usage and template processing.

bind_tools()

def bind_tools(self, tools: list)

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 usage
  • chat_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.