Upload 2 files
Browse files- src/index.js +14 -15
src/index.js
CHANGED
|
@@ -8,17 +8,6 @@ const crypto = require('crypto')
|
|
| 8 |
const app = new Koa()
|
| 9 |
const router = new Router()
|
| 10 |
|
| 11 |
-
// 添加重定向中间件
|
| 12 |
-
app.use(async (ctx, next) => {
|
| 13 |
-
console.log('收到请求:', ctx.method, ctx.path, ctx.query)
|
| 14 |
-
if (ctx.path.startsWith('/v1/')) {
|
| 15 |
-
console.log('重定向前路径:', ctx.path)
|
| 16 |
-
ctx.path = '/hf' + ctx.path
|
| 17 |
-
console.log('重定向后路径:', ctx.path)
|
| 18 |
-
}
|
| 19 |
-
await next()
|
| 20 |
-
})
|
| 21 |
-
|
| 22 |
// 使用 bodyParser 中间件
|
| 23 |
app.use(bodyParser())
|
| 24 |
|
|
@@ -30,6 +19,19 @@ app.use(bodyParser({
|
|
| 30 |
textLimit: '30mb', // text 数据大小限制
|
| 31 |
}))
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
const makeRequest = async (session_id, requestModel, messages) => {
|
| 35 |
console.log('开始请求 genspark.ai:', { session_id, requestModel })
|
|
@@ -169,7 +171,7 @@ router.post('/v1/chat/completions', async (ctx) => {
|
|
| 169 |
|
| 170 |
// console.log(content.delta)
|
| 171 |
|
| 172 |
-
//
|
| 173 |
ctx.res.write(`data: ${JSON.stringify({
|
| 174 |
"id": `chatcmpl-${messageId}`,
|
| 175 |
"choices": [
|
|
@@ -429,9 +431,6 @@ router.get('/hf/v1/models', async (ctx) => {
|
|
| 429 |
}
|
| 430 |
})
|
| 431 |
|
| 432 |
-
// 注册路由
|
| 433 |
-
app.use(router.routes()).use(router.allowedMethods())
|
| 434 |
-
|
| 435 |
// 错误处理中间件
|
| 436 |
app.use(async (ctx, next) => {
|
| 437 |
try {
|
|
|
|
| 8 |
const app = new Koa()
|
| 9 |
const router = new Router()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
// 使用 bodyParser 中间件
|
| 12 |
app.use(bodyParser())
|
| 13 |
|
|
|
|
| 19 |
textLimit: '30mb', // text 数据大小限制
|
| 20 |
}))
|
| 21 |
|
| 22 |
+
// 添加重定向中间件 - 移到路由注册之前
|
| 23 |
+
app.use(async (ctx, next) => {
|
| 24 |
+
console.log('收到请求:', ctx.method, ctx.path, ctx.query)
|
| 25 |
+
const originalPath = ctx.path
|
| 26 |
+
if (originalPath.startsWith('/v1/')) {
|
| 27 |
+
ctx.url = '/hf' + originalPath
|
| 28 |
+
console.log('重定向:', originalPath, '->', ctx.url)
|
| 29 |
+
}
|
| 30 |
+
await next()
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
// 注册路由
|
| 34 |
+
app.use(router.routes()).use(router.allowedMethods())
|
| 35 |
|
| 36 |
const makeRequest = async (session_id, requestModel, messages) => {
|
| 37 |
console.log('开始请求 genspark.ai:', { session_id, requestModel })
|
|
|
|
| 171 |
|
| 172 |
// console.log(content.delta)
|
| 173 |
|
| 174 |
+
// 发送增量内���
|
| 175 |
ctx.res.write(`data: ${JSON.stringify({
|
| 176 |
"id": `chatcmpl-${messageId}`,
|
| 177 |
"choices": [
|
|
|
|
| 431 |
}
|
| 432 |
})
|
| 433 |
|
|
|
|
|
|
|
|
|
|
| 434 |
// 错误处理中间件
|
| 435 |
app.use(async (ctx, next) => {
|
| 436 |
try {
|