diff --git a/controller/channel-test.go b/controller/channel-test.go index 1b0b745a..d20f22ff 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -63,7 +63,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 01267fbf..3a44907e 100644 --- a/controller/relay-audio.go +++ b/controller/relay-audio.go @@ -101,7 +101,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 0ff18309..1c2ae00f 100644 --- a/controller/relay-image.go +++ b/controller/relay-image.go @@ -127,7 +127,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 37867843..49745f51 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 = append(parts, "") + } + 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 dd9e7153..4734c3f0 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -249,6 +249,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) @@ -344,6 +349,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 654a5d51..95179684 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: