Skip to content

Commit

Permalink
📷 fix: Pass Base64 to Gemini Vision Payload when using CDN URLs (dann…
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-avila authored Feb 2, 2024
1 parent 454ba47 commit fbbe24b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions api/server/services/Files/images/encode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const axios = require('axios');
const { EModelEndpoint, FileSources } = require('librechat-data-provider');
const { getStrategyFunctions } = require('../strategies');
const { logger } = require('~/config');

/**
* Fetches an image from a URL and returns its base64 representation.
*
* @async
* @param {string} url The URL of the image.
* @returns {Promise<string>} The base64-encoded string of the image.
* @throws {Error} If there's an issue fetching the image or encoding it.
*/
async function fetchImageToBase64(url) {
try {
const response = await axios.get(url, {
responseType: 'arraybuffer',
});
return Buffer.from(response.data).toString('base64');
} catch (error) {
logger.error('Error fetching image to convert to base64', error);
throw error;
}
}

/**
* Encodes and formats the given files.
Expand All @@ -26,6 +48,13 @@ async function encodeAndFormat(req, files, endpoint) {
}

encodingMethods[source] = prepareImagePayload;

/* Google doesn't support passing URLs to payload */
if (source !== FileSources.local && endpoint === EModelEndpoint.google) {
const [_file, imageURL] = await prepareImagePayload(req, file);
promises.push([_file, await fetchImageToBase64(imageURL)]);
continue;
}
promises.push(prepareImagePayload(req, file));
}

Expand Down

0 comments on commit fbbe24b

Please sign in to comment.