44 "context"
55 "encoding/base64"
66 "encoding/binary"
7+ "encoding/json"
78 "errors"
89 "math"
910 "net/http"
@@ -160,6 +161,9 @@ type EmbeddingRequest struct {
160161 // Dimensions The number of dimensions the resulting output embeddings should have.
161162 // Only supported in text-embedding-3 and later models.
162163 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"`
163167}
164168
165169func (r EmbeddingRequest ) Convert () EmbeddingRequest {
@@ -187,6 +191,9 @@ type EmbeddingRequestStrings struct {
187191 // Dimensions The number of dimensions the resulting output embeddings should have.
188192 // Only supported in text-embedding-3 and later models.
189193 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"`
190197}
191198
192199func (r EmbeddingRequestStrings ) Convert () EmbeddingRequest {
@@ -196,6 +203,7 @@ func (r EmbeddingRequestStrings) Convert() EmbeddingRequest {
196203 User : r .User ,
197204 EncodingFormat : r .EncodingFormat ,
198205 Dimensions : r .Dimensions ,
206+ ExtraBody : r .ExtraBody ,
199207 }
200208}
201209
@@ -219,6 +227,9 @@ type EmbeddingRequestTokens struct {
219227 // Dimensions The number of dimensions the resulting output embeddings should have.
220228 // Only supported in text-embedding-3 and later models.
221229 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"`
222233}
223234
224235func (r EmbeddingRequestTokens ) Convert () EmbeddingRequest {
@@ -228,6 +239,7 @@ func (r EmbeddingRequestTokens) Convert() EmbeddingRequest {
228239 User : r .User ,
229240 EncodingFormat : r .EncodingFormat ,
230241 Dimensions : r .Dimensions ,
242+ ExtraBody : r .ExtraBody ,
231243 }
232244}
233245
@@ -241,11 +253,29 @@ func (c *Client) CreateEmbeddings(
241253 conv EmbeddingRequestConverter ,
242254) (res EmbeddingResponse , err error ) {
243255 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+
244273 req , err := c .newRequest (
245274 ctx ,
246275 http .MethodPost ,
247276 c .fullURL ("/embeddings" , withModel (string (baseReq .Model ))),
248- withBody (baseReq ),
277+ withBody (body ), // Main request body.
278+ withExtraBody (extraBody ), // Merge ExtraBody fields.
249279 )
250280 if err != nil {
251281 return
0 commit comments