I used the code below for an Express app. However, it throws an error around the messages property in the openai.createCompletion function. Does anyone have an idea?
let conversation = [
{
role: 'system',
content: 'You will follow the conversation and respond to the queries asked by the \'user\'\'s content. You will act as the assistant'
}
];
app.post('/', async(req, res) => {
try {
const prompt = req.body.prompt;
conversation.push({
role: 'user',
content: prompt
})
const response = await openai.createCompletion({
model: 'text-davinci-003',
// messages: conversation.map(({ role, content }) => ({ role, content })),
messages: conversation
});
conversation.push({
role: 'assistant',
content: response.data.choices[0].text
})
res.status(200).send({
bot: response.data.choices[0].text
});
} catch (error) {
console.error(error)
res.status(500).send(error || 'Something went wrong');
}
})
Davinci-003 does not have a chat mode, that is for conversational models such as gpt-3.5-turbo and gpt-4, try changing the model name to ‘gpt-3.5-turbo’
I modified to the code below, but I still get this error (Error: Request failed with status code 404
)
Code:
let conversation = [
{
role: 'system',
content: "You will follow the conversation and respond to the queries asked by the 'user's content. You will act as the assistant"
}
];
app.post('/chat', async (req, res) => {
try {
// prompt+='user: ' +req.body.prompt + '\n'
let prompt = req.body.prompt
conversation.push(
{
role: 'user',
content: prompt
}
)
const response = await openai.createCompletion({
model: "gpt-3.5-turbo",
messages: conversation,
max_tokens:500
});
// prompt += 'assistant: ' + response.data.choices[0].text.trim() + '\n\n'
// console.log(prompt)
conversation.push(
{
role: 'assistant',
content: response.data.choices[0].text
})
res.status(200).send({
bot: response.data.choices[0].text
});
} catch (error) {
console.error(error)
res.status(500).send(error || 'Something went wrong');
}
})```
Looks like you are calling the old creatcompletion function, try it like this
const completion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: 'what is the first online game to be played competitively?'
}
]
});
1 Like