Spaces:
Running
Running
File size: 1,905 Bytes
8a8fe1d |
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 |
import Koa, {Context, Next} from 'koa';
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser';
import {ChatModelFactory, Model} from "./model";
import dotenv from 'dotenv';
dotenv.config();
const app = new Koa();
const router = new Router();
const errorHandler = async (ctx: Context, next: Next) => {
try {
await next();
} catch (err: any) {
console.error(err);
ctx.body = JSON.stringify(err);
ctx.res.end();
}
};
app.use(errorHandler);
app.use(bodyParser());
const chatModel = new ChatModelFactory();
interface AskReq {
prompt: string;
model: Model;
}
router.get('/ask', async (ctx) => {
const {prompt, model = Model.Mcbbs, ...options} = ctx.query as unknown as AskReq;
if (!prompt) {
ctx.body = 'please input prompt';
return;
}
const chat = chatModel.get(model);
if (!chat) {
ctx.body = 'Unsupported model';
return;
}
const res = await chat.ask({prompt: prompt as string, options});
ctx.body = res.text;
});
router.get('/ask/stream', async (ctx) => {
const {prompt, model = Model.Mcbbs, ...options} = ctx.query as unknown as AskReq;
if (!prompt) {
ctx.body = 'please input prompt';
return;
}
const chat = chatModel.get(model);
if (!chat) {
ctx.body = 'Unsupported model';
return;
}
ctx.set({
"Content-Type": "text/event-stream;charset=utf-8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
});
const res = await chat.askStream({prompt: prompt as string, options});
ctx.body = res?.text;
})
app.use(router.routes());
(async () => {
const server = app.listen(3000, () => {
console.log("Now listening: 127.0.0.1:3000");
});
process.on('SIGINT', () => {
server.close(() => {
process.exit(0);
});
});
})()
|