Defining a clear set of Tools is crucial to performance. As we discussed in unit 1, clear tool interfaces are easier for LLMs to use. Much like a software API interface for human engineers, they can get more out of the tool if it’s easy to understand how it works.
There are four main types of tools in LlamaIndex:
FunctionTool
: Convert any Python function into a tool that an agent can use. It automatically figures out how the function works.QueryEngineTool
: A tool that lets agents use query engines. Since agents are built on query engines, they can also use other agents as tools.Toolspecs
: Sets of tools created by the community, which often include tools for specific services like Gmail.Utility Tools
: Special tools that help handle large amounts of data from other tools.We will go over each of them in more detail below.
A FunctionTool provides a simple way to wrap any Python function and make it available to an agent.
You can pass either a synchronous or asynchronous function to the tool, along with optional name
and description
parameters.
The name and description are particularly important as they help the agent understand when and how to use the tool effectively.
Let’s look at how to create a FunctionTool below.
from llama_index.core.tools import FunctionTool
def get_weather(location: str) -> str:
"""Usfeful for getting the weather for a given location."""
...
tool = FunctionTool.from_defaults(
fn=get_weather,
# async_fn=aget_weather,
# name="...",
# description="...",
)
The QueryEngine
we defined in the previous unit can be easily transformed into a tool using the QueryEngineTool
class.
Let’s see how to create a QueryEngineTool
from a QueryEngine
in the example below.
from llama_index.core import StorageContext, load_index_from_storage
from llama_index.core.tools import QueryEngineTool
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPILM
from llama_index.embeddings.huggingface_api import HuggingFaceInferenceAPIEmbedding
embed_model = HuggingFaceInferenceAPIEmbedding("BAAI/bge-small-en-v1.5")
storage_context = StorageContext.from_defaults(persist_dir="path/to/vector/store")
index = load_index_from_storage(storage_context, embed_model=embed_model)
llm = HuggingFaceInferenceAPILM(model_name="meta-llama/Meta-Llama-3-8B-Instruct")
query_engine = index.as_query_engine(llm=llm)
tool = QueryEngineTool.from_defaults(query_engine)
Think of ToolSpecs
as collections of tools that work together harmoniously - like a well-organized professional toolkit.
Just as a mechanic’s toolkit contains complementary tools that work together for vehicle repairs, a ToolSpec
combines related tools for specific purposes.
For example, an accounting agent’s ToolSpec
might elegantly integrate spreadsheet capabilities, email functionality, and calculation tools to handle financial tasks with precision and efficiency.
pip install llama-index-tools-google
And now we can load the toolspec and convert it to a list of tools.
from llama_index.tools.google import GmailToolSpec
tool_spec = GmailToolSpec()
tool_spec_list = tool_spec.to_tool_list()
Oftentimes, directly querying an API can return an excessive amount of data, some of which may be irrelevant, overflow the context window of the LLM, or unnecessarily increase the number of tokens that you are using. Let’s walk through our two main utility tools below.
OnDemandToolLoader
: This tool turns any existing LlamaIndex data loader (BaseReader class) into a tool that an agent can use. The tool can be called with all the parameters needed to trigger load_data
from the data loader, along with a natural language query string. During execution, we first load data from the data loader, index it (for instance with a vector store), and then query it ‘on-demand’. All three of these steps happen in a single tool call.LoadAndSearchToolSpec
: The LoadAndSearchToolSpec takes in any existing Tool as input. As a tool spec, it implements to_tool_list
, and when that function is called, two tools are returned: a load tool and then a search tool. The load Tool execution would call the underlying Tool, and the index the output (by default with a vector index). The search Tool execution would take in a query string as input and call the underlying index.Now that we understand the basics of agents and tools in LlamaIndex, let’s see how we can use LlamaIndex to create configurable and manageable workflows!
< > Update on GitHub