From 5f6d4311363126c4b6b627cd48e7a561079ff1f9 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 2 Feb 2024 01:37:40 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=B7=20fix:=20Pass=20Base64=20to=20Gemi?= =?UTF-8?q?ni=20Vision=20Payload=20when=20using=20CDN=20URLs=20(#1705)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/services/Files/images/encode.js | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/api/server/services/Files/images/encode.js b/api/server/services/Files/images/encode.js index 809ec0e840..d8dc5839ad 100644 --- a/api/server/services/Files/images/encode.js +++ b/api/server/services/Files/images/encode.js @@ -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} 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. @@ -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)); }