Hello,
I have the following code. It used to work, but now it always return Waiting... Status: in_progress
even for a very small file. Does anyone know what might be the problem?
Thank you
Timur
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function createFile(filePath) {
let result;
if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
// Download the file content from the URL
const res = await fetch(filePath);
const buffer = await res.buffer(); // for Node.js, use .buffer()
const urlParts = filePath.split("/");
const fileName = urlParts[urlParts.length - 1];
// Write buffer to a temporary file
const tempPath = `./tmp_${fileName}`;
fs.writeFileSync(tempPath, buffer);
result = await openai.files.create({
file: fs.createReadStream(tempPath),
purpose: "assistants",
});
// Optional: clean up temp file
fs.unlinkSync(tempPath);
} else {
// Handle local file path
const fileContent = fs.createReadStream(filePath);
result = await openai.files.create({
file: fileContent,
purpose: "assistants",
});
}
return result.id;
}
// Example usage
const fileId = await createFile(
// "https://cdn.openai.com/API/docs/deep_research_blog.pdf"
// "./documents/a_special_file.pdf"
"./documents/a_special_file.txt"
);
console.log(fileId);
// Create a vector store
const vectorStore = await openai.vectorStores.create({
// name: "a_special_file",
name: "a_special_file",
});
console.log(vectorStore.id);
// Add the file to the vector store
try {
const fileAddResponse = await openai.vectorStores.files.create(vectorStore.id, { file_id: fileId });
console.log("File successfully added to vector store:", JSON.stringify(fileAddResponse, null, 2));
} catch (error) {
console.error("Error adding file to vector store:", error.toString());
throw error;
}
async function waitForFileToBeProcessed(vectorStoreId, fileId, timeoutMs = 160000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
let fileList;
try {
fileList = await openai.vectorStores.files.list(vectorStoreId);
console.log("Current file list in vector store:", JSON.stringify(fileList, null, 2));
} catch (error) {
console.error("Error listing files in vector store:", error.toString());
throw error;
}
console.log("Full file list just before timeout:", fileList);
const fileEntry = fileList.data.find(f => f.id === fileId);
console.log(`Waiting... Status: ${fileEntry?.status} (${((Date.now() - start) / 1000).toFixed(1)}s elapsed)`);
if (fileEntry && fileEntry.status === 'completed') {
return true;
}
if (fileEntry && fileEntry.status === 'failed') {
throw new Error(`File processing failed: ${fileEntry.last_error?.message || 'unknown error'}`);
}
await new Promise(r => setTimeout(r, 2000)); // wait 2 seconds before next check
}
throw new Error('Timed out waiting for file to be processed.');
}
await waitForFileToBeProcessed(vectorStore.id, fileId);
// Check status
const result = await openai.vectorStores.files.list(vectorStore.id);
console.log(result);
////////// file search end