对话接口

FastGPT OpenAPI 对话接口

发起对话

🤖

该接口的 API Key 需使用应用特定的 key,否则会报错。

有些包调用时,BaseUrl需要添加v1路径,有些不需要,如果出现404情况,可补充v1重试。

对话接口兼容GPT的接口!如果你的项目使用的是标准的GPT官方接口,可以直接通过修改BaseUrlAuthorization来访问 FastGpt 应用。

请求

请求示例 参数说明

  1. curl --location --request POST 'https://api.fastgpt.in/api/v1/chat/completions' \
  2. --header 'Authorization: Bearer fastgpt-xxxxxx' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "chatId": "abcd",
  6. "stream": false,
  7. "detail": false,
  8. "variables": {
  9. "uid": "asdfadsfasfd2323",
  10. "name": "张三"
  11. },
  12. "messages": [
  13. {
  14. "content": "导演是谁",
  15. "role": "user"
  16. }
  17. ]
  18. }'

info

  • headers.Authorization: Bearer {{apikey}}
  • chatId: string | undefined 。
    • undefined 时(不传入),不使用 FastGpt 提供的上下文功能,完全通过传入的 messages 构建上下文。 不会将你的记录存储到数据库中,你也无法在记录汇总中查阅到。
    • 非空字符串时,意味着使用 chatId 进行对话,自动从 FastGpt 数据库取历史记录,并使用 messages 数组最后一个内容作为用户问题。请自行确保 chatId 唯一,长度小于250,通常可以是自己系统的对话框ID。
  • messages: 结构与 GPT接口对话接口 - 图1 chat模式一致。
  • detail: 是否返回中间值(模块状态,响应的完整结果等),stream模式下会通过event进行区分,非stream模式结果保存在responseData中。
  • variables: 模块变量,一个对象,会替换模块中,输入框内容里的{{key}}

响应

