ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Conductor Python 集成 LangGraph:将 React Agent 编译为持久化工作流执行

Conductor Python 集成 LangGraph:将 React Agent 编译为持久化工作流执行 Conductor Python 集成 LangGraph将 React Agent 编译为持久化工作流执行【免费下载链接】conductorConductor is an event driven agentic workflow engine providing durable and highly resilient execution engine for applications and AI Agents项目地址: https://gitcode.com/GitHub_Trending/co/conductorLangGraph 是构建有状态、图结构 LLM Agent 的主流框架而 Conductor 是一个事件驱动的 Agentic 工作流引擎。本指南基于仓库内的官方速查文档 ui-next/src/pages/agent/guides/python/langgraph.md讲解如何用conductor-python[langgraph]扩展包把已有的 LangGraph 图交给 Conductor 的持久化运行时执行读完本文你将掌握安装与连接配置、用AgentRuntime运行create_react_agent构建的图、理解 LangGraph 被编译为工作流的底层机制并能把部署后的 Agent 通过AGENT任务嵌入任意工作流。1. 安装与连接配置LangGraph 属于 Conductor 的框架 Agent路线你保留 LangGraph 自己定义的 agent 对象Conductor SDK 负责把它编译并执行为一次可观测的持久化执行。第一步是安装带 LangGraph 支持的 Python SDKpython -m pip install conductor-python[langgraph]安装完成后需要让运行时能够连接 Conductor 服务器并访问模型提供方。参考仓库文档 docs/quickstart/connect.md 中的连接步骤设置以下环境变量export CONDUCTOR_SERVER_URL{{CONDUCTOR_SERVER_URL}} # For authenticated Conductor servers: # export CONDUCTOR_AUTH_KEYYOUR_AUTH_KEY # export CONDUCTOR_AUTH_SECRETYOUR_AUTH_SECRET export CONDUCTOR_AGENT_LLM_MODELopenai/gpt-4o-mini各变量作用如下CONDUCTOR_SERVER_URLConductor 服务器 API 地址。本地开发服务器一般为http://localhost:8080/api可参考 docs/quickstart/connect.md 的本地服务器与 Docker 启动方式也可以指向云端 Developer Edition 的/api端点。CONDUCTOR_AUTH_KEY/CONDUCTOR_AUTH_SECRET访问受认证保护的 Conductor 服务器时使用如 Developer Edition 的应用访问密钥本地未开启认证的服务器可省略。CONDUCTOR_AGENT_LLM_MODELAgent 运行时默认使用的模型标识这里指定为openai/gpt-4o-mini。在启动服务器或 Agent 工作进程前还需要让服务器能调用你的模型提供方本地服务器需在启动前导出提供方 API Key如export OPENAI_API_KEYyour-openai-api-key详见 docs/devguide/ai/llm-orchestration.md。同时部署或调用 Conductor Agent 前需要在服务器配置中启用 AI 集成即设置conductor.integrations.ai.enabledtrue见 docs/devguide/ai/conductor-agents.md。2. 运行一个 LangGraph 图安装并配置完成后就可以把 LangGraph 图直接交给 Conductor 运行。仓库速查文档给出了最小可运行示例保存为langgraph_agent.pyfrom conductor.ai.agents import AgentRuntime from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent graph create_react_agent( ChatOpenAI(modelgpt-4o-mini, temperature0), tools[], namelanggraph_assistant, ) with AgentRuntime() as runtime: result runtime.run(graph, What makes a workflow durable?) result.print_result()代码要点create_react_agent是 LangGraph 预构建的 ReAct 风格 Agent这里使用langchain_openai.ChatOpenAI作为底层模型temperature0保证输出确定性更好tools[]表示本示例不挂任何工具name用于标识图。AgentRuntime是 Conductor 的运行时上下文用with语句管理生命周期对应runtime.shutdown()的释放。runtime.run(graph, ...)接收的不是 Conductor 自己的 Agent 对象而是原生的 LangGraph 图——这正是框架 Agent路线的核心authoring 仍然属于 LangGraphConductor 只负责其外围的持久化执行。运行python langgraph_agent.py运行结束后print_result()会打印最终结果与此同时这次执行在 Conductor UI 中生成一条完整的、可检视的执行记录。仓库文档 docs/quickstart/first-agent.md 的Verify and recover一节建议在 UI 中定位本次执行核对终止状态并检查任务时间线、输入与输出如果运行无法触达模型先确认服务器 URL 与工作进程环境中的提供方凭证再检查失败任务。3. 进阶为 LangGraph Agent 挂载工具实际 Agent 几乎都需要调用工具。仓库文档 docs/quickstart/framework-agents.md 的 LangGraph 一节给出了带自定义工具的完整示例——用langchain_core.tools.tool装饰一个数学计算函数把它作为create_react_agent的工具传入import math from conductor.ai.agents import AgentRuntime from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent tool def calculate(expression: str) - str: Evaluate a limited math expression. return str(eval(expression, {__builtins__: {}}, {sqrt: math.sqrt, pi: math.pi})) graph create_react_agent( ChatOpenAI(modelgpt-4o-mini, temperature0), tools[calculate], namemath_agent ) with AgentRuntime() as runtime: result runtime.run(graph, What is sqrt(256) 2**10?) result.print_result()与第 2 节最小示例相比变化只有一个tools[calculate]。运行时同样通过AgentRuntime提交Conductor 会把模型调用工具→拿到结果→继续下一轮的循环完整落为工作流中的任务序列。这个示例刻意使用{__builtins__: {}}限制eval的命名空间只暴露sqrt与pi体现的是对工具代码安全性的基本约束并非建议在生产中直接eval任意表达式。4. 底层原理LangGraph 图如何被编译为工作流框架 Agent 能变成持久化执行并非魔法。在仓库的agentspan模块中LangGraphNormalizer见 agentspan/src/main/java/org/conductoross/conductor/ai/agentspan/runtime/normalizer/LangGraphNormalizer.java负责把 LangGraph 的序列化配置rawConfig规范化为统一的AgentConfig。从源码注释与实现看它支持三条序列化路径完整提取Full extractionrawConfig中带model与带_worker_ref标记的tools规范化结果与 OpenAI 路线一致——生成一个AI_MODEL任务加每个工具一个SIMPLE任务model、temperature、max_tokens等参数被映射到AgentConfig对应字段工具被映射为ToolConfigtoolType: worker。图结构Graph-structurerawConfig带_graph键包含节点nodes与边edges/conditional_edges。每个节点变成一个SIMPLE任务 worker边定义工作流结构其中_llm_node节点会额外注册 prep/finish worker 并由编译器构建 LLM 任务_subgraph_node会递归规范化子图配置并注册子图 worker_human_node则直接映射为 Conductor 的HUMAN系统任务无需 worker。模型名保留用于可观测性。透传PassthroughrawConfig带_worker_name自定义StateGraph整个图在单个SIMPLE任务中运行。也就是说LangGraph 图的节点、边、条件分支会被翻译成 Conductor 的工作流结构模型调用与工具调用分别成为工作流任务。这与 docs/devguide/concepts/agents.md 中Agents are workflows underneath的说明一致一次 Agent 运行就是一次工作流执行每一轮对话都被持久化崩溃或重启可从最后一个已完成步骤继续重试与超时遵循同样的策略且每次运行都留下完整可回放的历史。对应测试用例可参考 agentspan/src/test/java/org/conductoross/conductor/ai/agentspan/runtime/normalizer/LangGraphNormalizerTest.java。5. 生命周期从 run 到 deploy、serve、AGENT 任务docs/devguide/ai/agent-framework-recipes.md 总结了框架 Agent 的四个阶段在 Python SDK 中它们是对同一个AgentRuntime的四个调用from conductor.ai.agents import AgentRuntime from langchain.agents import create_agent agent create_agent(openai:gpt-4o-mini, tools[], system_promptYou are a helpful assistant.) with AgentRuntime() as runtime: runtime.run(agent, Is the token set?) # develop: compile and execute once runtime.plan(agent) # CI: inspect the compiled graph runtime.deploy(agent) # release: register without executing runtime.serve(agent) # operate: run tool workers and blockrun开发期使用编译并立即执行一次第一次运行即可在 UI 看到执行记录plan只检查编译后的工作流图适合开发期与 CI 中部署前的预检deploy把编译后的 Agent 以带版本号的名称注册到服务器此后调用方无需引入 LangGraph 及其依赖即可调用serve阻塞式运行工具 worker 进程用于生产环境持续执行 Agent 的工具。serve()会阻塞因此生产环境应把它放在独立的长生命周期 worker 进程中而deploy()放在 CI/CD 流程里。Agent 部署后父工作流通过AGENT任务按名称调用它见 docs/devguide/ai/conductor-agents.md{ name: run_agent, taskReferenceName: run_agent_ref, type: AGENT, inputParameters: { agentType: conductor, name: deployed-agent-name, prompt: ${workflow.input.prompt}, pollIntervalSeconds: 5 } }需要注意agentType选择的是执行模式而非 authoring 框架——agentType: conductor表示运行按name选中的已部署 Conductor Agent而 LangGraph 只是 SDK 侧的编写路径不是agentType的取值。AGENT任务会写入executionId、agentName、state、text及完成后的结构化outputstate采用归一化的 A2A 生命周期值working、input-required、completed、failed、canceled。当 Agent 等待外部输入时任务状态映射为COMPLETED并携带waiting: true工作流可借助HUMAN任务收集答复后用executionIdprompt再次发起AGENT任务恢复同一运行参见 docs/devguide/ai/conductor-agents.md 的 resume 示例。仓库 ai/examples 目录下的31-conductor-agent-basic.json至34-conductor-agent-cancel.json分别演示了已部署 Agent 作为工作流一步、WAITING→HUMAN→按executionId恢复、并行多 Agent 以及取消传播等契约。6. 验证与排错要点参考 docs/quickstart/framework-agents.md 的Verify and recover一节无论哪种框架核对打印的最终结果并在 Conductor UI 中找到对应的执行记录失败时依次排查运行时服务器 URL、框架包是否正确安装、模型提供方凭证是否可用定位到失败任务后再重试在未明确某个 Agent 动作的幂等性与恢复策略之前不要重试可能已产生外部副作用side effect的动作——这正是把 Agent 交给持久化执行引擎的收益之一每一步有记录、可恢复恢复边界清晰可查。7. 继续深入完整的框架 Agent 速查OpenAI Agents、LangChain、LangGraph、Google ADKdocs/quickstart/framework-agents.md框架 Agent 参考页支持矩阵与维护示例docs/devguide/ai/agent-framework-recipes.md已部署 Agent 的运行时行为调用、等待、恢复、取消、输出契约docs/devguide/ai/conductor-agents.mdAgent 概念编译为工作流图、AGENT任务、三种编写方式docs/devguide/concepts/agents.mdLangGraph 规范化源码实现agentspan/src/main/java/org/conductoross/conductor/ai/agentspan/runtime/normalizer/LangGraphNormalizer.java【免费下载链接】conductorConductor is an event driven agentic workflow engine providing durable and highly resilient execution engine for applications and AI Agents项目地址: https://gitcode.com/GitHub_Trending/co/conductor创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表