ARTICLE DETAIL

资讯详情

深耕商务建站与企业官网运营的一线实战洞察。

ZenML 集成 AWS Strands Agent 实战:从数学计算到实时 HTTP 部署

ZenML 集成 AWS Strands Agent 实战:从数学计算到实时 HTTP 部署 ZenML 集成 AWS Strands Agent 实战从数学计算到实时 HTTP 部署【免费下载链接】zenmlZenML : One AI Platform from Pipelines to Agents. https://zenml.io.项目地址: https://gitcode.com/GitHub_Trending/ze/zenmlAWS Strands 是 Amazon 推出的轻量级 Agent 框架以简洁的tool装饰器、可直接调用的 Agent 接口著称。本指南基于 ZenML 官方仓库中的 aws_strands 示例完整演示如何将 Strands Agent 封装进 ZenML 流水线实现数学计算与天气查询等任务并借助 ZenML 的 Pipeline Deployment 功能将其一键部署为实时 HTTP 服务。读完本文你将掌握 Strands 工具定义、ZenML 步骤封装、Docker 环境注入 API Key以及zenml pipeline deploy/zenml deployment invoke的完整部署与调用链路。一、示例概览AWS Strands 与 ZenML 的集成价值在 ZenML 的 Agent 框架集成示例集中AWS Strands 被定位为「Simple」类型的框架——它的核心卖点是直接可调用的 Agent 接口与内建工具。正如 examples/agent_framework_integrations/README.md 中的框架对比表所描述Strands 在 ZenML 生态中代表的是最简集成路径无需多 Agent 协商、无需图式工作流一个agent(query)调用即可完成推理。整个示例只有三个核心文件职责划分非常清晰文件职责agent.py定义 Strands Agent、工具与模型配置run.py将 Agent 封装进 ZenML 流水线配置 Docker 环境requirements.txt声明运行时依赖strands-agents、zenml 等这种「框架代码与编排代码分离」的结构正是 ZenML 集成任何第三方 Agent 框架的通用模式框架负责智能ZenML 负责编排、追踪与部署。二、环境准备从零开始运行 Strands 流水线2.1 依赖安装示例要求 Python 3.11 与 uv 包管理器。在 requirements.txt 中声明了以下核心依赖strands-agents[openai]1.0.0 strands-agents-tools0.0.1 zenml[local] jinja23.1.0 secure0.3.0安装步骤与 ZenML 官方推荐的 Agent 框架快速启动流程一致参见 examples/agent_framework_integrations/README.mdexport OPENAI_API_KEYyour-api-key-here uv venv --python 3.11 source .venv/bin/activate uv pip install -r requirements.txt说明strands-agents[openai]使用 OpenAI 兼容接口因此需要提前申请 OpenAI API Key 并通过环境变量注入密钥不应硬编码在代码中详见下文模型配置部分。2.2 初始化 ZenML 并登录zenml init zenml loginzenml init在当前目录初始化 ZenML 仓库建立本地存储与配置。zenml login连接到 ZenML 服务端本地或远程之后所有流水线运行记录、产物artifacts都会写入服务端可在 ZenML Dashboard 中查看。2.3 运行流水线python run.py从 run.py 的入口逻辑可以看到脚本执行后会打印运行提示并调用agent_pipeline()流水线完成后会提示在 ZenML Dashboard 中查看详细结果与产物。三、Agent 定义Strands 的工具与模型配置agent.py 是整个示例的智能核心它演示了 Strands 框架的两个关键概念tool装饰器与可调用的 Agent 对象。3.1 使用tool定义工具from strands import Agent, tool tool def get_weather(city: str) - str: Very naive weather lookup that *always* returns a sunny forecast. Args: city: Name of the city. Returns: A short weather blurb. return fIt’s always sunny in {city}!这是一个「朴素」的天气查询工具——为了演示目的它总是返回晴天预报。关键在于它展示了 Strands 的工具定义模式任意 Python 函数加上tool装饰器即可成为 Agent 可调用的函数参数与返回值类型注解会被框架自动解析并注入到 LLM 的 function calling 流程中。3.2 配置 OpenAI 模型import os from strands.models.openai import OpenAIModel model OpenAIModel( client_args{ api_key: os.getenv(OPENAI_API_KEY, ), }, model_idgpt-5-nano, params{temperature: 0.7}, )两点值得注意密钥从环境变量读取os.getenv(OPENAI_API_KEY, )避免了在源码中硬编码凭据这与 ZenML 的安全实践一致——敏感信息应当通过环境变量或 ZenML Secret 管理本示例采用环境变量方式见下文 Docker 配置中的environment注入。模型参数可调temperature: 0.7控制生成随机性model_id可替换为其他 OpenAI 兼容模型。3.3 组装全局 Agentagent: Agent Agent( modelmodel, tools[get_weather], system_promptYou are a helpful assistant that can check weather for cities., )示例以模块级全局变量的形式导出agent对象并附带一条关键注释agent.pyPanAgent discovers theagentvariable at runtime; donotexecute the agent from within this module.这意味着不要在 agent.py 模块内部直接执行agent()否则框架在运行时动态发现agent变量时会与已执行的调用冲突。正确的做法是像 run.py 那样从外部导入agent后再调用。四、流水线封装把 Agent 编排成 ZenML Pipelinerun.py 演示了将 Strands Agent 封装进 ZenML 的标准三步模式步骤step执行 Agent → 步骤格式化结果 → 流水线pipeline串联。4.1 配置 Docker 构建环境from zenml import pipeline, step from zenml.config import DockerSettings, PythonPackageInstaller docker_settings DockerSettings( python_package_installerPythonPackageInstaller.UV, requirementsrequirements.txt, # relative to the pipeline directory environment{ OPENAI_API_KEY: os.getenv(OPENAI_API_KEY), }, )这里的配置非常关键python_package_installerPythonPackageInstaller.UV指定使用 uv 作为容器内 Python 包安装器与本地环境的 uv 工作流保持一致可显著加快依赖安装速度。requirementsrequirements.txt声明容器构建时安装的依赖文件路径相对于流水线目录确保容器内strands-agents、zenml等包可用。environment{OPENAI_API_KEY: ...}将宿主环境变量注入容器运行时使 Agent 在容器内也能读取到 API Key避免密钥硬编码。4.2 定义步骤第一步执行 Agent 并捕获异常step def run_strands_agent(query: str) - Annotated[str, agent_response]: Execute the Strands agent and return the response. try: response agent(query) return str(response) except Exception as e: return fAgent error: {str(e)}输出使用Annotated[str, agent_response]命名产物ZenML 会将该输出注册为名为agent_response的 artifact供 Dashboard 追踪与后续步骤消费。通过 try/except 兜底Agent 失败时返回错误字符串而非中断流水线——这正是示例集中强调的「优雅降级」错误处理模式见 examples/agent_framework_integrations/README.md。第二步格式化响应step def format_weather_response( response: str, ) - Annotated[str, formatted_response]: Format the agent response into a readable summary. formatted f️ WEATHER REPORT { * 30} {response} Powered by AWS Strands Agent return formatted.strip()此步骤将 Agent 的原始响应包装成带标题的分隔格式输出产物命名为formatted_response。4.3 定义流水线pipeline(settings{docker: docker_settings}, enable_cacheFalse) def agent_pipeline(query: str Whats the weather like in Tokyo?) - str: ZenML pipeline that orchestrates the AWS Strands weather agent. Returns: Formatted weather response # Run the agent response run_strands_agent(queryquery) # Format the results summary format_weather_response(response) return summary两个值得深挖的配置settings{docker: docker_settings}将前面定义的 Docker 构建配置挂载到流水线实际运行时每个步骤都会基于该配置构建隔离容器环境。enable_cacheFalse禁用步骤缓存。Agent 调用具有非确定性LLM 输出每次都可能不同关闭缓存可确保每次运行都真实执行推理而非复用旧产物。默认查询参数为「Tokyo 的天气」。流水线返回str意味着整条流水线的最终产物可直接作为函数返回值传递这也是后续zenml pipeline deploy将其暴露为可调用 HTTP 接口的基础。五、实时部署从批处理流水线到 HTTP 服务ZenML 的 Pipeline Deployment 特性可以将这类「批处理风格」的 Agent 流水线直接转换为持久的实时 HTTP 服务。示例中的部署命令aws_strands/README.md与整套 Agent 集成示例的通用部署模式examples/agent_framework_integrations/README.md完全一致。5.1 部署流水线# Deploy the pipeline as an HTTP service zenml pipeline deploy run.agent_pipeline --name aws-strands-agent该命令对应源码中的 deploy_pipeline 函数其核心参数包括参数作用source位置参数可导入的流水线实例来源此处为run.agent_pipeline--name部署名称如aws-strands-agent--config-path可选流水线配置文件路径--stack-name-or-id可选指定部署目标 stack--timeout等待部署完成的最大秒数--update/--overtake同名部署已存在时的更新策略从源码实现看zenml pipeline deploy会先导入流水线实例再基于当前活跃 stack 构建镜像并启动常驻服务若在非 ZenML 仓库目录下运行CLI 会提示当前目录被作为源码根目录用于解析步骤类详见 pipeline.py 的警告逻辑。5.2 通过 CLI 调用zenml deployment invoke aws-strands-agent --queryCalculate the square root of 144zenml deployment invoke的实现位于 src/zenml/cli/deployment.py它接收部署名称/ID 与任意--keyvalue参数将其解析为流水线输入并转发给底层 invoke_deployment 工具。默认超时 300 秒5 分钟也可用--timeout覆盖使用--no-wait可提交后台执行并立即返回运行 ID。若传入参数不合法CLI 会提示通过zenml deployment describe --schema name查看部署的输入 schema。5.3 通过 HTTP API 调用curl -X POST http://localhost:8000/invoke \ -H Content-Type: application/json \ -d {parameters: {query: What is 15 * 23?}}HTTP 接口是 CLI 调用的等价形式POST /invoke端点接收{parameters: {...}}负载parameters中的字段即流水线的输入参数此处是query。在源码的 pretty_print_deployment 函数 中可以看到部署就绪后 CLI 还会给出配套运维命令zenml deployment describe name查看部署详情包含 Endpoint URL、Swagger 文档地址与鉴权信息--show-secret可揭示密钥zenml deployment logs name -f实时跟踪部署日志zenml deployment invoke name args再次调用zenml deployment deprovision name/zenml deployment delete name下线或彻底删除部署。得益于 Pipeline 的返回值类型str部署服务可以直接把格式化后的天气/计算结果作为响应返回实现真正的「实时 Agent 服务」。六、实战价值与适用场景6.1 从示例中可以迁移的模式框架无关的封装step内直接调用agent(query)、外部统一格式化这一模式可平移到 LangChain、CrewAI、PydanticAI 等任何框架参见 examples/agent_framework_integrations/README.md 的框架特性对比。环境注入Docker 容器内依赖 API Key 的environment注入是远程执行 Agent 流水线时避免密钥硬编码的标准做法。产物命名与追踪Annotated[..., name]让每次 Agent 推理的输入输出都成为可追溯的 artifact配合 ZenML Dashboard 可审计每一次调用。6.2 适用场景批处理夜间批量处理成百上千条查询如批量数学计算、批量城市天气查询实时推理服务将 Agent 作为微服务嵌入 Web 应用或独立部署通过 HTTP 获得即时响应Agent 评估与对比在 ZenML 中统一编排不同框架Autogen、LangGraph、Strands 等在测试数据集上系统化对比效果交互式 Demo一键部署可分享的 Agent 端点。七、快速参考完整命令清单# 1. 环境准备 export OPENAI_API_KEYyour-api-key-here uv venv --python 3.11 source .venv/bin/activate uv pip install -r requirements.txt # 2. 初始化 ZenML zenml init zenml login # 3. 本地运行批处理流水线 python run.py # 4. 部署为实时 HTTP 服务 zenml pipeline deploy run.agent_pipeline --name aws-strands-agent # 5. CLI 调用 zenml deployment invoke aws-strands-agent --queryCalculate the square root of 144 # 6. HTTP 调用 curl -X POST http://localhost:8000/invoke \ -H Content-Type: application/json \ -d {parameters: {query: What is 15 * 23?}} # 7. 运维 zenml deployment describe aws-strands-agent zenml deployment logs aws-strands-agent -f zenml deployment deprovision aws-strands-agent八、深入阅读示例源码examples/agent_framework_integrations/aws_strands/含 run.py、agent.py、requirements.txt同类集成示例总览examples/agent_framework_integrations/README.md部署命令实现src/zenml/cli/pipeline.pyzenml pipeline deploy调用命令实现src/zenml/cli/deployment.pyzenml deployment invoke部署信息展示src/zenml/cli/utils.pyEndpoint、Swagger、鉴权与调用示例【免费下载链接】zenmlZenML : One AI Platform from Pipelines to Agents. https://zenml.io.项目地址: https://gitcode.com/GitHub_Trending/ze/zenml创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表
PREV
查看更多资讯
NEXT
返回资讯列表