I am building a chatbot that responds based on an initial context, but every prompt is answered by the API [object Promise]. How do I get the function to actually extract the text content from the chat completion.
async function getCompletionFromMessages( messages, model = 'gpt-3.5-turbo', temperature = 0 ) {
console.log('clicked');
const options = {
method: 'POST',
headers: {
'Authorization': Bearer ${api_key},
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
messages: messages,
maxTokens: 200,
temperature: temperature
})
}
try {
const completion = await fetch("OPENAI CHAT COMPLETION ENDPOINT", options)
const data = await response.json();
console.log(data);
return data.choices[0].message.content;
} catch {
console.error(error);
}
};
This is the chat completion method I am using to return the bot response, but as I said, it returns [object Promise] for every output. Help needed!
return data.choices[0].message.content.text;
^
1 Like
It still returns [object Promise]
If you are calling this function somewhere else, you should also use await
to get the actual data returned from the function, not the Promise object.
e.g.
async function someFunction() {
let messages = ["hello", "world"]; // or whatever your messages are
let result = await getCompletionFromMessages(messages);
console.log(result); // this should log the data you want
}
*ChatGPT
I have the async function in another file that looks like this and follows the same syntax:
async function getBotResponse(input) {
try {
return await getCompletionFromMessages([...messages, {role: 'user', content: input}])
} catch {
console.error(error)
return "My name is Immanuel"
}
}
Ok, and how do you call that function then?
Do you use the await there as well?
sps
June 25, 2023, 4:47pm
7
The issue in your code lies in the line where you’re trying to extract the JSON response from the fetch request. You’re assigning the response to a variable named completion
, but then you’re trying to extract the JSON from a variable named response
, which doesn’t exist.
2 Likes
I fixed that. Thanks.
Now, it gives the promise fulfilled result, but it is still only showing the Promise. I need to extract result.
Update: It’s saying undefined instead of [object Promise].
Now the Promise is somehow fulfilled, but saying the the result is ‘undefined’.
How do I get this to not happen?
responses.js
async function getCompletionFromMessages( messages, model = 'gpt-3.5-turbo', temperature = 0 ) {
console.log('clicked');
const options = {
method: 'POST',
headers: {
'Authorization': `Bearer ${api_key}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
messages: messages,
maxTokens: 200,
temperature: temperature
})
}
try {
const completion = await fetch("OPENAI CHAT COMPLETION ENDPOINT", options)
const data = await completion.json();
console.log(data);
return data.choices[0].message.content.text;
} catch(error) {
console.log(error);
console.log("Something went wrong.")
}
};
chat.js
async function getHardResponse(userText) {
let botHtml = "";
let botResponse = await getBotResponse(userText);
// console.log(botResponse)
botHtml = '<p class="botText"><span>' + botResponse + '</span></p>'
$('#chatbox').append(botHtml);
messages.push({ role: 'user', content: userText }, { role: 'assistant', content: botResponse });
// console.log(messages);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
}
async function getBotResponse(input) {
try {
return await getCompletionFromMessages([...messages, { role: 'user', content: input }])
} catch (error) {
console.log(error)
}
}
function getResponse() {
let userText = $('#textInput').val();
if (userText == "") {
userText = "What is your name?";
}
let userHtml = '<p class="userText"><span>' + userText + '</span></p>';
document.getElementById("textInput").value = "";
$("#chatbox").append(userHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
setTimeout(async () => {
await getHardResponse(userText);
}, 1000);
}
EricGT
June 25, 2023, 7:06pm
10
FYI
The correct way to enter Markdown code blocks in a Discourse forum is to use
```
while your post are using
/`/`/`
which is not formatting correctly.
1 Like
sps
June 25, 2023, 7:13pm
11
You are getting this error because you are trying to return the completion without checking if it actually has received a response.
You should add a check
e.g.
if(data) {
return data.choices[0].message.content;
}
I’d also recommend that you use Node.js library by OpenAI, and switch to Typescript.
1 Like