1. 简介
在 AI Agent 开发领域,我们除了面临Agent框架选择(LangChain/LangGraph/AgentScope)等, 我们经常面临“开发容易,上线难”的尴尬:
本地运行 AgentScope 脚本非常顺滑,但一旦要转化为支撑高并发、具备标准接口、且能实时监控的生产级服务,往往涉及复杂的环境配置、网络穿透和接口封装。
本文将分享如何利用 AI Agent A2Z 部署和平台,结合 AgentScope 框架,快速实现 Agent 的生产级部署与冷启动,可以1键丝滑上线,得到/chat接口 endpoint
2. 实战分享
2.1 项目选型:AgentScope x AI Agent A2Z
AgentScope:由阿里团队开源,擅长多智能体协作(Multi-Agent)和灵活的消息机制,是构建复杂业务逻辑的首选。
AI Agent A2Z:一个专注于“高代码(High-Code)”的 Agent 部署与 Router 分发平台。它解决了 Agent 上线部署的最后一步——将 Python/NodeJs 等Agent代码转化为可以提供服务Agent,提供启动脚本权限(uvicorn/npm)等, 支持具备独立域名(.aiagenta2z.com/{project}/chat 等) 的 标准 API,可以直接通过Agent Router Web界面入口完成上线和给用户使用,流量冷启动阶段。
2.2 准备工作
- 代码框架:使用 AgentScope 定义 Deep Research Agent 的逻辑,我们以官方GitHub的模板为例 deep research agent https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/deep_research_agent 。
- 封装模板:使用 agent-mcp-deployment-templates(https://github.com/aiagenta2z/agent-mcp-deployment-templates ) 进行标准化封装。把这个DeepResearchAgent 本地转化为Uvicorn的FastAPI应用,提供/chat 接口。
- 部署托管平台:AI Agent A2Z Workspace (https://deepnlp.org/workspace/deploy) 和开源模板 (https://github.com/aiagenta2z/ai-agent-marketplace)
- 最终交付:例如 https://agentscope.aiagenta2z.com/deep_research_agent/chat POST 接口:标准 HTTP/SSE 接口,支持 messages类型参数请求Playground:用户可直接在线调测的 UI 界面。
2.3 完整案例
第一步:准备代码模板
我们以AgentScope代码库中 deep research agent (https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/deep_research_agent) 为例,
经过把本地agent 改造为 FastAPI的服务之后提供一个/chat 接口出来,完成Agent的部署。
这里原先案例入口是 main.py文件,转化为 FastAPI服务的接口为 main_server.py 文件
https://github.com/aiagenta2z/agent-mcp-deployment-templates/blob/main/agentscope_examples/deep_research_agent/main_server.py
核心的 FastAPI入口在这里,
初始化:startup_event中新建了agent 类,完成了 tavily等工具mcp的注册和连接。
stream_generator 定义了一个异步生成器,里面 agent.reply 返回接口 (原先的agent返回实现是耗时比较久,我们这边先不做改造 )
` response_msg = await agent.reply(msg) print (f"Agent Reply: response {response_msg}") response_content = response_msg.content print (f"Agent Reply: response response_content {response_content}")
FastAPI应用暴露Agent的接口到/chat里面,接受入参为对话 messages的Json格式
@app.post("/chat") async def chat(request: ChatRequest) -> StreamingResponse: """ Convert the DeepResearch Agent to Live Service.""" global tavily_search_client, agent if agent is None: raise HTTPException(status_code=503, detail="Agent not initialized") try: messages = request.messages user_query = "" try: user_query = messages[-1]["content"] if len(messages) > 0 else "hello" except Exception as e1: print (f"Failed to process input messages: {e1}") print (f"DEBUG: user: {user_query}") user_name = "USER_" ## set to 1 iteration for example msg = Msg( user_name, content=user_query, role="user" ) return StreamingResponse( stream_generator(agent, msg), media_type="application/json", ) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to chat: {str(e)}") from e async def stream_generator(agent, msg): """ Generator for Chat Messages Assemble AgentScope Response message_type: "user", "assistant" output_format: "text", "html" content_type: "text/markdown", mime-type SECTION_THINK = "think" SECTION_ANSWER = "answer" SECTION_TOOL = "tool" SECTION_SYSTEM_MSG = "system_msg" SECTION_CONTEXT = "context" TEMPLATE_REASONING_HTML = "reason_html" TEMPLATE_STREAMING_CONTENT_TYPE = "streaming_content_type" """ message_type = "assistant" output_format = "text" content_type = "text/markdown" section = "answer" streaming_separator = "\n" TEMPLATE_STREAMING_CONTENT_TYPE = "streaming_content_type" ## Initial Chunk initial_chunk = json.dumps(assembly_message(message_type, output_format, "DeepResearch Task Starting...", content_type=content_type, section=section, message_id= str(uuid.uuid4()), template=TEMPLATE_STREAMING_CONTENT_TYPE) ) yield initial_chunk + streaming_separator ## result is a message class Msg response_msg = await agent.reply(msg) print (f"Agent Reply: response {response_msg}") response_content = response_msg.content print (f"Agent Reply: response response_content {response_content}") output_message_id = response_msg.id content_type_chunk = json.dumps(assembly_message(message_type, output_format, response_content, content_type=content_type, section=section, message_id=output_message_id, template=TEMPLATE_STREAMING_CONTENT_TYPE) ) print (f"stream_generator response Result: {response_msg}") yield content_type_chunk + streaming_separator
第三步:在平台创建部署
登录 AI Agent A2Z 部署平台。
1. 首先创建一个Agent (owner/repo) 作为部署的unique_id项目,
2. 进入部署平台DeepNLP Workspace -> Deployment,选择好之前创建项目 (owner/repo) , 上传你的 Agent 代码包或关联 GitHub 仓库。
这里我们直接从github模板部署:
选择: GitHub,输入Public URL 选择: https://github.com/aiagenta2z/agent-mcp-deployment-templates
Entry Point启动命令行:uvicorn agentscope.deep_research_agent.main_server:app
注意:这里是从以 module方式启动的,项目根目录是 github下载后的目录,相当于你 cd aiagenta2z/agent-mcp-deployment-templates进入了,
启动命令行从 github的项目root开始,所以是 agentscope.xxxx
3. 设置配置环境变量(如 LLM API Key、AgentScope Config 等)。
可以从 Dashscope官网和Tavily 官网下载,这里用dummy 的 设置不影响部署流程。
export DASHSCOPE_API_KEY="your_dashscope_api_key_here" export TAVILY_API_KEY="your_tavily_api_key_here"
4. 点击Deploy 查看日志
INFO: Started server process [1] INFO: Waiting for application startup. Application startup... Tavily MCP server running on stdio 2026-02-11 04:00:19,324 | INFO | _stateful_client_base:connect:66 - MCP client connected. Lifecycle closed at the end... INFO: Application startup complete. > SUCCESS: ✅ Deployment Complete!
最终交付:
上线Agent 最终Agent的Chat接口:agentscope.aiagenta2z.com/deep_research_agent/chat
上线Agent Playground:
第四步、验证
我们可以通过 curl POST 请求来访问到你部署的Agent应用,
比如我们让Deep Research agent调研 prompt: 1+1等于?
curl -X POST "https://agentscope.aiagenta2z.com/deep_research_agent/chat" \ -H "Content-Type: application/json" \ -d '{"messages":[{"role":"user","content":"Calculate 1+1 result"}]}'
最终返回的Streaming Chunk的结果
{"type": "assistant", "format": "text", "content": "DeepResearch Task Starting...", "section": "answer", "message_id": "202d21fd-c71b-4a11-ba35-e2cb3c7d5947", "content_type": "text/markdown", "template": "streaming_content_type", "task_ids": "", "tab_message_ids": "", "tab_id": ""} {"type": "assistant", "format": "text", "content": "{\n \"type\": \"text\",\n \"text\": \"Overwrite /app/agentscope_examples/deep_research_agent/deepresearch_agent_demo_env/Friday260211040019_detailed_report.md successfully.\\n# Calculation of 1 + 1: A Foundational Arithmetic Operation\\n\\n## Step 1: Confirming the Context of the Operation\\n\\nThe expression \\\"1 + 1\\\" is interpreted within the standard framework of elementary arithmetic unless otherwise specified. In this context:\\n\\n- The numerals \\\"1\\\" represent the natural number one, which is the first positive integer in the set \u2115 = {1, 2, 3, ...} (or sometimes defined to include 0, depending on convention).\\n- The symbol \\\"+\\\" denotes the binary operation of addition as defined in the Peano axioms for natural numbers or as commonly taught in primary education.\\n- The numeral system assumed is base-10 (decimal), which is the standard positional numeral system used globally for everyday arithmetic.\\n\\nNo alternative interpretations\u2014such as those from Boolean logic, modular arithmetic, or abstract algebra\u2014are indicated in the subtask, so we proceed under the assumption of classical arithmetic in the natural numbers.\\n\\n## Step 2: Performing the Calculation\\n\\nUsing the definition of addition for natural numbers:\\n\\n- By the successor function in Peano arithmetic, the number 2 is defined as the successor of 1, denoted S(1).\\n- Addition is recursively defined such that:\\n - \\\\( a + 0 = a \\\\)\\n - \\\\( a + S(b) = S(a + b) \\\\)\\n\\nThus:\\n\\\\[\\n1 + 1 = 1 + S(0) = S(1 + 0) = S(1) = 2\\n\\\\]\\n\\nAlternatively, from empirical and educational foundations:\\n- Counting one object and then adding another yields a total of two objects.\\n- This is consistent across physical, symbolic, and computational representations.\\n\\nTherefore, **1 + 1 = 2**.\\n\\n## Step 3: Validation\\n\\nThis result is universally accepted in standard mathematics and has been formally verified in foundational logical systems. Notably:\\n\\n- In *Principia Mathematica* by Alfred North Whitehead and Bertrand Russell (1910\u20131913), the proposition \\\"1 + 1 = 2\\\" is rigorously derived from set-theoretic and logical axioms. It appears as Proposition \u221754.43 in Volume I, with the actual proof completed in Volume II after hundreds of pages of preliminary logic. While famously taking over 300 pages to reach, this underscores the depth of formal verification possible\u2014even for seemingly trivial statements.\\n\\n- Modern computational systems (e.g., calculators, programming languages like Python, MATLAB, or Wolfram Language) all return `2` when evaluating `1 + 1`.\\n\\n- Educational curricula worldwide introduce this as the first non-trivial addition fact, reinforcing its role as a cornerstone of numerical literacy.\\n\\n## Conclusion\\n\\nUnder standard arithmetic in the base-10 numeral system, using the conventional meaning of numerals and the addition operator, the expression **1 + 1 evaluates unequivocally to 2**. This result is mathematically sound, logically consistent, empirically verifiable, and computationally confirmed.\\n\\n**Final Answer: 2**\"\n}", "section": "answer", "message_id": "My5UpF5iRxcWbyooMHqogZ", "content_type": "text/markdown", "template": "streaming_content_type", "task_ids": "", "tab_message_ids": "", "tab_id": ""}