2
2
3
3
from __future__ import annotations
4
4
5
+ import array
5
6
import base64
6
7
from typing import List , Union , Iterable , cast
7
8
from typing_extensions import Literal
@@ -102,7 +103,7 @@ def create(
102
103
"dimensions" : dimensions ,
103
104
"encoding_format" : encoding_format ,
104
105
}
105
- if not is_given (encoding_format ) and has_numpy () :
106
+ if not is_given (encoding_format ):
106
107
params ["encoding_format" ] = "base64"
107
108
108
109
def parser (obj : CreateEmbeddingResponse ) -> CreateEmbeddingResponse :
@@ -113,12 +114,14 @@ def parser(obj: CreateEmbeddingResponse) -> CreateEmbeddingResponse:
113
114
for embedding in obj .data :
114
115
data = cast (object , embedding .embedding )
115
116
if not isinstance (data , str ):
116
- # numpy is not installed / base64 optimisation isn't enabled for this model yet
117
117
continue
118
-
119
- embedding .embedding = np .frombuffer ( # type: ignore[no-untyped-call]
120
- base64 .b64decode (data ), dtype = "float32"
121
- ).tolist ()
118
+ if not has_numpy ():
119
+ # use array for base64 optimisation
120
+ embedding .embedding = array .array ("f" , base64 .b64decode (data )).tolist ()
121
+ else :
122
+ embedding .embedding = np .frombuffer ( # type: ignore[no-untyped-call]
123
+ base64 .b64decode (data ), dtype = "float32"
124
+ ).tolist ()
122
125
123
126
return obj
124
127
@@ -215,7 +218,7 @@ async def create(
215
218
"dimensions" : dimensions ,
216
219
"encoding_format" : encoding_format ,
217
220
}
218
- if not is_given (encoding_format ) and has_numpy () :
221
+ if not is_given (encoding_format ):
219
222
params ["encoding_format" ] = "base64"
220
223
221
224
def parser (obj : CreateEmbeddingResponse ) -> CreateEmbeddingResponse :
@@ -226,12 +229,14 @@ def parser(obj: CreateEmbeddingResponse) -> CreateEmbeddingResponse:
226
229
for embedding in obj .data :
227
230
data = cast (object , embedding .embedding )
228
231
if not isinstance (data , str ):
229
- # numpy is not installed / base64 optimisation isn't enabled for this model yet
230
232
continue
231
-
232
- embedding .embedding = np .frombuffer ( # type: ignore[no-untyped-call]
233
- base64 .b64decode (data ), dtype = "float32"
234
- ).tolist ()
233
+ if not has_numpy ():
234
+ # use array for base64 optimisation
235
+ embedding .embedding = array .array ("f" , base64 .b64decode (data )).tolist ()
236
+ else :
237
+ embedding .embedding = np .frombuffer ( # type: ignore[no-untyped-call]
238
+ base64 .b64decode (data ), dtype = "float32"
239
+ ).tolist ()
235
240
236
241
return obj
237
242
0 commit comments