Skip to content

Commit

Permalink
feat: 增加动态路由
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed Jul 4, 2023
1 parent fdda100 commit 9ff258b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const AskHandle: Middleware = async (ctx) => {
prompt,
model = ModelType.GPT3p5Turbo,
site = Site.You
} = {...ctx.query as any, ...ctx.request.body as any} as AskReq;
} = {...ctx.query as any, ...ctx.request.body as any, ...ctx.params as any} as AskReq;
if (!prompt) {
ctx.body = {error: `need prompt in query`} as AskRes;
return;
Expand All @@ -61,7 +61,7 @@ const AskStreamHandle: (ESType: new () => EventStream) => Middleware = (ESType)
prompt,
model = ModelType.GPT3p5Turbo,
site = Site.You
} = {...ctx.query as any, ...ctx.request.body as any} as AskReq;
} = {...ctx.query as any, ...ctx.request.body as any, ...ctx.params as any} as AskReq;
ctx.set({
"Content-Type": "text/event-stream;charset=utf-8",
"Cache-Control": "no-cache",
Expand Down Expand Up @@ -126,8 +126,8 @@ router.get('/ask', AskHandle);
router.post('/ask', AskHandle);
router.get('/ask/stream', AskStreamHandle(EventStream))
router.post('/ask/stream', AskStreamHandle(EventStream))
router.post('/v1/chat/completions', async (ctx, next) => {
const {stream} = {...ctx.query as any, ...ctx.request.body as any} as OpenAIReq;
const openAIHandle: Middleware = async (ctx, next) => {
const {stream} = {...ctx.query as any, ...ctx.request.body as any, ...ctx.params as any} as OpenAIReq;
(ctx.request.body as any).prompt = JSON.stringify((ctx.request.body as any).messages);
if (stream) {
AskStreamHandle(OpenaiEventStream)(ctx, next);
Expand All @@ -153,7 +153,10 @@ router.post('/v1/chat/completions', async (ctx, next) => {
"total_tokens": 100 + getTokenSize(ctx.body.content)
}
}
})
};

router.post('/v1/chat/completions', openAIHandle)
router.post('/:site/v1/chat/completions', openAIHandle)

app.use(router.routes());

Expand Down

0 comments on commit 9ff258b

Please sign in to comment.