chore: move config key to package ctxkey

This commit is contained in:
JustSong 2024-04-21 18:55:13 +08:00
parent d87c55f542
commit e30ebda0fe
9 changed files with 23 additions and 25 deletions

View File

@ -1,6 +1,6 @@
package ctxkey package ctxkey
var ( const (
RequestModel = "request_model" RequestModel = "request_model"
ConvertedRequest = "converted_request" ConvertedRequest = "converted_request"
OriginalModel = "original_model" OriginalModel = "original_model"

View File

@ -3,7 +3,6 @@ package middleware
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/model" "github.com/songquanpeng/one-api/model"
@ -69,18 +68,18 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
// this is for backward compatibility // this is for backward compatibility
switch channel.Type { switch channel.Type {
case channeltype.Azure: case channeltype.Azure:
c.Set(config.KeyAPIVersion, channel.Other) c.Set(ctxkey.ConfigAPIVersion, channel.Other)
case channeltype.Xunfei: case channeltype.Xunfei:
c.Set(config.KeyAPIVersion, channel.Other) c.Set(ctxkey.ConfigAPIVersion, channel.Other)
case channeltype.Gemini: case channeltype.Gemini:
c.Set(config.KeyAPIVersion, channel.Other) c.Set(ctxkey.ConfigAPIVersion, channel.Other)
case channeltype.AIProxyLibrary: case channeltype.AIProxyLibrary:
c.Set(config.KeyLibraryID, channel.Other) c.Set(ctxkey.ConfigLibraryID, channel.Other)
case channeltype.Ali: case channeltype.Ali:
c.Set(config.KeyPlugin, channel.Other) c.Set(ctxkey.ConfigPlugin, channel.Other)
} }
cfg, _ := channel.LoadConfig() cfg, _ := channel.LoadConfig()
for k, v := range cfg { for k, v := range cfg {
c.Set(config.KeyPrefix+k, v) c.Set(ctxkey.ConfigPrefix+k, v)
} }
} }

View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
@ -34,7 +34,7 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
return nil, errors.New("request is nil") return nil, errors.New("request is nil")
} }
aiProxyLibraryRequest := ConvertRequest(*request) aiProxyLibraryRequest := ConvertRequest(*request)
aiProxyLibraryRequest.LibraryId = c.GetString(config.KeyLibraryID) aiProxyLibraryRequest.LibraryId = c.GetString(ctxkey.ConfigLibraryID)
return aiProxyLibraryRequest, nil return aiProxyLibraryRequest, nil
} }

View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
"github.com/songquanpeng/one-api/relay/model" "github.com/songquanpeng/one-api/relay/model"
@ -47,8 +47,8 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *me
if meta.Mode == relaymode.ImagesGenerations { if meta.Mode == relaymode.ImagesGenerations {
req.Header.Set("X-DashScope-Async", "enable") req.Header.Set("X-DashScope-Async", "enable")
} }
if c.GetString(config.KeyPlugin) != "" { if c.GetString(ctxkey.ConfigPlugin) != "" {
req.Header.Set("X-DashScope-Plugin", c.GetString(config.KeyPlugin)) req.Header.Set("X-DashScope-Plugin", c.GetString(ctxkey.ConfigPlugin))
} }
return nil return nil
} }

View File

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey" "github.com/songquanpeng/one-api/common/ctxkey"
"io" "io"
"net/http" "net/http"
@ -25,9 +24,9 @@ import (
) )
func newAwsClient(c *gin.Context) (*bedrockruntime.Client, error) { func newAwsClient(c *gin.Context) (*bedrockruntime.Client, error) {
ak := c.GetString(config.KeyAK) ak := c.GetString(ctxkey.ConfigAK)
sk := c.GetString(config.KeySK) sk := c.GetString(ctxkey.ConfigSK)
region := c.GetString(config.KeyRegion) region := c.GetString(ctxkey.ConfigRegion)
client := bedrockruntime.New(bedrockruntime.Options{ client := bedrockruntime.New(bedrockruntime.Options{
Region: region, Region: region,
Credentials: aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(ak, sk, "")), Credentials: aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(ak, sk, "")),

View File

@ -2,14 +2,14 @@ package azure
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
) )
func GetAPIVersion(c *gin.Context) string { func GetAPIVersion(c *gin.Context) string {
query := c.Request.URL.Query() query := c.Request.URL.Query()
apiVersion := query.Get("api-version") apiVersion := query.Get("api-version")
if apiVersion == "" { if apiVersion == "" {
apiVersion = c.GetString(config.KeyAPIVersion) apiVersion = c.GetString(ctxkey.ConfigAPIVersion)
} }
return apiVersion return apiVersion
} }

View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/relay/adaptor" "github.com/songquanpeng/one-api/relay/adaptor"
"github.com/songquanpeng/one-api/relay/adaptor/openai" "github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/songquanpeng/one-api/relay/meta" "github.com/songquanpeng/one-api/relay/meta"
@ -34,7 +34,7 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
if request == nil { if request == nil {
return nil, errors.New("request is nil") return nil, errors.New("request is nil")
} }
request.User = c.GetString(config.KeyUserID) request.User = c.GetString(ctxkey.ConfigUserID)
return ConvertRequest(*request), nil return ConvertRequest(*request), nil
} }

View File

@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/common/random" "github.com/songquanpeng/one-api/common/random"
@ -280,7 +280,7 @@ func getAPIVersion(c *gin.Context, modelName string) string {
return apiVersion return apiVersion
} }
apiVersion = c.GetString(config.KeyAPIVersion) apiVersion = c.GetString(ctxkey.ConfigAPIVersion)
if apiVersion != "" { if apiVersion != "" {
return apiVersion return apiVersion
} }

View File

@ -2,7 +2,7 @@ package meta
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/relay/adaptor/azure" "github.com/songquanpeng/one-api/relay/adaptor/azure"
"github.com/songquanpeng/one-api/relay/channeltype" "github.com/songquanpeng/one-api/relay/channeltype"
"github.com/songquanpeng/one-api/relay/relaymode" "github.com/songquanpeng/one-api/relay/relaymode"
@ -41,7 +41,7 @@ func GetByContext(c *gin.Context) *Meta {
Group: c.GetString("group"), Group: c.GetString("group"),
ModelMapping: c.GetStringMapString("model_mapping"), ModelMapping: c.GetStringMapString("model_mapping"),
BaseURL: c.GetString("base_url"), BaseURL: c.GetString("base_url"),
APIVersion: c.GetString(config.KeyAPIVersion), APIVersion: c.GetString(ctxkey.ConfigAPIVersion),
APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "), APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
Config: nil, Config: nil,
RequestURLPath: c.Request.URL.String(), RequestURLPath: c.Request.URL.String(),