Its answering from file its fine. But I have many Assistant and the every user will answer from selected file.
Like I have two assistant ( A1, A2 ) and three file ( F1, F2 F3) A1 will answer from F1 and A2 will answer from F1 and F2.
How to achieve that.
const askQuestion = async (fileId, question, retryCount = 0) => {
// Constructing the prompt for OpenAI API
const prompt = Answer the question based on the file content (ID: ${fileId}): ${question}
;
try {
// Sending request to OpenAI API
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: question }
],
prompt: prompt, // Include the constructed prompt
max_tokens: 100 // Optional: You can adjust this as needed
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`
}
});
return response.data.choices[0].text;
} catch (error) {
console.log(error);
if (error.response && error.response.status === 429 && retryCount < 3) {
const backoffTime = exponentialBackoff(retryCount);
// console.log(`Rate limit exceeded. Retrying after ${backoffTime}ms...`);
await new Promise(resolve => setTimeout(resolve, backoffTime));
return askQuestion(fileId, question, retryCount + 1); // Retry recursively
} else {
throw error; // Throw if retries are exhausted or for non-rate limit errors
}
}
};
app.post(‘/ask’, async (req, res) => {
const { fileId, question } = req.body;
// Check if fileId and question are provided
if (!fileId || !question) {
return res.status(400).send('fileId and question are required');
}
try {
const answer = await askQuestion(fileId, question);
res.json({ answer });
} catch (error) {
console.error('Error asking question:', error);
res.status(500).json({ error: 'Failed to get response from OpenAI API' });
}
});