const https = require('https');
// OpenAI Completion API endpoint
const apiEndpoint = "api.openai.com";
const apiPath = "/v1/completions";
// Your API key
const apiKey = "YOUR_API_KEY_GOES_HERE";
// Text prompt
const textPrompt = "Hello, how are you?";
// Request body
const requestBody = JSON.stringify({
"model": "text-davinci-002",
"prompt": textPrompt,
"temperature": 0.7,
"max_tokens": 60,
"n": 1,
"stop": "."
});
const options = {
hostname: apiEndpoint,
path: apiPath,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(JSON.parse(data));
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(requestBody);
req.end();