@@ -106,20 +106,24 @@ export class Ollama {
106
106
}
107
107
108
108
/**
109
- * Encodes an image to base64 if it is a Uint8Array.
110
- * @param image {Uint8Array | string} - The image to encode.
111
- * @returns {Promise<string> } - The base64 encoded image.
112
- */
113
- async encodeImage ( image : Uint8Array | string ) : Promise < string > {
114
- if ( typeof image !== 'string' ) {
115
- // image is Uint8Array convert it to base64
116
- const uint8Array = new Uint8Array ( image )
117
- const numberArray = Array . from ( uint8Array )
118
- return btoa ( String . fromCharCode . apply ( null , numberArray ) )
109
+ * Encodes an image to base64 if it is a Uint8Array.
110
+ * @param image {Uint8Array | string} - The image to encode.
111
+ * @returns {Promise<string> } - The base64 encoded image.
112
+ */
113
+ async encodeImage ( image : Uint8Array | string ) : Promise < string > {
114
+ if ( typeof image !== 'string' ) {
115
+ // image is Uint8Array, convert it to base64
116
+ const uint8Array = new Uint8Array ( image ) ;
117
+ let byteString = '' ;
118
+ const len = uint8Array . byteLength ;
119
+ for ( let i = 0 ; i < len ; i ++ ) {
120
+ byteString += String . fromCharCode ( uint8Array [ i ] ) ;
119
121
}
120
- // the string may be base64 encoded
121
- return image
122
+ return btoa ( byteString ) ;
122
123
}
124
+ // the string may be base64 encoded
125
+ return image ;
126
+ }
123
127
124
128
generate (
125
129
request : GenerateRequest & { stream : true } ,
0 commit comments