发起聊天(智能体)

建立websocket

curl -X WS 'wss://HOST:PORT/api/v1/third_interface/assistant/chat?chai_id=聊天ID&key=秘钥&token=令牌/access_token'

鉴权

出于安全考虑,必须将 TOKEN 存储于服务端并通过后端接口调用,避免在前端代码中直接暴露,以防止凭证泄露造成损失

请在 Websocket 请求的 请求参数中携带 token,见Request Params

TOKEN获取可通过 个人令牌 和 OAuth 应用获取,具体见 个人令牌授权OAuth 授权码授权

应用秘钥key 的获取具体见 生成应用密钥

Request Params

  • chat_id (str) 聊天ID (非必填) 在原有聊天基础上继续对话,可通过对话消息返回的 chat_id 获取
  • key* (str)秘钥
  • token* (str)令牌

发送请求

直接发送 Json 字符串,格式如下

  • chatHistory (arr) 聊天历史列表 ,从获取历史消息接口中获取, 需要过滤category为node的记录
  • inputs(obj)
    • input(str) 用户问题
    • file_list(array) 文件列表
{
  "chatHistory": [
    {
      "is_bot": false,
      "source": 0,
      "message": {
        "input": "李明今天赚了100元人民币",
        "file_list": []
      },
      "extra": "\"{}\"",
      "type": "human",
      "category": "question",
      "flow_id": "ac5da15cc68f4b289a1123b2486c551f",
      "chat_id": "5f85f74988e830aad4332b09d7f13eab",
      "user_id": "3",
      "liked": 0,
      "solved": 0,
      "sender": null,
      "receiver": null,
      "intermediate_steps": null,
      "files": null,
      "remark": null,
      "create_time": "2024-08-27T14:01:16",
      "update_time": null,
      "user_question": null,
      "knowledge_source": null,
      "answer_files": null,
      "id": null,
      "isSend": true
    },
    {
      "is_bot": true,
      "source": 0,
      "message": "李明今天赚了100元人民币。",
      "extra": "\"{}\"",
      "type": "end_cover",
      "category": "answer",
      "flow_id": "ac5da15cc68f4b289a1123b2486c551f",
      "chat_id": "5f85f74988e830aad4332b09d7f13eab",
      "user_id": "3",
      "liked": 0,
      "solved": 0,
      "sender": null,
      "receiver": null,
      "intermediate_steps": null,
      "files": null,
      "remark": null,
      "create_time": "2024-08-27T14:01:16",
      "update_time": null,
      "user_question": null,
      "knowledge_source": [],
      "answer_files": [],
      "id": "157c3a24049d47f2a51530de80af877e",
      "isSend": false
    }
  ],
  "inputs": {
    "input": "你好",
    "file_list": []
  }
}

Response

消息返回参数说明

返回参数key 类型 含义
is_bot bool true是大模型回复,false是用户问题
message str/ json 返回的消息最终结果
type str begin/close 表示整轮会话的开始和结束,start/stream/end 表示stream的开始和结束
category str processing: 暂无特别实用 question/answer/report/system: 对应前端不同的展示样式
user_id int 当前用户id
message_id int 消息id
source bool 是否支持溯源, 0 不支持, 1 支持, 2有权限限制,3 问答库, 4 QA库
flow_id str 当前聊天的技能id
chat_id str 当前会话id
knowledge_source json 知识库来源列表 包含file_name和file_url

当完整构建一条消息,至少需要后端给前端发送4条消息:

type=begin, category=system
type=start, category=system
type=end, message=""
type=close, category=system

开始处理对话的消息

表示已经接受到消息,准备开始执行

{
  "is_bot": true,
  "message": "",
  "type": "begin",
  "category": "system",
  "user_id": 3,
  "flow_id": "ac5da15c-c68f-4b28-9a11-23b2486c551f",
  "chat_id": "5f85f74988e830aad4332b09d7f13eac"
}

流式开始输出的消息

表示接下来准备输出流式消息

{
  "is_bot": true,
  "message": "",
  "type": "start",
  "category": "processing",
  "user_id": 3,
  "flow_id": "ac5da15c-c68f-4b28-9a11-23b2486c551f",
  "chat_id": "5f85f74988e830aad4332b09d7f13eac"
}

流式输出消息

message为输出的片段

{
  "is_bot": true,
  "message": "你好",
  "type": "stream",
  "category": "processing",
  "user_id": 3,
  "flow_id": "ac5da15c-c68f-4b28-9a11-23b2486c551f",
  "chat_id": "5f85f74988e830aad4332b09d7f13eac"
}

