From 2e1bf66d3de65b3ba709bbe1022505fb911f03d8 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Mon, 11 Nov 2024 15:41:12 +0800 Subject: [PATCH] perf: auto add empty response (#3113) --- .../zh-cn/docs/development/openapi/chat.md | 143 ++++++++++++++++++ .../service/core/workflow/dispatch/index.ts | 8 + 2 files changed, 151 insertions(+) diff --git a/docSite/content/zh-cn/docs/development/openapi/chat.md b/docSite/content/zh-cn/docs/development/openapi/chat.md index 9f61a97c1ae5..7f03bb6e6616 100644 --- a/docSite/content/zh-cn/docs/development/openapi/chat.md +++ b/docSite/content/zh-cn/docs/development/openapi/chat.md @@ -312,6 +312,149 @@ event取值: {{< /tabs >}} +### 交互节点响应 + +如果工作流中包含交互节点,依然是调用该 API 接口,需要设置`detail=true`,并可以从`event=interactive`的数据中获取交互节点的配置信息。如果是`stream=false`,则可以从 choice 中获取`type=interactive`的元素,获取交互节点的选择信息。 + +当你调用一个带交互节点的工作流时,如果工作流遇到了交互节点,那么会直接返回,你可以得到下面的信息: + +{{< tabs tabTotal="2" >}} +{{< tab tabName="用户选择" >}} +{{< markdownify >}} + +```json +{ + "interactive": { + "type": "userSelect", + "params": { + "description": "测试", + "userSelectOptions": [ + { + "value": "Confirm", + "key": "option1" + }, + { + "value": "Cancel", + "key": "option2" + } + ] + } + } +} +``` + +{{< /markdownify >}} +{{< /tab >}} + +{{< tab tabName="表单输入" >}} +{{< markdownify >}} + +```json +{ + "interactive": { + "type": "userInput", + "params": { + "description": "测试", + "inputForm": [ + { + "type": "input", + "key": "测试 1", + "label": "测试 1", + "description": "", + "value": "", + "defaultValue": "", + "valueType": "string", + "required": false, + "list": [ + { + "label": "", + "value": "" + } + ] + }, + { + "type": "numberInput", + "key": "测试 2", + "label": "测试 2", + "description": "", + "value": "", + "defaultValue": "", + "valueType": "number", + "required": false, + "list": [ + { + "label": "", + "value": "" + } + ] + } + ] + } + } +} +``` + +{{< /markdownify >}} +{{< /tab >}} +{{< /tabs >}} + +### 交互节点继续运行 + +紧接着上一节,当你接收到交互节点信息后,可以根据这些数据进行 UI 渲染,引导用户输入或选择相关信息。然后需要再次发起对话,来继续工作流。调用的接口与仍是该接口,你需要按以下格式来发起请求: + +{{< tabs tabTotal="2" >}} +{{< tab tabName="用户选择" >}} +{{< markdownify >}} + +对于用户选择,你只需要直接传递一个选择的结果给 messages 即可。 + +```bash +curl --location --request POST 'https://api.fastgpt.in/api/v1/chat/completions' \ +--header 'Authorization: Bearer fastgpt-xxx' \ +--header 'Content-Type: application/json' \ +--data-raw '{ + "stream": true, + "detail": true, + "chatId":"22222231", + "messages": [ + { + "role": "user", + "content": "Confirm" + } + ] +}' +``` + +{{< /markdownify >}} +{{< /tab >}} + +{{< tab tabName="表单输入" >}} +{{< markdownify >}} + +表单输入稍微麻烦一点,需要将输入的内容,以对象形式并序列化成字符串,作为`messages`的值。对象的 key 对应表单的 key,value 为用户输入的值。务必确保`chatId`是一致的。 + +```bash +curl --location --request POST 'https://api.fastgpt.in/api/v1/chat/completions' \ +--header 'Authorization: Bearer fastgpt-xxxx' \ +--header 'Content-Type: application/json' \ +--data-raw '{ + "stream": true, + "detail": true, + "chatId":"22231", + "messages": [ + { + "role": "user", + "content": "{\"测试 1\":\"这是输入框的内容\",\"测试 2\":666}" + } + ] +}' +``` + +{{< /markdownify >}} +{{< /tab >}} +{{< /tabs >}} + + ## 请求插件 插件的接口与对话接口一致,仅请求参数略有区别,有以下规定: diff --git a/packages/service/core/workflow/dispatch/index.ts b/packages/service/core/workflow/dispatch/index.ts index 9d1f9fa51e3b..411f30e92f71 100644 --- a/packages/service/core/workflow/dispatch/index.ts +++ b/packages/service/core/workflow/dispatch/index.ts @@ -699,5 +699,13 @@ export const mergeAssistantResponseAnswerText = (response: AIChatItemValueItemTy result.push(item); } + // If result is empty, auto add a text message + if (result.length === 0) { + result.push({ + type: ChatItemValueTypeEnum.text, + text: { content: '' } + }); + } + return result; };