Agents 课程文档
在 LlamaIndex 中使用工具
并获得增强的文档体验
开始使用
在 LlamaIndex 中使用工具
定义一套清晰的工具对于性能至关重要。 正如我们在单元 1 中讨论的那样,清晰的工具接口更容易让 LLM 使用。就像人类工程师的软件 API 接口一样,如果容易理解工具的工作原理,他们就可以从工具中获得更多。
LlamaIndex 中有 四种主要类型的工具
FunctionTool
:将任何 Python 函数转换为 agent 可以使用的工具。它会自动弄清楚函数的工作原理。QueryEngineTool
:一种允许 agent 使用查询引擎的工具。由于 agent 构建在查询引擎之上,它们也可以使用其他 agent 作为工具。Toolspecs
:社区创建的工具集,通常包括用于特定服务(如 Gmail)的工具。Utility Tools
:特殊工具,可帮助处理来自其他工具的大量数据。
我们将在下面更详细地介绍它们中的每一个。
创建 FunctionTool
FunctionTool 提供了一种简单的方法来包装任何 Python 函数,并使其可供 agent 使用。您可以将同步或异步函数以及可选的 name
和 description
参数传递给该工具。名称和描述尤为重要,因为它们可以帮助 agent 理解何时以及如何有效地使用该工具。让我们看看如何在下面创建一个 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")
创建 QueryEngineTool
我们在上一单元中定义的 QueryEngine
可以使用 QueryEngineTool
类轻松转换为工具。让我们看看如何在下面的示例中从 QueryEngine
创建 QueryEngineTool
。
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
将 ToolSpecs
视为协同工作的工具集合 - 就像组织良好的专业工具包。正如机械师的工具包包含用于车辆维修的互补工具一样,ToolSpec
组合了用于特定目的的相关工具。例如,会计 agent 的 ToolSpec
可能会优雅地集成电子表格功能、电子邮件功能和计算工具,以精确高效地处理财务任务。
安装 Google Toolspec
正如关于 LlamaHub 的部分中介绍的那样,我们可以使用以下命令安装 Google toolspecpip install llama-index-tools-google
现在我们可以加载 toolspec 并将其转换为工具列表。
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 还允许通过 LlamaHub 上的 ToolSpec 使用 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 类)转换为 agent 可以使用的工具。可以使用触发数据加载器的load_data
所需的所有参数以及自然语言查询字符串来调用该工具。在执行期间,我们首先从数据加载器加载数据,对其进行索引(例如使用向量存储),然后“按需”查询它。所有这三个步骤都在单个工具调用中完成。LoadAndSearchToolSpec
:LoadAndSearchToolSpec 接受任何现有工具作为输入。作为工具规范,它实现了to_tool_list
,当调用该函数时,将返回两个工具:加载工具和搜索工具。加载工具执行将调用底层工具,并索引输出(默认情况下使用向量索引)。搜索工具执行将接受查询字符串作为输入并调用底层索引。
现在我们了解了 LlamaIndex 中 agent 和工具的基础知识,让我们看看我们如何使用 LlamaIndex 创建可配置和可管理的工作流!
< > 在 GitHub 上更新