Skip to content
LlamaIndex 文档版本 跟随上游 源快照 6a105bac 更新于 2026-10-15

在 LlamaIndex 里组装一个 agent,做法是定义一组工具并交给我们的 ReActAgent 或 FunctionAgent 实现。这里我们配合 OpenAI 使用,但它适用于任何能力足够强的 LLM。

一般而言,对于 API 里自带函数调用/工具能力的 LLM(如 OpenAI、Anthropic、Gemini 等),应优先使用 FunctionAgent。

from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import ReActAgent, FunctionAgent

# define sample Tool
def multiply(a: int, b: int) -> int:
    """Multiply two integers and returns the result integer"""
    return a * b

# initialize llm
llm = OpenAI(model="gpt-4o")

# initialize agent
agent = FunctionAgent(
    tools=[multiply],
    system_prompt="You are an agent that can invoke a tool for multiplication when assisting a user.",
)

这些工具可以是上面那样的 Python 函数,也可以是 LlamaIndex 的查询引擎:

from llama_index.core.tools import QueryEngineTool

query_engine_tools = [
    QueryEngineTool.from_defaults(
        query_engine=sql_agent,
        name="sql_agent",
        description="Agent that can execute SQL queries.",
    ),
]

agent = FunctionAgent(
    tools=query_engine_tools,
    system_prompt="You are an agent that can invoke an agent for text-to-SQL execution.",
)

更多内容见我们的Agent 模块指南或端到端 agent 教程。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。