Skip to content
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

Can add custom headers? #688

Open
zjvn opened this issue Mar 21, 2024 · 3 comments
Open

Can add custom headers? #688

zjvn opened this issue Mar 21, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@zjvn
Copy link

zjvn commented Mar 21, 2024

I use a third party proxy for openai and they require some custom headers to verify my identity.
Some third parties that process OpenAI may require additional headers to perform some verification on the basis of complying with OpenAI rules. For example, if I preset some tools on a third-party platform, I need to specify certain headers to tell him where the tools I need are.

@zjvn zjvn added the enhancement New feature or request label Mar 21, 2024
@sashabaranov
Copy link
Owner

@zjvn you can customize it withHTTPClient field in ClientConfig

@savishy
Copy link

savishy commented May 17, 2024

I had the same problem. The following should be a quick and dirty example for how to do this.

type myTransport struct{}

// custom transport to add header
func (t *myTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	req.Header.Add("X-My-Header", "my-header-value")
	return http.DefaultTransport.RoundTrip(req)
}

func main() {
	// create custom http client using myTransport.
	httpClient := &http.Client{Transport: &myTransport{}}
	config := openai.DefaultAzureConfig("API-Token", "URL")
	config.HTTPClient = httpClient
        ....

@appleboy
Copy link
Contributor

Try the following code:

package openai

import (
	"net/http"
	"strings"
)

// DefaultHeaderTransport is an http.RoundTripper that adds the given headers to
type DefaultHeaderTransport struct {
	Origin http.RoundTripper
	Header http.Header
}

// RoundTrip implements the http.RoundTripper interface.
func (t *DefaultHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	for key, values := range t.Header {
		for _, value := range values {
			req.Header.Add(key, value)
		}
	}
	return t.Origin.RoundTrip(req)
}

// NewHeaders creates a new http.Header from the given slice of headers.
func NewHeaders(headers []string) http.Header {
	h := make(http.Header)
	for _, header := range headers {
		// split header into key and value with = as delimiter
		vals := strings.Split(header, "=")
		if len(vals) != 2 {
			continue
		}
		h.Add(vals[0], vals[1])
	}
	return h
}

See https://github.com/appleboy/CodeGPT/blob/66d623dc4bf4c98c0d90088e602f1d5730a2bb5c/openai/client.go#L8-L36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants