智能体课程文档

在 LlamaIndex 中使用智能体

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

在 LlamaIndex 中使用智能体

还记得阿尔弗雷德,我们之前那乐于助人的管家智能体吗?他即将获得升级!现在我们了解了 LlamaIndex 中可用的工具,我们可以赋予阿尔弗雷德新的能力,以便更好地为我们服务。

但在我们继续之前,让我们回顾一下是什么让像阿尔弗雷德这样的智能体运作起来。在第一单元中,我们学到:

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

LlamaIndex 支持三种主要的推理智能体类型:

Agents

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

初始化智能体

你可以参照此笔记本中的代码,你可以使用 Google Colab 运行它。

要创建一个智能体,我们首先要为其提供一组定义其能力的函数/工具。让我们看看如何使用一些基本工具创建一个智能体。截至本文撰写之时,智能体将自动使用函数调用 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 是一种强大的方式,可以使用智能体来回答关于您的数据的问题。 我们可以向阿尔弗雷德传递各种工具来帮助他回答问题。然而,阿尔弗雷德可以决定使用任何其他工具或流程来回答问题,而不是自动地基于文档回答问题。

Agentic 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 中关于智能体和工具的更多内容,请查阅AgentWorkflow 基本介绍智能体学习指南,您可以在其中阅读有关流式传输、上下文序列化和人机交互的更多信息!

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

< > 在 GitHub 上更新