Hello. I’m new to this so please bear with me.
I have an app that uses OpenAI to generate a backstory for a fictional RPG character.
This uses a Netlify function to run the OpenAI query server-side.
This works fine with netlify dev, but when I run it in production, it does not. The ‘prompt’ query parameter simply does not get passed to the function; queryStringParameters is blank.
I’m calling the function from my React app like this:
const prompt = `Write a character story about a ${character.race} ${character.occ} named ${character.name} `
const url = "/.netlify/functions/generateBackstory" + "?prompt=" + prompt
Yet the server-side Netlify function shows queryStringParameters as {}.
The parameters are visible in the rawQuery var, however.
The Netlify function looks like this:
const handler = async (event, context) => {
var message
console.log('event', event)
const prompt = event.queryStringParameters.prompt
console.log('handler', event.queryStringParameters.prompt)
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.OPENAI_API_KEY
},
body: JSON.stringify({ model: 'text-davinci-003',
prompt: prompt,
max_tokens: 64,
temperature: 0.9,
top_p: 1,
presence_penalty: 0,
frequency_penalty: 0
})
}
try {
console.log('trying to fetch', requestOptions)
const result = await fetch('https://api.openai.com/v1/completions', requestOptions)
const data = await result.json()
const text = data.choices[0].text
const message = {
statusCode: 200,
body: JSON.stringify({ message: text })
}
return message
} catch (error) {
console.log('error', error)
return { statusCode: 500, body: error.toString() }
}
}
Am I capturing and passing the ‘prompt’ query parameter incorrectly? Again, this works fine when I run from netlify dev.
Any suggestions welcome. Please let me know if I should provide additional information.
Thank you in advance!