forked from ErSanSan233/prose-polish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (56 loc) · 1.97 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const path = require('path');
const app = express();
const port = 3000;
// 启用 CORS
app.use(cors());
app.use(express.json());
// 服务静态文件
app.use(express.static(path.join(__dirname, '.')));
// 代理 API 请求
app.post('/api/chat', async (req, res) => {
const model = req.body.model || 'qwen-turbo';
const apiKey = req.headers.authorization;
if (!apiKey) {
return res.status(401).json({ error: 'API Key is required' });
}
try {
let response;
if (model === 'qwen-turbo') {
response = await fetch('https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation', {
method: 'POST',
headers: {
'Authorization': apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
});
} else if (model === 'deepseek-chat' || model === 'deepseek-reasoner') {
// DeepSeek API 转发
response = await fetch('https://api.deepseek.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
});
} else {
return res.status(400).json({ error: 'Unsupported model' });
}
if (!response.ok) {
const error = await response.json();
return res.status(response.status).json(error);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('API request failed:', error);
res.status(500).json({ error: 'Failed to process request' });
}
});
app.listen(port, () => {
console.log(`服务器运行在 http://localhost:${port}`);
});