工具调用消息

  • 开始调用
{
  "is_bot": true,
  "message": "{\"tool_key\": \"get_current_time\", \"serialized\": {\"name\": \"get_current_time\", \"description\": \"get_current_time(timezone='Asia/Shanghai') - 获取当前UTC时间以及主要时区的时间,可用于时间、日期等场景相关的计算。当问题涉及到时间,调用此工具来查询和时间有关的内容。\"}, \"input_str\": \"{'timezone': 'Asia/Shanghai'}\"}",
  "type": "start",
  "category": "tool",
  "intermediate_steps": "Tool input: \n\n{'timezone': 'Asia/Shanghai'}\n\n",
  "extra": "{\"run_id\": \"1290858382e742829fccf4cfef88d0c4\"}",
  "flow_id": "702c3638-7d50-4b54-9916-0827696c98d2"
}
  • 调用结束
{
  "is_bot": true,
  "message": "{\"tool_key\": \"get_current_time\", \"output\": \"Tuesday, August 27, 2024 05:40 PM\"}",
  "type": "end",
  "category": "tool",
  "intermediate_steps": "Tool output: \n\nTuesday, August 27, 2024 05:40 PM",
  "extra": "{\"run_id\": \"1290858382e742829fccf4cfef88d0c4\"}",
  "flow_id": "702c3638-7d50-4b54-9916-0827696c98d2"
}

知识库调用消息

  • 开始调用
{
  "is_bot": true,
  "message": "{知识库查询数据}",
  "type": "start",
  "category": "knowledge",
  "intermediate_steps": "Tool input: \n\n{'query': '李明 成绩'}\n\n",
  "extra": "{\"run_id\": \"a65fb455c29f409789422728143165d5\"}",
  "flow_id": "b1490d3b-8a5e-49a0-9970-590dc0145339"
}
  • 调用结束
{
  "is_bot": true,
  "message": "{知识库回答}",
  "type": "end",
  "category": "knowledge",
  "extra": "{\"run_id\": \"a65fb455c29f409789422728143165d5\"}",
  "flow_id": "b1490d3b-8a5e-49a0-9970-590dc0145339"
}

知识库来源信息消息

成对出现

第一条 类型为start

{
  "is_bot": true,
  "message": "knowledge_source",
  "type": "start",
  "category": "knowledge_source",
  "flow_id": "b1490d3b-8a5e-49a0-9970-590dc0145339",
  "knowledge_source": [
    {
      "filename": "测试文件.docx",
      "url": "http://Host:PORT/tmp-dir/测试文件.docx"
    }
  ]
}

第二条 类型为end

{
  "is_bot": true,
  "message": "knowledge_source",
  "type": "end",
  "category": "knowledge_source",
  "flow_id": "b1490d3b-8a5e-49a0-9970-590dc0145339",
  "knowledge_source": [
    {
      "filename": "测试文件.docx",
      "url": "http://Host:PORT/tmp-dir/测试文件.docx"
    }
  ]
}

流式输出结束消息

{
  "is_bot": true,
  "message": "",
  "type": "end",
  "category": "processing",
  "user_id": 3,
  "flow_id": "f9688199-4d7c-47a8-b118-0110b3b32a6a",
  "chat_id": "ed34c807e84f7d7d375a543c493a1b47"
}

最终回答的消息

将流式输出拼接成完成的一段话,并且返回文件、知识库来源、微应用等信息

{
  "is_bot": true,
  "message": "你好!有什么我可以帮助你的吗?",
  "type": "end_cover",
  "category": "answer",
  "user_id": 3,
  "message_id": "580228c75f134cada43a625515529988",
  "flow_id": "ac5da15c-c68f-4b28-9a11-23b2486c551f",
  "chat_id": "5f85f74988e830aad4332b09d7f13eac",
  "knowledge_source": [
    {
      "file_name": "日常用语.txt",
      "file_url": "http://bywin-file/tmp-dir/mydocx.txt"
    }
  ],
  "answer_files": [
    {
      "file_url": "public-files/tmp_answer_files/cae48dcfa9b2a7e14fe4a2655c560b61/技能基本信息.docx",
      "file_name": "技能基本信息.docx"
    }
  ],
  "is_microApp": {
    "id": "MicroApp-b8378",
    "pageList": [
      {
        "key": "0",
        "code": "<!DOCTYPE html>前端代码</html>",
        "label": "首页",
        "pageId": "0"
      },
      {
        "key": "1",
        "code": "<!DOCTYPE html>前端代码</html>",
        "label": "成功",
        "pageId": "success"
      }
    ]
  }
}

回答结束消息

表示该次回答结束

{
  "is_bot": true,
  "message": "",
  "type": "close",
  "category": "processing",
  "user_id": 3,
  "flow_id": "f9688199-4d7c-47a8-b118-0110b3b32a6a",
  "chat_id": "ed34c807e84f7d7d375a543c493a1b47"
}