Merge branch 'main' into patch/images-edits

This commit is contained in:
JustSong
2024-04-27 16:18:01 +08:00
committed by GitHub
59 changed files with 882 additions and 228 deletions

View File

@@ -66,21 +66,29 @@ func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, mode
c.Set(ctxkey.OriginalModel, modelName) // for retry
c.Request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", channel.Key))
c.Set(ctxkey.BaseURL, channel.GetBaseURL())
cfg, _ := channel.LoadConfig()
// this is for backward compatibility
switch channel.Type {
case channeltype.Azure:
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.Xunfei:
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.Gemini:
c.Set(ctxkey.ConfigAPIVersion, channel.Other)
if cfg.APIVersion == "" {
cfg.APIVersion = channel.Other
}
case channeltype.AIProxyLibrary:
c.Set(ctxkey.ConfigLibraryID, channel.Other)
if cfg.LibraryID == "" {
cfg.LibraryID = channel.Other
}
case channeltype.Ali:
c.Set(ctxkey.ConfigPlugin, channel.Other)
}
cfg, _ := channel.LoadConfig()
for k, v := range cfg {
c.Set(ctxkey.ConfigPrefix+k, v)
if cfg.Plugin == "" {
cfg.Plugin = channel.Other
}
}
c.Set(ctxkey.Config, cfg)
}

View File

@@ -4,14 +4,14 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
)
func SetUpLogger(server *gin.Engine) {
server.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
var requestID string
if param.Keys != nil {
requestID = param.Keys[ctxkey.RequestId].(string)
requestID = param.Keys[helper.RequestIdKey].(string)
}
return fmt.Sprintf("[GIN] %s | %s | %3d | %13v | %15s | %7s %s\n",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),

View File

@@ -11,10 +11,10 @@ import (
func RequestId() func(c *gin.Context) {
return func(c *gin.Context) {
id := helper.GenRequestID()
c.Set(ctxkey.RequestId, id)
ctx := context.WithValue(c.Request.Context(), ctxkey.RequestId, id)
c.Set(helper.RequestIdKey, id)
ctx := context.WithValue(c.Request.Context(), helper.RequestIdKey, id)
c.Request = c.Request.WithContext(ctx)
c.Header(ctxkey.RequestId, id)
c.Header(helper.RequestIdKey, id)
c.Next()
}
}

View File

@@ -14,7 +14,7 @@ import (
func abortWithMessage(c *gin.Context, statusCode int, message string) {
c.JSON(statusCode, gin.H{
"error": gin.H{
"message": helper.MessageWithRequestId(message, c.GetString(ctxkey.RequestId)),
"message": helper.MessageWithRequestId(message, c.GetString(helper.RequestIdKey)),
"type": "one_api_error",
},
})