node koa接入ChatGPT

安装

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

标签:

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
无相关信息
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top