44 行
977 B
TypeScript
44 行
977 B
TypeScript
![]() |
import {You} from "./model/you";
|
||
|
import Koa from 'koa';
|
||
|
import Router from 'koa-router'
|
||
|
import bodyParser from 'koa-bodyparser';
|
||
|
import {Readable} from "stream";
|
||
|
|
||
|
const app = new Koa();
|
||
|
const router = new Router();
|
||
|
app.use(bodyParser());
|
||
|
const you = new You();
|
||
|
|
||
|
interface AskReq {
|
||
|
prompt: string;
|
||
|
}
|
||
|
|
||
|
router.get('/ask', async (ctx) => {
|
||
|
const {prompt} = ctx.query;
|
||
|
if (!prompt) {
|
||
|
ctx.body = 'please input prompt';
|
||
|
return;
|
||
|
}
|
||
|
const res = await you.ask({prompt: prompt as string});
|
||
|
ctx.body = res.text;
|
||
|
});
|
||
|
|
||
|
router.get('/ask/stream',async(ctx)=>{
|
||
|
const {prompt} = ctx.query;
|
||
|
if (!prompt) {
|
||
|
ctx.body = 'please input prompt';
|
||
|
return;
|
||
|
}
|
||
|
ctx.set({
|
||
|
"Content-Type": "text/event-stream",
|
||
|
"Cache-Control": "no-cache",
|
||
|
"Connection": "keep-alive",
|
||
|
})
|
||
|
const res = await you.askStream({prompt: prompt as string});
|
||
|
ctx.body = res.text;
|
||
|
})
|
||
|
|
||
|
app.use(router.routes());
|
||
|
|
||
|
app.listen(3000);
|