创建对话
一次对话包含用户问和智能体回答两个动作
标签:对话
请求说明
字段 | 值 |
---|---|
请求地址 | https://openapi.wps.cn/v7/devhub/app/{app_id}/chat/create |
HTTP 方法 | POST |
接口描述 | 开始对话 |
签名方式 | KSO-1 |
权限要求 | 智能体对话管理(用户授权) kso.devhub_chat.readwrite |
路径参数 (Path)
属性名 | 类型 | 是否必填 | 描述 | 可选值 |
---|---|---|---|---|
app_id | string | 是 | - |
请求体(Body)
请求体格式: application/json
没有可用的数据
响应体(Response)
状态码: 200
没有可用的数据
代码示例
go
package main
import (
"context"
"log"
"net/http"
"ksogit.kingsoft.net/open-std/wps365-apiclient-go/wps365"
)
// 构造一个 API Client
var apiclient = wps365.NewAPIClient(
wps365.OptBasePath("https://api.wps.cn"),
wps365.OptHTTPClient(
&http.Client{
Transport: http.DefaultTransport,
},
),
)
// 开始对话
func DevhubAppChatCreateSample() {
// 将 sid 设置到上下文
ctx := wps365.WithAuth(context.Background(), "...")
// 构造一个 Request body
reqBody := wps365.NewDevhubAppChatCreateReqBody(
// text,用户输入
`text`,
)
// 开始对话
DevhubAppChatCreateRespBody, err := apiclient.NewDevhubAppChatCreateRequest(
// app_id,
`app_id`,
).ReqBody(reqBody).Execute(ctx)
if err != nil {
log.Fatalf(err.Error())
}
log.Printf("\n%+v", DevhubAppChatCreateRespBody)
}
func main() {
DevhubAppChatCreateSample()
}
非流式对话响应体
名称 | 参数类型 | 说明 |
---|---|---|
code | integer | 响应代码。非 0 表示失败,参照《状态码说明》 |
msg | string | 响应信息 |
data | object | 响应数据 |
∟ session_id | string | 会话id,请求参数中如果没传会话id则会新生成一个会话,并返回id |
∟ chat_id | string | 对话id |
非流式响应体示例
json
{
"code": 0,
"msg": "success",
"data": {
"session_id":"xxxxxxxxx",
"chat_id":"xxxxxxxxxxx"
}
}
流式响应
响应结果是多个流式消息,每个流式消息结构体如下
{"event": "end", "data": {}}
event代表事件类型,枚举如下
- vertices_sorted运行前对所有节点排序
- add_message 流程中一个节点运行输出的信息
- end_vertex 流程中一个节点运行结束
- token 智能体流式返回
- end 整个流程结束
data代表事件数据,具体数据样例如下
{"event": "add_message", "data": {"timestamp": "2025-05-09T15:03:47.532881", "sender": "Machine", "sender_name": "Agent", "session_id": "7038a190-c4e9-44c3-9305-f560266be942", "text": "\u4e2d\u56fd\u7684\u603b\u9762\u79ef\u5927\u7ea6\u4e3a960\u4e07\u5e73\u65b9\u516c\u91cc\uff0c\u662f\u4e16\u754c\u4e0a\u9762\u79ef\u7b2c\u4e09\u6216\u7b2c\u56db\u5927\u7684\u56fd\u5bb6\uff0c\u5177\u4f53\u6392\u540d\u53d6\u51b3\u4e8e\u5bf9\u6709\u4e89\u8bae\u5730\u533a\u7684\u7edf\u8ba1\u65b9\u5f0f\u3002\u8fd9\u4f7f\u5f97\u4e2d\u56fd\u5728\u4e9a\u6d32\u662f\u9762\u79ef\u6700\u5927\u7684\u56fd\u5bb6\uff0c\u5e76\u4e14\u6709\u7740\u975e\u5e38\u4e30\u5bcc\u548c\u591a\u6837\u7684\u5730\u7406\u666f\u89c2\u3002", "files": [], "error": false, "edit": false, "properties": {"text_color": null, "background_color": null, "edited": false, "source": {"id": null, "display_name": null, "source": null}, "icon": "Bot", "allow_markdown": false, "positive_feedback": null, "state": "complete", "targets": []}, "category": "message", "content_blocks": [{"title": "Agent Steps", "contents": [{"type": "text", "duration": 18, "header": {"title": "Input", "icon": "MessageSquare"}, "text": "**Input**: \u4f60\u77e5\u9053\u4e2d\u56fd\u6709\u591a\u5927\u4e48"}, {"type": "text", "duration": 33, "header": {"title": "Input", "icon": "MessageSquare"}, "text": "**Input**: \u4f60\u77e5\u9053\u4e2d\u56fd\u6709\u591a\u5927\u4e48"}, {"type": "text", "duration": 0, "header": {"title": "Input", "icon": "MessageSquare"}, "text": "**Input**: \u4f60\u77e5\u9053\u4e2d\u56fd\u6709\u591a\u5927\u4e48"}, {"type": "text", "duration": 4012, "header": {"title": "Output", "icon": "MessageSquare"}, "text": "\u4e2d\u56fd\u7684\u603b\u9762\u79ef\u5927\u7ea6\u4e3a960\u4e07\u5e73\u65b9\u516c\u91cc\uff0c\u662f\u4e16\u754c\u4e0a\u9762\u79ef\u7b2c\u4e09\u6216\u7b2c\u56db\u5927\u7684\u56fd\u5bb6\uff0c\u5177\u4f53\u6392\u540d\u53d6\u51b3\u4e8e\u5bf9\u6709\u4e89\u8bae\u5730\u533a\u7684\u7edf\u8ba1\u65b9\u5f0f\u3002\u8fd9\u4f7f\u5f97\u4e2d\u56fd\u5728\u4e9a\u6d32\u662f\u9762\u79ef\u6700\u5927\u7684\u56fd\u5bb6\uff0c\u5e76\u4e14\u6709\u7740\u975e\u5e38\u4e30\u5bcc\u548c\u591a\u6837\u7684\u5730\u7406\u666f\u89c2\u3002"}], "allow_markdown": true, "media_url": null}], "id": "a550de06-0198-4a57-a5cd-75000522bf1d", "flow_id": "72cdefc1-940c-428d-bd8e-198386f3de5c"}}
{"event": "end", "data": {}}
流式对话中获取智能体回复方法如下:
- 监听event是add_message类型,且data中"sender": "Machine","sender_name": "Agent"的事件,data中的id是消息id
- 获取事件中的data.text内容即为智能体流式回复
- 消息中存在chat_id用于后续取消对话