export async function createCompletion(){
console.log('Button clicked!');
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "REDACTED",
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: " I put some product reviews in here",
maxTokens: 64,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0
});
const data = await response.json();
const summary = data.choices[0].text;
document.getElementById("result").innerHTML = summary;
}
I have an index.js file to import this also, and an index.html as the interface.
At first glance: the require
statement should be at the top of the file, outside of the createCompletion
function.
If it doesn’t work see below:
Most frequent reasons for 400 Error (invalid request to the API):
- Incorrect syntax: Double-check the request syntax is correct, including all required parameters and syntax for the chosen model;
- Authentication error: API key is invalid or expired - it also gets a 400 error. Double-check that the API key is still valid and has the necessary permissions.
- Rate limit exceeded: for the API key - it also gets a 400 error. Wait a while and try the request again later.
- Invalid input: If the input to the API is not in the expected format or doesn’t meet the requirements of the model, then it gets a 400 error. Double-check that the input is correct and meets the requirements of the chosen model.