Upload index.js
Browse files
index.js
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express')
|
2 |
+
const bodyParser = require('body-parser')
|
3 |
+
const axios = require('axios')
|
4 |
+
const app = express()
|
5 |
+
const uuid = require('uuid')
|
6 |
+
const { uploadImage } = require('./image')
|
7 |
+
|
8 |
+
app.use(bodyParser.json())
|
9 |
+
// 设置上传文件大小限制
|
10 |
+
app.use(bodyParser.json({ limit: '50mb' }))
|
11 |
+
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
|
12 |
+
|
13 |
+
|
14 |
+
const isJson = (str) => {
|
15 |
+
try {
|
16 |
+
JSON.parse(str)
|
17 |
+
return true
|
18 |
+
} catch (error) {
|
19 |
+
return false
|
20 |
+
}
|
21 |
+
}
|
22 |
+
|
23 |
+
app.get('/v1/models', async (req, res) => {
|
24 |
+
try {
|
25 |
+
const response = await axios.get('https://chat.qwenlm.ai/api/models',
|
26 |
+
{
|
27 |
+
headers: {
|
28 |
+
"Authorization": req.headers.authorization,
|
29 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
30 |
+
}
|
31 |
+
})
|
32 |
+
res.json(response.data)
|
33 |
+
} catch (error) {
|
34 |
+
res.status(403)
|
35 |
+
.json({
|
36 |
+
error: "请提供正确的 Authorization token"
|
37 |
+
})
|
38 |
+
}
|
39 |
+
})
|
40 |
+
|
41 |
+
app.post('/v1/chat/completions', async (req, res) => {
|
42 |
+
if (!req.headers.authorization) {
|
43 |
+
return res.status(403)
|
44 |
+
.json({
|
45 |
+
error: "请提供正确的 Authorization token"
|
46 |
+
})
|
47 |
+
}
|
48 |
+
const messages = req.body.messages
|
49 |
+
let imageId = null
|
50 |
+
const isImageMessage = Array.isArray(messages[messages.length - 1].content) === true && messages[messages.length - 1].content[1].image_url.url
|
51 |
+
if (isImageMessage) {
|
52 |
+
imageId = await uploadImage(messages[messages.length - 1].content[1].image_url.url, req.headers.authorization)
|
53 |
+
messages[messages.length - 1].content[1] = {
|
54 |
+
"type": "image",
|
55 |
+
"image": imageId
|
56 |
+
}
|
57 |
+
}
|
58 |
+
|
59 |
+
const stream = req.body.stream
|
60 |
+
|
61 |
+
const notStreamResponse = async (response) => {
|
62 |
+
const bodyTemplate = {
|
63 |
+
"id": `chatcmpl-${uuid.v4()}`,
|
64 |
+
"object": "chat.completion",
|
65 |
+
"created": new Date().getTime(),
|
66 |
+
"model": req.body.model,
|
67 |
+
"choices": [
|
68 |
+
{
|
69 |
+
"index": 0,
|
70 |
+
"message": {
|
71 |
+
"role": "assistant",
|
72 |
+
"content": response.choices[0].message.content
|
73 |
+
},
|
74 |
+
"finish_reason": "stop"
|
75 |
+
}
|
76 |
+
],
|
77 |
+
"usage": {
|
78 |
+
"prompt_tokens": JSON.stringify(req.body.messages).length,
|
79 |
+
"completion_tokens": response.choices[0].message.content.length,
|
80 |
+
"total_tokens": JSON.stringify(req.body.messages).length + response.choices[0].message.content.length
|
81 |
+
}
|
82 |
+
}
|
83 |
+
res.json(bodyTemplate)
|
84 |
+
}
|
85 |
+
|
86 |
+
const streamResponse = async (response) => {
|
87 |
+
const id = uuid.v4()
|
88 |
+
const decoder = new TextDecoder('utf-8')
|
89 |
+
let backContent = null
|
90 |
+
response.on('data', (chunk) => {
|
91 |
+
const decodeText = decoder.decode(chunk)
|
92 |
+
|
93 |
+
const lists = decodeText.split('\n').filter(item => item.trim() !== '')
|
94 |
+
for (const item of lists) {
|
95 |
+
const decodeJson = isJson(item.replace(/^data: /, '')) ? JSON.parse(item.replace(/^data: /, '')) : null
|
96 |
+
|
97 |
+
if (decodeJson === null) {
|
98 |
+
continue
|
99 |
+
}
|
100 |
+
|
101 |
+
let content = decodeJson.choices[0].delta.content
|
102 |
+
|
103 |
+
if (backContent === null) {
|
104 |
+
backContent = content
|
105 |
+
} else {
|
106 |
+
const temp = content
|
107 |
+
content = content.replace(backContent, '')
|
108 |
+
backContent = temp
|
109 |
+
}
|
110 |
+
|
111 |
+
const StreamTemplate = {
|
112 |
+
"id": `chatcmpl-${id}`,
|
113 |
+
"object": "chat.completion.chunk",
|
114 |
+
"created": new Date().getTime(),
|
115 |
+
"choices": [
|
116 |
+
{
|
117 |
+
"index": 0,
|
118 |
+
"delta": {
|
119 |
+
"content": content
|
120 |
+
},
|
121 |
+
"finish_reason": null
|
122 |
+
}
|
123 |
+
]
|
124 |
+
}
|
125 |
+
res.write(`data: ${JSON.stringify(StreamTemplate)}\n\n`)
|
126 |
+
}
|
127 |
+
})
|
128 |
+
|
129 |
+
response.on('end', () => {
|
130 |
+
res.write(`data: [DONE]\n\n`)
|
131 |
+
res.end()
|
132 |
+
})
|
133 |
+
|
134 |
+
}
|
135 |
+
|
136 |
+
try {
|
137 |
+
const response = await axios.post('https://chat.qwenlm.ai/api/chat/completions',
|
138 |
+
req.body,
|
139 |
+
{
|
140 |
+
headers: {
|
141 |
+
"Authorization": req.headers.authorization,
|
142 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
143 |
+
},
|
144 |
+
responseType: stream ? 'stream' : 'json'
|
145 |
+
}
|
146 |
+
)
|
147 |
+
if (stream) {
|
148 |
+
res.set({
|
149 |
+
'Content-Type': 'text/event-stream',
|
150 |
+
'Cache-Control': 'no-cache',
|
151 |
+
'Connection': 'keep-alive',
|
152 |
+
})
|
153 |
+
streamResponse(response.data)
|
154 |
+
} else {
|
155 |
+
res.set({
|
156 |
+
'Content-Type': 'application/json',
|
157 |
+
})
|
158 |
+
notStreamResponse(response.data)
|
159 |
+
}
|
160 |
+
|
161 |
+
} catch (error) {
|
162 |
+
console.log(error)
|
163 |
+
res.status(500)
|
164 |
+
.json({
|
165 |
+
error: "罢工了,不干了!!!"
|
166 |
+
})
|
167 |
+
}
|
168 |
+
|
169 |
+
})
|
170 |
+
|
171 |
+
app.listen(3000, () => {
|
172 |
+
console.log('Server is running on port 3000')
|
173 |
+
})
|
174 |
+
|