mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 Support custom AI request User-Agent header https://github.com/siyuan-note/siyuan/issues/10351
This commit is contained in:
parent
3df54adcc5
commit
7e87c8c8ad
11 changed files with 59 additions and 8 deletions
|
|
@ -75,17 +75,32 @@ func ChatGPT(msg string, contextMsgs []string, c *openai.Client, model string, m
|
|||
return
|
||||
}
|
||||
|
||||
func NewOpenAIClient(apiKey, apiProxy, apiBaseURL string) *openai.Client {
|
||||
func NewOpenAIClient(apiKey, apiProxy, apiBaseURL, apiUserAgent string) *openai.Client {
|
||||
config := openai.DefaultConfig(apiKey)
|
||||
transport := &http.Transport{}
|
||||
if "" != apiProxy {
|
||||
proxyUrl, err := url.Parse(apiProxy)
|
||||
if nil != err {
|
||||
logging.LogErrorf("OpenAI API proxy failed: %v", err)
|
||||
} else {
|
||||
config.HTTPClient = &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
|
||||
transport.Proxy = http.ProxyURL(proxyUrl)
|
||||
}
|
||||
}
|
||||
|
||||
config.HTTPClient = &http.Client{Transport: newAddHeaderTransport(transport, apiUserAgent)}
|
||||
config.BaseURL = apiBaseURL
|
||||
return openai.NewClientWithConfig(config)
|
||||
}
|
||||
|
||||
type AddHeaderTransport struct {
|
||||
RoundTripper http.RoundTripper
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
func (adt *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Add("User-Agent", adt.UserAgent)
|
||||
return adt.RoundTripper.RoundTrip(req)
|
||||
}
|
||||
|
||||
func newAddHeaderTransport(transport *http.Transport, userAgent string) *AddHeaderTransport {
|
||||
return &AddHeaderTransport{RoundTripper: transport, UserAgent: userAgent}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue