M-08-call-orchestration.json 10.3 KB
{
  "id": "M-08",
  "name": "LLM 调用编排服务",
  "fileKey": "call-orchestration",
  "desc": "核心业务编排服务,接收调用请求、匹配模型账号配置、加载提示词模板、组装请求参数、调用适配引擎、记录调用日志",
  "apis": [
    {
      "method": "POST",
      "path": "/api/v1/calls/sync",
      "name": "同步调用",
      "description": "同步调用:接收请求→匹配配置→调用模型→返回结果(全程阻塞等待)。支持 thinking_enabled 参数临时开启思考模式",
      "params": [
        {"name": "account_id", "in": "body", "type": "string", "required": false, "desc": "指定账号 ID(不指定则自动匹配)"},
        {"name": "model_name", "in": "body", "type": "string", "required": true, "desc": "目标模型名称"},
        {"name": "prompt_template_id", "in": "body", "type": "string", "required": false, "desc": "提示词模板 ID"},
        {"name": "messages", "in": "body", "type": "array", "required": true, "desc": "消息列表,格式:[{\"role\": \"user\", \"content\": \"...\"}]"},
        {"name": "variables", "in": "body", "type": "object", "required": false, "desc": "模板变量替换参数"},
        {"name": "biz_type", "in": "body", "type": "string", "required": false, "desc": "业务类型"},
        {"name": "thinking_enabled", "in": "body", "type": "boolean", "required": false, "desc": "临时开启思考模式(覆盖账号配置)。开启后注入 reasoning_effort 参数,响应中返回推理过程"}
      ],
      "responses": [
        {"code": 200, "desc": "调用成功", "schema": "CallRecord"},
        {"code": 400, "desc": "参数错误", "schema": ""},
        {"code": 429, "desc": "限流", "schema": ""},
        {"code": 502, "desc": "模型调用失败", "schema": ""}
      ],
      "backend": [
        {"tag": "receive", "text": "接收 POST /api/v1/calls/sync 请求"},
        {"tag": "validate", "text": "验证请求参数"},
        {"tag": "write", "text": "向数据库表插入新记录"},
        {"tag": "query", "text": "从数据库表按条件查询记录"},
        {"tag": "write", "text": "向数据库表插入新记录"},
        {"tag": "return", "text": "返回操作结果"}
      ]
    },
    {
      "method": "POST",
      "path": "/api/v1/calls/async",
      "name": "异步调用",
      "description": "异步调用:接收请求→创建任务记录→提交 Celery→返回 taskId。支持 thinking_enabled 参数临时开启思考模式",
      "params": [
        {"name": "account_id", "in": "body", "type": "string", "required": false, "desc": "指定账号 ID"},
        {"name": "model_name", "in": "body", "type": "string", "required": true, "desc": "目标模型名称"},
        {"name": "prompt_template_id", "in": "body", "type": "string", "required": false, "desc": "提示词模板 ID"},
        {"name": "messages", "in": "body", "type": "array", "required": true, "desc": "消息列表"},
        {"name": "variables", "in": "body", "type": "object", "required": false, "desc": "模板变量替换参数"},
        {"name": "biz_type", "in": "body", "type": "string", "required": false, "desc": "业务类型"},
        {"name": "thinking_enabled", "in": "body", "type": "boolean", "required": false, "desc": "临时开启思考模式(覆盖账号配置)"}
      ],
      "responses": [
        {"code": 202, "desc": "已接受", "schema": "{task_id}"},
        {"code": 400, "desc": "参数错误", "schema": ""}
      ],
      "backend": [
        {"tag": "receive", "text": "接收 POST /api/v1/calls/async 请求"},
        {"tag": "validate", "text": "验证必填字段和参数格式"},
        {"tag": "write", "text": "向数据库表插入新记录"},
        {"tag": "write", "text": "向数据库表插入新记录"},
        {"tag": "return", "text": "返回操作结果"}
      ]
    },
    {
      "method": "GET",
      "path": "/api/v1/calls/async/:task_id",
      "name": "查询异步结果",
      "description": "查询异步调用结果(轮询模式)",
      "params": [
        {"name": "task_id", "in": "path", "type": "string", "required": true, "desc": "任务 ID"}
      ],
      "responses": [
        {"code": 200, "desc": "成功", "schema": "AsyncTask"},
        {"code": 404, "desc": "任务不存在", "schema": ""}
      ],
      "backend": [
        {"tag": "receive", "text": "接收 GET /api/v1/calls/async/:task_id 请求"},
        {"tag": "validate", "text": "验证请求参数"},
        {"tag": "query", "text": "从数据库表按条件查询记录"},
        {"tag": "return", "text": "返回操作结果"}
      ]
    },
    {
      "method": "POST",
      "path": "/api/v1/calls/async/batch",
      "name": "批量查询",
      "description": "批量查询多个异步调用结果",
      "params": [
        {"name": "task_ids", "in": "body", "type": "array", "required": true, "desc": "任务 ID 列表"}
      ],
      "responses": [
        {"code": 200, "desc": "成功", "schema": "AsyncTask[]"},
        {"code": 400, "desc": "参数错误", "schema": ""}
      ],
      "backend": [
        {"tag": "receive", "text": "接收 POST /api/v1/calls/async/batch 请求"},
        {"tag": "validate", "text": "验证请求参数"},
        {"tag": "query", "text": "从数据库表按条件查询记录"},
        {"tag": "return", "text": "返回操作结果"}
      ]
    },
    {
      "method": "POST",
      "path": "/api/v1/calls/by-template",
      "name": "业务调用(by-template)",
      "description": "业务侧直接调用接口:提供 template_id(支持UUID或业务编码) + account_id(支持UUID或业务编码) + content + mode(sync/async),系统自动按 template_id 先查 UUID 再查 code 加载提示词模板和账号配置、拼装请求、编排调用。同步模式返回 LLM 响应,异步模式返回 task_id。创建调用记录时自动设置 biz_type = 模板的业务编码(code)。",
      "params": [
        {"name": "template_id", "in": "body", "type": "string", "required": false, "desc": "提示词模板 UUID 或业务编码(如 \"TEMPLATE-GENERAL-001\"),系统自动按 UUID→业务编码顺序匹配"},
        {"name": "account_id", "in": "body", "type": "string", "required": false, "desc": "账号 UUID 或业务编码(如 \"ACCOUNT-QWEN-001\"),系统自动按 UUID→业务编码顺序匹配"},
        {"name": "content", "in": "body", "type": "string", "required": true, "desc": "用户输入的提示词内容,作为 user message"},
        {"name": "mode", "in": "body", "type": "string", "required": true, "desc": "调用模式:sync(同步,直接返回结果)/ async(异步,返回 task_id)"}
      ],
      "responses": [
        {"code": 200, "desc": "同步调用成功", "schema": "{status, model_name, call_record_id, account_id, response_content}"},
        {"code": 202, "desc": "异步调用已接受", "schema": "{status: 'accepted', task_id, call_record_id}"},
        {"code": 400, "desc": "参数错误", "schema": ""},
        {"code": 404, "desc": "模板或账号不存在", "schema": ""},
        {"code": 502, "desc": "模型调用失败", "schema": ""}
      ],
      "backend": [
        {"tag": "receive", "text": "接收 POST /api/v1/calls/by-template 请求"},
        {"tag": "validate", "text": "验证 content/mode 必填字段,校验 template_id 和 account_id 至少提供一组"},
        {"tag": "query", "text": "根据 template_id 查找:先尝试按 UUID 匹配 prompt_templates 表,未找到则按 code 匹配。读取 system_prompt"},
        {"tag": "query", "text": "根据 account_id 查找:先尝试按 UUID 匹配 model_account_config 表,未找到则按 code 匹配。获取 model_name/endpoint 等信息"},
        {"tag": "write", "text": "构建消息:system_prompt = 模板.system_prompt,user message = content"},
        {"tag": "validate", "text": "mode=sync → 调用 adapter_invoke 同步调用模型"},
        {"tag": "validate", "text": "mode=async → 创建 AsyncTask,提交 Celery 任务"},
        {"tag": "write", "text": "创建或更新 CallRecord 记录,设置 biz_type = prompt_template.code(模板的业务编码)"},
        {"tag": "return", "text": "返回结果:sync 模式返回 {status, model_name, call_record_id, account_id, response_content(大模型原始返回内容字符串)},async 模式返回 {status: accepted, task_id, call_record_id}"}
      ]
    }
  ],
  "changelogs": [
    {"date": "2026-06-16", "id": "CHG-011", "type": "add", "title": "新增业务调用接口", "desc": "新增 POST /api/v1/calls/by-template 接口,业务侧提供 template_id + account_id + content + mode 即可完成编排调用", "done": true},
    {"date": "2026-06-16", "id": "CHG-012", "type": "modify", "title": "by-template 接口支持 code 查找", "desc": "POST /api/v1/calls/by-template 新增 template_code 和 account_code 参数,支持按业务编码查找模板和账号,同时兼容原有的 template_id 和 account_id", "done": true},
    {"date": "2026-06-16", "id": "CHG-013", "type": "modify", "title": "template_id/account_id 统一接受 UUID 或业务编码", "desc": "template_id 和 account_id 同时接受 UUID 或业务编码,后端自动识别。移除独立的 template_code/account_code 字段,简化 API", "done": true},
    {"date": "2026-06-16", "id": "CHG-014", "type": "modify", "title": "by-template 同步响应字段 response_body 改为 response_content", "desc": "sync 模式响应中 response_body 字段重命名为 response_content,值改为大模型原始返回内容字符串(不再包裹在 dict 中)", "done": true},
    {"date": "2026-06-16", "id": "CHG-016", "type": "modify", "title": "编排 API 新增 thinking_enabled 参数", "desc": "POST /api/v1/calls/sync 和 POST /api/v1/calls/async 新增 thinking_enabled(bool) 参数。同时影响 POST /api/v1/calls/orchestrate/sync、POST /api/v1/calls/orchestrate/stream、POST /api/v1/calls/by-template,支持临时覆盖账号思考模式配置。流式调用 StreamCallRequest 同步新增 thinking_enabled", "done": true},
    {"date": "2026-06-29", "id": "CHG-031", "type": "modify", "title": "by-template 创建调用记录时 biz_type 自动设为模板业务编码", "desc": "POST /api/v1/calls/by-template 创建 CallRecord 时自动设置 biz_type = prompt_template.code。接口描述和 backend 步骤已更新。", "done": true}
  ]
}