curl -X POST 'http://192.168.1.7/v1/chat-messages' \ --header 'Authorization: Bearer {api_key}' \ --header 'Content-Type: application/json' \ --data-raw '{ "inputs": {}, "query": "What are the specs of the iPhone 13 Pro Max?", "response_mode": "streaming", "conversation_id": "", "user": "abc-123", "files": [ { "type": "image", "transfer_method": "remote_url", "url": "https://cloud.dify.ai/logo/logo-site.png" } ] }' POST /chat-messages 这是与AI大模型对接的请求参数和路径
时间: 2025-05-23 16:24:13 浏览: 55
### 使用 `curl` 命令向 Dify 的 `/v1/chat-messages` 接口发送 POST 请求
要通过 `curl` 命令与 AI 大模型接口对接,需按照指定的 URL、头部信息和请求体参数格式构造请求。以下是详细的说明和示例:
---
#### 1. **URL**
目标地址为 Dify 提供的 API 端点:
`http://<host>:<port>/v1/chat-messages`
假设本地部署的服务运行在 `localhost:8000` 上,则完整的 URL 为:
`http://localhost:8000/v1/chat-messages`
---
#### 2. **头部信息**
必需的头部字段如下:
- `Authorization`: 认证令牌,格式为 `"Bearer <your_api_key>"`。
- `Content-Type`: 指定内容类型为 `application/json`。
---
#### 3. **请求体参数**
请求体应为 JSON 格式,包含以下主要字段:
- `inputs`: 用户输入的消息内容。
- `query`: 查询字符串(某些情况下可能不需要)。
- `response_mode`: 控制返回模式,例如流式传输 (`stream`) 或一次性返回 (`complete`)。
其他可选参数包括但不限于:
- `model`: 指定使用的模型名称,默认可能是预设模型。
- `max_tokens`: 输出的最大 token 数量。
- `temperature`: 控制随机性的温度值,范围通常是 `[0, 1]`。
- `top_p`: 核采样概率阈值。
- `n`: 返回候选结果的数量。
---
#### 4. **完整示例**
以下是一个完整的 `curl` 命令示例,展示如何向 Dify 的 `/v1/chat-messages` 接口发送 POST 请求:
```bash
curl --request POST \
--url http://localhost:8000/v1/chat-messages \
--header 'Authorization: Bearer your_api_key_here' \
--header 'Content-Type: application/json' \
--data '{
"inputs": [
{"role": "user", "content": "请解释一下量子计算的基本原理"}
],
"response_mode": "complete",
"model": "deepseek-7b-chat",
"max_tokens": 512,
"temperature": 0.7,
"top_p": 0.9,
"n": 1
}'
```
---
#### 参数详解
| 参数名 | 描述 | 示例值 |
|-----------------|------------------------------------------------------------------------------------------|--------------------------------|
| `inputs` | 输入消息列表,每条消息由角色 (`role`) 和内容 (`content`) 组成 | `{ "role": "user", "content": "..." }` |
| `response_mode` | 返回模式,`complete` 表示一次性返回,`stream` 表示分片流式传输 | `complete` |
| `model` | 使用的模型名称 | `deepseek-7b-chat` |
| `max_tokens` | 输出的最大 token 数 | `512` |
| `temperature` | 温度值,控制输出多样性 | `0.7` |
| `top_p` | 核采样概率阈值 | `0.9` |
| `n` | 返回的候选项数量 | `1` |
---
#### 注意事项
- 如果需要启用多轮对话功能,可以在下一次请求中将之前的助手回复作为上下文传递给新的请求[^3]。
- 对于流式传输 (`stream=true`),服务器将以事件流的形式逐步返回结果,客户端需具备解析 SSE(Server-Sent Events)的能力。
---
### 示例代码解读
上述命令的作用是:
1. 向 `http://localhost:8000/v1/chat-messages` 发送一个 POST 请求。
2. 设置认证头 `Authorization` 和内容类型头 `Content-Type`。
3. 在请求体中提供用户提问内容以及其他必要的配置参数。
---
阅读全文
相关推荐



















{
"status_code": 403,
"body": "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html><head>\n<meta type=\"copyright\" content=\"Copyright (C) 1996-2020 The Squid Software Foundation and contributors\">\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<title>ERROR: The requested URL could not be retrieved</title>\n<style type=\"text/css\"></style>\n</head><body id=ERR_ACCESS_DENIED>\n\nERROR\nThe requested URL could not be retrieved\n\n
\n\n\nThe following error was encountered while trying to retrieve the URL: http://172.25.128.1:13000/api/resume_analysis:create\n\n\nAccess Denied.\n
\n\nAccess control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.\n\nYour cache administrator is webmaster.\n
\n\n\n
\n\nGenerated Thu, 05 Jun 2025 16:15:17 GMT by squid-proxy-7877d5d865-z2vtt (squid)\n\n\n</body></html>\n",
"headers": {
"server": "squid",
"mime-version": "1.0",
"date": "Thu, 05 Jun 2025 16:15:17 GMT",
"content-type": "text/html;charset=utf-8",
"content-length": "4148",
"x-squid-error": "ERR_ACCESS_DENIED 0",
"vary": "Accept-Language",
"content-language": "en",
"x-cache": "MISS from squid-proxy-7877d5d865-z2vtt",
"x-cache-lookup": "NONE from squid-proxy-7877d5d865-z2vtt:3128",
"connection": "keep-alive"
},
"files": []
}

