Skip to content

Commit

Permalink
perf: auto add empty response (#3113)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored Nov 11, 2024
1 parent 952771e commit 2e1bf66
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
143 changes: 143 additions & 0 deletions docSite/content/zh-cn/docs/development/openapi/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 >}}


## 请求插件

插件的接口与对话接口一致,仅请求参数略有区别,有以下规定:
Expand Down
8 changes: 8 additions & 0 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 2e1bf66

Please sign in to comment.