-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support extra_body parameters #1069
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
base: master
Are you sure you want to change the base?
Changes from all commits
9626571
e373ca0
c18b4e2
2112a9d
ed7f01d
095ee5f
a727da4
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -325,6 +325,11 @@ type ChatCompletionRequest struct { | |||||||||||
// We recommend hashing their username or email address, in order to avoid sending us any identifying information. | ||||||||||||
// https://platform.openai.com/docs/api-reference/chat/create#chat_create-safety_identifier | ||||||||||||
SafetyIdentifier string `json:"safety_identifier,omitempty"` | ||||||||||||
// ExtraBody provides configuration options for the generation process in Gemini API. | ||||||||||||
// Additional configuration parameters to control model behavior. Will be passed directly to the Gemini API. | ||||||||||||
// Such as thinking mode for Gemini. "extra_body": {"google": {"thinking_config": {"include_thoughts": true}}} | ||||||||||||
// https://ai.google.dev/gemini-api/docs/openai | ||||||||||||
ExtraBody map[string]any `json:"extra_body,omitempty"` | ||||||||||||
// Embedded struct for non-OpenAI extensions | ||||||||||||
ChatCompletionRequestExtensions | ||||||||||||
} | ||||||||||||
|
@@ -477,11 +482,28 @@ func (c *Client) CreateChatCompletion( | |||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
// The body map is used to dynamically construct the request payload for the chat completion 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 := request.ExtraBody | ||||||||||||
request.ExtraBody = nil | ||||||||||||
|
||||||||||||
// Serialize request to JSON | ||||||||||||
jsonData, err := json.Marshal(request) | ||||||||||||
if err != nil { | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Deserialize JSON to map[string]any | ||||||||||||
var body map[string]any | ||||||||||||
_ = json.Unmarshal(jsonData, &body) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The json.Unmarshal error is being ignored with blank identifier. This could cause silent failures if the JSON unmarshaling fails. The error should be checked and returned.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
|
||||||||||||
req, err := c.newRequest( | ||||||||||||
ctx, | ||||||||||||
http.MethodPost, | ||||||||||||
c.fullURL(urlSuffix, withModel(request.Model)), | ||||||||||||
withBody(request), | ||||||||||||
withBody(body), // Main request body. | ||||||||||||
withExtraBody(extraBody), // Merge ExtraBody fields. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you are using this correctly:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your inspection. I have seen it working correctly during testing and use. For example:
After the request is processed as the body, it will change to:
For the gemini example, I'm sorry that I didn't give a clear example that caused your misunderstanding. In Gemini's official API, they have additional support for extra_body fields. Let me explain the example call in the documentation:
They use OpenAI's official python library OpenAI. In OpenAI extra_body will be promoted to root. The final body is:
So Gemini's example is actually to ensure that extra_body is still under extra_body after being extracted to root, so there is an extra layer of extra_body.
|
||||||||||||
) | ||||||||||||
if err != nil { | ||||||||||||
return | ||||||||||||
|
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.
The comment describes general benefits of using a map but doesn't explain the specific purpose of this code block, which is to handle ExtraBody field merging. Consider updating to: 'Create a dynamic request body by merging ExtraBody fields into the main request payload.'
Copilot uses AI. Check for mistakes.