Welcome to this video on Build LLM Agents with Tools.
In this video, you will learn the first steps in manual tool calling. You'll begin by
initializing the chat model for tool interactions. You'll then define and bind custom tools
to the LLM and add more tools for expanded functionality. Finally, you will use mapping
dictionaries for dynamic function calls.
In this video, you will enable an LLM to manually call a tool. This is a key building block
for transforming a basic model into a real, interactive agent capable of engaging with
the real world.
First begin by building an agent that can do simple arithmetic operations using tools
for addition, subtraction, and multiplication. In this case, you'll take an LLM and give
it access to a set of tools. For example, if the input query is, what is 3 plus 2? The
LLM will first extract the parameters, which are 3 and 2, and determine which tool to
call. Next, the parameters are fed into the selected tool, which then processes the input
and generates the output. Finally, the result is returned to the LLM, which combines this
information to produce the final response. The addition of 3 and 2 is 5.
Let's initialize the chat model to set up the LLM for tool interactions. First, import
initChatModel from LangChain's ChatModels module. This function handles all the setup
needed to connect to a chat model. Next, load GPT 4.0 mini with model underscore provider
equals openAI, creating an object that can send and receive messages. Store this object
in a variable called LLM. From this point on, whenever you see LLM dot invoke, it means
you're interacting with the same model instance. In the diagram, the LLM box represents this
LLM object, the central hub for all tool calls and user queries.
With the model now live, the next step is to define a custom tool that the LLM can call.
First, enable the model to perform basic arithmetic by providing it with a simple addition tool.
Second, import the atTool decorator. This tells LangChain that the function can be called
by the model. Next, define a function named add that takes
two integers a and b and returns their sum. The doc string add a and b is what the model
uses to determine when to use this tool. At this point, the tool is defined and ready,
but it hasn't been connected to the model yet.
Next, connect the add function to the model so it recognizes it as a callable tool. To
do this, first place the add function in a list of tools. Then, bind this list to the
chat model, creating a new object called LLMWithTools.
This wraps the original LLM in a way that makes it aware of every function in the tools
list. From this point on, whenever the call is invoked, the model will recognize and use
the add tool whenever it needs to compute a sum.
Let's add more tools to the LLM. First, create a subtract tool for basic subtraction. Similarly,
create a multiplication tool to handle multiplication. Later on, you will need to call a function
dynamically using its name. To achieve this, first create a mapping dictionary that links
tool names as strings to their corresponding tool functions.
Next, define the input arguments as a dictionary, where the keys match the function's parameter
names. For example, a equals 1 and b equals 2. Then, call the function by using the tool
name as the key, for example add, to access the function from the mapping dictionary.
Finally, use invoke(Input) to call the function which automatically matches the dictionary
keys a and b to the function's parameters and executes the tool.
Next, create a list of tools that includes the add, subtract, and multiply functions.
You can then bind this list to the model, allowing the LLM to recognize and use these
tools as needed.
In this video, you learned how to transform LLMs into interactive agents by integrating
custom tools, enabling more context-aware responses.
Set up chat models to handle real-world inputs, allowing the LLM to process user queries accurately.
Create and connect tools such as addition, subtraction, and multiplication functions
for dynamic calculations.
Use mapping dictionaries for flexible, name-based function execution, expanding the LLM's
capabilities.
Understand how LLMs identify the right tool and manage parameter inputs for precise outputs.
You can manage chat history, preserving conversation context for more accurate, personalized responses.