智慧體課程文件

在 LlamaIndex 中使用工具

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

在 LlamaIndex 中使用工具

定義一套清晰的工具對於效能至關重要。正如我們在第一單元中討論的那樣,清晰的工具介面更容易被大型語言模型(LLM)使用。就像人類工程師使用的軟體 API 介面一樣,如果工具易於理解其工作原理,他們就能更好地利用它。

LlamaIndex 中有四種主要型別的工具

Tools

  1. FunctionTool:將任何 Python 函式轉換為代理可以使用的工具。它會自動找出函式的工作原理。
  2. QueryEngineTool:一種允許代理使用查詢引擎的工具。由於代理是建立在查詢引擎之上的,它們也可以將其他代理用作工具。
  3. Toolspecs:由社群建立的工具集,通常包括用於特定服務(如 Gmail)的工具。
  4. Utility Tools:幫助處理來自其他工具的大量資料的特殊工具。

我們將在下面詳細介紹每種工具。

建立 FunctionTool

您可以按照此 Jupyter Notebook 中的程式碼操作,您可以使用 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")
當使用具有函式呼叫的代理或 LLM 時,所選的工具(以及為該工具編寫的引數)強烈依賴於工具名稱以及工具目的和引數的描述。在函式呼叫指南中瞭解更多關於函式呼叫的資訊。

建立 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 toolspec
pip 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 課程

安裝 MCP Toolspec正如LlamaHub 部分中介紹的,我們可以使用以下命令安裝 MCP toolspec
pip install llama-index-tools-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 數量。下面我們來詳細介紹我們的兩個主要實用工具。

  1. OnDemandToolLoader:此工具將任何現有 LlamaIndex 資料載入器(BaseReader 類)轉換為代理可以使用的工具。該工具可以與觸發資料載入器 `load_data` 所需的所有引數一起呼叫,以及一個自然語言查詢字串。在執行過程中,我們首先從資料載入器載入資料,對其進行索引(例如使用向量儲存),然後“按需”查詢它。所有這三個步驟都在一個工具呼叫中完成。
  2. LoadAndSearchToolSpec:`LoadAndSearchToolSpec` 將任何現有工具作為輸入。作為一個工具規範,它實現了 `to_tool_list`,當呼叫該函式時,將返回兩個工具:一個載入工具和一個搜尋工具。載入工具的執行將呼叫底層工具,然後索引輸出(預設情況下使用向量索引)。搜尋工具的執行將以查詢字串作為輸入並呼叫底層索引。
您可以在LlamaHub上找到工具規範和實用工具

現在我們瞭解了 LlamaIndex 中代理和工具的基礎知識,接下來我們看看如何**使用 LlamaIndex 建立可配置和可管理的工作流!**

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.