diff --git a/controller/channel-test.go b/controller/channel-test.go index 1974ef6e..2d61c786 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -61,7 +61,9 @@ func testChannel(channel *model.Channel, request ChatRequest) (err error, openai if channel.Type == common.ChannelTypeAzure { req.Header.Set("api-key", channel.Key) } else { - req.Header.Set("Authorization", "Bearer "+channel.Key) + apiKey, organization := parseOpenaiConfig(channel.Key) + req.Header.Set("Authorization", "Bearer "+apiKey) + req.Header.Set("OpenAI-Organization", organization) } req.Header.Set("Content-Type", "application/json") resp, err := httpClient.Do(req) diff --git a/controller/relay-audio.go b/controller/relay-audio.go index 381c6feb..4b604df7 100644 --- a/controller/relay-audio.go +++ b/controller/relay-audio.go @@ -78,7 +78,14 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode if err != nil { return errorWrapper(err, "new_request_failed", http.StatusInternalServerError) } + + apiKey := c.Request.Header.Get("Authorization") + apiKey, organization := parseOpenaiConfig(apiKey) + c.Request.Header.Set("Authorization", apiKey) + c.Request.Header.Set("OpenAI-Organization", organization) + req.Header.Set("Authorization", c.Request.Header.Get("Authorization")) + req.Header.Set("OpenAI-Organization", c.Request.Header.Get("OpenAI-Organization")) req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) req.Header.Set("Accept", c.Request.Header.Get("Accept")) diff --git a/controller/relay-image.go b/controller/relay-image.go index 998a7851..437b0166 100644 --- a/controller/relay-image.go +++ b/controller/relay-image.go @@ -106,7 +106,13 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode if err != nil { return errorWrapper(err, "new_request_failed", http.StatusInternalServerError) } + apiKey := c.Request.Header.Get("Authorization") + apiKey, organization := parseOpenaiConfig(apiKey) + c.Request.Header.Set("Authorization", apiKey) + c.Request.Header.Set("OpenAI-Organization", organization) + req.Header.Set("Authorization", c.Request.Header.Get("Authorization")) + req.Header.Set("OpenAI-Organization", c.Request.Header.Get("OpenAI-Organization")) req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) req.Header.Set("Accept", c.Request.Header.Get("Accept")) diff --git a/controller/relay-openai.go b/controller/relay-openai.go index 6bdfbc08..717c5e17 100644 --- a/controller/relay-openai.go +++ b/controller/relay-openai.go @@ -11,6 +11,16 @@ import ( "strings" ) +func parseOpenaiConfig(config string) (apiKey string, organization string) { + parts := strings.Split(config, "|") + if len(parts) != 2 { + parts[1] = "" + } + apiKey = parts[0] + organization = parts[1] + return +} + func openaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*OpenAIErrorWithStatusCode, string) { responseText := "" scanner := bufio.NewScanner(resp.Body) diff --git a/controller/relay-text.go b/controller/relay-text.go index fba49a7f..04b49dab 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -239,6 +239,11 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { requestBody = c.Request.Body } switch apiType { + case APITypeOpenAI: + apiKey := c.Request.Header.Get("Authorization") + apiKey, organization := parseOpenaiConfig(apiKey) + c.Request.Header.Set("Authorization", apiKey) + c.Request.Header.Set("OpenAI-Organization", organization) case APITypeClaude: claudeRequest := requestOpenAI2Claude(textRequest) jsonStr, err := json.Marshal(claudeRequest) @@ -334,6 +339,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { req.Header.Set("api-key", apiKey) } else { req.Header.Set("Authorization", c.Request.Header.Get("Authorization")) + req.Header.Set("OpenAI-Organization", c.Request.Header.Get("OpenAI-Organization")) if channelType == common.ChannelTypeOpenRouter { req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api") req.Header.Set("X-Title", "One API") diff --git a/web/src/pages/Channel/EditChannel.js b/web/src/pages/Channel/EditChannel.js index 82077c1f..c0be63f1 100644 --- a/web/src/pages/Channel/EditChannel.js +++ b/web/src/pages/Channel/EditChannel.js @@ -13,6 +13,8 @@ const MODEL_MAPPING_EXAMPLE = { function type2secretPrompt(type) { // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥') switch (type) { + case 1: + return '按照如下格式输入:APIKey 或者 APIKey|OpenAI-Organization'; case 15: return '按照如下格式输入:APIKey|SecretKey'; case 18: