1
+ const axios = require ( 'axios' ) ;
1
2
const { EModelEndpoint, FileSources } = require ( 'librechat-data-provider' ) ;
2
3
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
+ }
3
25
4
26
/**
5
27
* Encodes and formats the given files.
@@ -26,6 +48,13 @@ async function encodeAndFormat(req, files, endpoint) {
26
48
}
27
49
28
50
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
+ }
29
58
promises . push ( prepareImagePayload ( req , file ) ) ;
30
59
}
31
60
0 commit comments