安装
npm i chatgpt koa koa-bodyparser koa-router -S
package.json 配置
"type": "module",
app.js
import Koa from "koa";
import bodyParser from "koa-bodyparser";
import Router from "koa-router";
import {sendMsg} from "./chatMain.js"
const app = new Koa();
const router = new Router();
app.use(bodyParser());
router.post("/chat-api/tracking", async(ctx) => {
const body = ctx.request.body;
const question = body.question;
const res = await sendMsg(question)
ctx.body = {
res
};
});
app.use(router.routes());
app.listen(3007, () => {
console.log("Server is running on http://localhost:3007");
});
chatMain.js
import { ChatGPTAPI } from "chatgpt";
import { oraPromise } from "ora";
const api = new ChatGPTAPI({
apiKey: "填入申请的apiKey",
debug: false,
});
export const sendMsg=(prompt)=>{
return new Promise(async (resolve, reject) => {
try {
const res = await oraPromise(api.sendMessage(prompt), {
text: prompt,
});
console.log(res);
resolve(res.text);
} catch (error) {
reject(error)
}
});
}
用js写一个冒泡排序
页面更新:2024-04-23