node restify框架使用openai(chatgpt)接口

1.首先说明下,调用openai api接口,国内的话需要用到代理,不然无法获取到数据;
2.openai api调用时需要传上下文,不然无法记录之前聊的内容

第一步引入
npm install  restify
使用
const restify = require('restify');
const server = restify.createServer();
server.use(restify.plugins.bodyParser()); // 需要body传参的话,需要引用这个,不然无法接收到参数

function respond(req, res, next) {
 res.send(req.body);
}
server.post('/chatgpt', respond); // post请求
server.listen(3031, function () { // 监听
  console.log('%s listening at %s', server.name, server.url);
});
第一步下载
npm install socks-proxy-agent
使用
const { SocksProxyAgent } = require("socks-proxy-agent");
new SocksProxyAgent(process.env.PROXY_AGENT); // 连接
第一步下载
npm install openai
使用
const { Configuration, OpenAIApi } = require("openai"); // openai api
// openai KEY
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const data = await openai.cre

猜你喜欢

转载自blog.csdn.net/qq_43384836/article/details/130115339