hello,
i was trying simple req/res from chatGPT openai api and i got this:
data: {
id: ‘cmpl-6t0toKrE5sSwubu4uGmg5iURXyT30’,
object: ‘text_completion’,
created: 1678569516,
model: ‘text-davinci-003’,
choices: [ [Object] ],
usage: { prompt_tokens: 1, completion_tokens: 16, total_tokens: 17 }
any idea how to return the response? it should be in choices
code i use below:
const { Configuration, OpenAIApi } = require(“openai”);
const configuration = new Configuration({
apiKey: ‘*********’
});
const openai = new OpenAIApi(configuration);
async function runChatGpt() {
const response = await openai.createCompletion({
model: “text-davinci-003”,
prompt: “hi”
});
console.log(response);
}
runChatGpt();
To start, it sounds like your think you’re using the ChatGPT API but you’re actually using text-davinci-003
, which may be causing some of your confusion.
thanks, but why i got choices: [ [Object] ], ? why the choices is undefined?
What you’re describing is correct for the model you’re using. You have to access the first object with choices[0].
thank you again, but i dont think so, as when i tried for example : “console.log(response.choices[0].text);”
i got an error that : Cannot read properties of undefined (reading ‘0’) because the choices object is undefined
i have edit the code to this , and now its working
const { Configuration, OpenAIApi } = require(“openai”);
const config = new Configuration({
apiKey: “*******”,
});
const openai = new OpenAIApi(config);
const runChat = async () => {
const prompt = “hi”;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: 2048,
temperature: .6,
});
console.log(response.data.choices[0].text);
};
runChat();
Correct (above)
You @sarah.salah.rizk were mistakenly trying to access the text before:
sarah.salah.rizk:
response.choices[0].text
Glad you figured it out @sarah.salah.rizk