4
4
"context"
5
5
"encoding/base64"
6
6
"encoding/binary"
7
+ "encoding/json"
7
8
"errors"
8
9
"math"
9
10
"net/http"
@@ -160,6 +161,9 @@ type EmbeddingRequest struct {
160
161
// Dimensions The number of dimensions the resulting output embeddings should have.
161
162
// Only supported in text-embedding-3 and later models.
162
163
Dimensions int `json:"dimensions,omitempty"`
164
+ // The ExtraBody field allows for the inclusion of arbitrary key-value pairs
165
+ // in the request body that may not be explicitly defined in this struct.
166
+ ExtraBody map [string ]any `json:"extra_body,omitempty"`
163
167
}
164
168
165
169
func (r EmbeddingRequest ) Convert () EmbeddingRequest {
@@ -187,6 +191,9 @@ type EmbeddingRequestStrings struct {
187
191
// Dimensions The number of dimensions the resulting output embeddings should have.
188
192
// Only supported in text-embedding-3 and later models.
189
193
Dimensions int `json:"dimensions,omitempty"`
194
+ // The ExtraBody field allows for the inclusion of arbitrary key-value pairs
195
+ // in the request body that may not be explicitly defined in this struct.
196
+ ExtraBody map [string ]any `json:"extra_body,omitempty"`
190
197
}
191
198
192
199
func (r EmbeddingRequestStrings ) Convert () EmbeddingRequest {
@@ -196,6 +203,7 @@ func (r EmbeddingRequestStrings) Convert() EmbeddingRequest {
196
203
User : r .User ,
197
204
EncodingFormat : r .EncodingFormat ,
198
205
Dimensions : r .Dimensions ,
206
+ ExtraBody : r .ExtraBody ,
199
207
}
200
208
}
201
209
@@ -219,6 +227,9 @@ type EmbeddingRequestTokens struct {
219
227
// Dimensions The number of dimensions the resulting output embeddings should have.
220
228
// Only supported in text-embedding-3 and later models.
221
229
Dimensions int `json:"dimensions,omitempty"`
230
+ // The ExtraBody field allows for the inclusion of arbitrary key-value pairs
231
+ // in the request body that may not be explicitly defined in this struct.
232
+ ExtraBody map [string ]any `json:"extra_body,omitempty"`
222
233
}
223
234
224
235
func (r EmbeddingRequestTokens ) Convert () EmbeddingRequest {
@@ -228,6 +239,7 @@ func (r EmbeddingRequestTokens) Convert() EmbeddingRequest {
228
239
User : r .User ,
229
240
EncodingFormat : r .EncodingFormat ,
230
241
Dimensions : r .Dimensions ,
242
+ ExtraBody : r .ExtraBody ,
231
243
}
232
244
}
233
245
@@ -241,11 +253,29 @@ func (c *Client) CreateEmbeddings(
241
253
conv EmbeddingRequestConverter ,
242
254
) (res EmbeddingResponse , err error ) {
243
255
baseReq := conv .Convert ()
256
+
257
+ // The body map is used to dynamically construct the request payload for the embedding API.
258
+ // Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields
259
+ // based on their presence, avoiding unnecessary or empty fields in the request.
260
+ extraBody := baseReq .ExtraBody
261
+ baseReq .ExtraBody = nil
262
+
263
+ // Serialize baseReq to JSON
264
+ jsonData , err := json .Marshal (baseReq )
265
+ if err != nil {
266
+ return
267
+ }
268
+
269
+ // Deserialize JSON to map[string]any
270
+ var body map [string ]any
271
+ _ = json .Unmarshal (jsonData , & body )
272
+
244
273
req , err := c .newRequest (
245
274
ctx ,
246
275
http .MethodPost ,
247
276
c .fullURL ("/embeddings" , withModel (string (baseReq .Model ))),
248
- withBody (baseReq ),
277
+ withBody (body ), // Main request body.
278
+ withExtraBody (extraBody ), // Merge ExtraBody fields.
249
279
)
250
280
if err != nil {
251
281
return
0 commit comments