DEV Community

Cover image for AI Agents - Tool Calling And Its Types
Ramya Perumal
Ramya Perumal

Posted on

AI Agents - Tool Calling And Its Types

Tool means a function. The Agent will execute the function when it is needed. The tool should be written with a proper description. Without a proper description, the Agent cannot identify the tool properly.

Below are the following types of tool calling.

1. Function Call - Manual Loop

Here, tools will be bound with the LLM using a function called bind_tools. When this is done, the LLM will get to know the tool's name, description, and JSON Schema.

When the user asks a query, we check whether the response has an attribute called tool_calls. If the tool_calls list is empty, we take it as the final response. But if it is not empty, it means there are some tools that need to be called.

Our code will execute the tool and return the result to the LLM. This process will be repeated until the entire query is processed. Finally, the LLM generates the answer.

Here, the description of the tool should be clear so that the LLM can identify the tools carefully.

2. Custom Tools

We can build the tool in 3 ways:

  1. Using decorators, which is a function call.
  2. Using Pydantic - we can specify the specification, i.e., validating the input used in the tool. Basically, passing the arguments to the tool that need to be validated.
  3. Structured tool - use the existing function and make it a tool.

It will be used in legacy code that we want to use as a tool along with parameters.

3. External API Tools

Here, tools will be calling an API URL to get the result and process the query according to the result.

Example:

User asks about the current weather of Chennai.

To get the weather, first we need to get the coordinates and then the weather. To get the coordinates, we will use a tool that calls an API to get the coordinates of the particular place. Here, it is Chennai.

Then the Agent will initiate the get_weather tool call to get the weather of the place. Using the result of the tool call, the LLM will generate the result in natural language.

DB Tools

We are using tools to get the result from the database.

For example,

User asks the query: Which customer from Chennai has spent the most in total, and how much?

We are using 2 tools called list_tables and run_sql_query.

list_tables will get all the table schemas from the database in a read-only mode and form a SQL query that will be used to fetch the result from the tables.

run_sql_query will take the SQL query and fetch the result.

Using the result of the tool call, the LLM will generate the result in natural language.

We should be more cautious about what type of query we give permission to execute. Suppose we give permission to execute a DELETE query; we will end up losing the data.

We need to define the schema properly.

Top comments (0)