Welcome to the next step in your journey through the course Expanding CrewAI Capabilities and Integration. In our previous lessons, you learned how to organize agent code using CrewBase
and how to use Pydantic models for structured outputs. These foundational skills are crucial as we move forward to enhance the functionality of our CrewAI projects.
One of the most powerful aspects of CrewAI is the ability to equip agents with tools that extend their capabilities beyond just reasoning. Tools allow agents to interact with external systems, retrieve information, and perform actions in the real world. Search tools are particularly valuable as they enable agents to access up-to-date information from the web, making their responses more accurate and relevant.
There are many search tools that you can easily integrate into your crew, but most of them require you to set up a payment method and use API keys. In this lesson, we will learn how to create and integrate our own free search tool using DuckDuckGo. We'll cover not only how to create custom tools but also how agents are given access to these tools within the CrewAI framework.
By the end of this lesson, you will understand the process of creating custom tools, specifically a search tool that doesn't require API keys, and how to properly configure agents to utilize these tools in their decision-making processes. This knowledge will empower you to extend your CrewAI projects with custom capabilities while avoiding unnecessary costs.
Before we dive into building our custom search tool, let's ensure our environment is ready. For this lesson, we will use two key libraries: LangChain Community and DuckDuckGoSearch. These libraries will enable us to perform web searches and integrate them into our CrewAI project.
To install these libraries on your local machine, you can use the following pip commands:
Once again, if you are working within the CodeSignal environment, you don't need to worry about installation, as these libraries are pre-installed.
Now, let's build our custom search tool. We will create a new tool by subclassing BaseTool
from the crewai.tools
module. This custom tool will use DuckDuckGoSearchResults
from the langchain_community.tools
module to perform web searches.
Here's how you can define the CustomSearchTool
:
In this code, we define a class CustomSearchTool
that inherits from BaseTool
. The _run
method is where the search logic resides. It uses DuckDuckGoSearchResults
to perform a web search based on the query provided. The result is then returned as a response.
With our custom search tool ready, the next step is to integrate it into the travel planner crew. This involves adding the tool within the crew's initialization and ensuring it is accessible to the relevant agents and tasks.
In the TravelPlannerCrew
class, we initialize the CustomSearchTool
and integrate it with the researcher agent:
Here, the CustomSearchTool
is instantiated and assigned to self.search_tool
. The researcher agent is then created with access to this tool, allowing it to perform web searches as part of its tasks.
With our custom search tool ready, we need to update our agent and task configurations to fully leverage this new capability. The agent configuration should emphasize the use of web search tools, while the task configuration should explicitly instruct the agent to perform searches.
Let's update the researcher agent configuration in agents.yaml
:
This configuration emphasizes that the researcher uses "real-time web data" and is an expert at "using the latest information from the web." These phrases signal to the agent that it should utilize the search tool we've provided.
Similarly, we need to update the research task in tasks.yaml
to explicitly instruct the agent to perform web searches:
The task description now includes a clear instruction to "Search for popular attractions and local customs," which encourages the agent to use the search tool we've provided.
By aligning our agent and task configurations with the capabilities of our custom search tool, we ensure that the agent understands when and how to use the tool effectively during the execution of its tasks.
When you run your crew with the custom search tool, you can observe how the agent uses it to gather information. Let's see what happens when we run our travel planner crew for a trip to Dubai with verbose mode enabled:
In this output, you can see:
- The agent identifies the need to search for information about Dubai attractions and customs
- It selects the DuckDuckGo Search Tool we provided
- It formulates a search query combining both attractions and cultural insights
- The tool returns real search results with snippets from various websites about Dubai's culture, attractions, and customs
This demonstrates how our custom search tool empowers the agent with the ability to access up-to-date information from the web. The agent can now use these search results to compile a more accurate and comprehensive travel plan for Dubai, including both popular attractions and important cultural insights that travelers should be aware of.
In this lesson, you learned how to create and integrate a custom search tool into a CrewAI project. We covered the setup of necessary libraries, the creation of the CustomSearchTool
, and its integration into the travel planner crew.
As you move forward, you'll have the opportunity to apply these concepts in hands-on practice exercises. These exercises will reinforce your understanding and help you gain practical experience with CrewAI and custom tool integration. Congratulations on advancing your skills with CrewAI capabilities, and get ready for more exciting lessons ahead!
