I am attempting to create a frontend upload interface to upload files into the Assistant API. However, I am constantly getting the same error 400.
If I don’t use a frontend and just put in the path of the files into the backend, I don’t get the error.
Could someone help point me to where I am making the mistake?
Thank you so much!
Frontend
uploadBtn.addEventListener('click', uploadFiles);
async function uploadFiles() {
const input = document.getElementById('uploadFilesInput');
const files = input.files;
const formData = new FormData();
for (const file of files) {
formData.append('files', file);
}
try {
const response = await fetch('/upload-files', {
method: 'POST',
body: formData
});
const result = await response.json();
chatResponse.textContent = result.message;
} catch(error) {
console.error('Error upload files: ', error);
}
}
Backend
app.post('/upload-files', upload.array('files'), async (req, res) => {
try {
//Create File Stream
const files = req.files.map(file => toFile(fs.createReadStream(file.path)));
//Upload files into created vector store
await openai.beta.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, { files });
//Update assistant to use uploaded file
await openai.beta.assistants.update(assistant.id, {
tools: [{ type: 'file_search'}],
tool_resources: {
file_search: { vector_store_ids: [vectorStore.id]}
}
});
res.json({ message: 'Files uploaded successfully. '});
} catch (error) {
console.error('Error uploading files: ', error);
res.status(500).json({ message: 'Failed to upload files.' })
}
})