如何快速使用Agent Protocol官方Python客户端ap_client:5个示例覆盖Runs与Threads全场景
如何快速使用Agent Protocol官方Python客户端ap_client:5个示例覆盖Runs与Threads全场景
如果你想在 Python 中对接 LLM Agent 服务,Agent Protocol 官方 Python 客户端 ap_client 是最省事的起点。它是基于 OpenAPI 规范(v0.1.6)自动生成的 SDK,内置 Runs、Threads、Agents、Store 五大 API 类,无需手写任何 HTTP 请求。本文用 5 个由浅入深的示例,带你从"一次性提问"到"多轮会话管理",覆盖 Runs 与 Threads 的常见使用场景。
一、准备工作:安装 ap_client 并启动本地服务
ap_client 需要 Python 3.9+,仓库中位于 client-python/ 目录:
cd client-python
pip install .
客户端默认连接 http://localhost。想先跑通流程,可以用仓库自带的 FastAPI 服务桩(见 server/ap_server/main.py)在本地起一个服务:
uvicorn ap_server.main:app
客户端使用方式是统一的三步:创建 Configuration → 创建 ApiClient → 实例化对应的 API 类:
import ap_client
configuration = ap_client.Configuration(host="http://localhost")
with ap_client.ApiClient(configuration) as api_client:
runs_api = ap_client.RunsApi(api_client)
# 后续所有示例都在这个上下文里调用
💡 连接超时、4xx/5xx 等异常统一抛出自 ap_client/exceptions.py 的
ApiException,用try/except捕获即可。
二、示例1:无状态一次性 Run(create_and_wait_run)
最简单的场景:问一句话、拿最终结果、不留痕迹。RunsApi.create_and_wait_run 对应 POST /runs/wait,会创建一个临时 Run,等待执行完毕并直接返回最终输出。
from ap_client.models import Message
with api_client:
response = runs_api.create_and_wait_run(run_stream={
"messages": [Message(role="user", content="用一句话介绍 Agent Protocol")]
})
print(response)
不传 thread_id 时就是无状态交互,Run 结束后线程自动删除——非常适合工具调用、批处理这类"用完即走"的请求。方法定义见 runs_api.py。
三、示例2:流式输出 Run(create_and_stream_run)
大模型逐 token 输出体验更好。RunsApi.create_and_stream_run 对应 POST /runs/stream,通过 stream_mode 控制流的粒度,支持四种模式(见 stream_mode.py):
| stream_mode | 输出内容 |
|---|---|
values |
每步的完整状态值 |
messages |
消息级别输出 |
updates |
每步的增量更新 |
custom |
自定义数据 |
response = runs_api.create_and_stream_run(run_stream={
"messages": [Message(role="user", content="写一首关于代码的诗")],
"stream_mode": ["updates"]
})
如果连接中途断开,可以之后通过 BackgroundRunsApi.stream_run 重新接入同一个 Run 的输出流,不用担心丢内容。
四、示例3:后台 Run——创建、查询与取消
需要"发射后不管"或后续控制时,用 BackgroundRunsApi(对应 background_runs_api.py):
background_api = ap_client.BackgroundRunsApi(api_client)
# 1. 创建后台 Run(fire and forget)
run = background_api.create_run(run_create={
"input": {"query": "帮我整理一份数据报告"}
})
run_id = run.run_id
# 2. 随时查询状态
status = background_api.get_run(run_id)
# 3. 不想等了?取消它
background_api.cancel_run(run_id, action="interrupt")
cancel_run 的 action 参数有两个取值:interrupt 仅停止执行;rollback 则停止并删除该 Run 及其检查点,让线程回到运行前的状态。执行完成后还可以用 wait_run(run_id) 拿最终输出,或用 delete_run(run_id) 清理记录。
五、示例4:创建 Threads 开启多轮会话
Threads 是 Agent Protocol 组织多轮对话的核心概念。先创建线程,之后每个 Run 都带上同一个 thread_id,服务端就会自动维护上下文(见 thread_create.py):
threads_api = ap_client.ThreadsApi(api_client)
# 创建线程(可自定义 thread_id 和 metadata)
thread = threads_api.create_thread(thread_create={
"thread_id": "chat-2026-001",
"metadata": {"user": "demo"}
})
# 在同一线程中执行 Run,自动携带上下文
runs_api.create_and_wait_run(run_stream={
"thread_id": "chat-2026-001",
"messages": [Message(role="user", content="帮我列一个三天的行程")]
})
# 追问——同一个 thread_id,Agent 记得上文
runs_api.create_and_wait_run(run_stream={
"thread_id": "chat-2026-001",
"messages": [Message(role="user", content="第二天改成自由活动")]
})
一个线程同一时间只允许一个活跃 Run,协议内置了并发保护,不会出现对话状态互相覆盖的混乱。
六、示例5:线程检索、修改、复制与删除
多轮会话跑多了,需要管理线程。ThreadsApi(对应 threads_api.py)提供了完整的 CRUD:
# 按 metadata 检索线程(默认返回 10 条)
results = threads_api.search_threads(thread_search_request={
"metadata": {"user": "demo"},
"limit": 5
})
# 修改线程的值或元数据(修改 values 会生成新的历史版本)
threads_api.patch_thread("chat-2026-001", thread_patch={
"metadata": {"topic": "旅行规划"}
})
# 基于现有线程创建独立副本(含状态与检查点)
copy = threads_api.copy_thread("chat-2026-001")
# 用完即删
threads_api.delete_thread("chat-2026-001")
配合 get_thread_history 还能回溯线程历次的状态版本,方便调试 Agent 的推理轨迹。
七、高频参数速查表
RunStream 模型(见 run_stream.py)里有几个容易忽略但很实用的字段:
| 参数 | 取值 | 说明 |
|---|---|---|
on_completion |
delete / keep |
Run 结束后是否删除线程,无 thread_id 时默认 delete |
on_disconnect |
cancel / continue |
客户端断连时,Run 是取消还是继续执行 |
if_not_exists |
reject / create |
指定的 thread_id 不存在时报错还是自动创建 |
webhook |
URL 字符串 | Run 完成后回调通知 |
八、小结:如何选择 API 类
| 需求 | 使用 API 类 | 源码位置 |
|---|---|---|
| 一次性同步调用 | RunsApi |
runs_api.py |
| 后台创建/取消/查询 Run | BackgroundRunsApi |
background_runs_api.py |
| 多轮会话与线程管理 | ThreadsApi |
threads_api.py |
| 长期记忆存储 | StoreApi |
store_api.py |
| 探知 Agent 能力与输入输出 Schema | AgentsApi |
agents_api.py |
掌握以上 5 个示例,基本就能覆盖生产环境里 Runs 与 Threads 的全部常见场景。想要更完整的字段说明,可以查看客户端自带的文档目录 client-python/docs/ 中的 RunsApi.md、ThreadsApi.md 等文件;协议整体设计思路则写在仓库根目录的 README.md 中。