Skip to content

Commit 8863099

Browse files
committed
chore: default to base64 if user didn't provide encoding_format
Closes #1310
1 parent 8750a70 commit 8863099

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

src/resources/embeddings.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,50 @@ export class Embeddings extends APIResource {
1111
body: EmbeddingCreateParams,
1212
options?: Core.RequestOptions<EmbeddingCreateParams>,
1313
): Core.APIPromise<CreateEmbeddingResponse> {
14-
const userInitialEncodingFormat = body.encoding_format;
1514
Core.debug('request', 'Sending request with arguments:', { body, ...options });
1615

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', {
1828
body: {
1929
...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,
2331
},
2432
...options,
2533
});
2634

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;
4838
}
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+
});
4958
}
5059
}
5160

0 commit comments

Comments
 (0)