You need to enable JavaScript to run this app.
文档中心
AgentKit

AgentKit

复制全文
下载 pdf
SDK参考
Runtime
复制全文
下载 pdf
Runtime
本指南将引导您完成从环境配置、本地开发调试,到最终将Agent部署到生产环境的全过程。​
前提条件​
  • 已安装Python,且版本不低于3.10。查看Python版本的命令示例:python --version。​
步骤一:环境配置​
推荐使用uv或python3 -m venv来管理您的Python虚拟环境。本文以使用uv为例进行介绍。​
uv安装指南请参考:uv installation。​
  1. 创建并激活虚拟环境。​
  • ​
    mkdir agentkit-runtime-quickstart​
    cd agentkit-runtime-quickstart​
    uv venv --python 3.12.0​
    source .venv/bin/activate​
    ​
  1. 安装核心依赖。​
您可以通过--index参数指定镜像源以加快安装速度。​
  • ​
    uv add veadk-python​
    uv add agentkit-sdk-python​
    ​
步骤二:创建并本地调试Agent​
初始化Agent项目​
推荐您使用agentkit-cli快速启动项目。init命令可以从模板创建一个结构完整的、支持流式输出的Agent项目。​
​
# 使用 agentkit-cli 从模板创建项目​
agentkit init​
​
执行命令后会自动生成一个包含simple_agent.py和requirements.txt的项目结构,让您能立即开始开发。​
智能体代码概览​
使用basic_stream模板生成的simple_agent.py核心代码如下。它展示了如何基于AgentkitSimpleApp包装一个VeADK的Agent,并通过Runner执行,最终以SSE(Server-Sent Events)事件流返回结果。​
​
import json​
import logging​
​
from google.adk.agents import RunConfig​
from google.adk.agents.run_config import StreamingMode​
from google.genai.types import Content, Part​
from veadk import Agent, Runner​
​
from agentkit.apps import AgentkitSimpleApp​
from veadk.prompts.agent_default_prompt import DEFAULT_DESCRIPTION, DEFAULT_INSTRUCTION​
​
logger = logging.getLogger(__name__)​
logger.setLevel(logging.INFO)​
​
app = AgentkitSimpleApp()​
​
app_name = "simple_streamable_app"​
​
agent_name = "Agent"​
description = DEFAULT_DESCRIPTION​
system_prompt = DEFAULT_INSTRUCTION​
​
​
tools = []​
​
# from veadk.tools.builtin_tools.web_search import web_search​
# tools.append(web_search)​
​
​
agent = Agent(​
name=agent_name,​
description=description,​
instruction=system_prompt,​
tools=tools,​
)​
agent.model._additional_args["stream_options"] = {"include_usage": True}​
runner = Runner(agent=agent, app_name=app_name)​
​
​
@app.entrypoint​
async def run(payload: dict, headers: dict):​
prompt = payload["prompt"]​
user_id = headers["user_id"]​
session_id = headers["session_id"]​
​
logger.info(​
f"Running agent with prompt: {prompt}, user_id: {user_id}, session_id: {session_id}"​
)​
​
session_service = runner.short_term_memory.session_service # type: ignore​
​
# prevent session recreation​
session = await session_service.get_session(​
app_name=app_name, user_id=user_id, session_id=session_id​
)​
if not session:​
await session_service.create_session(​
app_name=app_name, user_id=user_id, session_id=session_id​
)​
​
new_message = Content(role="user", parts=[Part(text=prompt)])​
try:​
async for event in runner.run_async(​
user_id=user_id,​
session_id=session_id,​
new_message=new_message,​
run_config=RunConfig(streaming_mode=StreamingMode.SSE),​
):​
# Format as SSE data​
sse_event = event.model_dump_json(exclude_none=True, by_alias=True)​
logger.debug("Generated event in agent run streaming: %s", sse_event)​
yield sse_event​
except Exception as e:​
logger.exception("Error in event_generator: %s", e)​
# You might want to yield an error event here​
error_data = json.dumps({"error": str(e)})​
yield error_data​
​
​
@app.ping​
def ping() -> str:​
return "pong!"​
​
​
if __name__ == "__main__":​
app.run(host="0.0.0.0", port=8000)​
​
代码亮点:​
  • 默认使用火山方舟的豆包模型,并自动处理模型API Key的获取。​
  • 内置了会话管理逻辑,确保多轮对话的连续性。​
  • 通过AgentkitSimpleApp实现了与AgentKit Runtime的无缝集成。​
配置环境变量​
在启动Agent服务之前,您需要配置火山引擎的访问凭证:​
​
# 必需:配置您的火山引擎访问凭证​
export VOLCENGINE_ACCESS_KEY=your_ak​
export VOLCENGINE_SECRET_KEY=your_sk​
​
本地启动并调用服务​
运行以下命令启动Agent服务。​
​
python simple_agent.py​
​
服务将监听http://0.0.0.0:8000。现在,打开另一个终端,使用 curl 来测试您的 Agent:​
​
curl --location 'http://localhost:8000/invoke' \​
--header 'Content-Type: application/json' \​
--header 'user_id: test' \​
--header 'session_id: local_session' \​
--data '{"prompt": "讲一个笑话!"}'​
​
参数说明:​
  • user_id:用户标识符​
  • session_id:会话标识符​
  • prompt:用户的自然语言请求​
执行日志示例​
调用成功后,控制台会输出详细的执行日志:​
​
INFO: Started server process [38120]​
INFO: Waiting for application startup.​
INFO: Application startup complete.​
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)​
INFO: 127.0.0.1:60807 - "POST /invoke HTTP/1.1" 200 OK​
INFO:__main__:Running agent with prompt: 讲一个笑话!, user_id: test, session_id: local_session​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: Why​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: did​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: the​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: AI​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: apply​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: for​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: a​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: job​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: at​
2025-12-01 20:12:50 | DEBUG | runner.py:151 - Event output: the​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: bakery​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: ?​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: Because​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: it​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: wanted​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: to​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: work​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: on​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: its​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: **​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: d​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: ough​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: **​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: processing​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: skills​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: !​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: 😄​
2025-12-01 20:12:51 | DEBUG | runner.py:151 - Event output: Why did the AI apply for a job at the bakery?​
​
Because it wanted to work on its **dough** processing skills! 😄​
​
步骤三:部署到生产环境​
完成本地开发和测试后,您可以将智能体部署到 AgentKit 平台:​
  1. 打包代码:确保所有依赖都已正确配置在requirements.txt 中,使用模版生成的代码默认已经包含了所有必要的依赖。​
  1. 平台部署:根据[快速开始]指南,通过agentkit config和agentkit launch命令,配置和部署应用。​
  1. 调用应用:部署完成后,您可以通过平台提供的 API 或 SDK 调用智能体。调用示例:​
​
agentkit invoke "讲一个笑话!"​
​
至此,您已成功完成了从本地开发到云端部署的全过程!​
最近更新时间:2026.01.08 10:02:18
这个页面对您有帮助吗?
有用
有用
无用
无用