anything i’m missing? each time i send message seems like it’s creating a new thread id or not sticking into 1 thread id, for that reason it’s not remembering the previous messages or the whole conversation.
async function askGPT(question) {
try {
conversationHistory.push({"role": "user", "content": question});
// Retrieve the Assistant
const myAssistant = await openai.beta.assistants.retrieve(assistantId);
console.log(myAssistant); // For debugging
// Create a new thread for each conversation
const threadResponse = await openai.beta.threads.create();
// Add a message to the thread with the user's question
await openai.beta.threads.messages.create(threadResponse.id ,{
role: "user",
content: question
});
// Run the assistant to get a response
const run = await openai.beta.threads.runs.create(
threadResponse.id,
{ assistant_id: assistantId }
);
let runStatus = await openai.beta.threads.runs.retrieve(
threadResponse.id,
run.id
);
// Polling for run completion
while (runStatus.status !== 'completed') {
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
runStatus = await openai.beta.threads.runs.retrieve(threadResponse.id, run.id);
}
// Retrieve the messages after the assistant run is complete
const messagesResponse = await openai.beta.threads.messages.list(threadResponse.id);
const aiMessages = messagesResponse.data.filter(msg => msg.role === 'assistant');
// Assuming the last message is the assistant's response
return aiMessages[aiMessages.length - 1].content[0].text.value;
} catch (error) {
console.error('Error in askGPT:', error.response ? error.response.data : error);
return 'An error occurred while processing your request.'; // Placeholder response
}
}