Files
chat-api-demo/README.md

179 lines
3.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# chat-api-demo
调用 AI 的 demoNode.js
## 1. 安装依赖
```bash
npm install --registry=https://registry.npmjs.org
```
## 2. 配置环境变量
```bash
cp .env.example .env
```
`.env` 中填入你的 `QWEN_API_KEY`DashScope API Key
如需调用 DeepSeek再填入 `DEEPSEEK_API_KEY`
如需调用火山引擎,再填入 `VOLCENGINE_API_KEY``VOLCENGINE_MODEL`(方舟 Endpoint ID
## 3. 启动服务
```bash
npm run dev
```
默认端口:`3000`
## 4. 调用千问接口 demo
### 普通接口地址
`POST /api/qwen/chat`
### 普通请求示例
```bash
curl -X POST http://localhost:3000/api/qwen/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"你好,请介绍一下你自己"}
]
}'
```
### 普通返回示例
```json
{
"id": "xxx",
"model": "qwen-plus",
"content": "你好,我是一个 AI 助手...",
"raw": {}
}
```
## 5. 流式输出SSEdemo
### 流式接口地址
`POST /api/qwen/chat/stream`
### 流式请求示例
```bash
curl -N -X POST http://localhost:3000/api/qwen/chat/stream \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"请用三句话介绍流式输出"}
]
}'
```
### 返回格式(事件流)
每个增量片段:
```text
data: {"delta":"..."}
```
结束标记:
```text
data: [DONE]
```
## 6. 调用 DeepSeek 接口 demo
### DeepSeek 普通接口地址
`POST /api/deepseek/chat`
### DeepSeek 普通请求示例
```bash
curl -X POST http://localhost:3000/api/deepseek/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"你好,请介绍一下 DeepSeek"}
]
}'
```
### DeepSeek 流式接口地址
`POST /api/deepseek/chat/stream`
### DeepSeek 流式请求示例
```bash
curl -N -X POST http://localhost:3000/api/deepseek/chat/stream \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"请用三句话介绍你自己"}
]
}'
```
## 7. 目录结构
每个平台接口都独立在自己的目录中,`index.js` 只负责挂载路由:
```text
.
├── index.js
└── platforms
├── deepseek
│ └── router.js
├── qwen
│ └── router.js
└── volcengine
└── router.js
```
## 8. 调用火山引擎 Chat 接口 demo
### 火山普通接口地址
`POST /api/volcengine/chat`
### 火山普通请求示例
```bash
curl -X POST http://localhost:3000/api/volcengine/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"你好,请介绍一下你自己"}
]
}'
```
### 火山流式接口地址
`POST /api/volcengine/chat/stream`
### 火山流式请求示例
```bash
curl -N -X POST http://localhost:3000/api/volcengine/chat/stream \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role":"system","content":"你是一个简洁的助手"},
{"role":"user","content":"请用三句话介绍流式输出"}
]
}'
```