Skip to content

Commit

Permalink
fix: correct base64 encoding for Uint8Array in encodeImage function (#90
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jzevin authored Jun 5, 2024
1 parent 944c54c commit e575b2b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,24 @@ export class Ollama {
}

/**
* Encodes an image to base64 if it is a Uint8Array.
* @param image {Uint8Array | string} - The image to encode.
* @returns {Promise<string>} - The base64 encoded image.
*/
async encodeImage(image: Uint8Array | string): Promise<string> {
if (typeof image !== 'string') {
// image is Uint8Array convert it to base64
const uint8Array = new Uint8Array(image)
const numberArray = Array.from(uint8Array)
return btoa(String.fromCharCode.apply(null, numberArray))
* Encodes an image to base64 if it is a Uint8Array.
* @param image {Uint8Array | string} - The image to encode.
* @returns {Promise<string>} - The base64 encoded image.
*/
async encodeImage(image: Uint8Array | string): Promise<string> {
if (typeof image !== 'string') {
// image is Uint8Array, convert it to base64
const uint8Array = new Uint8Array(image);
let byteString = '';
const len = uint8Array.byteLength;
for (let i = 0; i < len; i++) {
byteString += String.fromCharCode(uint8Array[i]);
}
// the string may be base64 encoded
return image
return btoa(byteString);
}
// the string may be base64 encoded
return image;
}

generate(
request: GenerateRequest & { stream: true },
Expand Down

0 comments on commit e575b2b

Please sign in to comment.