File size: 4,661 Bytes
e7c8845 |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
const express = require('express')
const bodyParser = require('body-parser')
const axios = require('axios')
const app = express()
const uuid = require('uuid')
const { uploadImage } = require('./image')
app.use(bodyParser.json())
// 设置上传文件大小限制
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
app.get('/v1/models', async (req, res) => {
try {
const response = await axios.get('https://chat.qwenlm.ai/api/models',
{
headers: {
"Authorization": req.headers.authorization,
"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"
}
})
res.json(response.data)
} catch (error) {
res.status(403)
.json({
error: "请提供正确的 Authorization token"
})
}
})
app.post('/v1/chat/completions', async (req, res) => {
if (!req.headers.authorization) {
return res.status(403)
.json({
error: "请提供正确的 Authorization token"
})
}
const messages = req.body.messages
let imageId = null
const isImageMessage = Array.isArray(messages[messages.length - 1].content) === true && messages[messages.length - 1].content[1].image_url.url
if (isImageMessage) {
imageId = await uploadImage(messages[messages.length - 1].content[1].image_url.url, req.headers.authorization)
messages[messages.length - 1].content[1] = {
"type": "image",
"image": imageId
}
}
const stream = req.body.stream
const notStreamResponse = async (response) => {
const bodyTemplate = {
"id": `chatcmpl-${uuid.v4()}`,
"object": "chat.completion",
"created": new Date().getTime(),
"model": req.body.model,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": response.choices[0].message.content
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": JSON.stringify(req.body.messages).length,
"completion_tokens": response.choices[0].message.content.length,
"total_tokens": JSON.stringify(req.body.messages).length + response.choices[0].message.content.length
}
}
res.json(bodyTemplate)
}
const streamResponse = async (response) => {
const id = uuid.v4()
const decoder = new TextDecoder('utf-8')
let backContent = null
response.on('data', (chunk) => {
const decodeText = decoder.decode(chunk)
const lists = decodeText.split('\n').filter(item => item.trim() !== '')
for (const item of lists) {
const decodeJson = JSON.parse(item.replace(/^data: /, ''))
let content = decodeJson.choices[0].delta.content
if (backContent === null) {
backContent = content
} else {
const temp = content
content = content.replace(backContent, '')
backContent = temp
}
const StreamTemplate = {
"id": `chatcmpl-${id}`,
"object": "chat.completion.chunk",
"created": new Date().getTime(),
"choices": [
{
"index": 0,
"delta": {
"content": content
},
"finish_reason": null
}
]
}
res.write(`data: ${JSON.stringify(StreamTemplate)}\n\n`)
}
})
response.on('end', () => {
res.write(`data: [DONE]\n\n`)
res.end()
})
}
try {
const response = await axios.post('https://chat.qwenlm.ai/api/chat/completions',
req.body,
{
headers: {
"Authorization": req.headers.authorization,
"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"
},
responseType: stream ? 'stream' : 'json'
}
)
if (stream) {
res.set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
})
streamResponse(response.data)
} else {
res.set({
'Content-Type': 'application/json',
})
notStreamResponse(response.data)
}
} catch (error) {
console.log(error)
res.status(500)
.json({
error: "罢工了,不干了!!!"
})
}
})
app.listen(3000, () => {
console.log('Server is running on port 3000')
})
|