Section Insights
Introduction to Custom Tools for Managed Deep Agents
What are custom tools and why are they important for managed deep agents?
Custom tools allow managed deep agents to interact with external systems, databases, and APIs, enhancing their capabilities beyond built-in tools.
- Custom tools enable agents to perform actions and retrieve information from various sources.
- Built-in tools are provided by the underlying model, but custom tools can be tailored to specific needs.
- The video will demonstrate how to create and integrate custom tools into managed deep agents.
Creating a Custom Tool Function
How do you define a custom tool function for a managed deep agent?
A custom tool function is defined using a tool decorator from LangChain, which transforms a standard function into a tool that the agent can utilize.
- The function name becomes the tool's name, and its parameters are required inputs for the agent.
- A docstring is essential as it describes the tool's functionality to the agent.
- The tool decorator is crucial for enabling the agent to recognize and use the function.
Implementing the Custom Tool
What are the steps to implement a custom tool in a managed deep agent?
To implement a custom tool, create a new directory for tools, define the function with a tool decorator, and import it into the agent's code.
- The custom tool must include a docstring for the agent to understand its usage.
- The implementation process involves creating a file for the tool and importing it into the agent's main code.
- Once implemented, the agent can list and utilize the custom tool alongside built-in tools.
Using the Custom Tool in the Agent
How does the agent utilize the custom tool once it is defined?
The agent can call the custom tool to perform specific tasks, such as looking up customer information, and it will display the results based on the defined parameters.
- The agent can interact with both built-in and custom tools seamlessly.
- When a tool is called, the agent passes the required arguments and receives a response.
- The tool's description and parameters are crucial for the agent's understanding and execution.
Understanding Tool Definitions and Their Importance
Why are tool definitions critical for the functionality of managed deep agents?
Tool definitions provide essential information to the agent, ensuring it can use the tools correctly and effectively, which is vital for expanding the agent's capabilities.
- Accurate tool definitions enhance the agent's ability to perform tasks correctly.
- The parse docstring feature helps present tool information to the LLM, improving interaction.
- Mastering custom tools is key to leveraging the full potential of managed deep agents.
Transcript
0:00 In this video, we're going to show how to add custom tools to your managed deep agent. So, tools are how the agent interacts with the outside world. They can look things up in databases, look things up online, they can take actions via external APIs. Tools are basically what gives the agent the capability to do things. And so, we're going to show how to write and hook up custom tools to your managed deep agent here. So, if you haven't already done the quick start, please do that video because we're going to be working off of that. In that quick start, you did give the agent a tool. You gave it this web search tool.
0:33 And this is what we call a built-in tool. So, this is provided by the underlying model. So, OpenAI in this case offers a web search tool that is behind their API and can do web search. And so, that's great, but often times you want to add in custom tools that interacts with databases or APIs that OpenAI isn't going to know how to deal with. So, that's what we're going to cover in this video. So, going back to the tools page, we can see some basic overviews on how these tools are enabled.
1:03 So, again, we'll be using the Python version of managed deep agents. If you want to follow along in TypeScript, you absolutely can. In either language, they are just files. So, in Python, it's just a Python file where you define your tool as a custom function, and then you import it and expose it to your agent. Same with TypeScript, just a custom TypeScript function. So, in order to add a tool, as mentioned, it's just a function with a few special things. So, first, we're going to use this tool decorator. This is from LangChain tools, and this basically will parse this tool and transform this tool from a function into a tool that deep agents knows how to use. But, a lot of the regular just like function capabilities are very relevant here. So, the name of the function, this is going to be the name of the tool.
1:47 It's just mapped one-to-one. The parameters of the function, these are just parameters that the LLM needs to fill out when it calls the tool. The description, the docstring here, this will be the description of the tool when it's used by the agent. And you notice that we have this parse docstring thing, and we'll show actually what this does and how it's presented to the user. Because all of this information, the name, the parameters, the docstring, these are all relevant not just for you as a human, but for the agent. This is how the agent knows how to interact with this tool. And then inside this tool, you just have your normal logic. In this case, we have a mock thing that just returns a standard string. So, let's go ahead and implement this. So, let's copy this code. Let's go over here. This is our This is our agent from our example from our quickstart before. Let's create a new directory called tools.
2:36 Let's create a new file in there. let's call it lookup. let's add this function here. Let's then go back to our agent, and we're going to do from tools.lookup import lookup_customer, and then we're just going to pass that right here. And that's it. This is This is how we defined a tool for our agent to use. And again, the only thing that really needs to be in this tool is we need this tool decorator on top of a function, and this function has to have a docstring. So, that is one requirement because this docstring is how the agent knows how to use it.
3:11 So, let's go to the terminal and let's spin up this agent using MDA dev. It opens the studio, and we can start chatting with it. Let's ask it what tools it has. And you so you can see it start to list out a bunch of different tools that it has. And so, looking at some of these, so search tools, so this is the built-in search tool. File workspace tools, so these are these are tools for interacting with files. These are actually built into the deep agent harness. So, this comes from the model.
3:48 These come from the harness itself. The task tool for launching subagents, this comes from deepagents itself. The CRM tool, so so looking up a customer record, this is the one we added. And then parallel tool wrapper, and so this is this is an again kind of like a built-in harness tool. So let's tell it to look up info on customer 123. And so here we can see it calling the lookup customer tool. And if we go into it, we can see the arguments. So customer ID 123, we can see the response. Customer 123 is on the enterprise plan. And if we look at the tool that we defined, we can see that yeah, this is the name of the function. This is the argument that's required. And then this is the response that we expect to get.
4:33 So just so you can see how exactly this tool description is is given to the LLM, let's take a look at the trace. So let's go over here. Let's click on trace. let's open this up in in a new window. Let's click into the first model call. So here we can see exactly what the LLM sees. And so we can see that it's given these tools. It's given a bunch of tools. So these are the ones listed before. It's got this task tool.
4:57 This is for spawning subagents. It's got this web search tool. It's got a bunch of other read and write and execute tools for interacting with the file system. And it's got this lookup customer tool. And if we click into this, we can see that the description, look up a customer record by ID. If we go back to our functions, that's exactly this. And then if you notice here, we have this args thing and customer ID, customer ID from the CRM. And we have this parse docstring equals true. This basically is saying parse this docstring and pass it in and basically pass it in that way to the LLM. So if we go back here, we look up type string for and so this is customer ID, the customer ID parameter, type string. Description.
5:33 This is exactly what we had down here. So it's getting parsed and associated with this. And so that's what parse docstring will do. It will basically help present the LLM with this information about the tool. And so that's why tool definitions are so important. It's not just for you the human, it's also for the LLM to know how to use the tool. If the LLM isn't using the tool correctly, you might not be describing it correctly. And so that's basically how you define custom tools for the agent to use. You write this Python function here, you give it a tool decorator, and then in the agent file, you can add any more functions you want right here. It's super easy. This is how you expand the capabilities of your agent. Custom tools are super powerful for that reason. They let you expand the capabilities of what the agent can do.
6:15 And so I definitely play around with these and make sure that you know and master these custom tools.
Summary
- Custom tools enable agents to interact with external databases and APIs.
- The tutorial builds on a previous quick start video that introduced a built-in web search tool.
- Tools are defined as Python or TypeScript functions, requiring a decorator for integration.
- Key components of a tool include its name, parameters, and a docstring for description.
- The docstring is crucial for the agent to understand how to use the tool effectively.
- The video demonstrates creating a lookup tool for customer records and integrating it into the agent.
- The importance of tool definitions is emphasized for both human users and the LLM (language model).
- Custom tools significantly expand the capabilities of managed deep agents, allowing for tailored functionalities.
Questions Answered
What are custom tools and why are they important for managed deep agents?
Custom tools allow managed deep agents to interact with external systems, databases, and APIs, enhancing their capabilities beyond built-in tools.
How do you define a custom tool function for a managed deep agent?
A custom tool function is defined using a tool decorator from LangChain, which transforms a standard function into a tool that the agent can utilize.
What are the steps to implement a custom tool in a managed deep agent?
To implement a custom tool, create a new directory for tools, define the function with a tool decorator, and import it into the agent's code.
How does the agent utilize the custom tool once it is defined?
The agent can call the custom tool to perform specific tasks, such as looking up customer information, and it will display the results based on the defined parameters.
Why are tool definitions critical for the functionality of managed deep agents?
Tool definitions provide essential information to the agent, ensuring it can use the tools correctly and effectively, which is vital for expanding the agent's capabilities.