From 0fb98e44a7dc4cd816777c4271fe44dddff2a87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=91=E7=A7=A6=E5=A7=8B=E7=9A=87?= Date: Mon, 18 Mar 2024 02:11:21 -0700 Subject: [PATCH 1/2] fix: make the 'openai_organization' parameter actually work. --- middleware/distributor.go | 3 +++ relay/channel/openai/adaptor.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/middleware/distributor.go b/middleware/distributor.go index ed457a3..0cb9658 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -155,6 +155,9 @@ func Distribute() func(c *gin.Context) { if channel.AutoBan != nil && *channel.AutoBan == 0 { ban = false } + if nil != channel.OpenAIOrganization { + c.Request.Header.Set("OpenAI-Organization", *channel.OpenAIOrganization) + } c.Set("auto_ban", ban) c.Set("model_mapping", channel.GetModelMapping()) c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key)) diff --git a/relay/channel/openai/adaptor.go b/relay/channel/openai/adaptor.go index 188ff4c..b27b98d 100644 --- a/relay/channel/openai/adaptor.go +++ b/relay/channel/openai/adaptor.go @@ -49,6 +49,12 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *re req.Header.Set("api-key", info.ApiKey) return nil } + if info.ChannelType == common.ChannelTypeOpenAI { + orgId := c.GetHeader("OpenAI-Organization") + if "" != orgId { + req.Header.Set("OpenAI-Organization", orgId) + } + } req.Header.Set("Authorization", "Bearer "+info.ApiKey) //if info.ChannelType == common.ChannelTypeOpenRouter { // req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api") From f63ad9c03c43a815bb8c5d0de1c55485319e011e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=91=E7=A7=A6=E5=A7=8B=E7=9A=87?= Date: Mon, 18 Mar 2024 02:19:59 -0700 Subject: [PATCH 2/2] fix: make the 'openai_organization' parameter actually work. --- middleware/distributor.go | 2 +- relay/channel/openai/adaptor.go | 7 ++----- relay/common/relay_info.go | 2 ++ 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/middleware/distributor.go b/middleware/distributor.go index 0cb9658..a5e40b0 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -156,7 +156,7 @@ func Distribute() func(c *gin.Context) { ban = false } if nil != channel.OpenAIOrganization { - c.Request.Header.Set("OpenAI-Organization", *channel.OpenAIOrganization) + c.Set("channel_organization", *channel.OpenAIOrganization) } c.Set("auto_ban", ban) c.Set("model_mapping", channel.GetModelMapping()) diff --git a/relay/channel/openai/adaptor.go b/relay/channel/openai/adaptor.go index b27b98d..92621d5 100644 --- a/relay/channel/openai/adaptor.go +++ b/relay/channel/openai/adaptor.go @@ -49,11 +49,8 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *re req.Header.Set("api-key", info.ApiKey) return nil } - if info.ChannelType == common.ChannelTypeOpenAI { - orgId := c.GetHeader("OpenAI-Organization") - if "" != orgId { - req.Header.Set("OpenAI-Organization", orgId) - } + if info.ChannelType == common.ChannelTypeOpenAI && "" != info.Organization { + req.Header.Set("OpenAI-Organization", info.Organization) } req.Header.Set("Authorization", "Bearer "+info.ApiKey) //if info.ChannelType == common.ChannelTypeOpenRouter { diff --git a/relay/common/relay_info.go b/relay/common/relay_info.go index aea3cc7..27ed9a9 100644 --- a/relay/common/relay_info.go +++ b/relay/common/relay_info.go @@ -24,6 +24,7 @@ type RelayInfo struct { ApiVersion string PromptTokens int ApiKey string + Organization string BaseUrl string } @@ -52,6 +53,7 @@ func GenRelayInfo(c *gin.Context) *RelayInfo { ApiType: apiType, ApiVersion: c.GetString("api_version"), ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "), + Organization: c.GetString("channel_organization"), } if info.BaseUrl == "" { info.BaseUrl = common.ChannelBaseURLs[channelType]