实战教程—NodeJS 接入 ChatGPT

5,276 阅读1分钟

1.chatgpt-api

github:github.com/transitive-…

该库是对gpt的api进行的封装,是使用的text-davinci-003模型

2.创建node项目 chatGPTServer

准备好在Open AI上申请的apiKeys,如果没申请的需要申请一下

升级node版本到18,因为chatgpt-api的包中预用了node18的fetch API

n ls-remote 18.14.0

mac下创建一个node项目

 mkdir chatGPTServer

 cd ./chatGPTServer

 npm init

 touch index.js

  // 安装chatgpt-api
 npm install chatgpt

 // 安装node服务,我用的express,也可用koa、hapi,也可以直接用node原生http模块,选择很多
 npm install express

 // post接口入参解析
 npm install body-parser -D

 // package.json中配置启动脚本
 "scripts": {
    "start": "node index.js",
    "start:hot": "nodemon index.js",
  },

 // 安装依赖
 npm install

3.创建post接口,写对接逻辑

4.postman进行post请求

node对接chatgpt01.png