Hello.
I created a Cloud Funtion to call GPT's API using Cloud Funtions, but I am getting an INTERNAL error.Has anyone succeeded in this? Thank you in advance for your help.
First, Cloud Funtion (named createQuiz) looks like this. The code is described in text below.
button to start createQuiz.
The result was an error, displaying "internal".
I looked at the console explorer and this is what I saw: the GPT response seems to have been obtained.
Here are the details of the error.
Actually, there were several times when internal did not appear. However, while I was editing various things, an internal error occurred again, and I restored it to the state before the error occurred, but for some reason the error did not go away. Even if I deleted CloudFuntion on Firebase and Deployed it, the problem could not be improved.
Best regards.
Cloud Functions code↓
const functions = require(‘firebase-functions’);
const admin = require(‘firebase-admin’);
const https = require(‘https’);
exports.createQuiz = functions.region(‘us-central1’)
.runWith({
timeoutSeconds: 100,
memory: ‘512MB’
})
.https.onCall((data, context) => {
const input = data.input;
return new Promise((resolve, reject) => {
// Write your code below!
const options = {
hostname: 'api.openai.com',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxx',
}
};
const requestBody = JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": input}],
"stream": false,
"temperature": 0.8,
"top_p": 0.8,
"presence_penalty": 0.2
});
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => { responseData += chunk; });
res.on('end', () => {
console.log('test:1');
console.log(responseData);
try {
console.log('test:2');
const parsedResponse = JSON.parse(responseData);
resolve(parsedResponse.choices ? parsedResponse.choices[0].text.trim() : '');
} catch (error) {
console.error('Error parsing response:', error);
reject('Internal error');
}
});
});
req.on('error', (error) => {
console.error(`Error: ${error.message}`);
reject('error');
});
req.write(requestBody);
req.end();
// Write your code above!
});
});