File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -1282,6 +1282,27 @@ export const toBase64 = (str: string | null | undefined): string => {
1282
1282
throw new OpenAIError ( 'Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined' ) ;
1283
1283
} ;
1284
1284
1285
+ /**
1286
+ * Converts a Base64 encoded string to a Float32Array.
1287
+ * @param base64Str - The Base64 encoded string.
1288
+ * @returns An Array of numbers interpreted as Float32 values.
1289
+ */
1290
+ export const toFloat32Array = ( base64Str : string ) : Array < number > => {
1291
+ if ( typeof Buffer !== 'undefined' ) {
1292
+ // for Node.js environment
1293
+ return Array . from ( new Float32Array ( Buffer . from ( base64Str , 'base64' ) . buffer ) ) ;
1294
+ } else {
1295
+ // for legacy web platform APIs
1296
+ const binaryStr = atob ( base64Str ) ;
1297
+ const len = binaryStr . length ;
1298
+ const bytes = new Uint8Array ( len ) ;
1299
+ for ( let i = 0 ; i < len ; i ++ ) {
1300
+ bytes [ i ] = binaryStr . charCodeAt ( i ) ;
1301
+ }
1302
+ return Array . from ( new Float32Array ( bytes . buffer ) ) ;
1303
+ }
1304
+ } ;
1305
+
1285
1306
export function isObj ( obj : unknown ) : obj is Record < string , unknown > {
1286
1307
return obj != null && typeof obj === 'object' && ! Array . isArray ( obj ) ;
1287
1308
}
Original file line number Diff line number Diff line change @@ -37,11 +37,8 @@ export class Embeddings extends APIResource {
37
37
return base64Response . _thenUnwrap ( ( response ) => {
38
38
if ( response && response . data ) {
39
39
response . data . forEach ( ( embeddingBase64Obj ) => {
40
- console . log ( embeddingBase64Obj ) ;
41
40
const embeddingBase64Str = embeddingBase64Obj . embedding as unknown as string ;
42
- embeddingBase64Obj . embedding = Array . from (
43
- new Float32Array ( Buffer . from ( embeddingBase64Str , 'base64' ) . buffer ) ,
44
- ) ;
41
+ embeddingBase64Obj . embedding = Core . toFloat32Array ( embeddingBase64Str ) ;
45
42
} ) ;
46
43
Core . debug ( 'response' , 'Decoded embeddings:' , response . data ) ;
47
44
}
You can’t perform that action at this time.
0 commit comments