Skip to content

Commit

Permalink
feat: 支持post
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangsx committed Jun 27, 2023
1 parent 398d00d commit 33cfa99
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Koa, {Context, Next} from 'koa';
import Koa, {Context, Middleware, Next} from 'koa';
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser';
import {ChatModelFactory, Site} from "./model";
import dotenv from 'dotenv';
import {ChatRequest, ChatResponse, ModelType, PromptToString} from "./model/base";
import {Event, EventStream} from "./utils";

process.setMaxListeners(30); // 将限制提高到20个

dotenv.config();

const app = new Koa();
Expand All @@ -20,7 +22,7 @@ const errorHandler = async (ctx: Context, next: Next) => {
}
};
app.use(errorHandler);
app.use(bodyParser());
app.use(bodyParser({jsonLimit: '10mb'}));
const chatModel = new ChatModelFactory();

interface AskReq extends ChatRequest {
Expand All @@ -30,8 +32,8 @@ interface AskReq extends ChatRequest {
interface AskRes extends ChatResponse {
}

router.get('/ask', async (ctx) => {
const {prompt, model = ModelType.GPT3p5Turbo, site = Site.You} = ctx.query as unknown as AskReq;
const AskHandle: Middleware = async (ctx) => {
const {prompt, model = ModelType.GPT3p5Turbo, site = Site.You} = {...ctx.query as any, ...ctx.request.body as any} as AskReq;
if (!prompt) {
ctx.body = {error: `need prompt in query`} as AskRes;
return;
Expand All @@ -47,10 +49,10 @@ router.get('/ask', async (ctx) => {
return;
}
ctx.body = await chat.ask({prompt: PromptToString(prompt, tokenLimit), model});
});
}

router.get('/ask/stream', async (ctx) => {
const {prompt, model = ModelType.GPT3p5Turbo, site = Site.You} = ctx.query as unknown as AskReq;
const AskStreamHandle: Middleware = async (ctx) => {
const {prompt, model = ModelType.GPT3p5Turbo, site = Site.You} = {...ctx.query as any, ...ctx.request.body as any} as AskReq;
ctx.set({
"Content-Type": "text/event-stream;charset=utf-8",
"Cache-Control": "no-cache",
Expand All @@ -77,7 +79,12 @@ router.get('/ask/stream', async (ctx) => {
}
await chat.askStream({prompt: PromptToString(prompt, tokenLimit), model}, es);
ctx.body = es.stream();
})
}

router.get('/ask', AskHandle);
router.post('/ask', AskHandle);
router.get('/ask/stream', AskStreamHandle)
router.post('/ask/stream', AskStreamHandle)

app.use(router.routes());

Expand All @@ -90,5 +97,8 @@ app.use(router.routes());
process.exit(0);
});
});
process.on('uncaughtException', (e) => {
console.error(e);
})
})()

0 comments on commit 33cfa99

Please sign in to comment.