2024-04-05 17:31:44 +00:00
|
|
|
package meta
|
2024-01-21 15:21:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-21 10:55:13 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
2024-04-26 15:05:48 +00:00
|
|
|
"github.com/songquanpeng/one-api/model"
|
2024-04-05 16:44:33 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channeltype"
|
|
|
|
"github.com/songquanpeng/one-api/relay/relaymode"
|
2024-01-21 15:21:42 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
type Meta struct {
|
2024-07-16 15:48:54 +00:00
|
|
|
Mode int
|
|
|
|
ChannelType int
|
|
|
|
ChannelId int
|
|
|
|
TokenId int
|
|
|
|
TokenName string
|
|
|
|
UserId int
|
|
|
|
Group string
|
|
|
|
ModelMapping map[string]string
|
2024-07-21 03:28:22 +00:00
|
|
|
// BaseURL is the proxy url set in the channel config
|
|
|
|
BaseURL string
|
|
|
|
APIKey string
|
|
|
|
APIType int
|
|
|
|
Config model.ChannelConfig
|
|
|
|
IsStream bool
|
2024-07-16 15:48:54 +00:00
|
|
|
// OriginModelName is the model name from the raw user request
|
2024-02-17 16:15:31 +00:00
|
|
|
OriginModelName string
|
2024-07-16 15:48:54 +00:00
|
|
|
// ActualModelName is the model name after mapping
|
2024-02-17 16:15:31 +00:00
|
|
|
ActualModelName string
|
|
|
|
RequestURLPath string
|
|
|
|
PromptTokens int // only for DoResponse
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 17:31:44 +00:00
|
|
|
func GetByContext(c *gin.Context) *Meta {
|
|
|
|
meta := Meta{
|
2024-04-26 15:05:48 +00:00
|
|
|
Mode: relaymode.GetByPath(c.Request.URL.Path),
|
|
|
|
ChannelType: c.GetInt(ctxkey.Channel),
|
|
|
|
ChannelId: c.GetInt(ctxkey.ChannelId),
|
|
|
|
TokenId: c.GetInt(ctxkey.TokenId),
|
|
|
|
TokenName: c.GetString(ctxkey.TokenName),
|
|
|
|
UserId: c.GetInt(ctxkey.Id),
|
|
|
|
Group: c.GetString(ctxkey.Group),
|
|
|
|
ModelMapping: c.GetStringMapString(ctxkey.ModelMapping),
|
|
|
|
OriginModelName: c.GetString(ctxkey.RequestModel),
|
|
|
|
BaseURL: c.GetString(ctxkey.BaseURL),
|
|
|
|
APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
|
|
|
|
RequestURLPath: c.Request.URL.String(),
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
2024-04-26 15:05:48 +00:00
|
|
|
cfg, ok := c.Get(ctxkey.Config)
|
|
|
|
if ok {
|
|
|
|
meta.Config = cfg.(model.ChannelConfig)
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
if meta.BaseURL == "" {
|
2024-04-05 18:03:59 +00:00
|
|
|
meta.BaseURL = channeltype.ChannelBaseURLs[meta.ChannelType]
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
2024-04-05 16:44:33 +00:00
|
|
|
meta.APIType = channeltype.ToAPIType(meta.ChannelType)
|
2024-01-21 15:21:42 +00:00
|
|
|
return &meta
|
|
|
|
}
|