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 => {
12821282 throw new OpenAIError ( 'Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined' ) ;
12831283} ;
12841284
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+
12851306export function isObj ( obj : unknown ) : obj is Record < string , unknown > {
12861307 return obj != null && typeof obj === 'object' && ! Array . isArray ( obj ) ;
12871308}
Original file line number Diff line number Diff line change @@ -37,11 +37,8 @@ export class Embeddings extends APIResource {
3737 return base64Response . _thenUnwrap ( ( response ) => {
3838 if ( response && response . data ) {
3939 response . data . forEach ( ( embeddingBase64Obj ) => {
40- console . log ( embeddingBase64Obj ) ;
4140 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 ) ;
4542 } ) ;
4643 Core . debug ( 'response' , 'Decoded embeddings:' , response . data ) ;
4744 }
You can’t perform that action at this time.
0 commit comments