I’m currently using the Azure OpenAI Code Interpreter (Assistant API) with Gpt-4o to summarize file content, including PDFs, Excel files, and other document types. However, I am encountering intermittent issues with text extraction. The error message I receive is:
*”Text extraction from the file is failing intermittently, causing issues with the overall text extraction process.”
*
Sometimes the extraction works perfectly, but at other times, it fails without any clear pattern. This inconsistency is affecting our workflow, and I’m looking for advice on how to troubleshoot or resolve this issue.
Has anyone else experienced similar problems, and if so, how did you address them? Any insights or suggestions would be greatly appreciated.
Thank you!
This is my implementation.
app.post('/createAssistant', (req, res) => {
// Use the multer middleware to handle the file upload
uploadmul(req, res, async (err) => {
if (err) {
console.error('Error uploading file:', err);
return res.status(500).json({ error: err.message });
}
// Extract form data from the request body
const { name, model, instructions } = req.body;
const files = req.files;
if (!files) {
return res.status(400).json({ error: 'No file uploaded.' });
}
try {
const fileUploadPromises = files.map(async (file) => {
// Read the content from the uploaded file in binary mode
const fileContent = await fs.promises.readFile(file.path);
// Define the filename for the upload
const filename = Buffer.from(file.originalname, 'latin1').toString('utf8');
// Upload the binary file content using Assistants SDK
const uploadAssistantFile = await assistantsClient.uploadFile(fileContent, "assistants", { filename });
// Return file id and filename as an object
return { fileId: uploadAssistantFile.id, filename: filename };
});
// Wait for all file uploads to complete
const fileIdsWithNames = await Promise.all(fileUploadPromises);
// Extract fileIds and filenames separately
const fileIds = fileIdsWithNames.map(file => file.fileId);
// Create an assistant using Assistants SDK
const assistantResponse = await assistantsClient.createAssistant({
model: model,
name: name,
instructions: instructions,
tools: [{ type: "code_interpreter" }],
fileIds: fileIds,
});
console.log("Assistant created:", assistantResponse);
// Create a thread using Assistants SDK
const assistantThread = await assistantsClient.createThread({});
console.log("Assistant thread created:", assistantThread);
// Respond with success message
res.json({
success: true,
message: 'Assistant created successfully!',
assistant: assistantResponse,
});
} catch (error) {
console.error('Error creating assistant:', error);
res.status(500).json({ error: 'Error creating assistant.' });
}
});
});
app.post('/ChatWithAssistant', async (req, res) => {
try {
const name = decodeURIComponent(req.headers['x-user-name']);
const userQuestion = decodeURIComponent(req.headers['x-user-question']);
const assistantID = decodeURIComponent(req.headers['x-assistant-id']);
const threadID = decodeURIComponent(req.headers['x-thread-id']);
console.log("== Chat With Assistant ==");
const colonIndex = assistantID.indexOf(':');
const assistantId = assistantID.substring(colonIndex + 2);
console.log("assistantID", assistantId);
console.log("userQuestion", userQuestion);
// Send user's question to OpenAI API
const threadResponse = await assistantsClient.createMessage(threadID, "user", userQuestion);
const response = await assistantsClient.createRun(threadID, { assistantId });
console.log("response", response);
// Poll for run completion
let runResponse;
do {
await new Promise((r) => setTimeout(r, 500));
runResponse = await assistantsClient.getRun(threadID, response.id);
} while (runResponse.status === "queued" || runResponse.status === "in_progress");
// List run messages and output the first message only
let botResponse = '';
const runMessages = await assistantsClient.listMessages(threadID);
for (const runMessageDatum of runMessages.data) {
for (const item of runMessageDatum.content) {
if (item.type === "text") {
botResponse = item.text?.value || '';
break;
}
}
if (botResponse) {
break;
}
}
// Log and send the response back to the client
console.log("Bot Response:", botResponse);
res.json({ botResponse });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: error.message });
}
});