@@ -11,41 +11,50 @@ export class Embeddings extends APIResource {
11
11
body : EmbeddingCreateParams ,
12
12
options ?: Core . RequestOptions < EmbeddingCreateParams > ,
13
13
) : Core . APIPromise < CreateEmbeddingResponse > {
14
- const userInitialEncodingFormat = body . encoding_format ;
15
14
Core . debug ( 'request' , 'Sending request with arguments:' , { body, ...options } ) ;
16
15
17
- const base64Response = this . _client . post < EmbeddingCreateParams , CreateEmbeddingResponse > ( '/embeddings' , {
16
+ const hasUserProvidedEncodingFormat = body . encoding_format !== undefined ;
17
+ let encoding_format : 'float' | 'base64' = 'float' ; // current API defaults to float
18
+
19
+ if ( hasUserProvidedEncodingFormat === false ) {
20
+ // No encoding_format specified, defaulting to base64 for performance reasons
21
+ // See https://github.com/openai/openai-node/pull/1312
22
+ encoding_format = 'base64' ;
23
+ } else {
24
+ Core . debug ( 'Request' , 'User defined encoding_format:' , body . encoding_format ) ;
25
+ }
26
+
27
+ const response = this . _client . post < EmbeddingCreateParams , CreateEmbeddingResponse > ( '/embeddings' , {
18
28
body : {
19
29
...body ,
20
- // Force base64 encoding for vector embeddings creation
21
- // See https://github.com/openai/openai-node/issues/1310
22
- encoding_format : 'base64' ,
30
+ encoding_format,
23
31
} ,
24
32
...options ,
25
33
} ) ;
26
34
27
- if ( userInitialEncodingFormat === 'base64' ) {
28
- // if the user requested base64 encoding_format, return the response as-is
29
- return base64Response ;
30
- } else {
31
- // we decode the base64 embeddings to float32 array if:
32
- // 1- the user requested 'float' encoding_format,
33
- // 2- the user did not specify an encoding_format (which defaults to 'float') in order to keep backwards compatibility
34
- Core . debug ( 'response' , `User requested encoding_format=${ userInitialEncodingFormat || 'default' } ` ) ;
35
- Core . debug ( 'response' , 'Decoding base64 embeddings to float32 array' ) ;
36
-
37
- return base64Response . _thenUnwrap ( ( response ) => {
38
- if ( response && response . data ) {
39
- response . data . forEach ( ( embeddingBase64Obj ) => {
40
- const embeddingBase64Str = embeddingBase64Obj . embedding as unknown as string ;
41
- embeddingBase64Obj . embedding = Core . toFloat32Array ( embeddingBase64Str ) ;
42
- } ) ;
43
- Core . debug ( 'response' , 'Decoded embeddings:' , response . data ) ;
44
- }
45
-
46
- return response ;
47
- } ) ;
35
+ // if the user specified an encoding_format, return the response as-is
36
+ if ( hasUserProvidedEncodingFormat ) {
37
+ return response ;
48
38
}
39
+
40
+ // in this stage, we are sure the user did not specify an encoding_format
41
+ // and we defaulted to base64 for performance reasons
42
+ // we are sure then that the response is base64 encoded, let's decode it
43
+ // the returned result will be a float32 array since this is OpenAI API's default encoding
44
+ Core . debug ( 'response' , `User requested encoding_format=${ encoding_format || 'default' } ` ) ;
45
+ Core . debug ( 'response' , 'Decoding base64 embeddings to float32 array' ) ;
46
+
47
+ return response . _thenUnwrap ( ( response ) => {
48
+ if ( response && response . data ) {
49
+ response . data . forEach ( ( embeddingBase64Obj ) => {
50
+ const embeddingBase64Str = embeddingBase64Obj . embedding as unknown as string ;
51
+ embeddingBase64Obj . embedding = Core . toFloat32Array ( embeddingBase64Str ) ;
52
+ } ) ;
53
+ Core . debug ( 'response' , 'Decoded embeddings:' , response . data ) ;
54
+ }
55
+
56
+ return response ;
57
+ } ) ;
49
58
}
50
59
}
51
60
0 commit comments