* **Request**
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=7374752000116113452' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "734829333445931****",
"user_id": "123456789",
"stream": true,
"auto_save_history":true,
"additional_messages":[
{
"role":"user",
"content":"2024年10月1日是星期几",
"content_type":"text"
}
]
}'
```
* **Response**
```JSON
event:conversation.chat.created
// 在 chat 事件里,data 字段中的 id 为 Chat ID,即会话 ID。
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0}}
event:conversation.chat.in_progress
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0}}
event:conversation.message.delta
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"2","content_type":"text","chat_id":"7382159487131697202"}
event:conversation.message.delta
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"0","content_type":"text","chat_id":"7382159487131697202"}
//省略模型回复的部分中间事件event:conversation.message.delta
......
event:conversation.message.delta
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"星期三","content_type":"text","chat_id":"7382159487131697202"}
event:conversation.message.delta
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"。","content_type":"text","chat_id":"7382159487131697202"}
event:conversation.message.completed
data:{"id":"7382159494123470858","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"2024 年 10 月 1 日是星期三。","content_type":"text","chat_id":"7382159487131697202"}
event:conversation.message.completed
data:{"id":"7382159494123552778","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"verbose","content":"{\"msg_type\":\"generate_answer_finish\",\"data\":\"\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7382159487131697202"}
event:conversation.chat.completed
data:{"id":"7382159487131697202","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792949,"last_error":{"code":0,"msg":""},"status":"completed","usage":{"token_count":633,"output_count":19,"input_count":614}}
event:done
data:"[DONE]"
```
* Request
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=7374752000116113452' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "737946218936519****",
"user_id": "123456789",
"stream": true,
"auto_save_history":true,
"additional_messages":[
{
"role":"user",
"content":"[{\"type\":\"image\",\"file_url\":\"https://lf-bot-studio-plugin-resource.coze.cn/obj/bot-studio-platform-plugin-tos/artist/image/4ca71a5f55d54efc95ed9c06e019ff4b.png\"},{\"type\":\"text\",\"text\":\"帮我看看这张图片里都有什么\"}]",
"content_type":"object_string"
}
]
}'
```
* Response
```JSON
event:conversation.chat.created
// 在 chat 事件里,data 字段中的 id 为 Chat ID,即会话 ID。
data:{"id":"7382158397837344768","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792697,"last_error":{"code":0,"msg":""},"status":"created","usage":{"token_count":0,"output_count":0,"input_count":0}}
event:conversation.chat.in_progress
data:{"id":"7382158397837344768","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792697,"last_error":{"code":0,"msg":""},"status":"in_progress","usage":{"token_count":0,"output_count":0,"input_count":0}}
event:conversation.message.completed
data:{"id":"7382158491307212815","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"function_call","content":"{\"name\":\"tupianlijie-imgUnderstand\",\"arguments\":{\"text\":\"描述图片里有什么\",\"url\":\"https://lf-bot-studio-plugin-resource.coze.cn/obj/bot-studio-platform-plugin-tos/artist/image/4ca71a5f55d54efc95ed9c06e019ff4b.png\"},\"plugin_id\":7379227414322217010,\"api_id\":7379227414322233394,\"plugin_type\":1,\"thought\":\"需求为描述图片(https://lf-bot-studio-plugin-resource.coze.cn/obj/bot-studio-platform-plugin-tos/artist/image/4ca71a5f55d54efc95ed9c06e019ff4b.png)里都有什么,需要调用tupianlijie-imgUnderstand工具理解图片\"}","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.completed
data:{"id":"7382158637826998312","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"tool_response","content":"{\"content_type\":1,\"response_for_model\":\" 这幅图像描绘了一片宁静而神秘的森林景象。画面中心是一条蜿蜒流淌的小溪,溪水清澈见底,溪床上散布着几块大小不一的石头。溪流两岸覆盖着厚厚的青苔,与周围的树木形成鲜明对比。树木高大挺拔,树干粗壮,树皮呈深褐色,树枝上长满了翠绿的针叶,阳光透过树叶的缝隙洒在地面上,形成斑驳的光影。整个场景被一层淡淡的雾气笼罩,增添了一丝神秘和幽静的氛围。画面远处的树木逐渐变得模糊,与天空的灰白色融为一体,整个画面色彩以绿色和棕色为主,营造出一种深邃而古老的感觉。整体上,这幅图像传达了一种与自然和谐共处的宁静与平和。\",\"type_for_model\":1}","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.delta
data:{"id":"7382158479043379234","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"这","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.delta
data:{"id":"7382158479043379234","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"是","content_type":"text","chat_id":"7382158397837344768"}
//省略模型回复的部分中间事件event:conversation.message.delta
......
event:conversation.message.delta
data:{"id":"7382158479043379234","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"树木","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.delta
data:{"id":"7382158479043379234","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"。","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.completed
data:{"id":"7382158479043379234","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"answer","content":"这是一幅非常漂亮的森林图片,里面有小溪、石头和青苔覆盖的树木。","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.message.completed
data:{"id":"7382158652519645218","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","role":"assistant","type":"verbose","content":"{\"msg_type\":\"generate_answer_finish\",\"data\":\"\",\"from_module\":null,\"from_unit\":null}","content_type":"text","chat_id":"7382158397837344768"}
event:conversation.chat.completed
data:{"id":"7382158397837344768","conversation_id":"7381473525342978089","bot_id":"7379462189365198898","completed_at":1718792697,"last_error":{"code":0,"msg":""},"status":"completed","usage":{"token_count":2308,"output_count":111,"input_count":2197}}
event:done
data:"[DONE]"
```
## 非流式响应
* **Request**
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=737475200011611****' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "734829333445931****",
"user_id": "123456789",
"stream": false,
"auto_save_history":true,
"additional_messages":[
{
"role":"user",
"content":"今天杭州天气如何",
"content_type":"text"
}
]
}'
```
* **Response**
```JSON
{
"data":{
// data 字段中的 id 为 Chat ID,即会话 ID。
"id": "123",
"conversation_id": "123456",
"bot_id": "222",
"created_at": 1710348675,
"completed_at": 1710348675,
"last_error": null,
"meta_data": {},
"status": "completed",
"usage": {
"token_count": 3397,
"output_count": 1173,
"input_count": 2224
}
},
"code":0,
"msg":""
}
```
* Request
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=737475200011611****' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "737946218936519****",
"user_id": "123456789",
"stream": false,
"auto_save_history":true,
"additional_messages":[
{
"role":"user",
"content":"[{\"type\":\"image\",\"file_url\":\"https://lf-bot-studio-plugin-resource.coze.cn/obj/bot-studio-platform-plugin-tos/artist/image/4ca71a5f55d54efc95ed9c06e019ff4b.png\"},{\"type\":\"text\",\"text\":\"帮我看看这张图片里都有什么\"}]",
"content_type":"object_string"
}
]
}'
```
* Response
```JSON
{
"data":{
// data 字段中的 id 为 Chat ID,即会话 ID。
"id": "123",
"conversation_id": "123456",
"bot_id": "222",
"created_at": 1710348675,
"completed_at": 1710348675,
"last_error": null,
"meta_data": {},
"status": "compleated",
"usage": {
"token_count": 3397,
"output_count": 1173,
"input_count": 2224
}
},
"code":0,
"msg":""
}
```
* Request
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=737475200011611****' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "737946218936519****",
"user_id": "123456789",
"stream": false,
"auto_save_history":true,
"additional_messages":[],
"shortcut_command":{
"command_id":"745701083352557****",
"parameters":{
"news_query":"[{\"type\":\"text\",\"text\":\"娱乐新闻\"}]"
}
}
}'
```
* Response
```JSON
{
"data":{
// data 字段中的 id 为 Chat ID,即会话 ID。
"id": "123",
"conversation_id": "123456",
"bot_id": "222",
"created_at": 1710348675,
"completed_at": 1710348675,
"last_error": null,
"meta_data": {},
"status": "compleated",
"usage": {
"token_count": 3397,
"output_count": 1173,
"input_count": 2224
}
},
"code":0,
"msg":""
}
```
## 给自定义参数赋值
自定义参数用于在智能体交互中存储和管理每个用户的特定信息,例如用户ID、地理位置等,以便实现个性化处理和差异化响应。你可以对话流中输入自定义参数,并在用户与智能体对话时动态更新和读取变量值。
1. 在对话流开始节点的输入参数中设置自定义用户变量。本文以自定义参数 `user` 为例,你可以根据实际业务场景设置其他参数。

2. 在调用发起对话接口时,通过 `parameters` 参数传入变量的值。例如给自定义参数 `user` 赋值,API 调用的示例代码如下:
```Shell
curl --location --request POST 'https://api.coze.cn/v3/chat?conversation_id=7374752000116113452' \
--header 'Authorization: Bearer pat_OYDacMzM3WyOWV3Dtj2bHRMymzxP****' \
--header 'Content-Type: application/json' \
--data-raw '{
"bot_id": "734829333445931****",
"user_id": "123456789",
"stream": true,
"auto_save_history": true,
"parameters": {
"user": [
{
"user_id": "123456",
"user_name": "user"
}
]
},
"additional_messages": [
{
"role": "user",
"content": "2024年10月1日是星期几",
"content_type": "text"
}
]
}'
```
## Prompt 变量
例如在智能体的 Prompt 中定义了一个 {{bot_name}} 的变量,在调用接口时,可以通过 **custom_variables** 参数传入变量值。