gpt4 book ai didi

node.js - OpenAI GPT-3 API 错误 : "TypeError: Converting circular structure to JSON" using ExpressJS

转载 作者:行者123 更新时间:2023-12-02 05:47:29 25 4
gpt4 key购买 nike

只是尝试使用 OpenAI 的 api 并启动并运行一个非常基本的 express 应用程序。我想要做的只是让它通过基本输入向我发送适当的响应,但它目前一直失败。

我正在使用 Postman 迭代本地主机上的代码。肯定安装了所有包并且 API key 正确并在 .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

最佳答案

第 1 步:运行 Express 服务器

在终端中运行 nodemon test.js

test.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');
});

第 2 步:在请求正文中提供 modelprompt 参数

{
"model": "text-davinci-003",
"prompt": "Say this is a test"
}

截图:

Parameters

此外,不要忘记提供您的 API key !

截图:

API key

第 3 步:点击“发送”发出 HTTP POST 请求

您应该得到 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
}
}

关于node.js - OpenAI GPT-3 API 错误 : "TypeError: Converting circular structure to JSON" using ExpressJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75136962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com