发送消息
最新更新:2023-11-02

发送消息

向指定的对话 ID 发送消息,并获取 Bot 响应信息。支持提交文本和/或图片作为消息内容。

请求方式

POST

调用地址

https://api.gptbots.ai/v1/conversation/message

调用验证

详情参见 API 概述的鉴权方式说明。

请求

请求示例

curl -X POST https://api.gptbots.ai/v1/conversation/message \ -H 'Authorization: Bearer your_apikey' \ -H 'Content-Type: application/json' \ -d '{ "text": "你好!", "conversation_id": "xxxxxx", "response_mode": "streaming", "short_term_memory": true, "long_term_memory": false, "files":[ { "base64_content": "…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=", "url": "https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.jpg", "name": "something.jpg", "width": 200, "height": 200 }, { "base64_content": "…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=", "url": "https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.pdf", "name": "something.pdf" } ], "knowledge": { "data_ids": [ "1234567890", "4567898012" ] } }'
          curl -X POST https://api.gptbots.ai/v1/conversation/message \ 
  -H 'Authorization: Bearer your_apikey' \ 
  -H 'Content-Type: application/json' \ 
  -d '{
        "text": "你好!",
        "conversation_id": "xxxxxx",
        "response_mode": "streaming",
        "short_term_memory": true,
        "long_term_memory": false,
        "files":[
          {
            "base64_content": "…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=",
            "url": "https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.jpg",
            "name": "something.jpg",
            "width": 200,
            "height": 200
          },
          {
            "base64_content": "…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=",
            "url": "https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.pdf",
            "name": "something.pdf"
          }
        ],
        "knowledge": {
            "data_ids": [
                "1234567890",
                "4567898012"
            ]
        }
}'

        
此代码块在浮窗中显示

请求头

字段 类型 描述
Authorization Bearer ${token} 使用 Authorization: Bearer ${token}进行调用验证,请在 API 密钥页面获取密钥作为 token。
Content-Type application/json 数据类型,取值为 application/json。

请求体

字段 类型 必填 描述
text string text 与 files 必须至少输入其一。
用户的文本消息,文本内容的长度不能超过 Bot 配置的 tokens 长度限制。
files JSON Array text 与 files 必须至少输入其一。
用户上传的文件,支持提交文件的网络路径,最多上传9个文件,每个文件大小不超过20MB。
支持的文件类型:.jpg,.jpeg,.png,.gif,.webp,.pdf,.txt,.docx,.html,.c,.cpp,.java,.json,.md,.php,.pptx,.py,.rb,.tex,.css,.js,.ts,.xml等
文件配置(图片需要传宽度和高度):
  • base64_content,string,文件流
  • url,string,文件的链接
  • name, string, 文件名
  • width,int,图片宽度
  • height,int,图片高度

[{"base64_content": “…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=","url":"https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.jpg","name":"something.jpg","width":200,"height":200},{"base64_content": “…AiBMI0gWSQpvHbI4A1/VtR7/8ECllS3fqMNBMAAAAASUVORK5CYII=","url":"https://res.gptbots.cc/ailab/botchat/file/38f13465ad5246190b759b3289ecba51.pdf","name":"something.pdf"}]
conversation_id string 对话标识符,传入需要继续对话的 conversation_id。
response_mode string
  • blocking:阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。
  • streaming:流式返回,基于 SSE(Server-Sent Events)实现流式返回。
  • short_term_memory boolean 本次发送消息,是否携带对话内的短记忆作为上下文。若不填写,则遵循 Bot 的记忆设置。
    long_term_memory boolean 本次发送消息,是否携带对话内的长记忆作为上下文。若不填写,则遵循 Bot 的记忆设置。
    knowledge object 本次消息发送,在知识库检索中,仅检索该入参内定义的知识数据作为检索范围。
    - data_ids array 知识库检索的知识 ID。若不提交该参数,视为不限制检索范围,即查询整个 Bot 的所有知识文档。若提交该参数,且空数组,如[],则视为不检索任何知识文档。若数组内有data_ids,则基于这些data_ids执行检索。

    响应

    响应示例

    { "message_id": "65a4ccfC7ce58e728d5897e0", "message_type": "ANSWER", "text": "Hi, is there anything I can help you?", "flow_output": [ { "content": "你好", "branch": "1", "from_component_name": "User Input" } ], "create_time": 1679587005, "conversation_id": "657303a8a764d47094874bbe" }
              {
      "message_id": "65a4ccfC7ce58e728d5897e0",
      "message_type": "ANSWER",
      "text": "Hi, is there anything I can help you?",
      "flow_output": [
        {
          "content": "你好",
          "branch": "1",
          "from_component_name": "User Input"
        }
      ],
      "create_time": 1679587005,
      "conversation_id": "657303a8a764d47094874bbe"
    }
    
            
    此代码块在浮窗中显示

    成功响应(阻塞)

    字段 类型 描述
    message_id string 消息的唯一标识。
    message_type string 消息类型,取值:ANSWER、QUESTION。
    text string 回复内容。
    flow_output JSON Array flow bot 回复内容。
    content string flow bot 组件回复内容。
    branch string flow bot 分支。
    from_component_name string flow bot 上游组件名称。
    create_time long 回复的这条消息产生的时间戳。
    conversation_id string 对话标识符。

    成功响应(流式)

    字段 类型 描述
    code int 消息的类型编码,3-文本类型、10-flowbot 输出、0-结束标识。
    message string 消息类型,取值:Text、FlowOutput、End。
    data object 回复内容。

    流式数据分多次返回:

    {"code":3,"message":"Text","data":"我"} {"code":3,"message":"Text","data":"可以"} {"code":3,"message":"Text","data":"帮"} {"code":3,"message":"Text","data":"助"} {"code":3,"message":"Text","data":"你"} {"code":3,"message":"Text","data":"的"} {"code":3,"message":"Text","data":"吗"} {"code":3,"message":"Text","data":"?"} {"code":10,"message":"FlowOutput","data":[{"content":"你好","branch":null,"from_component_name": "User Input"}]} {"code":0,"message":"End","data":null}
              {"code":3,"message":"Text","data":"我"}  
    {"code":3,"message":"Text","data":"可以"}
    {"code":3,"message":"Text","data":"帮"}  
    {"code":3,"message":"Text","data":"助"}
    {"code":3,"message":"Text","data":"你"}  
    {"code":3,"message":"Text","data":"的"}
    {"code":3,"message":"Text","data":"吗"}
    {"code":3,"message":"Text","data":"?"}
    {"code":10,"message":"FlowOutput","data":[{"content":"你好","branch":null,"from_component_name": "User Input"}]}
    {"code":0,"message":"End","data":null}
    
            
    此代码块在浮窗中显示

    失败响应

    字段 类型 描述
    code int 错误码。
    message string 错误详情。

    错误码

    Code Message
    40000 参数错误
    40127 开发者鉴权失败
    40356 会话不存在
    50000 系统内部错误
    40364 该 Bot 未使用支持图片模态的 LLM
    20059 Bot 已删除
    20040 超过问题长度限制
    40358 conversation_id 无法匹配 Bot 或用户
    20022 积分不足