init: 初始化demo项目,新增deepseek和千问api调用demo;

This commit is contained in:
2026-04-15 16:25:05 +08:00
parent 8e8b19cce3
commit 2c1cd85e77
7 changed files with 1360 additions and 1 deletions

138
README.md
View File

@@ -1,3 +1,139 @@
# chat-api-demo
调用ai的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`
## 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
```