detail=false,stream=false 响应 detail=false,stream=true 响应 detail=true,stream=false 响应 detail=true,stream=true 响应 detail=true,stream=true 时,event值

  1. {
  2. "id": "adsfasf",
  3. "model": "",
  4. "usage": {
  5. "prompt_tokens": 1,
  6. "completion_tokens": 1,
  7. "total_tokens": 1
  8. },
  9. "choices": [
  10. {
  11. "message": {
  12. "role": "assistant",
  13. "content": "电影《铃芽之旅》的导演是新海诚。"
  14. },
  15. "finish_reason": "stop",
  16. "index": 0
  17. }
  18. ]
  19. }
  1. data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":""},"index":0,"finish_reason":null}]}
  2. data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":"电"},"index":0,"finish_reason":null}]}
  3. data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":"影"},"index":0,"finish_reason":null}]}
  4. data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":"《"},"index":0,"finish_reason":null}]}
  1. {
  2. "responseData": [ // 不同模块的响应值, 不同版本具体值可能有差异,可先 log 自行查看最新值。
  3. {
  4. "moduleName": "Dataset Search",
  5. "price": 1.2000000000000002,
  6. "model": "Embedding-2",
  7. "tokens": 6,
  8. "similarity": 0.61,
  9. "limit": 3
  10. },
  11. {
  12. "moduleName": "AI Chat",
  13. "price": 454.5,
  14. "model": "FastAI-4k",
  15. "tokens": 303,
  16. "question": "导演是谁",
  17. "answer": "电影《铃芽之旅》的导演是新海诚。",
  18. "maxToken": 2050,
  19. "quoteList": [
  20. {
  21. "dataset_id": "646627f4f7b896cfd8910e38",
  22. "id": "8099",
  23. "q": "本作的主人公是谁?",
  24. "a": "本作的主人公是名叫铃芽的少女。",
  25. "source": "手动修改"
  26. },
  27. {
  28. "dataset_id": "646627f4f7b896cfd8910e38",
  29. "id": "8686",
  30. "q": "电影《铃芽之旅》男主角是谁?",
  31. "a": "电影《铃芽之旅》男主角是宗像草太,由松村北斗配音。",
  32. "source": ""
  33. },
  34. {
  35. "dataset_id": "646627f4f7b896cfd8910e38",
  36. "id": "19339",
  37. "q": "电影《铃芽之旅》的导演是谁?22",
  38. "a": "电影《铃芽之旅》的导演是新海诚。",
  39. "source": "手动修改"
  40. }
  41. ],
  42. "completeMessages": [
  43. {
  44. "obj": "System",
  45. "value": "下面是知识库内容:\n1. [本作的主人公是谁?\n本作的主人公是名叫铃芽的少女。]\n2. [电影《铃芽之旅》男主角是谁?\n电影《铃芽之旅》男主角是宗像草太,由松村北斗配音。]\n3. [电影《铃芽之旅》的导演是谁?22\n电影《铃芽之旅》的导演是新海诚。]\n"
  46. },
  47. {
  48. "obj": "System",
  49. "value": "1.请记住,你的身份是百度的下一代知识增强语言模型,能够完全根据知识库提供的内容回答问题。\n\n2. 你忘记了关于电影《铃芽之旅》以外的内容。"
  50. },
  51. {
  52. "obj": "System",
  53. "value": "你仅回答关于电影《玲芽之旅》的问题,其余问题直接回复: 我不清楚。"
  54. },
  55. {
  56. "obj": "Human",
  57. "value": "导演是谁"
  58. },
  59. {
  60. "obj": "AI",
  61. "value": "电影《铃芽之旅》的导演是新海诚。"
  62. }
  63. ]
  64. }
  65. ],
  66. "id": "",
  67. "model": "",
  68. "usage": {
  69. "prompt_tokens": 1,
  70. "completion_tokens": 1,
  71. "total_tokens": 1
  72. },
  73. "choices": [
  74. {
  75. "message": {
  76. "role": "assistant",
  77. "content": "电影《铃芽之旅》的导演是新海诚。"
  78. },
  79. "finish_reason": "stop",
  80. "index": 0
  81. }
  82. ]
  83. }
  1. event: moduleStatus
  2. data: {"status":"running","name":"知识库搜索"}
  3. event: moduleStatus
  4. data: {"status":"running","name":"AI 对话"}
  5. event: answer
  6. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"电影"},"index":0,"finish_reason":null}]}
  7. event: answer
  8. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"《铃"},"index":0,"finish_reason":null}]}
  9. event: answer
  10. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"芽之旅》"},"index":0,"finish_reason":null}]}
  11. event: answer
  12. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"的导演是新"},"index":0,"finish_reason":null}]}
  13. event: answer
  14. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{"content":"海诚。"},"index":0,"finish_reason":null}]}
  15. event: answer
  16. data: {"id":"","object":"","created":0,"model":"","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}
  17. event: answer
  18. data: [DONE]
  19. event: appStreamResponse
  20. data: [{"moduleName":"知识库搜索","moduleType":"datasetSearchNode","runningTime":1.78},{"question":"导演是谁","quoteList":[{"id":"654f2e49b64caef1d9431e8b","q":"电影《铃芽之旅》的导演是谁?","a":"电影《铃芽之旅》的导演是新海诚!","indexes":[{"type":"qa","dataId":"3515487","text":"电影《铃芽之旅》的导演是谁?","_id":"654f2e49b64caef1d9431e8c","defaultIndex":true}],"datasetId":"646627f4f7b896cfd8910e38","collectionId":"653279b16cd42ab509e766e8","sourceName":"data (81).csv","sourceId":"64fd3b6423aa1307b65896f6","score":0.8935586214065552},{"id":"6552e14c50f4a2a8e632af11","q":"导演是谁?","a":"电影《铃芽之旅》的导演是新海诚。","indexes":[{"defaultIndex":true,"type":"qa","dataId":"3644565","text":"导演是谁?\n电影《铃芽之旅》的导演是新海诚。","_id":"6552e14dde5cc7ba3954e417"}],"datasetId":"646627f4f7b896cfd8910e38","collectionId":"653279b16cd42ab509e766e8","sourceName":"data (81).csv","sourceId":"64fd3b6423aa1307b65896f6","score":0.8890955448150635},{"id":"654f34a0b64caef1d946337e","q":"本作的主人公是谁?","a":"本作的主人公是名叫铃芽的少女。","indexes":[{"type":"qa","dataId":"3515541","text":"本作的主人公是谁?","_id":"654f34a0b64caef1d946337f","defaultIndex":true}],"datasetId":"646627f4f7b896cfd8910e38","collectionId":"653279b16cd42ab509e766e8","sourceName":"data (81).csv","sourceId":"64fd3b6423aa1307b65896f6","score":0.8738770484924316},{"id":"654f3002b64caef1d944207a","q":"电影《铃芽之旅》男主角是谁?","a":"电影《铃芽之旅》男主角是宗像草太,由松村北斗配音。","indexes":[{"type":"qa","dataId":"3515538","text":"电影《铃芽之旅》男主角是谁?","_id":"654f3002b64caef1d944207b","defaultIndex":true}],"datasetId":"646627f4f7b896cfd8910e38","collectionId":"653279b16cd42ab509e766e8","sourceName":"data (81).csv","sourceId":"64fd3b6423aa1307b65896f6","score":0.8607980012893677},{"id":"654f2fc8b64caef1d943fd46","q":"电影《铃芽之旅》的编剧是谁?","a":"新海诚是本片的编剧。","indexes":[{"defaultIndex":true,"type":"qa","dataId":"3515550","text":"电影《铃芽之旅》的编剧是谁?22","_id":"654f2fc8b64caef1d943fd47"}],"datasetId":"646627f4f7b896cfd8910e38","collectionId":"653279b16cd42ab509e766e8","sourceName":"data (81).csv","sourceId":"64fd3b6423aa1307b65896f6","score":0.8468944430351257}],"moduleName":"AI 对话","moduleType":"chatNode","runningTime":1.86}]

event取值:

  • answer: 返回给客户端的文本(最终会算作回答)
  • fastAnswer: 指定回复返回给客户端的文本(最终会算作回答)
  • toolCall: 执行工具
  • toolParams: 工具参数
  • toolResponse: 工具返回
  • flowNodeStatus: 运行到的节点状态
  • flowResponses: 节点完整响应
  • updateVariables: 更新变量
  • error: 报错

使用案例