智能体课程文档
在 LlamaIndex 中使用工具
并获得增强的文档体验
开始使用
在 LlamaIndex 中使用工具
定义一套清晰的工具对于性能至关重要。正如我们在第一单元中讨论的那样,清晰的工具接口更容易被大型语言模型(LLM)使用。就像人类工程师使用的软件 API 接口一样,如果工具易于理解其工作原理,他们就能更好地利用它。
LlamaIndex 中有四种主要类型的工具
FunctionTool
:将任何 Python 函数转换为代理可以使用的工具。它会自动找出函数的工作原理。QueryEngineTool
:一种允许代理使用查询引擎的工具。由于代理是建立在查询引擎之上的,它们也可以将其他代理用作工具。Toolspecs
:由社区创建的工具集,通常包括用于特定服务(如 Gmail)的工具。Utility Tools
:帮助处理来自其他工具的大量数据的特殊工具。
我们将在下面详细介绍每种工具。
创建 FunctionTool
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")
创建 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` 也将相关工具组合在一起以用于特定目的。例如,会计代理的 `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 服务器并通过以下实现开始使用它。
如果您想深入了解 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 的上下文窗口,或者不必要地增加您使用的 token 数量。下面我们来详细介绍我们的两个主要实用工具。
OnDemandToolLoader
:此工具将任何现有 LlamaIndex 数据加载器(BaseReader 类)转换为代理可以使用的工具。该工具可以与触发数据加载器 `load_data` 所需的所有参数一起调用,以及一个自然语言查询字符串。在执行过程中,我们首先从数据加载器加载数据,对其进行索引(例如使用向量存储),然后“按需”查询它。所有这三个步骤都在一个工具调用中完成。LoadAndSearchToolSpec
:`LoadAndSearchToolSpec` 将任何现有工具作为输入。作为一个工具规范,它实现了 `to_tool_list`,当调用该函数时,将返回两个工具:一个加载工具和一个搜索工具。加载工具的执行将调用底层工具,然后索引输出(默认情况下使用向量索引)。搜索工具的执行将以查询字符串作为输入并调用底层索引。
现在我们了解了 LlamaIndex 中代理和工具的基础知识,接下来我们看看如何**使用 LlamaIndex 创建可配置和可管理的工作流!**
< > 在 GitHub 上更新