Yeah, I understand, except I see no problem in my code. But thank you anyway!
// Endpoint for free generation
app.post(‘/api/get-ai-response-free’, async (req, res) => {
const { inputs, startDate } = req.body;
console.log(`Received request with inputs: ${inputs}, startDate: ${startDate}`);
try {
const assistantId = FREE_GENERATION_ASSISTANT_ID;
// Step 1: Create a new thread
const threadResponse = await axios.post(https://api.openai.com/v1/threads
, {}, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY}
,
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
const threadId = threadResponse.data.id;
console.log(‘Created new thread with ID:’, threadId);
// Step 2: Add a message to the thread
await axios.post(https://api.openai.com/v1/threads/${threadId}/messages
, {
role: ‘user’,
content: User's inputs: ${inputs}. They started on: ${startDate}. Please provide advice based on this information.
}, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY}
,
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
// Step 3: Run the assistant
let run_res = await axios.post(https://api.openai.com/v1/threads/${threadId}/runs
, { assistant_id: assistantId }, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY}
,
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
console.log(Running assistant with ID: ${assistantId}, run ID:
, run_res.data.id);
// Wait for the assistant to finish
while (run_res.data.status !== ‘completed’) {
await new Promise(resolve => setTimeout(resolve, 1000));
run_res = await axios.get(https://api.openai.com/v1/threads/${threadId}/runs/${run_res.data.id}
, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY}
,
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
console.log(‘Assistant run status:’, run_res.data.status);
}
// Step 4: Retrieve the response
const finalResponse = await axios.get(https://api.openai.com/v1/threads/${threadId}/messages
, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY}
,
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
const assistantMessage = finalResponse.data.data.find(msg => msg.role === ‘assistant’).content[0];
console.log(‘Received response from assistant:’, assistantMessage.text.value);
res.json({ response: assistantMessage.text.value });