i have built an html page that using node.js and the assistant API. for the last week when i send prompts about 25% of the time i fail to get a response. is this a known problem. any suggestions ? My code looks like this: console.log(Adding message to thread ID ${thread_id}...
);
await openai.beta.threads.messages.create(thread_id, { role: ‘user’, content: message });
console.log("Running the thread...");
const runResponse = await openai.beta.threads.runs.create(thread_id, { assistant_id });
const runId = runResponse.id;
console.log("Run created with ID:", runId);
let runStatus = runResponse.status;
let runResult;
const timeout = 60000; // Timeout duration in milliseconds (e.g., 60 seconds)
const startTime = Date.now(); // Track when polling started
// Poll for completion
while (runStatus === 'queued' || runStatus === 'running') {
console.log("Polling run status:", runStatus);
// Check if the timeout has been exceeded
if (Date.now() - startTime > timeout) {
console.log("Polling timed out");
return {
statusCode: 504, // Gateway Timeout status code
body: JSON.stringify({ error: 'Request timed out' }),
};
}
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds before polling again
try {
runResult = await openai.beta.threads.runs.retrieve(thread_id, runId);
console.log("Poll Result:", runResult);
runStatus = runResult.status;
} catch (pollingError) {
console.error("Error during polling:", pollingError);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Error during polling', details: pollingError.message }),
};
}
}
if (runStatus === 'completed') {
// Retrieve messages from the thread
try {
const threadMessages = await openai.beta.threads.messages.list(thread_id);
const assistantMessage = threadMessages.data[0];
const messageValue = assistantMessage.content[0].text.value;
return {
statusCode: 200,
body: JSON.stringify({ message: messageValue }),
};
} catch (retrievalError) {
console.error("Error retrieving messages:", retrievalError);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Error retrieving messages', details: retrievalError.message }),
};
}
} else {
console.log("Run did not complete successfully:", runResult);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Run did not complete successfully', details: runResult }),
};
}
} catch (error) {
console.error("Error:", error);
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
}