Agent 课程文档

在 LlamaIndex 中使用 Agent

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

在 LlamaIndex 中使用 Agent

还记得 Alfred 吗,我们之前那个乐于助人的管家 Agent?他即将迎来升级!现在我们了解了 LlamaIndex 中可用的工具,我们可以赋予 Alfred 新的能力,以便更好地为我们服务。

但在我们继续之前,让我们回顾一下是什么让像 Alfred 这样的 Agent 运转起来。在单元 1 中,我们了解到

Agent 是一个利用 AI 模型与其环境交互以实现用户定义目标的系统。它结合了推理、规划和行动执行(通常通过外部工具)来完成任务。

LlamaIndex 支持三种主要的推理 Agent 类型:

Agents

  1. 函数调用 Agent - 这些 Agent 与可以调用特定函数的 AI 模型一起工作。
  2. ReAct Agent - 这些 Agent 可以与任何具有聊天或文本端点的 AI 一起工作,并处理复杂的推理任务。
  3. 高级自定义 Agent - 这些 Agent 使用更复杂的方法来处理更复杂的任务和工作流。
BaseWorkflowAgent 上找到关于高级 Agent 的更多信息

初始化 Agent

你可以按照这个 notebook 中的代码进行操作,你可以使用 Google Colab 运行它。

要创建一个 Agent,我们首先需要为其提供一组定义其能力的函数/工具。让我们看看如何使用一些基本工具创建一个 Agent。截至本文撰写之时,Agent 将自动使用函数调用 API(如果可用)或标准的 ReAct Agent 循环。

支持工具/函数 API 的 LLM 相对较新,但它们提供了一种强大的工具调用方式,通过避免特定的提示,并允许 LLM 基于提供的模式创建工具调用。

ReAct Agent 也擅长复杂的推理任务,并且可以与任何具有聊天或文本完成功能的 LLM 一起工作。它们更冗长,并显示了他们采取某些行动背后的推理。

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
)

Agent 默认是无状态的,使用 Context 对象选择性启用记住过去的交互。如果你想使用需要记住先前交互的 Agent,例如在多条消息中保持上下文的聊天机器人,或者需要跟踪一段时间内进度的任务管理器,这可能很有用。

# 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 中的 Agent 是异步的,因为它们使用了 Python 的 await 运算符。如果你是 Python 异步代码的新手,或者需要复习一下,他们有一个很棒的异步指南

现在我们已经掌握了基础知识,让我们来看看如何在我们的 Agent 中使用更复杂的工具。

使用 QueryEngineTools 创建 RAG Agent

Agentic RAG 是一种使用 Agent 回答有关你数据问题的强大方法。我们可以将各种工具传递给 Alfred,以帮助他回答问题。但是,Alfred 可以决定使用任何其他工具或流程来回答问题,而不是自动在文档之上回答问题。

Agentic RAG

QueryEngine 包装为 Agent 的工具很容易。这样做时,我们需要定义名称和描述。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. "
)

创建多 Agent 系统

AgentWorkflow 类也直接支持多 Agent 系统。通过为每个 Agent 提供名称和描述,系统保持单个活跃发言者,每个 Agent 都有能力移交给另一个 Agent。

通过缩小每个 Agent 的范围,我们可以帮助提高他们在响应用户消息时的总体准确性。

LlamaIndex 中的 Agent 也可以直接用作其他 Agent 的工具,以用于更复杂和自定义的场景。

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 中的 Agent 和工具,还有很多内容可以在 AgentWorkflow 基础介绍Agent 学习指南 中发现,你可以在其中阅读更多关于流式传输、上下文序列化和人机协作的内容!

现在我们了解了 LlamaIndex 中 Agent 和工具的基础知识,让我们看看如何使用 LlamaIndex 创建可配置和可管理的工作流!

< > 在 GitHub 上更新