智慧體課程文件
在 LlamaIndex 中使用智慧體
並獲得增強的文件體驗
開始使用
在 LlamaIndex 中使用智慧體
還記得阿爾弗雷德,我們之前那樂於助人的管家智慧體嗎?他即將獲得升級!現在我們瞭解了 LlamaIndex 中可用的工具,我們可以賦予阿爾弗雷德新的能力,以便更好地為我們服務。
但在我們繼續之前,讓我們回顧一下是什麼讓像阿爾弗雷德這樣的智慧體運作起來。在第一單元中,我們學到:
智慧體是一個利用 AI 模型與其環境互動以實現使用者定義目標的系統。它結合了推理、規劃和行動執行(通常透過外部工具)來完成任務。
LlamaIndex 支援三種主要的推理智慧體型別:
函式呼叫智慧體
- 這些智慧體與可以呼叫特定函式的 AI 模型一起工作。ReAct 智慧體
- 這些智慧體可以與任何支援聊天或文字端點的 AI 一起工作,並處理複雜的推理任務。高階自定義智慧體
- 這些智慧體使用更復雜的方法來處理更復雜的任務和工作流。
初始化智慧體
要建立一個智慧體,我們首先要為其提供一組定義其能力的函式/工具。讓我們看看如何使用一些基本工具建立一個智慧體。截至本文撰寫之時,智慧體將自動使用函式呼叫 API(如果可用),或標準的 ReAct 智慧體迴圈。
支援工具/函式 API 的大型語言模型相對較新,但它們提供了一種強大的方式來呼叫工具,透過避免特定的提示並允許大型語言模型根據提供的模式建立工具呼叫。
ReAct 智慧體也擅長處理複雜的推理任務,並且可以與任何具有聊天或文字完成功能的大型語言模型一起工作。它們更冗長,並顯示其採取某些行動背後的推理過程。
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.tools import FunctionTool
# define sample Tool -- type annotations, function names, and docstrings, are all included in parsed schemas!
def multiply(a: int, b: int) -> int:
"""Multiplies two integers and returns the resulting integer"""
return a * b
# initialize llm
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
# initialize agent
agent = AgentWorkflow.from_tools_or_functions(
[FunctionTool.from_defaults(multiply)],
llm=llm
)
智慧體預設是無狀態的,但是,它們可以使用 Context
物件記住過去的互動。如果您想使用需要記住先前互動的智慧體,例如維護多個訊息上下文的聊天機器人或需要跟蹤進度的任務管理器,這可能很有用。
# stateless
response = await agent.run("What is 2 times 2?")
# remembering state
from llama_index.core.workflow import Context
ctx = Context(agent)
response = await agent.run("My name is Bob.", ctx=ctx)
response = await agent.run("What was my name again?", ctx=ctx)
您會注意到 LlamaIndex
中的智慧體是非同步的,因為它們使用 Python 的 await
運算子。如果您是 Python 非同步程式碼的新手,或者需要複習,他們有一個出色的非同步指南。
現在我們已經掌握了基礎知識,讓我們看看如何在智慧體中使用更復雜的工具。
使用 QueryEngineTools 建立 RAG 智慧體
智慧體 RAG 是一種強大的方式,可以使用智慧體來回答關於您的資料的問題。 我們可以向阿爾弗雷德傳遞各種工具來幫助他回答問題。然而,阿爾弗雷德可以決定使用任何其他工具或流程來回答問題,而不是自動地基於文件回答問題。
將 QueryEngine
封裝為智慧體工具非常簡單。在此過程中,我們需要定義一個名稱和描述。LLM 將使用這些資訊正確地使用該工具。讓我們看看如何使用我們在元件部分中建立的 QueryEngine
載入一個 QueryEngineTool
。
from llama_index.core.tools import QueryEngineTool
query_engine = index.as_query_engine(llm=llm, similarity_top_k=3) # as shown in the Components in LlamaIndex section
query_engine_tool = QueryEngineTool.from_defaults(
query_engine=query_engine,
name="name",
description="a specific description",
return_direct=False,
)
query_engine_agent = AgentWorkflow.from_tools_or_functions(
[query_engine_tool],
llm=llm,
system_prompt="You are a helpful assistant that has access to a database containing persona descriptions. "
)
建立多智慧體系統
AgentWorkflow
類也直接支援多智慧體系統。透過給每個智慧體一個名稱和描述,系統可以維持一個單一的活躍說話者,每個智慧體都有能力將控制權交給另一個智慧體。
透過縮小每個智慧體的範圍,我們可以幫助提高它們在響應使用者訊息時的一般準確性。
LlamaIndex 中的智慧體也可以直接用作其他智慧體的工具,以應對更復雜和自定義的場景。
from llama_index.core.agent.workflow import (
AgentWorkflow,
FunctionAgent,
ReActAgent,
)
# Define some tools
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
def subtract(a: int, b: int) -> int:
"""Subtract two numbers."""
return a - b
# Create agent configs
# NOTE: we can use FunctionAgent or ReActAgent here.
# FunctionAgent works for LLMs with a function calling API.
# ReActAgent works for any LLM.
calculator_agent = ReActAgent(
name="calculator",
description="Performs basic arithmetic operations",
system_prompt="You are a calculator assistant. Use your tools for any math operation.",
tools=[add, subtract],
llm=llm,
)
query_agent = ReActAgent(
name="info_lookup",
description="Looks up information about XYZ",
system_prompt="Use your tool to query a RAG system to answer information about XYZ",
tools=[query_engine_tool],
llm=llm
)
# Create and run the workflow
agent = AgentWorkflow(
agents=[calculator_agent, query_agent], root_agent="calculator"
)
# Run the system
response = await agent.run(user_msg="Can you add 5 and 3?")
現在我們已經瞭解了 LlamaIndex 中智慧體和工具的基礎知識,讓我們看看如何使用 LlamaIndex 來建立可配置和可管理的工作流!
< > 在 GitHub 上更新