跳转到内容
Hugging Face 智能体课程
1. 课程运作方式及问答环节
单元2. 智能体框架

在 LlamaIndex 中使用智能体

还记得之前提到的智能体吗?他是个乐于助人的管家。现在,既然我们了解了 LlamaIndex 中可用的工具,我们就可以为阿尔弗雷德增添新的功能,让他更好地为我们服务。

但在我们继续之前,让我们先回顾一下是什么让像阿尔弗雷德这样的智能体能够运转。在单元1中,我们了解到:

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

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

智能体

  1. Function Calling Agents 这些智能体与能够调用特定功能的AI模型协同工作。
  2. ReAct Agents 这些智能体可以与任何进行聊天或文本处理的人工智能协作,并能够处理复杂的推理任务。
  3. Advanced Custom Agents 这些使用更复杂的方法来处理更复杂的任务和工作流程。

[!TIP] 了解有关高级智能体的更多信息 基础智能体工作流

初始化智能体

[!TIP] 您可以按照代码进行操作。 这个笔记本 您可以使用 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是一种利用智能体来回答关于您的数据的问题的强大方法。 我们可以向 Alfred 传递各种工具,以帮助它回答问题。然而,Alfred 并非会自动在文档之上回答问题,而是可以选择使用其他工具或流程来回答问题。

智能体RAG

这很容易 包装 QueryEngine 作为工具 对于智能体而言。在这种情况下,我们需要 定义名称和描述. 该智能体将使用这些信息来正确调用工具。 让我们看看如何加载一个 QueryEngineTool 使用 QueryEngine 我们创建了 组件部分.

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?")

[!TIP] 还没有学够吗?在 LlamaIndex 中,关于智能体和工具还有更多内容需要了解。 智能体工作流程基础介绍 或 智能体学习指南在这里,您可以阅读更多关于流式处理、上下文序列化以及人工循环控制的内容!

现在我们已经了解了 LlamaIndex 中智能体和工具的基本用法,接下来看看如何利用 LlamaIndex 来实现 创建可配置且易于管理的工作流程!