Skip to content

Commit e575b2b

Browse files
authored
fix: correct base64 encoding for Uint8Array in encodeImage function (#90)
1 parent 944c54c commit e575b2b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/browser.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,24 @@ export class Ollama {
106106
}
107107

108108
/**
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]);
119121
}
120-
// the string may be base64 encoded
121-
return image
122+
return btoa(byteString);
122123
}
124+
// the string may be base64 encoded
125+
return image;
126+
}
123127

124128
generate(
125129
request: GenerateRequest & { stream: true },

0 commit comments

Comments
 (0)