[英]Express app and OpenAI API - createCompletion method not working
Just experimenting with OpenAI's api and have a very basic express app up and running.只是尝试使用 OpenAI 的 api 并启动并运行一个非常基本的 express 应用程序。 What I'm trying to do is just get it to send me back an appropriate response with a basic input but it currently keeps failing.
我想要做的只是让它通过基本输入向我发送适当的响应,但它目前一直失败。
I'm using Postman to iterate on the code on localhost.我正在使用 Postman 来迭代本地主机上的代码。 All packages are definitely installed and the API key is correct and specfied in the.env file.
肯定安装了所有软件包,并且 API 密钥正确并在 .env 文件中指定。
My current working file is below.我当前的工作文件如下。 I'm sure I'll kick myself but can anyone spot what dumb thing I've probably done?
我敢肯定我会踢自己,但有谁能看出我可能做了什么蠢事吗?
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');
});
Error generated in terminal终端产生错误
/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
Run nodemon test.js
in the terminal.在终端中运行
nodemon test.js
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
and prompt
parameters in request body model
和prompt
参数{
"model": "text-davinci-003",
"prompt": "Say this is a test"
}
Screenshot:截屏:
Also, don't forget to provide your API key!另外,不要忘记提供您的 API 密钥!
Screenshot:截屏:
You should get 200 OK
and the following response:您应该得到
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
}
}
Answer provided in this comment by @Boaz: "Inspect the response object. It's likely a complete HTTP response object and not just the response data..." @Boaz 在此评论中提供的答案:“检查响应 object。它可能是完整的 HTTP 响应 object 而不仅仅是响应数据......”
It was that.就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.