平台记忆

普通调用需要客户端每次把完整历史发给模型。开启平台记忆后,历史由书童在服务端保存,客户端只发本轮的新问题;对话变长时自动压缩,节省 tokens。

使用方法

  1. 第一轮请求加上 "conversation_id": "new"。
  2. 从响应中取回会话 ID:非流式在响应体的 conversation_id 字段,流式在每个数据块里,同时也在响应头 x-conversation-id 中。
  3. 之后的请求带上这个 ID,messages 只放本轮的新消息。
import os
from openai import OpenAI

client = OpenAI(base_url="https://shutong.ai/api/v1", api_key=os.environ["SHUTONG_API_KEY"])

conv = "new"
for q in ["记住:我们的项目代号是“北极星”", "项目代号是什么?"]:
    r = client.chat.completions.create(
        model="deepseek-chat",
        messages=[{"role": "user", "content": q}],  # 只发本轮新问题
        extra_body={"conversation_id": conv},
    )
    conv = r.model_extra["conversation_id"]  # 第一轮之后拿到会话 ID
    print(r.choices[0].message.content)

工作方式

管理会话

GET /api/v1/conversations列出我的会话(最近 100 个)
GET /api/v1/conversations/<id>查看摘要与全部消息
DELETE /api/v1/conversations/<id>删除会话
# 列出我的会话
curl https://shutong.ai/api/v1/conversations -H "Authorization: Bearer $SHUTONG_API_KEY"

# 查看某个会话的摘要与全部消息
curl https://shutong.ai/api/v1/conversations/会话ID -H "Authorization: Bearer $SHUTONG_API_KEY"

# 删除会话
curl -X DELETE https://shutong.ai/api/v1/conversations/会话ID -H "Authorization: Bearer $SHUTONG_API_KEY"