Understanding Agents and Tools in LlamaIndex

Let’s recap the definition of an Agent introduced in unit 1:

An Agent is a system that leverages an AI model to interact with its environment in order to achieve a user-defined objective. It combines reasoning, planning, and the execution of actions (often via external tools) to fulfill tasks.

Within LlamaIndex, we build data agents with two core components:

  1. A set of tools, discussed in unit 1 on tools
  2. A reasoning loop, discussed in unit 1 on internal reasoning

Using Tools in LlamaIndex

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. LlamaIndex allows you to define a Tool as well as a ToolSpec containing a series of customise functions under the hood.

When using an agent or LLM with function calling, the tool selected (and the arguments written for that tool) rely strongly on the tool name and description of the tools purpose and arguments.

Let’s explore the main types of tools in LlamaIndex:

  1. FunctionTool: Convert any Python function into a tool that an agent can use. It automatically figures out how the function works.
  2. QueryEngineTool: A tool that lets agents use query engines. Since agents are built on query engines, they can also use other agents as tools.
  3. Toolspecs: Tools created by the community to work with different services like Gmail.
  4. Utility Tools: Special tools that help handle large amounts of data from other tools.

We will go over each of these in more detail below.

Creating a FunctionTool

A function tool is a simple wrapper around any existing python function. We can choose to pass a sync or async function to the tool. Additionally, we can choose to name and describe the tool as we want. The code below shows how to create a FunctionTool from a function.

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(
get_weather,
    # async_fn=aget_weather, name="...", description="...",
)

Creating a QueryEngineTool

The QueryEngine we defined in the previous unit can be turned into a a tool using the QueryEngineTool class. The code below shows how to create a QueryEngineTool from a QueryEngine.

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,
    # name="...", description="..."
)

Creating Toolspecs

Custom Tools and ToolSpecs are created by the community and shared on the LlamaHub. You can think of ToolSpecs like a toolkit intended to be used together. Just like a professional toolkit, they cover relevant actions for a use case. For example, an accounting agent might have spreadsheet integration, an en email integration, and a calculator.

Similarly to components, these toolspecs need to be installed and follow a similar pattern.

pip install llama-index-tools-{toolspec_name}

Let’s install a toolspec that works on Google services.

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()

Utility Tools

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.

  1. 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.
  2. 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.
You can find other utility tools on the [LlamaHub](https://llamahub.ai/)

Using Agents in LlamaIndex

LlamaIndex supports three main types of reasoning agents:

  1. Function Calling Agents - These work with AI models that can call specific functions
  2. ReAct Agents - These can work with any AI that does chat or text endpoint and deal with complex reasoning tasks
  3. Advanced Agents - These use more complex methods like LLMCompiler or Chain-of-Abstraction

[!NOTE] Find more information on advanced agents on LlamaIndex GitHub

An agent is initialized from a set of Tools. Here’s an example of instantiating a ReAct agent from a set of Tools.

from llama_index.core.tools import FunctionTool
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPILM
from llama_index.core.agent import ReActAgent


# define sample Tool
def multiply(a: int, b: int) -> int:
    """Multiple two integers and returns the result integer"""
    return a * b


multiply_tool = FunctionTool.from_defaults(fn=multiply)

# initialize llm
llm = HuggingFaceInferenceAPILM(model_name="meta-llama/Meta-Llama-3-8B-Instruct")

# initialize ReAct agent
agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)

Similarly, we can use the AgentRunner to automatically pick the best agent reasoning flow depending on the LLM.

from llama_index.core.agent import AgentRunner

agent_runner = AgentRunner.from_llm(llm, verbose=True)

Agent supports both chat and query endpoints with query() and chat(), where chat interactions keep a history of messages.

response = agent.query("What is 2 times 2?")

Now we’ve gotten the basics, let’s take a look at how we can use tools in our agents.

Creating RAG Agents with QueryEngineTools

It is easy to wrap QueryEngine as tools for an agent. When doing so, we need to define a name and description within the ToolMetadata. The LLM will use this information to correctly use the tool. Let’s see how to load in a QueryEngineTool using the QueryEngine we created in the component section.

from llama_index.core.tools import QueryEngineTool, ToolMetadata

query_engine = index.as_query_engine(similarity_top_k=3) # as shown in the previous section

query_engine_tool = QueryEngineTool(
    query_engine=query_engine,
    metadata=ToolMetadata(
        name="a specific name",
        description="a specific description",
    ),
    return_direct=False,
)
query_engine_agent = ReActAgent.from_tools([query_engine_tool], llm=llm, verbose=True)

Creating Multi-agent systems

Agents in LlamaIndex can directly be used as tools for other agents by loading them as a QueryEngineTool.

from llama_index.core.tools import QueryEngineTool

query_engine_agent = # as defined in the previous section

query_engine_agent_tool = QueryEngineTool(
    query_engine=query_engine_agent,
    metadata=ToolMetadata(
        name="a specific name",
        description="a specific description",
    ),
)

multi_agent = ReActAgent.from_tools([query_engine_agent_tool], llm=llm, verbose=True)
Haven't learned enough yet? There is a lot more to discover about agents and tools in LlamaIndex within the [Agent Component Guide](https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/modules/) or the [Agent Learning Guide](https://docs.llamaindex.ai/en/stable/understanding/agent/).

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