Skip to content

Commit

Permalink
Add a custom header field to the ChatCompletionRequest function for s…
Browse files Browse the repository at this point in the history
…etting customer's own header information.
  • Loading branch information
DavadDi committed Aug 1, 2023
1 parent 71a2493 commit 7c4d433
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openai
import (
"context"
"errors"

"net/http"
)

Expand Down Expand Up @@ -88,6 +89,7 @@ type ChatCompletionRequest struct {
User string `json:"user,omitempty"`
Functions []FunctionDefinition `json:"functions,omitempty"`
FunctionCall any `json:"function_call,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}

type FunctionDefinition struct {
Expand Down Expand Up @@ -160,11 +162,17 @@ func (c *Client) CreateChatCompletion(
return
}

headers := request.Headers
request.Headers = nil // 保持兼容,设置为空
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL(urlSuffix, request.Model), withBody(request))
if err != nil {
return
}

for header, value := range headers {
req.Header.Set(header, value)
}

err = c.sendRequest(req, &response)
return
}
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ func (c *Client) setCommonHeaders(req *http.Request) {
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
} else {
// OpenAI or Azure AD authentication
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
if c.config.authToken != ""{
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
}

}
if c.config.OrgID != "" {
req.Header.Set("OpenAI-Organization", c.config.OrgID)
Expand Down

0 comments on commit 7c4d433

Please sign in to comment.