gpt4free-ts/index.ts

43 行
1005 B
TypeScript
Raw 通常表示 履歴

2023-05-04 22:32:21 +09:00
import {You} from "./model/you";
import Koa from 'koa';
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser';
const app = new Koa();
const router = new Router();
app.use(bodyParser());
2023-05-04 23:01:55 +09:00
const you = new You({proxy: process.env.https_proxy || process.env.http_proxy});
2023-05-04 22:32:21 +09:00
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;
});
2023-05-04 23:01:55 +09:00
router.get('/ask/stream', async (ctx) => {
2023-05-04 22:32:21 +09:00
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",
})
2023-05-04 23:01:55 +09:00
const res = await you.askStream({prompt: prompt as string});
2023-05-04 22:32:21 +09:00
ctx.body = res.text;
})
app.use(router.routes());
app.listen(3000);