Get startedGet started for free

Defining multiple tools

1. Defining multiple tools

Great work! Now that we know how to

2. Integrating multiple tools

integrate external tools with chatbots, let's learn how to build a multiple-tool chatbot that can automate selecting a tool per query.

3. Enhancing an education chatbot

Let's imagine our school wants to enhance their chatbot further. For history lessons, they'd like to incorporate a tool that looks up historical events. For English lessons, they'd like to feature a palindrome tool that checks whether a specific string is the same when reversed, for example the word "level", or the phrase "top spot".

4. Multiple ways to build tools

We can use a different approach for each tool. For historical events, we'll create a tool that directly invokes the LLM to look up natural language dates, such as "5th of November". To build a palindrome checker, we'll use standard Python to compare a reversed string to its original. Let's code the

5. Historical events tool

historical events tool first. We'll label our tool with the LangChain tool decorator, helping the LLM to identify custom tools. Next, we'll call this tool “date_checker”, setting it to expect dates as strings. We'll then write a docstring that provides the LLM with a general instruction to return important historical events by date. Next, we'll set up a try-except block for different outputs. Within the try-except block, we'll use the .invoke() method to invoke the LLM with the given date, passing another instruction that events must relate to the provided date. After saving the response in a variable called "answer", we'll return its content. If the LLM fails to respond, we'll define an exception that returns an error message.

6. Palindrome tool

Next, we'll create our palindrome tool. Starting with a tool decorator, we'll name the tool "check_palindrome", then set the expected input to string. Then we'll create a docstring with an instruction to check for a palindrome. We'll clean our text using .isalnum() to remove any non-alphanumeric characters and use .lower() to turn the remaining characters into lowercase, saving the result as a variable called "cleaned". Next, we set up an if-else statement that checks if the reversed version of "cleaned" is identical to the original, returning the appropriate response.

7. Binding multiple tools

Now we'll add these tools to a new chatbot. We'll import the ToolNode sub-module from langgraph.prebuilt, defining our tools list with the original wikipedia_tool, date_checker, and check_palindrome tools. This list could feature any tools you'd like your chatbot to have. We'll then create a tool_node, passing "tools" to the ToolNode() class. Finally, we'll bind the tools to our llm, creating "model_with_tools" using the .bind_tools() method.

8. Let's practice!

Now it's your turn to practice building multiple tools!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.