![](/img/trans.png)
[英]nodejs app crash on openai dall-e 2 api rejected request
[英]Express app and OpenAI API - createCompletion method not working
只是尝试使用 OpenAI 的 api 并启动并运行一个非常基本的 express 应用程序。 我想要做的只是让它通过基本输入向我发送适当的响应,但它目前一直失败。
我正在使用 Postman 来迭代本地主机上的代码。 肯定安装了所有软件包,并且 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
在终端中运行nodemon test.js
测试.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const { OpenAIApi } = require("openai");
const openai = new OpenAIApi();
app.post('https://api.openai.com/v1/completions', async (req, res) => {
try {
const request_options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer xxxxx`
},
body: {
model: req.body.model,
prompt: req.body.prompt,
max_tokens: 7,
temperature: 0
}
}
openai.createCompletion(request_options)
.then((response) => {
res.send({ response });
})
.catch(err => {
console.log(err.message);
})
} catch (error) {
console.log(error);
}
});
app.listen(3000, function() {
console.log('Server is listening on port 3000');
});
model
和prompt
参数{
"model": "text-davinci-003",
"prompt": "Say this is a test"
}
截屏:
另外,不要忘记提供您的 API 密钥!
截屏:
您应该得到200 OK
和以下响应:
{
"id": "cmpl-6ZNYkREx9KEThpq2keFD9E1Xic3q6",
"object": "text_completion",
"created": 1673890062,
"model": "text-davinci-003",
"choices": [
{
"text": "\n\nThis is indeed a test.",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 8,
"total_tokens": 13
}
}
@Boaz 在此评论中提供的答案:“检查响应 object。它可能是完整的 HTTP 响应 object 而不仅仅是响应数据......”
就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.