我只是在试用OpenAI的API,并运行了一个非常基本的express应用程序,我想做的只是让它用基本的输入返回一个适当的响应,但它目前一直失败。
我正在使用Postman迭代localhost上的代码。所有的包都已经安装,API密钥是正确的,并且在.env文件中指定。
我目前的工作文件如下。我肯定会踢自己,但有人能看出我可能做了什么蠢事吗?
const express = require('express');
const app = express();
require('dotenv').config();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const axios = require('axios'); // Come back to this
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
app.get('/api/v1', async (req, res) => {
let body = {
model: "text-davinci-003",
prompt: "How are you?",
temperature: 1,
max_tokens: 2086,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
};
const response = await openai.createCompletion(body);
res.send({ response });
});
// Listen for requests
app.listen(3000, function() {
console.log('Server is listening on port 3000');
});
终端中生成错误
/home/mint-pc/Desktop/projects/ebooks/api/node_modules/express/lib/response.js:1150
: JSON.stringify(value);
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'ClientRequest'
| property 'socket' -> object with constructor 'TLSSocket'
--- property '_httpMessage' closes the circle
at JSON.stringify (<anonymous>)
at stringify (/home/mint-pc/Desktop/projects/ebooks/api/node_modules/express/lib/response.js:1150:12)
at ServerResponse.json (/home/mint-pc/Desktop/projects/ebooks/api/node_modules/express/lib/response.js:271:14)
at ServerResponse.send (/home/mint-pc/Desktop/projects/ebooks/api/node_modules/express/lib/response.js:162:21)
at /home/mint-pc/Desktop/projects/ebooks/api/ghost_writer.js:48:7
2条答案
按热度按时间ttygqcqt1#
步骤1:运行Express服务器
在终端中运行
nodemon test.js
。步骤2:在请求正文中提供
model
和prompt
参数截图:
截图:
步骤3:单击"发送"发出HTTP POST请求
您应该得到
200 OK
和以下响应:h5qlskok2#
@Boaz在这篇评论中提供的答案:“检查响应对象。它可能是完整的HTTP响应对象,而不仅仅是响应数据...”
就是那个。