I am trying to make multiple API calls to OpenAI in node.js to answer multiple questions at once. To do this I am using an async function with promise.all and then calling await on the API request (using request). In theory this should wait until all API calls are complete before then returning the response but the res.json response is called async rather than waiting so it's responding with an empty array.
router.get('/multiplequestions', (req, res, next) => {
console.log("hit");
const questions = ["How long is a metre in feet", "What is 10 + 12", "How many miles to the moon"]; // Array of ids
const answers = [];
const start = async () => {
const answersResponse = await Promise.all(questions.map(async question => {
var options = {
'method': 'POST',
'url': 'https://api.openai.com/v1/completions',
'headers': {
'Authorization': 'Bearer sk-xxx',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"model": "text-davinci-003",
"prompt": question,
"temperature": 0.7,
"max_tokens": 2230,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
})
};
await request(options, function(error, response) {
if (error) throw new Error(error);
const article = JSON.parse(response.body);
const articleText = article.choices[0].text;
answers.push(articleText)
console.log(answers)
});
}));
};
async function wrap() {
await start().then(res.json({
answers
}))
}
wrap();
});
The API calls work and return correctly, I just can't get it to wait before before res.json is sent.
I did read of course using an Async function means that it will be executed async but I thought the purpose of the await was so the code would wait until the api call is executed?
I thought wrapping start in another async function would work but runs the same, assume I've missed something to do with promises so pointers would be helpful?
EDIT
Here is the answer, now using fetch and returning correctly
router.get('/multiplequestions', (req, res, next) => {
console.log("hit");
const questions = ["How long is a metre in feet", "What is 10 + 12", "How many miles to the moon"]; // Array of ids
const start = async () => {
const answersResponses = await Promise.all(
questions.map(async question => {
let call = {
"model": "text-davinci-003",
"prompt": question,
"temperature": 0.7,
"max_tokens": 2230,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
const response = await fetch('https://api.openai.com/v1/completions', {
method: 'POST',
body: JSON.stringify(call),
'headers': {
'Authorization': 'Bearer sk-xxx',
'Content-Type': 'application/json'
}
});
const data = await response.json();
console.log(data);
return data.choices[0].text
})
)
res.json({answersResponses});
};
start();
});