定义一套明确的工具对于性能至关重要。 正如我们在中讨论的那样 单元1清晰的工具界面使得LLM更容易使用。 就像人类工程师使用的软件API接口一样,如果工具的使用方式易于理解,那么它们就能发挥更大的作用。
有 LlamaIndex中的四种主要工具类型:

FunctionTool: 将任何 Python 函数转换为智能体可以使用的工具。它会自动了解该函数的运作方式。QueryEngineTool:一种让智能体使用查询引擎的工具。由于智能体是基于查询引擎构建的,因此它们也可以将其他智能体作为工具来使用。Toolspecs由社区创建的工具集,通常包含用于特定服务如Gmail的工具。Utility Tools: 用于帮助处理来自其他智能体的大量数据的特殊工具。
我们将在下面更详细地介绍每一个。
创建函数工具
[!TIP] 您可以按照代码进行操作。 这个笔记本 您可以使用 Google Colab来运行它。
FunctionTool提供了一种简单的方法,可以将任何Python函数封装起来,使其可供智能体使用。
您可以将同步或异步函数传递给工具,并附带可选的参数。 name 和 description 参数。名称和描述非常重要,因为它们有助于智能体理解何时以及如何使用工具来发挥有效作用。下面我们将了解如何创建一个FunctionTool,然后调用它。
from llama_index.core.tools import FunctionTool
def get_weather(location: str) -> str:
"""Useful for getting the weather for a given location."""
print(f"Getting weather for {location}")
return f"The weather in {location} is sunny"
tool = FunctionTool.from_defaults(
get_weather,
name="my_weather_tool",
description="Useful for getting the weather for a given location.",
)
tool.call("New York")
[!TIP] 在使用智能体或大语言模型进行函数调用时,所选工具(以及为该工具编写的参数)在很大程度上依赖于工具的名称以及其功能和参数的描述。更多关于函数调用的信息,请参阅…… 工具调用指南.
创建查询智能体工具
这个 QueryEngine 我们在前一个单元中定义的可以很容易地通过工具调用转化为 QueryEngineTool 让我们来看看如何创建一个 QueryEngineTool 从 QueryEngine 在下面的例子中。
from llama_index.core import VectorStoreIndex
from llama_index.core.tools import QueryEngineTool
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.vector_stores.chroma import ChromaVectorStore
embed_model = HuggingFaceEmbedding("BAAI/bge-small-en-v1.5")
db = chromadb.PersistentClient(path="./alfred_chroma_db")
chroma_collection = db.get_or_create_collection("alfred")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
query_engine = index.as_query_engine(llm=llm)
tool = QueryEngineTool.from_defaults(query_engine, name="some useful name", description="some useful description")
创建工具规范
想象 ToolSpecs 就像一组协同工作的工具集合——就像一套组织良好的专业工具箱。
就像机械师的工具箱包含互补的工具,它们共同协作进行车辆维修一样, ToolSpec 为特定目的组合相关工具。例如,一名会计智能体的 ToolSpec 可以巧妙地整合电子表格功能、电子邮件功能以及计算工具,以精确且高效的方式处理财务任务。
安装 Google Toolspec
如文中所述 LlamaHub的相关章节我们可以使用以下命令安装 Google toolspec:pip install llama-index-tools-google
现在我们可以加载工具规范,并将其转换为工具列表。
from llama_index.tools.google import GmailToolSpec
tool_spec = GmailToolSpec()
tool_spec_list = tool_spec.to_tool_list()
为了更详细地了解这些工具,我们可以看一下 metadata 每种工具的作用。
[(tool.metadata.name, tool.metadata.description) for tool in tool_spec_list]
LlamaIndex中的模型上下文协议(MCP)
LlamaIndex还允许通过智能体使用MCP工具。 LlamaHub上的ToolSpec您可以简单地运行一个MCP服务器,并通过以下实现开始使用它。
如果您想深入了解MCP,可以查看我们的 免费 MCP课程.
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
# We consider there is a mcp server running on 127.0.0.1:8000, or you can use the mcp client to connect to your own mcp server.
mcp_client = BasicMCPClient("http://127.0.0.1:8000/sse")
mcp_tool = McpToolSpec(client=mcp_client)
# get the agent
agent = await get_agent(mcp_tool)
# create the agent context
agent_context = Context(agent)
实用工具
通常,直接查询 API 可能返回过多的数据其中一些可能不相关,会溢出 LLM 的上下文窗口,或者不必要地增加你使用的标记数量。 让我们来了解一下下面的两个主要工具。
OnDemandToolLoader此工具可将任何现有的 LlamaIndex 数据加载器(BaseReader 类)转换为智能体可以使用的工具。该工具可以调用时带上触发所需的所有参数。load_data从数据加载器中,结合一个自然语言查询字符串。在执行过程中,我们首先从数据加载器中加载数据,对其进行索引处理(例如使用向量存储),然后按需进行查询。这三个步骤都通过一次工具调用来完成。LoadAndSearchToolSpec: LoadAndSearchToolSpec 接受任何现有的工具作为输入。作为工具规范,它实现了to_tool_list,当调用该函数时,会返回两个工具:一个加载工具和一个搜索工具。加载工具执行会调用底层工具,然后对输出结果进行索引处理(默认情况下使用向量索引)。搜索工具执行则会接收查询字符串作为输入,并调用底层索引。
[!TIP] 您可以在以下位置找到工具规范与实用工具: LlamaHub
现在我们已经了解了 LlamaIndex 中智能体和工具的基本用法,接下来看看我们该如何 使用 LlamaIndex 创建可配置且易于管理的工作流程!