Skip to content

Commit ecd9794

Browse files
author
brain.liu
committed
add extra_body in ChatCompletion
1 parent c4273cb commit ecd9794

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

chat.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ type ChatCompletionRequest struct {
322322
ServiceTier ServiceTier `json:"service_tier,omitempty"`
323323
// Embedded struct for non-OpenAI extensions
324324
ChatCompletionRequestExtensions
325+
// The ExtraBody field allows for the inclusion of arbitrary key-value pairs
326+
// in the request body that may not be explicitly defined in this struct.
327+
ExtraBody map[string]any `json:"extra_body,omitempty"`
325328
}
326329

327330
type StreamOptions struct {
@@ -472,11 +475,28 @@ func (c *Client) CreateChatCompletion(
472475
return
473476
}
474477

478+
// The body map is used to dynamically construct the request payload for the embedding API.
479+
// Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields
480+
// based on their presence, avoiding unnecessary or empty fields in the request.
481+
extraBody := request.ExtraBody
482+
request.ExtraBody = nil
483+
484+
// Serialize baseReq to JSON
485+
jsonData, err := json.Marshal(request)
486+
if err != nil {
487+
return
488+
}
489+
490+
// Deserialize JSON to map[string]any
491+
var body map[string]any
492+
_ = json.Unmarshal(jsonData, &body)
493+
475494
req, err := c.newRequest(
476495
ctx,
477496
http.MethodPost,
478497
c.fullURL(urlSuffix, withModel(request.Model)),
479-
withBody(request),
498+
withBody(body), // Main request body.
499+
withExtraBody(extraBody), // Merge ExtraBody fields.
480500
)
481501
if err != nil {
482502
return

chat_stream.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package openai
22

33
import (
44
"context"
5+
"encoding/json"
56
"net/http"
67
)
78

@@ -91,11 +92,28 @@ func (c *Client) CreateChatCompletionStream(
9192
return
9293
}
9394

95+
// The body map is used to dynamically construct the request payload for the embedding API.
96+
// Instead of relying on a fixed struct, the body map allows for flexible inclusion of fields
97+
// based on their presence, avoiding unnecessary or empty fields in the request.
98+
extraBody := request.ExtraBody
99+
request.ExtraBody = nil
100+
101+
// Serialize baseReq to JSON
102+
jsonData, err := json.Marshal(request)
103+
if err != nil {
104+
return
105+
}
106+
107+
// Deserialize JSON to map[string]any
108+
var body map[string]any
109+
_ = json.Unmarshal(jsonData, &body)
110+
94111
req, err := c.newRequest(
95112
ctx,
96113
http.MethodPost,
97114
c.fullURL(urlSuffix, withModel(request.Model)),
98-
withBody(request),
115+
withBody(body), // Main request body.
116+
withExtraBody(extraBody), // Merge ExtraBody fields.
99117
)
100118
if err != nil {
101119
return nil, err

0 commit comments

Comments
 (0)