Assistant with Tools Example
This example demonstrates how to create an AI assistant that can use external tools to enhance its capabilities. It shows how to integrate custom functions and enable the agent to use them during interactions.
Overview
The Assistant with Tools example shows how to: - Create an assistant with tool integration - Define and add custom tools - Enable tool usage for agents - Handle tool-based interactions
Implementation
Here's the complete implementation:
from yosrai import Agent, Action, ContextFunctions
# Define tools
def multiply(a: int, b: int) -> int:
"""Multiply a and b."""
return a * b
def add(a: int, b: int) -> int:
"""Adds a and b."""
return a + b
# Create assistant agent
assistant_agent = Agent(
agent_code='assistant_agent',
agent_name='Assistant Agent',
instructions_template='You are AI Assistant, you should chat friendly and reply to the user input, do not exceed {{ reply_words }} words in your reply',
prompt_template='{{ query }}',
outputs={'output': ContextFunctions.DEFAULT, 'messages': ContextFunctions.APPEND_MESSAGES}
)
# Set up action with tools
action = Action('Assistant with tools')
action.Context(query='', output='', reply_words=30)
action.add_agent(assistant_agent)
action.add_tool(multiply)
action.add_tool(add)
action.add_link(assistant_agent, "END")
action.enable_agent_tools(assistant_agent)
Workflow Visualization
The workflow shows how the assistant can interact with tools:
---
title: Assistant with tools
---
graph LR
START((Start)) --> assistant_agent[Assistant Agent]
assistant_agent(Assistant Agent) --> END((End))
assistant_agent(Assistant Agent) --> TOOLS{Tools}
TOOLS{Tools} --> assistant_agent[Assistant Agent]
Key Components
Action Setup
The action is created with a name that indicates tool capability:
Tool Definitions
Custom tools are defined as Python functions with clear documentation:
def multiply(a: int, b: int) -> int:
"""Multiply a and b."""
return a * b
def add(a: int, b: int) -> int:
"""Adds a and b."""
return a + b
Assistant Configuration
The assistant is configured with: - Friendly chat instructions - Word limit control - Standard output handling
assistant_agent = Agent(
agent_code='assistant_agent',
agent_name='Assistant Agent',
instructions_template='You are AI Assistant...',
prompt_template='{{ query }}',
outputs={'output': ContextFunctions.DEFAULT, 'messages': ContextFunctions.APPEND_MESSAGES}
)
Context Management
The context is initialized with basic parameters:
Tool Integration
Tools are integrated through: 1. Adding tools to the action 2. Enabling tool usage for the agent 3. Creating tool interaction paths
Example Interactions
Mathematical Expression
Input: "99 + 2 * 66 + 9987"
Process:
1. Agent uses multiply tool: 2 * 66 = 132
2. Agent uses add tool: 99 + 132 = 231
3. Agent uses add tool: 231 + 9987 = 10218
Output: "The final result is 10218."
Natural Language Query
Input: "we have 99 person in the room and 40 in another room how many people are there in total"
Process:
1. Agent uses add tool: 99 + 40 = 139
Output: "There are 139 people in total."
Best Practices
When implementing the Assistant with Tools pattern:
- Clear Tool Documentation: Provide clear docstrings for all tools
- Type Hints: Use type hints to ensure proper tool usage
- Error Handling: Implement proper error handling in tools
- Tool Scope: Keep tools focused on specific tasks
- Context Management: Properly manage tool inputs and outputs
Use Cases
This pattern is ideal for: - Calculation-heavy interactions - Data processing tasks - Multi-step operations - Integration with external systems - Enhanced chatbot capabilities
Next Steps
After implementing the Assistant with Tools pattern, you can: 1. Add more sophisticated tools 2. Implement tool chaining 3. Add tool usage analytics 4. Enhance error handling 5. Add tool-specific optimizations