This commit is contained in:
Pluto 2023-11-24 21:05:45 +08:00 committed by GitHub
commit 13d05ec338
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 1 deletions

View File

@ -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)

View File

@ -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"))

View File

@ -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"))

View File

@ -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)

View File

@ -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")

View File

@ -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: