|
|
|
@ -1,323 +1,391 @@
|
|
|
|
|
# **Equipping Autonomous Agents with Tools**
|
|
|
|
|
==========================================
|
|
|
|
|
The Swarms Tool System: Functions, Pydantic BaseModels as Tools, and Radical Customization
|
|
|
|
|
|
|
|
|
|
Tools play a crucial role in enhancing the capabilities of AI agents. Swarms, a powerful open-source framework, provides a robust and flexible environment for building and integrating tools with AI agents. In this comprehensive guide, we'll explore the process of creating tools in Swarms, including the 3-step process, tool decorator, adding types and doc strings, and integrating them into the Agent class.
|
|
|
|
|
This guide provides an in-depth look at the Swarms Tool System, focusing on its functions, the use of Pydantic BaseModels as tools, and the extensive customization options available. Aimed at developers, this documentation highlights how the Swarms framework works and offers detailed examples of creating and customizing tools and agents, specifically for accounting tasks.
|
|
|
|
|
|
|
|
|
|
## **Introduction to Swarms**
|
|
|
|
|
--------------------------
|
|
|
|
|
The Swarms Tool System is a flexible and extensible component of the Swarms framework that allows for the creation, registration, and utilization of various tools. These tools can perform a wide range of tasks and are integrated into agents to provide specific functionalities. The system supports multiple ways to define tools, including using Pydantic BaseModels, functions, and dictionaries.
|
|
|
|
|
|
|
|
|
|
Swarms is a Python-based framework that simplifies the development and deployment of AI agents. It provides a seamless integration with large language models (LLMs) and offers a wide range of tools and utilities to streamline the agent development process. One of the core features of Swarms is the ability to create and integrate custom tools, which can significantly extend the capabilities of AI agents.
|
|
|
|
|
Star, fork, and contribute to the swarms framework here:
|
|
|
|
|
|
|
|
|
|
Learn more here:
|
|
|
|
|
GitHub - kyegomez/swarms: Orchestrate Swarms of Agents From Any Framework Like OpenAI, Langchain…
|
|
|
|
|
|
|
|
|
|
[**GitHub - kyegomez/swarms: Build, Deploy, and Scale Reliable Swarms of Autonomous Agents for...**](https://github.com/kyegomez/swarms?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
Orchestrate Swarms of Agents From Any Framework Like OpenAI, Langchain, and Etc for Business Operation Automation. Join…
|
|
|
|
|
|
|
|
|
|
### [Build, Deploy, and Scale Reliable Swarms of Autonomous Agents for Workflow Automation. Join our Community...](https://github.com/kyegomez/swarms?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
github.com
|
|
|
|
|
|
|
|
|
|
[github.com](https://github.com/kyegomez/swarms?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
And, join our community now!
|
|
|
|
|
|
|
|
|
|
And, join our community for real-time support and conversations with friends!
|
|
|
|
|
Join the Agora Discord Server!
|
|
|
|
|
|
|
|
|
|
[**Join the Agora Discord Server!**](https://discord.gg/A8DrG5nj?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
Advancing Humanity through open source AI research. | 7314 members
|
|
|
|
|
|
|
|
|
|
### [Advancing Humanity through open source AI research. | 6319 members](https://discord.gg/A8DrG5nj?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
discord.gg
|
|
|
|
|
|
|
|
|
|
[discord.gg](https://discord.gg/A8DrG5nj?source=post_page-----49d146bcbf9e--------------------------------)
|
|
|
|
|
Architecture
|
|
|
|
|
|
|
|
|
|
**Installation**
|
|
|
|
|
================
|
|
|
|
|
The architecture of the Swarms Tool System is designed to be highly modular. It consists of the following main components:
|
|
|
|
|
|
|
|
|
|
First, download swarms with the following command. If you have any questions please refer to this video or ask us in the discord!
|
|
|
|
|
Agents: The primary entities that execute tasks.
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
pip3 install -U swarms
|
|
|
|
|
```
|
|
|
|
|
Tools: Functions or classes that perform specific operations.
|
|
|
|
|
|
|
|
|
|
**Necessary Imports**
|
|
|
|
|
---------------------
|
|
|
|
|
Schemas: Definitions of input and output data formats using Pydantic BaseModels.
|
|
|
|
|
|
|
|
|
|
Before we dive into the process of creating tools in Swarms, let's familiarize ourselves with the necessary imports:
|
|
|
|
|
Key Concepts
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from swarms import Agent, Anthropic, tool
|
|
|
|
|
import subprocess
|
|
|
|
|
```
|
|
|
|
|
Tools
|
|
|
|
|
|
|
|
|
|
- These imports provide access to the core components of the Swarms framework, including the `Agent` class, the `Anthropic` language model, and the `tool` decorator for creating custom tools.
|
|
|
|
|
Tools are the core functional units within the Swarms framework. They can be defined in various ways:
|
|
|
|
|
|
|
|
|
|
- `import subprocess`: This import allows us to interact with the system's terminal and execute shell commands, which can be useful for certain types of tools.
|
|
|
|
|
Pydantic BaseModels: Tools can be defined using Pydantic BaseModels to ensure data validation and serialization.
|
|
|
|
|
|
|
|
|
|
With these imports in place, we're ready to explore the process of creating tools in Swarms.
|
|
|
|
|
Functions: Tools can be simple or complex functions.
|
|
|
|
|
|
|
|
|
|
### **The 3-Step Process**
|
|
|
|
|
======================
|
|
|
|
|
Dictionaries: Tools can be represented as dictionaries for flexibility.
|
|
|
|
|
|
|
|
|
|
Creating tools in Swarms follows a straightforward 3-step process:
|
|
|
|
|
Agents
|
|
|
|
|
|
|
|
|
|
1\. Define the Tool Function\
|
|
|
|
|
2\. Decorate the Function with `[@tool](http://twitter.com/tool)` and add documentation with type hints.\
|
|
|
|
|
3\. Add the Tool to the `Agent` Instance
|
|
|
|
|
Agents utilize tools to perform tasks. They are configured with a set of tools and schemas, and they execute the tools based on the input they receive.
|
|
|
|
|
|
|
|
|
|
Let's go through each step in detail, accompanied by code examples.
|
|
|
|
|
Installation
|
|
|
|
|
|
|
|
|
|
### **Step 1: Define the Tool Function**
|
|
|
|
|
------------------------------------
|
|
|
|
|
pip3 install -U swarms pydantic
|
|
|
|
|
|
|
|
|
|
The first step in creating a tool is to define a Python function that encapsulates the desired functionality. This function will serve as the core logic for your tool. Here's an example of a tool function that allows you to execute code in the terminal:
|
|
|
|
|
Tool Definition
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
def terminal(code: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Run code in the terminal.
|
|
|
|
|
Using Pydantic BaseModels
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): The code to run in the terminal.
|
|
|
|
|
Pydantic BaseModels provide a structured way to define tool inputs and outputs. They ensure data validation and serialization, making them ideal for complex data handling.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: The output of the code.
|
|
|
|
|
"""
|
|
|
|
|
out = subprocess.run(
|
|
|
|
|
code, shell=True, capture_output=True, text=True
|
|
|
|
|
).stdout
|
|
|
|
|
return str(out)
|
|
|
|
|
```
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
In this example, the `terminal` function takes a string `code` as input and uses the `subprocess` module to execute the provided code in the system's terminal. The output of the code is captured and returned as a string.
|
|
|
|
|
Define Pydantic BaseModels for accounting tasks:
|
|
|
|
|
|
|
|
|
|
### **Let's break down the components of this function:**
|
|
|
|
|
-----------------------------------------------------
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
- **Function Signature:** The function signature `def terminal(code: str) -> str:` defines the function name (`terminal`), the parameter name and type (`code: str`), and the return type (`-> str`). This adheres to Python's type hinting conventions.
|
|
|
|
|
class CalculateTax(BaseModel):
|
|
|
|
|
income: float
|
|
|
|
|
|
|
|
|
|
- **Docstring**: The multiline string enclosed in triple quotes (`"""` ... `"""`) is a docstring, which provides a brief description of the function, its parameters, and its return value. Docstrings are essential for documenting your code and making it easier for the agent to understand and use your tools.
|
|
|
|
|
class GenerateInvoice(BaseModel):
|
|
|
|
|
client_name: str
|
|
|
|
|
amount: float
|
|
|
|
|
date: str
|
|
|
|
|
|
|
|
|
|
- **Function Body**: The body of the function contains the actual logic for executing the code in the terminal. It uses the `subprocess.run` function to execute the provided `code` in the shell, capturing the output (`capture_output=True`), and returning the output as text (`text=True`). The `stdout` attribute of the `CompletedProcess` object contains the captured output, which is converted to a string and returned.
|
|
|
|
|
class SummarizeExpenses(BaseModel):
|
|
|
|
|
expenses: list[dict]
|
|
|
|
|
|
|
|
|
|
This is a simple example, but it demonstrates the key components of a tool function: a well-defined signature with type hints, a descriptive docstring, and the core logic encapsulated within the function body.
|
|
|
|
|
Define tool functions using these models:
|
|
|
|
|
|
|
|
|
|
### **Step 2: Decorate the Function with `**[**@tool**](http://twitter.com/tool)**`**
|
|
|
|
|
---------------------------------------------------------------------------------
|
|
|
|
|
def calculate_tax(data: CalculateTax) -> dict:
|
|
|
|
|
tax_rate = 0.3 # Example tax rate
|
|
|
|
|
tax = data.income * tax_rate
|
|
|
|
|
return {"income": data.income, "tax": tax}
|
|
|
|
|
|
|
|
|
|
After defining the tool function, the next step is to decorate it with the `[@tool](http://twitter.com/tool)` decorator provided by Swarms. This decorator registers the function as a tool, allowing it to be used by AI agents within the Swarms framework.
|
|
|
|
|
def generate_invoice(data: GenerateInvoice) -> dict:
|
|
|
|
|
invoice = {
|
|
|
|
|
"client_name": data.client_name,
|
|
|
|
|
"amount": data.amount,
|
|
|
|
|
"date": data.date,
|
|
|
|
|
"invoice_id": "INV12345"
|
|
|
|
|
}
|
|
|
|
|
return invoice
|
|
|
|
|
|
|
|
|
|
Here's how you would decorate the `terminal` function from the previous example:
|
|
|
|
|
def summarize_expenses(data: SummarizeExpenses) -> dict:
|
|
|
|
|
total_expenses = sum(expense['amount'] for expense in data.expenses)
|
|
|
|
|
return {"total_expenses": total_expenses}
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@tool
|
|
|
|
|
def terminal(code: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Run code in the terminal.
|
|
|
|
|
Using Functions Directly
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
code (str): The code to run in the terminal.
|
|
|
|
|
Tools can also be defined directly as functions without using Pydantic models. This approach is suitable for simpler tasks where complex validation is not required.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: The output of the code.
|
|
|
|
|
"""
|
|
|
|
|
out = subprocess.run(
|
|
|
|
|
code, shell=True, capture_output=True, text=True
|
|
|
|
|
).stdout
|
|
|
|
|
return str(out)
|
|
|
|
|
```
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
The `[@tool](http://twitter.com/tool)` decorator is placed directly above the function definition. This decorator performs the necessary registration and configuration steps to integrate the tool with the Swarms framework.
|
|
|
|
|
def basic_tax_calculation(income: float) -> dict:
|
|
|
|
|
tax_rate = 0.25
|
|
|
|
|
tax = income * tax_rate
|
|
|
|
|
return {"income": income, "tax": tax}
|
|
|
|
|
|
|
|
|
|
### **Step 3: Add the Tool to the `Agent` Instance**
|
|
|
|
|
------------------------------------------------
|
|
|
|
|
Using Dictionaries
|
|
|
|
|
|
|
|
|
|
The final step in creating a tool is to add it to the `Agent` instance. The `Agent` class is a core component of the Swarms framework and represents an AI agent capable of interacting with humans and other agents, as well as utilizing the available tools.
|
|
|
|
|
Tools can be represented as dictionaries, providing maximum flexibility. This method is useful when the tool’s functionality is more dynamic or when integrating with external systems.
|
|
|
|
|
|
|
|
|
|
Here's an example of how to create an `Agent` instance and add the `terminal` tool:
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
# Model
|
|
|
|
|
llm = Anthropic(
|
|
|
|
|
temperature=0.1,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Agent
|
|
|
|
|
agent = Agent(
|
|
|
|
|
agent_name="Devin",
|
|
|
|
|
system_prompt=(
|
|
|
|
|
"Autonomous agent that can interact with humans and other"
|
|
|
|
|
" agents. Be Helpful and Kind. Use the tools provided to"
|
|
|
|
|
" assist the user. Return all code in markdown format."
|
|
|
|
|
),
|
|
|
|
|
llm=llm,
|
|
|
|
|
max_loops="auto",
|
|
|
|
|
autosave=True,
|
|
|
|
|
dashboard=False,
|
|
|
|
|
streaming_on=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
stopping_token="<DONE>",
|
|
|
|
|
interactive=True,
|
|
|
|
|
tools=[terminal],
|
|
|
|
|
code_interpreter=True,
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
basic_tool_schema = {
|
|
|
|
|
"name": "basic_tax_tool",
|
|
|
|
|
"description": "A basic tax calculation tool",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"income": {"type": "number", "description": "Income amount"}
|
|
|
|
|
},
|
|
|
|
|
"required": ["income"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
In this example, we first create an instance of the `Anthropic` language model, which will be used by the agent for natural language
|
|
|
|
|
def basic_tax_tool(income: float) -> dict:
|
|
|
|
|
tax_rate = 0.2
|
|
|
|
|
tax = income * tax_rate
|
|
|
|
|
return {"income": income, "tax": tax}
|
|
|
|
|
|
|
|
|
|
**The Necessity of Documentation**
|
|
|
|
|
----------------------------------
|
|
|
|
|
Tool Registration
|
|
|
|
|
|
|
|
|
|
Before creating tools, it's essential to understand the importance of documentation. Clear and concise documentation ensures that your code is easily understandable and maintainable, not only for yourself but also for other developers who may work with your codebase in the future.
|
|
|
|
|
Tools need to be registered with the agent for it to utilize them. This can be done by specifying the tools in the tools parameter during agent initialization.
|
|
|
|
|
|
|
|
|
|
Effective documentation serves several purposes:
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
1. Code Clarity: Well-documented code is easier to read and understand, making it more accessible for both developers and non-technical stakeholders.
|
|
|
|
|
from swarms import Agent
|
|
|
|
|
from llama_hosted import llama3Hosted
|
|
|
|
|
|
|
|
|
|
2. Collaboration: When working in a team or contributing to open-source projects, proper documentation facilitates collaboration and knowledge sharing.
|
|
|
|
|
# Define Pydantic BaseModels for accounting tasks
|
|
|
|
|
class CalculateTax(BaseModel):
|
|
|
|
|
income: float
|
|
|
|
|
|
|
|
|
|
3. Onboarding: Comprehensive documentation can significantly streamline the onboarding process for new team members or contributors, reducing the time required to familiarize themselves with the codebase.
|
|
|
|
|
class GenerateInvoice(BaseModel):
|
|
|
|
|
client_name: str
|
|
|
|
|
amount: float
|
|
|
|
|
date: str
|
|
|
|
|
|
|
|
|
|
4. Future Maintenance: As projects evolve and requirements change, well-documented code becomes invaluable for future maintenance and updates.
|
|
|
|
|
class SummarizeExpenses(BaseModel):
|
|
|
|
|
expenses: list[dict]
|
|
|
|
|
|
|
|
|
|
In the context of creating tools in Swarms, documentation plays a vital role in ensuring that your tools are easily discoverable, understandable, and usable by other developers and AI agents.
|
|
|
|
|
|
|
|
|
|
**Type Handling**
|
|
|
|
|
-----------------
|
|
|
|
|
# Define tool functions using these models
|
|
|
|
|
def calculate_tax(data: CalculateTax) -> dict:
|
|
|
|
|
tax_rate = 0.3
|
|
|
|
|
tax = data.income * tax_rate
|
|
|
|
|
return {"income": data.income, "tax": tax}
|
|
|
|
|
|
|
|
|
|
def generate_invoice(data: GenerateInvoice) -> dict:
|
|
|
|
|
invoice = {
|
|
|
|
|
"client_name": data.client_name,
|
|
|
|
|
"amount": data.amount,
|
|
|
|
|
"date": data.date,
|
|
|
|
|
"invoice_id": "INV12345"
|
|
|
|
|
}
|
|
|
|
|
return invoice
|
|
|
|
|
|
|
|
|
|
def summarize_expenses(data: SummarizeExpenses) -> dict:
|
|
|
|
|
total_expenses = sum(expense['amount'] for expense in data.expenses)
|
|
|
|
|
return {"total_expenses": total_expenses}
|
|
|
|
|
|
|
|
|
|
# Function to generate a tool schema for demonstration purposes
|
|
|
|
|
def create_tool_schema():
|
|
|
|
|
return {
|
|
|
|
|
"name": "execute",
|
|
|
|
|
"description": "Executes code on the user's machine",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"language": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "Programming language",
|
|
|
|
|
"enum": ["python", "java"]
|
|
|
|
|
},
|
|
|
|
|
"code": {"type": "string", "description": "Code to execute"}
|
|
|
|
|
},
|
|
|
|
|
"required": ["language", "code"]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Initialize the agent with the tools
|
|
|
|
|
agent = Agent(
|
|
|
|
|
agent_name="Accounting Agent",
|
|
|
|
|
system_prompt="This agent assists with various accounting tasks.",
|
|
|
|
|
sop_list=["Provide accurate and timely accounting services."],
|
|
|
|
|
llm=llama3Hosted(),
|
|
|
|
|
max_loops="auto",
|
|
|
|
|
interactive=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
tool_schema=BaseModel,
|
|
|
|
|
list_base_models=[
|
|
|
|
|
CalculateTax,
|
|
|
|
|
GenerateInvoice,
|
|
|
|
|
SummarizeExpenses
|
|
|
|
|
],
|
|
|
|
|
output_type=str,
|
|
|
|
|
metadata_output_type="json",
|
|
|
|
|
function_calling_format_type="OpenAI",
|
|
|
|
|
function_calling_type="json",
|
|
|
|
|
tools=[
|
|
|
|
|
calculate_tax,
|
|
|
|
|
generate_invoice,
|
|
|
|
|
summarize_expenses
|
|
|
|
|
],
|
|
|
|
|
list_tool_schemas_json=create_tool_schema(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Python is a dynamically-typed language, which means that variables can hold values of different types during runtime. While this flexibility can be advantageous in certain scenarios, it can also lead to potential errors and inconsistencies, especially in larger codebases.
|
|
|
|
|
Running the Agent
|
|
|
|
|
|
|
|
|
|
Type hints, introduced in Python 3.5, provide a way to explicitly annotate the expected types of variables, function parameters, and return values. By incorporating type hints into your code, you can:
|
|
|
|
|
The agent can execute tasks using the run method. This method takes a prompt and determines the appropriate tool to use based on the input.
|
|
|
|
|
|
|
|
|
|
1. Improve Code Readability: Type hints make it easier for developers to understand the expected data types, reducing the risk of introducing bugs due to type-related errors.
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
2. Enable Static Type Checking: With tools like mypy, you can perform static type checking, catching potential type-related issues before running the code.
|
|
|
|
|
# Example task: Calculate tax for an income
|
|
|
|
|
result = agent.run("Calculate the tax for an income of $50,000.")
|
|
|
|
|
print(f"Result: {result}")
|
|
|
|
|
|
|
|
|
|
3. Enhance Code Completion and Tooling: Modern IDEs and code editors can leverage type hints to provide better code completion, refactoring capabilities, and inline documentation.
|
|
|
|
|
# Example task: Generate an invoice
|
|
|
|
|
invoice_data = agent.run("Generate an invoice for John Doe for $1500 on 2024-06-01.")
|
|
|
|
|
print(f"Invoice Data: {invoice_data}")
|
|
|
|
|
|
|
|
|
|
In the context of creating tools in Swarms, type hints are crucial for ensuring that your tools are used correctly by AI agents and other developers. By clearly defining the expected input and output types, you can reduce the likelihood of runtime errors and improve the overall reliability of your tools.
|
|
|
|
|
# Example task: Summarize expenses
|
|
|
|
|
expenses = [
|
|
|
|
|
{"amount": 200.0, "description": "Office supplies"},
|
|
|
|
|
{"amount": 1500.0, "description": "Software licenses"},
|
|
|
|
|
{"amount": 300.0, "description": "Travel expenses"}
|
|
|
|
|
]
|
|
|
|
|
summary = agent.run("Summarize these expenses: " + str(expenses))
|
|
|
|
|
print(f"Expenses Summary: {summary}")
|
|
|
|
|
|
|
|
|
|
Now, let's continue with other tool examples!
|
|
|
|
|
Customizing Tools
|
|
|
|
|
|
|
|
|
|
### **Additional Tool Examples**
|
|
|
|
|
============================
|
|
|
|
|
Custom tools can be created to extend the functionality of the Swarms framework. This can include integrating external APIs, performing complex calculations, or handling specialized data formats.
|
|
|
|
|
|
|
|
|
|
To further illustrate the process of creating tools in Swarms, let's explore a few more examples of tool functions with varying functionalities.
|
|
|
|
|
Example: Custom Accounting Tool
|
|
|
|
|
|
|
|
|
|
**Browser Tool**
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@tool
|
|
|
|
|
def browser(query: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Search the query in the browser with the `browser` tool.
|
|
|
|
|
Args:
|
|
|
|
|
query (str): The query to search in the browser.
|
|
|
|
|
Returns:
|
|
|
|
|
str: The search results.
|
|
|
|
|
"""
|
|
|
|
|
import webbrowser
|
|
|
|
|
url = f"https://www.google.com/search?q={query}"
|
|
|
|
|
webbrowser.open(url)
|
|
|
|
|
return f"Searching for {query} in the browser."
|
|
|
|
|
```
|
|
|
|
|
class CustomAccountingTool(BaseModel):
|
|
|
|
|
data: dict
|
|
|
|
|
|
|
|
|
|
The `browser` tool allows the agent to perform web searches by opening the provided `query` in the default web browser. It leverages the `webbrowser` module to construct the search URL and open it in the browser. The tool returns a string indicating that the search is being performed.
|
|
|
|
|
def custom_accounting_tool(data: CustomAccountingTool) -> dict:
|
|
|
|
|
# Custom logic for the accounting tool
|
|
|
|
|
result = {
|
|
|
|
|
"status": "success",
|
|
|
|
|
"data_processed": len(data.data)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
**File Creation Tool**
|
|
|
|
|
# Register the custom tool with the agent
|
|
|
|
|
agent = Agent(
|
|
|
|
|
agent_name="Accounting Agent",
|
|
|
|
|
system_prompt="This agent assists with various accounting tasks.",
|
|
|
|
|
sop_list=["Provide accurate and timely accounting services."],
|
|
|
|
|
llm=llama3Hosted(),
|
|
|
|
|
max_loops="auto",
|
|
|
|
|
interactive=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
tool_schema=BaseModel,
|
|
|
|
|
list_base_models=[
|
|
|
|
|
CalculateTax,
|
|
|
|
|
GenerateInvoice,
|
|
|
|
|
SummarizeExpenses,
|
|
|
|
|
CustomAccountingTool
|
|
|
|
|
],
|
|
|
|
|
output_type=str,
|
|
|
|
|
metadata_output_type="json",
|
|
|
|
|
function_calling_format_type="OpenAI",
|
|
|
|
|
function_calling_type="json",
|
|
|
|
|
tools=[
|
|
|
|
|
calculate_tax,
|
|
|
|
|
generate_invoice,
|
|
|
|
|
summarize_expenses,
|
|
|
|
|
custom_accounting_tool
|
|
|
|
|
],
|
|
|
|
|
list_tool_schemas_json=create_tool_schema(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@tool
|
|
|
|
|
def create_file(file_path: str, content: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Create a file using the file editor tool.
|
|
|
|
|
Args:
|
|
|
|
|
file_path (str): The path to the file.
|
|
|
|
|
content (str): The content to write to the file.
|
|
|
|
|
Returns:
|
|
|
|
|
str: The result of the file creation operation.
|
|
|
|
|
"""
|
|
|
|
|
with open(file_path, "w") as file:
|
|
|
|
|
file.write(content)
|
|
|
|
|
return f"File {file_path} created successfully."
|
|
|
|
|
```
|
|
|
|
|
Advanced Customization
|
|
|
|
|
|
|
|
|
|
The `create_file` tool allows the agent to create a new file at the specified `file_path` with the provided `content`. It uses Python's built-in `open` function in write mode (`"w"`) to create the file and write the content to it. The tool returns a string indicating the successful creation of the file.
|
|
|
|
|
Advanced customization involves modifying the core components of the Swarms framework. This includes extending existing classes, adding new methods, or integrating third-party libraries.
|
|
|
|
|
|
|
|
|
|
**File Editor Tool**
|
|
|
|
|
Example: Extending the Agent Class
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@tool
|
|
|
|
|
def file_editor(file_path: str, mode: str, content: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Edit a file using the file editor tool.
|
|
|
|
|
Args:
|
|
|
|
|
file_path (str): The path to the file.
|
|
|
|
|
mode (str): The mode to open the file in.
|
|
|
|
|
content (str): The content to write to the file.
|
|
|
|
|
Returns:
|
|
|
|
|
str: The result of the file editing operation.
|
|
|
|
|
"""
|
|
|
|
|
with open(file_path, mode) as file:
|
|
|
|
|
file.write(content)
|
|
|
|
|
return f"File {file_path} edited successfully."
|
|
|
|
|
```
|
|
|
|
|
from swarms import Agent
|
|
|
|
|
|
|
|
|
|
The `file_editor` tool is similar to the `create_file` tool but provides more flexibility by allowing the agent to specify the mode in which the file should be opened (e.g., `"w"` for write, `"a"` for append). It writes the provided `content` to the file at the specified `file_path` and returns a string indicating the successful editing of the file.
|
|
|
|
|
class AdvancedAccountingAgent(Agent):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
These examples demonstrate the versatility of tools in Swarms and how they can be designed to perform a wide range of tasks, from executing terminal commands to interacting with files and performing web searches.
|
|
|
|
|
def custom_behavior(self):
|
|
|
|
|
print("Executing custom behavior")
|
|
|
|
|
|
|
|
|
|
### **Plugging Tools into the Agent**
|
|
|
|
|
=================================
|
|
|
|
|
def another_custom_method(self):
|
|
|
|
|
print("Another
|
|
|
|
|
|
|
|
|
|
After defining and decorating your tool functions, the next step is to integrate them into the `Agent` instance. This process involves passing the tools as a list to the `tools` parameter when creating the `Agent` instance.
|
|
|
|
|
custom method")
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
# Model
|
|
|
|
|
llm = Anthropic(temperature=0.1)
|
|
|
|
|
# Tools
|
|
|
|
|
tools = [terminal, browser, create_file, file_editor]
|
|
|
|
|
# Agent
|
|
|
|
|
agent = Agent(
|
|
|
|
|
agent_name="Devin",
|
|
|
|
|
system_prompt=(
|
|
|
|
|
"Autonomous agent that can interact with humans and other"
|
|
|
|
|
" agents. Be Helpful and Kind. Use the tools provided to"
|
|
|
|
|
" assist the user. Return all code in markdown format."
|
|
|
|
|
),
|
|
|
|
|
llm=llm,
|
|
|
|
|
# Initialize the advanced agent
|
|
|
|
|
advanced_agent = AdvancedAccountingAgent(
|
|
|
|
|
agent_name="Advanced Accounting Agent",
|
|
|
|
|
system_prompt="This agent performs advanced accounting tasks.",
|
|
|
|
|
sop_list=["Provide advanced accounting services."],
|
|
|
|
|
llm=llama3Hosted(),
|
|
|
|
|
max_loops="auto",
|
|
|
|
|
autosave=True,
|
|
|
|
|
dashboard=False,
|
|
|
|
|
streaming_on=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
stopping_token="<DONE>",
|
|
|
|
|
interactive=True,
|
|
|
|
|
tools=tools,
|
|
|
|
|
code_interpreter=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
tool_schema=BaseModel,
|
|
|
|
|
list_base_models=[
|
|
|
|
|
CalculateTax,
|
|
|
|
|
GenerateInvoice,
|
|
|
|
|
SummarizeExpenses,
|
|
|
|
|
CustomAccountingTool
|
|
|
|
|
],
|
|
|
|
|
output_type=str,
|
|
|
|
|
metadata_output_type="json",
|
|
|
|
|
function_calling_format_type="OpenAI",
|
|
|
|
|
function_calling_type="json",
|
|
|
|
|
tools=[
|
|
|
|
|
calculate_tax,
|
|
|
|
|
generate_invoice,
|
|
|
|
|
summarize_expenses,
|
|
|
|
|
custom_accounting_tool
|
|
|
|
|
],
|
|
|
|
|
list_tool_schemas_json=create_tool_schema(),
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In this example, we create a list `tools` containing the `terminal`, `browser`, `create_file`, and `file_editor` tools. This list is then passed to the `tools` parameter when creating the `Agent` instance.
|
|
|
|
|
# Call custom methods
|
|
|
|
|
advanced_agent.custom_behavior()
|
|
|
|
|
advanced_agent.another_custom_method()
|
|
|
|
|
|
|
|
|
|
Once the `Agent` instance is created with the provided tools, it can utilize these tools to perform various tasks and interact with external systems. The agent can call these tools as needed, passing the required arguments and receiving the corresponding return values.
|
|
|
|
|
Integrating External Libraries
|
|
|
|
|
|
|
|
|
|
### **Using Tools in Agent Interactions**
|
|
|
|
|
=====================================
|
|
|
|
|
You can integrate external libraries to extend the functionality of your tools. This is useful for adding new capabilities or leveraging existing libraries for complex tasks.
|
|
|
|
|
|
|
|
|
|
After integrating the tools into the `Agent` instance, you can utilize them in your agent's interactions with humans or other agents. Here's an example of how an agent might use the `terminal` tool:
|
|
|
|
|
Example: Integrating Pandas for Data Processing
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
out = agent("Run the command 'ls' in the terminal.")
|
|
|
|
|
print(out)
|
|
|
|
|
```
|
|
|
|
|
import pandas as pd
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
In this example, the human user instructs the agent to run the `"ls"` command in the terminal. The agent processes this request and utilizes the `terminal` tool to execute the command, capturing and returning the output.
|
|
|
|
|
class DataFrameTool(BaseModel):
|
|
|
|
|
data: list[dict]
|
|
|
|
|
|
|
|
|
|
Similarly, the agent can leverage other tools, such as the `browser` tool for web searches or the `file_editor` tool for creating and modifying files, based on the user's instructions.
|
|
|
|
|
def process_data_frame(data: DataFrameTool) -> dict:
|
|
|
|
|
df = pd.DataFrame(data.data)
|
|
|
|
|
summary = df.describe().to_dict()
|
|
|
|
|
return {"summary": summary}
|
|
|
|
|
|
|
|
|
|
# Register the tool with the agent
|
|
|
|
|
agent = Agent(
|
|
|
|
|
agent_name="Data Processing Agent",
|
|
|
|
|
system_prompt="This agent processes data frames.",
|
|
|
|
|
sop_list=["Provide data processing services."],
|
|
|
|
|
llm=llama3Hosted(),
|
|
|
|
|
max_loops="auto",
|
|
|
|
|
interactive=True,
|
|
|
|
|
verbose=True,
|
|
|
|
|
tool_schema=BaseModel,
|
|
|
|
|
list_base_models=[DataFrameTool],
|
|
|
|
|
output_type=str,
|
|
|
|
|
metadata_output_type="json",
|
|
|
|
|
function_calling_format_type="OpenAI",
|
|
|
|
|
function_calling_type="json",
|
|
|
|
|
tools=[process_data_frame],
|
|
|
|
|
list_tool_schemas_json=create_tool_schema(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
### **Conclusion**
|
|
|
|
|
==============
|
|
|
|
|
# Example task: Process a data frame
|
|
|
|
|
data = [
|
|
|
|
|
{"col1": 1, "col2": 2},
|
|
|
|
|
{"col1": 3, "col2": 4},
|
|
|
|
|
{"col1": 5, "col2": 6}
|
|
|
|
|
]
|
|
|
|
|
result = agent.run("Process this data frame: " + str(data))
|
|
|
|
|
print(f"Data Frame Summary: {result}")
|
|
|
|
|
|
|
|
|
|
Creating tools in Swarms is a powerful way to extend the capabilities of AI agents and enable them to interact with external systems and perform a wide range of tasks. By following the 3-step process of defining the tool function, decorating it with `@tool`, and adding it to the `Agent` instance, you can seamlessly integrate custom tools into your AI agent's workflow.
|
|
|
|
|
Conclusion
|
|
|
|
|
|
|
|
|
|
Throughout this blog post, we explored the importance of documentation and type handling, which are essential for maintaining code quality, facilitating collaboration, and ensuring the correct usage of your tools by other developers and AI agents.
|
|
|
|
|
The Swarms Tool System provides a robust and flexible framework for defining and utilizing tools within agents. By leveraging Pydantic BaseModels, functions, and dictionaries, developers can create highly customized tools to perform a wide range of tasks. The extensive customization options allow for the integration of external libraries and the extension of core components, making the Swarms framework suitable for diverse applications.
|
|
|
|
|
|
|
|
|
|
We also covered the necessary imports and provided detailed code examples for various types of tools, such as executing terminal commands, performing web searches, and creating and editing files. These examples demonstrated the flexibility and versatility of tools in Swarms, allowing you to tailor your tools to meet your specific project requirements.
|
|
|
|
|
This guide has covered the fundamental concepts and provided detailed examples to help you get started with the Swarms Tool System. With this foundation, you can explore and implement advanced features to build powerful
|
|
|
|
|
|
|
|
|
|
By leveraging the power of tools in Swarms, you can empower your AI agents with diverse capabilities, enabling them to tackle complex tasks, interact with external systems, and provide more comprehensive and intelligent solutions.
|
|
|
|
|
If you enjoyed this guide check out the swarms github and star us and fork it as well!
|