const { AzureOpenAI } = require('openai'); const fs = require('fs'); const path = require('path'); // Get environment variables const azureOpenAIKey = "48b4f7XXXXXXXX393664"; const azureOpenAIEndpoint = "https://XXXXXXXX.openai.azure.com"; const azureOpenAIVersion = "2024-08-01-preview"; // Initialize the Azure SDK client const getClient = () => { return new AzureOpenAI({ endpoint: azureOpenAIEndpoint, apiVersion: azureOpenAIVersion, apiKey: azureOpenAIKey, }); }; const assistantsClient = getClient(); // Define the toFile function const toFile = async (stream, filename) => { const chunks = []; for await (const chunk of stream) { chunks.push(chunk); } const buffer = Buffer.concat(chunks); return { name: filename, buffer, size: buffer.length, }; }; // Existing vector store ID const vectorStoreId = "vs_4qu7kRReXm2Q27Q8ZbJlmsgV"; // Upload Files to Existing Vector Store const uploadFilesToExistingVectorStore = async (filePaths) => { try { const fileStreams = filePaths.map(filePath => fs.createReadStream(filePath)); // Upload files using the correct method const uploadedFiles = await Promise.all(fileStreams.map(async (fileStream, index) => { const file = await toFile(fileStream, path.basename(filePaths[index])); return assistantsClient.files.create({ file, purpose: "assistants", }); })); console.log(`Files uploaded: ${uploadedFiles.map(file => file.id).join(', ')}`); } catch (error) { console.error(`Error uploading files: ${error.message}`); } };