Skip to content

Commit c569a06

Browse files
authored
📷 fix: Pass Base64 to Gemini Vision Payload when using CDN URLs (danny-avila#1705)
1 parent 3c90b6a commit c569a06

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

api/server/services/Files/images/encode.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
const axios = require('axios');
12
const { EModelEndpoint, FileSources } = require('librechat-data-provider');
23
const { getStrategyFunctions } = require('../strategies');
4+
const { logger } = require('~/config');
5+
6+
/**
7+
* Fetches an image from a URL and returns its base64 representation.
8+
*
9+
* @async
10+
* @param {string} url The URL of the image.
11+
* @returns {Promise<string>} The base64-encoded string of the image.
12+
* @throws {Error} If there's an issue fetching the image or encoding it.
13+
*/
14+
async function fetchImageToBase64(url) {
15+
try {
16+
const response = await axios.get(url, {
17+
responseType: 'arraybuffer',
18+
});
19+
return Buffer.from(response.data).toString('base64');
20+
} catch (error) {
21+
logger.error('Error fetching image to convert to base64', error);
22+
throw error;
23+
}
24+
}
325

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

2850
encodingMethods[source] = prepareImagePayload;
51+
52+
/* Google doesn't support passing URLs to payload */
53+
if (source !== FileSources.local && endpoint === EModelEndpoint.google) {
54+
const [_file, imageURL] = await prepareImagePayload(req, file);
55+
promises.push([_file, await fetchImageToBase64(imageURL)]);
56+
continue;
57+
}
2958
promises.push(prepareImagePayload(req, file));
3059
}
3160

0 commit comments

Comments
 (0)