Skip to content

Commit

Permalink
fix: Added logs
Browse files Browse the repository at this point in the history
  • Loading branch information
asafgardin committed Dec 4, 2024
1 parent fbb9092 commit 88e8283
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions examples/studio/conversational-rag/rag-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,41 @@ async function waitForFileProcessing(
async function uploadGetUpdateDelete(fileInput, path) {
const client = new AI21({ apiKey: process.env.AI21_API_KEY });
try {
console.log(`Uploading file with id ${fileInput}`);
console.log(`Starting upload for file:`, typeof fileInput);
const uploadFileResponse: UploadFileResponse = await client.files.create({
file: fileInput,
path: path,
});
console.log(`Uploaded file with id ${uploadFileResponse}`);
console.log(`✓ Upload completed. File ID: ${uploadFileResponse.fileId}`);

console.log('Waiting for file processing...');
let file: FileResponse = await waitForFileProcessing(client, uploadFileResponse.fileId);
console.log(file);
console.log(`✓ File processing completed with status: ${file.status}`);

if (file.status === 'PROCESSED') {
console.log('Now updating the file labels and publicUrl...');
console.log('Starting file update...');
await client.files.update({
fileId: uploadFileResponse.fileId,
labels: ['test99'],
publicUrl: 'https://www.miri.com',
});
file = await client.files.get(uploadFileResponse.fileId);
console.log(file);
console.log('✓ File update completed');
} else {
console.log(`File did not processed well, ended with status ${file.status}`);
console.log(`⚠ File processing failed with status ${file.status}`);
return; // Exit early if processing failed
}

console.log('Now deleting the file');
console.log('Starting file deletion...');
await client.files.delete(uploadFileResponse.fileId);
console.log('✓ File deletion completed');

// Add buffer time between operations
await sleep(2000);

} catch (error) {
console.error('Error:', error);
console.error('❌ Error in uploadGetUpdateDelete:', error);
throw error;
}
}

Expand All @@ -71,22 +79,29 @@ if (isBrowser) {
/* Run all operations sequentially */
(async () => {
try {
console.log('=== Starting first operation ===');
// First operation - upload file from path
const filePath = path.join(process.cwd(), 'examples/studio/conversational-rag/files', 'meerkat.txt');
await uploadGetUpdateDelete(filePath, Date.now().toString());
console.log('=== First operation completed ===\n');
await sleep(2000);

console.log('=== Starting second operation ===');
// Second operation - upload file from File instance
const fileContent = Buffer.from(
'Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.',
);
const dummyFile = new File([fileContent], 'example.txt', { type: 'text/plain' });
console.log('Running file upload in Node environment');
await uploadGetUpdateDelete(dummyFile, Date.now().toString());
console.log('=== Second operation completed ===\n');
await sleep(2000);

// Finally, list the files
console.log('=== Starting file listing ===');
await listFiles();
console.log('=== File listing completed ===');
} catch (error) {
console.error(error);
console.error('❌ Main execution error:', error);
process.exit(1); // Exit with error code if something fails
}
})();
}

0 comments on commit 88e8283

Please sign in to comment.