上一篇介绍了LlamaIndex的工作流(Workflow),其通过事件驱动的方式实现了工作流编排,其中事件(Event)和上下文(Context)是两个核心概念与关键要素。
在LlamaIndex中,智能体(Agent)的构建同样基于事件编排机制,其运行逻辑正是通过Workflow来实现的。
02 相关概念
LlamaIndex中定义了三种基本的Agent模式,分别是FunctionAgent、ReActAgent、CodeActAgent。
它们都继承至BaseWorkflowAgent类,而BaseWorkflowAgent又继承至Workflow,关系如下所示:
FunctionAgent:
使用大模型(LLM)原生Function Call能力驱动工具调用的Agent,工具的调用由LLM直接返回的tool_calls JSON触发。要求模型具有Function Call的能力。
ReActAgent:
借助LLM的提示词(思考 -> 执行 -> 反馈)这种文本协议驱动的推理式Agent,需要Agent手动解析文本得到工具调用信息。比FunctionAgent适用性更广,但是可能因解析工具失败而终止。
CodeActAgent:
允许模型用Python代码执行操作的Agent,可以借助LlamaIndex集成的CodeInterpreterTool等工具进行实现。类似OpenAI的Code Interpreter。
03 Agent实战
下面会依次介绍下Agent的简单使用、如何在Agent中使用上下文(Context)实现记忆、Human in the loop 人参与交互、多Agent协作、实现Agent结构化输出。示例主要使用FunctionAgent演示。
定义一个Agent
# pip install llama-index-core llama-index-llms-deepseek from llama_index.core.agent import FunctionAgent from llama_index.llms.deepseek import DeepSeek def multiply(a: float, b: float) -> float: """Multiply two numbers and returns the product""" print("execute multiply") return a * b def add(a: float, b: float) -> float: """Add two numbers and returns the sum""" print("execute add") return a + b llm = DeepSeek(model="deepseek-chat", api_key="sk-...") agent = FunctionAgent( tools=[multiply, add], llm=llm, system_prompt="You are an agent that can perform basic mathematical operations using tools.", verbose=True, ) # 执行 async def main(): result = await agent.run("what is 1 add 1") print(result) if __name__ == "__main__": import asyncio asyncio.run(main())示例首先定义两个自定义函数作为Agent的工具,通过llama-index-llms-deepseek集成并定义DeepSeek作为Agent的llm。然后实例化FunctionAgent并传入工具列表、模型和系统提示词,最后调用Agent的run方法。输出如下,可以看出Agent借助LLM成功调用了传入的工具:
execute add 1 + 1 = 2LLM提示词在任何以LLM为主的应用中都至关重要,查看示例的Agent在调用LLM时提示词:
首先,在代码最上面引入(以此打开Debug日志):
import logging logging.basicConfig(level=logging.DEBUG)然后运行查看完整提示词(摘取部分内容):
# 第一次输入模型提示词 [{'role': 'system', 'content': 'You are an agent that can perform basic mathematical operations using tools.'}, {'role': 'user', 'content': 'what is 1 add 1'}] 'tools': [{'type': 'function', 'function': {'name': 'multiply', 'description': 'multiply(a: float, b: float) -> float\nMultiply two numbers and returns the product', 'parameters': {'properties': {'a': {'title': 'A', 'type': 'number'}, 'b': {'title': 'B', 'type': 'number'}}, 'required': ['a', 'b'], 'type': 'object', 'additionalProperties': False}, 'strict': False}}, {'type': 'function', 'function': {'name': 'add', 'description': 'add(a: float, b: float) -> float\nAdd two numbers and returns the sum', 'parameters': {'properties': {'a': {'title': 'A', 'type': 'number'}, 'b': {'title': 'B', 'type': 'number'}}, 'required': ['a', 'b'], 'type': 'object', 'additionalProperties': False}, 'strict': False}}] # 第二次输入给模型提示词, # 可以看出第一次模型输出的assistant角色的tool_calls内容 [{'role': 'system', 'content': 'You are an agent that can perform basic mathematical operations using tools.'}, {'role': 'user', 'content': 'what is 1 add 1'}, {'role': 'assistant', 'content': "I'll calculate 1 + 1 for you.", 'tool_calls': [{'index': 0, 'id': 'call_00_HZ4zYEy8Id5u6hsh7EmUHJ8j', 'function': {'arguments': '{"a": 1, "b": 1}', 'name': 'add'}, 'type': 'function'}]}, {'role': 'tool', 'content': '2', 'tool_call_id': 'call_00_HZ4zYEy8Id5u6hsh7EmUHJ8j'}]示例中使用了自定义函数作为工具,还可以使用LlamaIndex集成的工具,通过访问https://llamahub.ai/?tab=tools方便查找,提供了使用步骤,例如通过使用DuckDuckGoSearchToolSpec工具实现搜索功能,如下所示:
# 使用集成的工具 pip install llama-index-tools-duckduckgo from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec dd_tools = DuckDuckGoSearchToolSpec().to_tool_list() dd_tools.extend([add, multiply]) # 定义Agent的tools tools=dd_toolsAgent也是基于Workflow的事件驱动的,上面的示例通过增加调用agent的events属性,可以打印出所有相关的事件,它们被定义在BaseWorkflowAgent基类中。通过BaseWorkflowAgent类定义的step可以得出,流程涉及事件及路径是:AgentWorkflowStartEvent -> AgentInput -> AgentSetup -> AgentOutput -> StopEvent | AgentInput | ToolCall (ToolCall -> ToolCallResult -> StopEvent | AgentInput ) | None。
竖线表示或的关系,圆括号可以理解为子流程。
Agent中使用上下文
在上一篇Workflow中已经介绍过关于Context诸多用法,在Agent中可以通过Context实现短期或者长期的记忆,短期记忆通过指定和使用Agent的上下文即可,长期记忆需要借助Context的序列化和外部数据库进行存储。
# 额外导入包,和Workflow一样的 from llama_index.core.workflow import Context # 定义上下文 ctx = Context(agent) # 执行 async def main(): result = await agent.run(user_msg="Hi, my name is DJ!", ctx=ctx) print(result) print("-" * 10) response2 = await agent.run(user_msg="What's my name?", ctx=ctx) print(response2) if __name__ == "__main__": import asyncio asyncio.run(main())示例是在上文示例的基础上更新的,定义了Context作为上下文,并在agent调用run的时候传入ctx参数,输出如下:
Hi DJ! Nice to meet you. I'm here to help you with basic mathematical operations. I can add or multiply numbers for you. What would you like me to help you with today? ---------- Your name is DJ! You introduced yourself at the beginning of our conversation.Human in the loop
人类参与循环(简写HITL)描述的是一种人机交互的过程。在Agent中,通过人类参与中间特定环节的输入和决策,来驱动Agent按预期的路径执行。
# 人类介入 from llama_index.core.agent.workflow import FunctionAgent from llama_index.llms.deepseek import DeepSeek from llama_index.core.workflow import( InputRequiredEvent, HumanResponseEvent, Context, ) llm = DeepSeek(model="deepseek-chat", api_key="sk-...") async def add_money(ctx: Context) -> str: """A task for add money""" print(ctx.to_dict()) print("starting add_money task") question = "Are you sure you want to proceed? (yes/no): " # 标准写法:只用 wait_for_event,它自己会把 waiter_event 写到流里 response = await ctx.wait_for_event( HumanResponseEvent, waiter_id=question, waiter_event=InputRequiredEvent( prefix=question, user_name="User", # 关键:带 user_name ), requirements={"user_name": "User"}, # 关键:匹配条件 ) print(f"response:{response.to_dict()}") # 根据输入处理 if response.response.strip().lower() == "yes": print("add_money response yes") return"add_money task completed successfully." else: print("add_money response no") return"add_money task aborted." # 使用 DeepSeek LLM 创建 workflow agent = FunctionAgent( tools=[add_money], llm=llm, # 使用 DeepSeek system_prompt="""You are a helpful assistant that can run add_money tools.""" ) # 执行 async def main(): handler = agent.run(user_msg="Use the add_money tool to proceed with the me 100 RMB.") async for event in handler.stream_events(): # if isinstance(event, AgentStream): # print(f"{event.delta}", end="", flush=True) if isinstance(event, InputRequiredEvent): print("需要人工介入:") response = input(event.prefix) print(f"人工输入:{response}") handler.ctx.send_event( HumanResponseEvent( response=response, user_name=event.user_name, # 用事件里的名字,和 requirements 对上 ) ) print("-"*10) # 获取最终结果 response = await handler print(f"结果: {response}") while True: await asyncio.sleep(1) if __name__ == "__main__": import asyncio asyncio.run(main())示例首先定义了add_money工具函数,代表这是一个危险动作,需要人介入确认,工具中调用上下文Context的wait_for_event方法设置一个等待的事件HumanResponseEvent,并会发起一个InputRequiredEvent事件;而后定义FunctionAgent指定了工具、模型和系统提示词;最后调用异步迭代器获取stream_events的事件,获取InputRequiredEvent事件后,引导人类输入并将输入结果包装在HumanResponseEvent中发送。
注意,HumanResponseEvent会重新唤起tool(这里是add_money函数)的重新调用,即从头开始执行,示例输出如下:
starting add_money task 需要人工介入: Are you sure you want to proceed? (yes/no): yes 人工输入:yes starting add_money task response:{'response': 'yes', 'user_name': 'User'} add_money response yes ---------- 结果: The add_money task has been completed successfully. The operation to add 100 RMB has been processed.踩一个坑
HITL示例执行一直非预期,输入yes后,Agent直接回复并结束,测试时使用的LlamaIndex是0.14.4版本,最后升级到0.14.10解决
大致原因:
wait_for_event 当前版本不再「阻塞等待」,而是通过抛出一个 “等待事件”专用异常来暂停当前步骤;但由于Agent内部工具执行有代码误用了except Exception,把这个异常吞掉了,导致工作流无法在等待人类输入后重回等待点。
相关issue:
https://github.com/run-llama/llama_index/pull/20173
相关代码:
https://github.com/run-llama/llama_index/pull/20173/commits/e1855c6d47b37c7cef40de9b9342d37924eee48d
查看版本:pip index versions llama-index-core
安装最新版:pip install --upgrade llama-index-core
多智能体
LlamaIndex框架定义了三种使用多Agent的模式。
1、使用内置的AgentWorkflow
AgentWorkflow继承至Workflow,通过指定多个Agent和一个root Agent,实现多智能体协作和自动交接,可以快速上手体验。
agent_workflow = AgentWorkflow( agents=[add_agent, multiply_agent], root_agent=add_agent.name, verbose=True, ) result = await agent_workflow.run("what is 2 multiply 3")2、使用工作流编排
工作流编排模式,定义常规的Workflow,然后通过将子Agent当作工具进行调用,借助Workflow执行路径更加稳定。
3、自定义执行计划
自定义方式最为灵活,可以自定义模型调用、解析响应、业务判断、执行路径等。
篇幅原因,自定义模式示例参考官方文档:
https://developers.llamaindex.ai/python/framework/understanding/agent/multi_agent/
选型建议:
从AgentWorkflow 起步 -> 需求变复杂时切换到 Orchestrator -> 真正落地生产时选 Custom Planner。
结构化输出
在一些基于LLM的应用中,需要LLM输出结构化数据便于继续处理后续业务,对于结构化输出,一些支持FunctionCall的LLM输出会更加稳定,而不支持的LLM就要靠提示词输出响应并解析文本响应内容。
在LlamaIndex的Agent调用流程中,支持两种结构化输出方式,不管哪一种,Agent的执行流程最后会经过llama_index.core.agent.workflow.base_agent.BaseWorkflowAgent.parse_agent_output处理Agent的结果。此处会判断Agent是否设置了结构化输出,如果有则会进一步处理,示例如下:
# 结构化输出 # 1、方式一,使用output_cls,指定输出模型(Pydantic BaseModel),输出为结构化数据 # 2、方式二,使用structured_output_fn自定义解析函数,输入是一些列的ChatMessage,输出为结构化数据 from llama_index.core.agent import FunctionAgent from llama_index.llms.deepseek import DeepSeek from pydantic import BaseModel, Field import json from llama_index.core.llms import ChatMessage from typing import List, Dict, Any def multiply(a: float, b: float) -> float: """Multiply two numbers and returns the product""" print("execute multiply") return a * b ## define structured output format and tools class MathResult(BaseModel): operation: str = Field(description="the performed operation") result: int = Field(description="the result of the operation") # 自定义格式化函数 async def structured_output_parsing( messages: List[ChatMessage], ) -> Dict[str, Any]: # 内部:StructuredLLM # 重要:内部实际上是再调用一次模型,生产结构化数据: # llama_index.llms.openai.base.OpenAI._stream_chat -》 openai.resources.chat.completions.completions.AsyncCompletions.create struct_llm = llm.as_structured_llm(MathResult) messages.append( ChatMessage( role="user", content="Given the previous message history, structure the output based on the provided format.", ) ) response = await struct_llm.achat(messages) return json.loads(response.message.content) llm = DeepSeek(model="deepseek-chat", api_key="sk-...") workflow = FunctionAgent( tools=[multiply], llm=llm, system_prompt="You are an agent that can perform basic mathematical operations using tools.", output_cls=MathResult, # 第一种实现方式 # structured_output_fn=structured_output_parsing, # 第二种实现方式 verbose=True, ) # 执行 async def main(): result = await workflow.run("what is 2 multi 3") # print(type(result)) # <class 'llama_index.core.agent.workflow.workflow_events.AgentOutput'> print(result) # 获取结构化结果 print(result.structured_response) # {'operation': '2 * 3', 'result': 6} print(result.get_pydantic_model(MathResult)) # operation='2 * 3' result=6 if __name__ == "__main__": import asyncio asyncio.run(main())示例代码中定义了两种方式:
方式一使用output_cls,指定输出模型(Pydantic BaseModel),输出为结构化数据。
方式二使用structured_output_fn自定义解析函数,自定义函数输入是一些列的ChatMessage,输出为结构化数据。
方式一、使用output_cls:
内部会结合历史对话自动生成提示词,多调用一次LLM输出结构化数据,LlamaIndex内部实现如下:
# LlamaInde定义如下 # llama_index.core.agent.utils.generate_structured_response async def generate_structured_response( messages: List[ChatMessage], llm: LLM, output_cls: Type[BaseModel] ) -> Dict[str, Any]: xml_message = messages_to_xml_format(messages) structured_response = await llm.as_structured_llm( output_cls, ).achat(messages=xml_message) return cast(Dict[str, Any], json.loads(structured_response.message.content))最终发起LLM调用时,判断是否支持FunctionCall:
llama_index.core.llms.function_calling.FunctionCallingLLM.apredict_and_call,debug如下所示:
方式二、使用structured_output_fn
调用的自定义函数,函数内采用的仍然是通过自定义提示词,调用LLM生成结构化数据,也可以是其它方式,自定义解析函数如下:
async def structured_output_parsing( messages: List[ChatMessage], ) -> Dict[str, Any]: struct_llm = llm.as_structured_llm(MathResult) messages.append( ChatMessage( role="user", content="Given the previous message history, structure the output based on the provided format.", ) ) response = await struct_llm.achat(messages) return json.loads(response.message.content)按示例的方式,最终还是会到方式一相同的LLM处理逻辑完成结构化输出:
llama_index.core.llms.function_calling.FunctionCallingLLM.apredict_and_call。
04 总结
总体而言,LlamaIndex 通过事件驱动的 Workflow 与 Agent 架构,成功打通了数据获取、知识处理、模型交互之间的完整链路。其内置的 Agent 体系不仅支持工具调用、多 Agent 协同、HITL(人类参与)等复杂能力,结合事件驱动也提供了足够的开放性与灵活性。
与此同时,LlamaIndex丰富的数据连接器生态与LlamaHub的便捷检索与集成方式,让开发者无需“重复造轮子”,即可快速构建业务级 RAG 与智能体应用。
想入门 AI 大模型却找不到清晰方向?备考大厂 AI 岗还在四处搜集零散资料?别再浪费时间啦!2025 年AI 大模型全套学习资料已整理完毕,从学习路线到面试真题,从工具教程到行业报告,一站式覆盖你的所有需求,现在全部免费分享!
👇👇扫码免费领取全部内容👇👇
一、学习必备:100+本大模型电子书+26 份行业报告 + 600+ 套技术PPT,帮你看透 AI 趋势
想了解大模型的行业动态、商业落地案例?大模型电子书?这份资料帮你站在 “行业高度” 学 AI:
1. 100+本大模型方向电子书
2. 26 份行业研究报告:覆盖多领域实践与趋势
报告包含阿里、DeepSeek 等权威机构发布的核心内容,涵盖:
- 职业趋势:《AI + 职业趋势报告》《中国 AI 人才粮仓模型解析》;
- 商业落地:《生成式 AI 商业落地白皮书》《AI Agent 应用落地技术白皮书》;
- 领域细分:《AGI 在金融领域的应用报告》《AI GC 实践案例集》;
- 行业监测:《2024 年中国大模型季度监测报告》《2025 年中国技术市场发展趋势》。
3. 600+套技术大会 PPT:听行业大咖讲实战
PPT 整理自 2024-2025 年热门技术大会,包含百度、腾讯、字节等企业的一线实践:
- 安全方向:《端侧大模型的安全建设》《大模型驱动安全升级(腾讯代码安全实践)》;
- 产品与创新:《大模型产品如何创新与创收》《AI 时代的新范式:构建 AI 产品》;
- 多模态与 Agent:《Step-Video 开源模型(视频生成进展)》《Agentic RAG 的现在与未来》;
- 工程落地:《从原型到生产:AgentOps 加速字节 AI 应用落地》《智能代码助手 CodeFuse 的架构设计》。
二、求职必看:大厂 AI 岗面试 “弹药库”,300 + 真题 + 107 道面经直接抱走
想冲字节、腾讯、阿里、蔚来等大厂 AI 岗?这份面试资料帮你提前 “押题”,拒绝临场慌!
1. 107 道大厂面经:覆盖 Prompt、RAG、大模型应用工程师等热门岗位
面经整理自 2021-2025 年真实面试场景,包含 TPlink、字节、腾讯、蔚来、虾皮、中兴、科大讯飞、京东等企业的高频考题,每道题都附带思路解析:
2. 102 道 AI 大模型真题:直击大模型核心考点
针对大模型专属考题,从概念到实践全面覆盖,帮你理清底层逻辑:
3. 97 道 LLMs 真题:聚焦大型语言模型高频问题
专门拆解 LLMs 的核心痛点与解决方案,比如让很多人头疼的 “复读机问题”:
![]()
三、路线必明: AI 大模型学习路线图,1 张图理清核心内容
刚接触 AI 大模型,不知道该从哪学起?这份「AI大模型 学习路线图」直接帮你划重点,不用再盲目摸索!
路线图涵盖 5 大核心板块,从基础到进阶层层递进:一步步带你从入门到进阶,从理论到实战。
L1阶段:启航篇丨极速破界AI新时代
L1阶段:了解大模型的基础知识,以及大模型在各个行业的应用和分析,学习理解大模型的核心原理、关键技术以及大模型应用场景。
L2阶段:攻坚篇丨RAG开发实战工坊
L2阶段:AI大模型RAG应用开发工程,主要学习RAG检索增强生成:包括Naive RAG、Advanced-RAG以及RAG性能评估,还有GraphRAG在内的多个RAG热门项目的分析。
L3阶段:跃迁篇丨Agent智能体架构设计
L3阶段:大模型Agent应用架构进阶实现,主要学习LangChain、 LIamaIndex框架,也会学习到AutoGPT、 MetaGPT等多Agent系统,打造Agent智能体。
L4阶段:精进篇丨模型微调与私有化部署
L4阶段:大模型的微调和私有化部署,更加深入的探讨Transformer架构,学习大模型的微调技术,利用DeepSpeed、Lamam Factory等工具快速进行模型微调,并通过Ollama、vLLM等推理部署框架,实现模型的快速部署。
L5阶段:专题集丨特训篇 【录播课】
![]()
四、资料领取:全套内容免费抱走,学 AI 不用再找第二份
不管你是 0 基础想入门 AI 大模型,还是有基础想冲刺大厂、了解行业趋势,这份资料都能满足你!
现在只需按照提示操作,就能免费领取:
👇👇扫码免费领取全部内容👇👇
2025 年想抓住 AI 大模型的风口?别犹豫,这份免费资料就是你的 “起跑线”!