免费网站建设凡科西安网站开发高端网站开发
2026/4/1 6:37:24 网站建设 项目流程
免费网站建设凡科,西安网站开发高端网站开发,深圳哪里有做网站的,企业网站重要吗LlamaIndex采用事件驱动工作流架构#xff0c;通过定义事件连接处理节点。文章介绍了四种内置事件及使用方法#xff0c;展示如何用step装饰器创建工作流步骤#xff0c;并通过Context实现流式输出、状态存储和事件协调。工作流可作为服务运行#xff0c;提供可视化调试页面…LlamaIndex采用事件驱动工作流架构通过定义事件连接处理节点。文章介绍了四种内置事件及使用方法展示如何用step装饰器创建工作流步骤并通过Context实现流式输出、状态存储和事件协调。工作流可作为服务运行提供可视化调试页面和API接口使复杂任务处理变得直观高效适合大模型应用开发。01 前言LlamaIndex的Workflow工作流采用了一种巧妙的事件驱动设计只需通过简单的事件定义就能实现节点之间的连接当一个节点输出特定事件时系统会自动触发下一个以该事件为输入的节点。结合内置定义的开始与结束节点整个流程被优雅地串联与编排起来让复杂任务的处理变得直观而高效。02 相关概念Event事件在LlamaIndex中Workflow模块下events文件中定义了4种基础的内置事件StartEvent、StopEvent、InputRequiredEvent、HumanResponseEvent。StartEvent开始事件即工作流开始执行时发送的默认事件。StopEvent结束事件即标志着工作流执行结束。该事件有个result属性可以获取工作流执行后的结果。InputRequiredEvent需要人类干预的事件用于实现Human in the loop流程对应的是HumanResponseEvent事件。HumanResponseEvent人类反馈事件完成人类输入或确认后发送该事件。上面的事件均继承至Event类它是LlamaIndex定义的基类事件扩展自定义事件可以继承Event或者已有的其它事件。Workflow工作流以事件驱动的Workflow工作流每个工作流节点都有一个或多个输入和输出事件由此构成了完整工作流。这种方式更加灵活和简单同时可以借助上下文Context控制工作流中的事件实现如跳转工作流到某步骤的效果。03 Workflow实战基础用法# pip install llama-index-core llama-index from llama_index.core.workflow import( StartEvent, StopEvent, Workflow, step, Event, ) # 自定义事件 class FirstEvent(Event): first_output: str class SecondEvent(Event): second_output: str step_one takes a StartEvent and returns a FirstEvent step_two takes a FirstEvent and returns a SecondEvent step_three takes a SecondEvent and returns a StopEvent class MyWorkflow(Workflow): step async def step_one(self, ev: StartEvent) - FirstEvent: # do something here print(ev.first_input_plus) returnFirstEvent(first_outputFirst step complete.) step async def step_two(self, ev: FirstEvent) - SecondEvent: # do something here print(ev.first_output) returnSecondEvent(second_outputSecond step complete.) step async def step_three(self, ev: SecondEvent) - StopEvent: # do something here print(ev.second_output) returnStopEvent(resultWorkflow complete.) # 执行 async def main(): w MyWorkflow(timeout10, verboseFalse) result await w.run(first_input_plusHello World!) print(result) if __name__ __main__: import asyncio asyncio.run(main())示例首先定义了两个自定义事件FirstEvent和SecondEvent以及它们的属性均继承至Event类然后定义MyWorkflow类继承至Workflow并定义多个step标记的异步“函数”这些函数都有一个事件作为输入一个事件作为输出的“节点”最终构成工作流step_one - step_two - step_three。然后实例化MyWorkflow并调用run方法得到StopEvent事件定义的result内容。执行后输出如下Hello World! First step complete. Second step complete. Workflow complete.关于step1、它是一个将普通Python函数标记为工作流中的一个步骤的装饰器。2、它有三个属性workflow指定独立函数所属工作流在工作流类中定义时忽略num_workers指定步骤的并行线程数retry_policy指定重试策略。3、修饰的函数的入参除了Event子类外还有可选的上下文Context可选择带有类型化的状态模型和 通过 typing.Annotated 进行的任何资源注入。流式输出对于耗时较久的任务可以边执行边输出提升用户体验。两种常见场景一个是与模型的对话一个是耗时任务如文件下载等。在上面示例的基础上改造首先多导入Contextfrom llama_index.core.workflow import( Context, )定义一个表示进度的事件class ProgressEvent(Event): msg: str重新定义工作流类 step_one -》 step_two -》 step_three class MyWorkflow(Workflow): step async def step_one(self, ctx: Context, ev: StartEvent) - FirstEvent: print(step_one run) ctx.write_event_to_stream(ProgressEvent(msgStep one is happening)) returnFirstEvent(first_outputFirst step complete.) step async def step_two(self, ctx: Context, ev: FirstEvent) - SecondEvent: print(step_two run) llm DeepSeek(modeldeepseek-chat, api_keysk-...) generator await llm.astream_complete( Please give me the first 3 paragraphs of Moby Dick, a book in the public domain. ) async for response in generator: # Allow the workflow to stream this piece of response ctx.write_event_to_stream(ProgressEvent(msgresponse.delta)) return SecondEvent( second_outputSecond step complete, full response attached ) step async def step_three(self, ctx: Context, ev: SecondEvent) - StopEvent: print(step_three run) ctx.write_event_to_stream(ProgressEvent(msgStep three is happening)) return StopEvent(resultWorkflow complete.) # 执行 async def main(): w MyWorkflow(timeout30, verboseTrue) handler w.run(first_inputHello World!) async for ev in handler.stream_events(): if isinstance(ev, ProgressEvent): print(ev.msg, end, flushTrue) final_result await handler print(\n Final result, final_result) if __name__ __main__: import asyncio asyncio.run(main())示例中工作流的节点函数入参均增加了ctx: Context通过Context的write_event_to_stream方法以stream的方式发送自定义的事件ProgressEvent。通过不加await的方式调用工作流的run方法进而获得工作流执行过程中的stream_events。执行后输出如下中间Of开始到Circumambulate开始的行由模型输出step_one run Step one is happeningstep_two run Of course. Here are the first three paragraphs of Herman Melvilles *Moby-Dick*. *** **Call me Ishmael.** Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me. There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs—commerce surrounds it with her surf. Right and left, the streets take you waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of water-gazers there. Circumambulate the city of a dreamy Sabbath afternoon. Go from Corlears Hook to Coenties Slip, and from thence, by Whitehall, northward. What do you see?—Posted like silent sentinels all around the town, stand thousands upon thousands of mortal men fixed in ocean reveries. Some leaning against the spiles; some seated upon the pier-heads; some looking over the bulwarks of ships from China; some high aloft in the rigging, as if striving to get a still better seaward peep. But these are all landsmen; of week days pent up in lath and plaster—tied to counters, nailed to benches, clinched to desks. How then is this? Are the green fields gone? What do they here?step_three run Step three is happening Final result Workflow complete.上下文ContextContext就像工作流中的大脑在步骤之间协调事件传递跟踪进行中的工作暴露全局状态存储并提供流式和同步工具。它由 Workflow 在运行时创建并且可以持久化和恢复。1、流式传出事件见上面write_event_to_stream使用。2、全局状态数据存储# 设置数据 await ctx.store.set(some_database, [1, 2, 3]) # 避免多agent场景的修改冲突可以使用原子操作使用with async with ctx.store.edit_state() as edit_state: edit_state[some_database] [1, 2, 3] # 读取 db await ctx.store.get(some_database, defaultNone)3、协调事件下面示例用于并行执行场景# 并行触发流程 step async def step_one(self, ctx: Context, ev: StartEvent) - StepTwoEvent: print(step_one run) ctx.send_event(StepTwoEvent(queryquery 1)) ctx.send_event(StepTwoEvent(queryquery 2)) ctx.send_event(StepTwoEvent(queryquery 3)) # 等待所有事件到达 res ctx.collect_events(ev, [StepThreeEvent] * 3) if res is None: return None4、持久化并进行恢复上下文# 存储序列化后的ctx_dict ctx_dict ctx.to_dict(serializerJsonSerializer()) my_db.set(key, json.dumps(ctx_dict)) # 重新获取上下文并加入工作流 ctx_dict my_db.get(key) restored_ctx Context.from_dict(my_workflow, json.loads(ctx_dict), serializerJsonSerializer()) result await my_workflow.run(..., ctxrestored_ctx)这样借助step和Context可以实现常见工作流类型包括条件判断、并行执行、多次循环执行、模型调用等任意任务。运行工作流作为服务LlamaIndex可以将工作流作为服务运行服务同时提供了可视化调用页面和API接口方便进行Debug。from workflows import Workflow, step from workflows.context import Context from workflows.events import Event, StartEvent, StopEvent from workflows.server import WorkflowServer import asyncio class StreamEvent(Event): sequence: int # Define a simple workflow class GreetingWorkflow(Workflow): step async def greet(self, ctx: Context, ev: StartEvent) - StopEvent: for i in range(3): ctx.write_event_to_stream(StreamEvent(sequencei)) await asyncio.sleep(0.3) name getattr(ev, name, World) return StopEvent(resultfHello, {name}!) greet_wf GreetingWorkflow() # Create a server instance server WorkflowServer() # Add the workflow to the server server.add_workflow(greet, greet_wf) async def main(): await server.start() # 异步启动方式访问http://127.0.0.1:8000/ if __name__ __main__: import uvicorn uvicorn.run(server.app, host127.0.0.1, port8000)示例启动后访问http://127.0.0.1:8000/即可打开可视化调试页面页面左侧是历史对话和输入中间是工作流的可视化右侧是工作流输出如图输入{“name”:“张三”}并运行参数会传入StartEvent事件属性中通过获取name属性值可得到“张三”工作流通过Context发送三次StreamEvent事件并设置sequence属性最终在有右侧展示出结果。04 总结LlamaIndex 采用事件驱动模型来构建工作流通过简洁的方式将各个处理步骤连接起来。每个步骤都是一个独立的 Python 函数专注于实现特定任务最终组合成完整工作流。配合全局上下文Context类对工作流生命周期进行统一管理进一步增强了流程的灵活性与可控性。关于Human in the loop放在下篇Agent中介绍。LamaIndex中的Agent实现也是基于Workflow的事件驱动的。​最后我在一线科技企业深耕十二载见证过太多因技术卡位而跃迁的案例。那些率先拥抱 AI 的同事早已在效率与薪资上形成代际优势我意识到有很多经验和知识值得分享给大家也可以通过我们的能力和经验解答大家在大模型的学习中的很多困惑。我整理出这套 AI 大模型突围资料包✅AI大模型学习路线图✅Agent行业报告✅100集大模型视频教程✅大模型书籍PDF✅DeepSeek教程✅AI产品经理入门资料完整的大模型学习和面试资料已经上传带到CSDN的官方了有需要的朋友可以扫描下方二维码免费领取【保证100%免费】​​为什么说现在普通人就业/升职加薪的首选是AI大模型人工智能技术的爆发式增长正以不可逆转之势重塑就业市场版图。从DeepSeek等国产大模型引发的科技圈热议到全国两会关于AI产业发展的政策聚焦再到招聘会上排起的长队AI的热度已从技术领域渗透到就业市场的每一个角落。智联招聘的最新数据给出了最直观的印证2025年2月AI领域求职人数同比增幅突破200%远超其他行业平均水平整个人工智能行业的求职增速达到33.4%位居各行业榜首其中人工智能工程师岗位的求职热度更是飙升69.6%。AI产业的快速扩张也让人才供需矛盾愈发突出。麦肯锡报告明确预测到2030年中国AI专业人才需求将达600万人人才缺口可能高达400万人这一缺口不仅存在于核心技术领域更蔓延至产业应用的各个环节。​​资料包有什么①从入门到精通的全套视频教程⑤⑥包含提示词工程、RAG、Agent等技术点② AI大模型学习路线图还有视频解说全过程AI大模型学习路线③学习电子书籍和技术文档市面上的大模型书籍确实太多了这些是我精选出来的④各大厂大模型面试题目详解⑤ 这些资料真的有用吗?这份资料由我和鲁为民博士共同整理鲁为民博士先后获得了北京清华大学学士和美国加州理工学院博士学位在包括IEEE Transactions等学术期刊和诸多国际会议上发表了超过50篇学术论文、取得了多项美国和中国发明专利同时还斩获了吴文俊人工智能科学技术奖。目前我正在和鲁博士共同进行人工智能的研究。所有的视频教程由智泊AI老师录制且资料与智泊AI共享相互补充。这份学习大礼包应该算是现在最全面的大模型学习资料了。资料内容涵盖了从入门到进阶的各类视频教程和实战项目无论你是小白还是有些技术基础的这份资料都绝对能帮助你提升薪资待遇转行大模型岗位。智泊AI始终秉持着“让每个人平等享受到优质教育资源”的育人理念‌通过动态追踪大模型开发、数据标注伦理等前沿技术趋势‌构建起前沿课程智能实训精准就业的高效培养体系。课堂上不光教理论还带着学员做了十多个真实项目。学员要亲自上手搞数据清洗、模型调优这些硬核操作把课本知识变成真本事‌​​​​如果说你是以下人群中的其中一类都可以来智泊AI学习人工智能找到高薪工作一次小小的“投资”换来的是终身受益应届毕业生‌无工作经验但想要系统学习AI大模型技术期待通过实战项目掌握核心技术。零基础转型‌非技术背景但关注AI应用场景计划通过低代码工具实现“AI行业”跨界‌。业务赋能 ‌突破瓶颈传统开发者Java/前端等学习Transformer架构与LangChain框架向AI全栈工程师转型‌。获取方式有需要的小伙伴可以保存图片到wx扫描二v码免费领取【保证100%免费】**​

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询