-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support for extra_body parameter for embeddings API #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8730782
5b15527
88775cf
b69335b
2e5a45e
fc52951
a30d2f8
745703a
e0ba3d6
bc7a940
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |||||||||
| "context" | ||||||||||
| "encoding/base64" | ||||||||||
| "encoding/binary" | ||||||||||
| "encoding/json" | ||||||||||
| "errors" | ||||||||||
| "math" | ||||||||||
| "net/http" | ||||||||||
|
|
@@ -160,6 +161,9 @@ type EmbeddingRequest struct { | |||||||||
| // Dimensions The number of dimensions the resulting output embeddings should have. | ||||||||||
| // Only supported in text-embedding-3 and later models. | ||||||||||
| Dimensions int `json:"dimensions,omitempty"` | ||||||||||
| // The ExtraBody field allows for the inclusion of arbitrary key-value pairs | ||||||||||
| // in the request body that may not be explicitly defined in this struct. | ||||||||||
| ExtraBody map[string]any `json:"extra_body,omitempty"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (r EmbeddingRequest) Convert() EmbeddingRequest { | ||||||||||
|
|
@@ -187,6 +191,9 @@ type EmbeddingRequestStrings struct { | |||||||||
| // Dimensions The number of dimensions the resulting output embeddings should have. | ||||||||||
| // Only supported in text-embedding-3 and later models. | ||||||||||
| Dimensions int `json:"dimensions,omitempty"` | ||||||||||
| // The ExtraBody field allows for the inclusion of arbitrary key-value pairs | ||||||||||
| // in the request body that may not be explicitly defined in this struct. | ||||||||||
| ExtraBody map[string]any `json:"extra_body,omitempty"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (r EmbeddingRequestStrings) Convert() EmbeddingRequest { | ||||||||||
|
|
@@ -196,6 +203,7 @@ func (r EmbeddingRequestStrings) Convert() EmbeddingRequest { | |||||||||
| User: r.User, | ||||||||||
| EncodingFormat: r.EncodingFormat, | ||||||||||
| Dimensions: r.Dimensions, | ||||||||||
| ExtraBody: r.ExtraBody, | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -219,6 +227,9 @@ type EmbeddingRequestTokens struct { | |||||||||
| // Dimensions The number of dimensions the resulting output embeddings should have. | ||||||||||
| // Only supported in text-embedding-3 and later models. | ||||||||||
| Dimensions int `json:"dimensions,omitempty"` | ||||||||||
| // The ExtraBody field allows for the inclusion of arbitrary key-value pairs | ||||||||||
| // in the request body that may not be explicitly defined in this struct. | ||||||||||
| ExtraBody map[string]any `json:"extra_body,omitempty"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (r EmbeddingRequestTokens) Convert() EmbeddingRequest { | ||||||||||
|
|
@@ -228,6 +239,7 @@ func (r EmbeddingRequestTokens) Convert() EmbeddingRequest { | |||||||||
| User: r.User, | ||||||||||
| EncodingFormat: r.EncodingFormat, | ||||||||||
| Dimensions: r.Dimensions, | ||||||||||
| ExtraBody: r.ExtraBody, | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -241,11 +253,29 @@ func (c *Client) CreateEmbeddings( | |||||||||
| conv EmbeddingRequestConverter, | ||||||||||
| ) (res EmbeddingResponse, err error) { | ||||||||||
| baseReq := conv.Convert() | ||||||||||
|
|
||||||||||
| // The body map is used to dynamically construct the request payload for the embedding API. | ||||||||||
| // Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields | ||||||||||
| // based on their presence, avoiding unnecessary or empty fields in the request. | ||||||||||
| extraBody := baseReq.ExtraBody | ||||||||||
| baseReq.ExtraBody = nil | ||||||||||
|
|
||||||||||
| // Serialize baseReq to JSON | ||||||||||
| jsonData, err := json.Marshal(baseReq) | ||||||||||
| if err != nil { | ||||||||||
| return | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Deserialize JSON to map[string]any | ||||||||||
| var body map[string]any | ||||||||||
| _ = json.Unmarshal(jsonData, &body) | ||||||||||
|
||||||||||
| _ = json.Unmarshal(jsonData, &body) | |
| if err := json.Unmarshal(jsonData, &body); err != nil { | |
| return res, err | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AyushSawant18588 just some tech debt :)
Uh oh!
There was an error while loading. Please reload this page.