diff options
author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 17:41:49 -0400 |
---|---|---|
committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-10-17 17:41:49 -0400 |
commit | c933ae724c1bf77fa8bd83c3c9e27d0029ef5cb0 (patch) | |
tree | df16f68e9bdb822e5f91c997a431c4b130377290 /src/server/ApiManagers/AssistantManager.ts | |
parent | 98d0bba3e59ab7ec9dfbf5e6c9c58e6ac1d22ae3 (diff) | |
parent | dd93f5175064850c6c0e47f025cd7bbba1f23106 (diff) |
Merge branch 'ajs-before-executable' of https://github.com/brown-dash/Dash-Web into ajs-before-executable
Diffstat (limited to 'src/server/ApiManagers/AssistantManager.ts')
-rw-r--r-- | src/server/ApiManagers/AssistantManager.ts | 116 |
1 files changed, 60 insertions, 56 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts index b7d4191ca..8447a4934 100644 --- a/src/server/ApiManagers/AssistantManager.ts +++ b/src/server/ApiManagers/AssistantManager.ts @@ -15,7 +15,6 @@ import * as fs from 'fs'; import { writeFile } from 'fs'; import { google } from 'googleapis'; import { JSDOM } from 'jsdom'; -import OpenAI from 'openai'; import * as path from 'path'; import * as puppeteer from 'puppeteer'; import { promisify } from 'util'; @@ -307,33 +306,35 @@ export default class AssistantManager extends ApiManager { // If the result contains image or table chunks, save the base64 data as image files if (result.chunks && Array.isArray(result.chunks)) { - for (const chunk of result.chunks) { - if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) { - const files_directory = '/files/chunk_images/'; - const directory = path.join(publicDirectory, files_directory); - - // Ensure the directory exists or create it - if (!fs.existsSync(directory)) { - fs.mkdirSync(directory); + await Promise.all( + result.chunks.map(chunk => { + if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) { + const files_directory = '/files/chunk_images/'; + const directory = path.join(publicDirectory, files_directory); + + // Ensure the directory exists or create it + if (!fs.existsSync(directory)) { + fs.mkdirSync(directory); + } + + const fileName = path.basename(chunk.metadata.file_path); // Get the file name from the path + const filePath = path.join(directory, fileName); // Create the full file path + + // Check if the chunk contains base64 encoded data + if (chunk.metadata.base64_data) { + // Decode the base64 data and write it to a file + const buffer = Buffer.from(chunk.metadata.base64_data, 'base64'); + fs.promises.writeFile(filePath, buffer).then(() => { + // Update the file path in the chunk's metadata + chunk.metadata.file_path = path.join(files_directory, fileName); + chunk.metadata.base64_data = undefined; // Remove the base64 data from the metadata + }); + } else { + console.warn(`No base64_data found for chunk: ${fileName}`); + } } - - const fileName = path.basename(chunk.metadata.file_path); // Get the file name from the path - const filePath = path.join(directory, fileName); // Create the full file path - - // Check if the chunk contains base64 encoded data - if (chunk.metadata.base64_data) { - // Decode the base64 data and write it to a file - const buffer = Buffer.from(chunk.metadata.base64_data, 'base64'); - await fs.promises.writeFile(filePath, buffer); - - // Update the file path in the chunk's metadata - chunk.metadata.file_path = path.join(files_directory, fileName); - chunk.metadata.base64_data = undefined; // Remove the base64 data from the metadata - } else { - console.warn(`No base64_data found for chunk: ${fileName}`); - } - } - } + }) + ); result.status = 'completed'; } else { result.status = 'pending'; @@ -355,39 +356,42 @@ export default class AssistantManager extends ApiManager { // Initialize an array to hold the formatted content const content: { type: string; text?: string; image_url?: { url: string } }[] = [{ type: 'text', text: '<chunks>' }]; - for (const chunk of relevantChunks) { - // Format each chunk by adding its metadata and content - content.push({ - type: 'text', - text: `<chunk chunk_id=${chunk.id} chunk_type="${chunk.metadata.type}">`, - }); - - // If the chunk is an image or table, read the corresponding file and encode it as base64 - if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') { - try { - const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path); // Get the file path - const imageBuffer = await readFileAsync(filePath); // Read the image file - const base64Image = imageBuffer.toString('base64'); // Convert the image to base64 - - // Add the base64-encoded image to the content array - if (base64Image) { - content.push({ - type: 'image_url', - image_url: { - url: `data:image/jpeg;base64,${base64Image}`, - }, + await Promise.all( + relevantChunks.map((chunk: { id: string; metadata: { type: string; text: TimeRanges; file_path: string } }) => { + // Format each chunk by adding its metadata and content + content.push({ + type: 'text', + text: `<chunk chunk_id=${chunk.id} chunk_type="${chunk.metadata.type}">`, + }); + + // If the chunk is an image or table, read the corresponding file and encode it as base64 + if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') { + try { + const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path); // Get the file path + readFileAsync(filePath).then(imageBuffer => { + const base64Image = imageBuffer.toString('base64'); // Convert the image to base64 + + // Add the base64-encoded image to the content array + if (base64Image) { + content.push({ + type: 'image_url', + image_url: { + url: `data:image/jpeg;base64,${base64Image}`, + }, + }); + } else { + console.log(`Failed to encode image for chunk ${chunk.id}`); + } }); - } else { - console.log(`Failed to encode image for chunk ${chunk.id}`); + } catch (error) { + console.error(`Error reading image file for chunk ${chunk.id}:`, error); } - } catch (error) { - console.error(`Error reading image file for chunk ${chunk.id}:`, error); } - } - // Add the chunk's text content to the formatted content - content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` }); - } + // Add the chunk's text content to the formatted content + content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` }); + }) + ); content.push({ type: 'text', text: '</chunks>' }); |