Files
new-api/relay/common/relay_info.go

216 lines
6.0 KiB
Go
Raw Normal View History

2024-02-29 01:08:18 +08:00
package common
import (
"one-api/common"
"one-api/constant"
"one-api/dto"
relayconstant "one-api/relay/constant"
2024-02-29 01:08:18 +08:00
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
2024-02-29 01:08:18 +08:00
)
type RelayInfo struct {
ChannelType int
ChannelId int
TokenId int
TokenKey string
UserId int
Group string
TokenUnlimited bool
StartTime time.Time
FirstResponseTime time.Time
IsFirstResponse bool
SendLastReasoningResponse bool
ApiType int
IsStream bool
IsPlayground bool
UsePrice bool
RelayMode int
UpstreamModelName string
OriginModelName string
//RecodeModelName string
2024-07-08 01:27:57 +08:00
RequestURLPath string
ApiVersion string
PromptTokens int
ApiKey string
Organization string
BaseUrl string
SupportStreamOptions bool
2024-07-08 02:00:39 +08:00
ShouldIncludeUsage bool
IsModelMapped bool
ClientWs *websocket.Conn
TargetWs *websocket.Conn
InputAudioFormat string
OutputAudioFormat string
RealtimeTools []dto.RealTimeTool
IsFirstRequest bool
2024-11-07 16:12:09 +08:00
AudioUsage bool
ReasoningEffort string
ChannelSetting map[string]interface{}
}
// 定义支持流式选项的通道类型
var streamSupportedChannels = map[int]bool{
common.ChannelTypeOpenAI: true,
common.ChannelTypeAnthropic: true,
common.ChannelTypeAws: true,
common.ChannelTypeGemini: true,
common.ChannelCloudflare: true,
common.ChannelTypeAzure: true,
common.ChannelTypeVolcEngine: true,
common.ChannelTypeOllama: true,
}
func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
info := GenRelayInfo(c)
info.ClientWs = ws
info.InputAudioFormat = "pcm16"
info.OutputAudioFormat = "pcm16"
info.IsFirstRequest = true
return info
2024-02-29 01:08:18 +08:00
}
func GenRelayInfo(c *gin.Context) *RelayInfo {
2024-08-03 22:41:47 +08:00
channelType := c.GetInt("channel_type")
2024-02-29 01:08:18 +08:00
channelId := c.GetInt("channel_id")
channelSetting := c.GetStringMap("channel_setting")
2024-04-04 16:35:44 +08:00
2024-02-29 01:08:18 +08:00
tokenId := c.GetInt("token_id")
2024-11-30 23:32:42 +08:00
tokenKey := c.GetString("token_key")
2024-02-29 01:08:18 +08:00
userId := c.GetInt("id")
group := c.GetString("group")
tokenUnlimited := c.GetBool("token_unlimited_quota")
startTime := c.GetTime(constant.ContextKeyRequestStartTime)
// firstResponseTime = time.Now() - 1 second
2024-02-29 01:08:18 +08:00
apiType, _ := relayconstant.ChannelType2APIType(channelType)
2024-02-29 01:08:18 +08:00
info := &RelayInfo{
IsFirstResponse: true,
RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
BaseUrl: c.GetString("base_url"),
RequestURLPath: c.Request.URL.String(),
ChannelType: channelType,
ChannelId: channelId,
TokenId: tokenId,
2024-11-30 23:32:42 +08:00
TokenKey: tokenKey,
UserId: userId,
Group: group,
TokenUnlimited: tokenUnlimited,
StartTime: startTime,
FirstResponseTime: startTime.Add(-time.Second),
2024-08-27 20:19:51 +08:00
OriginModelName: c.GetString("original_model"),
UpstreamModelName: c.GetString("original_model"),
//RecodeModelName: c.GetString("original_model"),
IsModelMapped: false,
ApiType: apiType,
ApiVersion: c.GetString("api_version"),
ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
Organization: c.GetString("channel_organization"),
ChannelSetting: channelSetting,
2024-02-29 01:08:18 +08:00
}
2024-09-26 00:59:09 +08:00
if strings.HasPrefix(c.Request.URL.Path, "/pg") {
info.IsPlayground = true
info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
info.RequestURLPath = "/v1" + info.RequestURLPath
}
2024-02-29 01:08:18 +08:00
if info.BaseUrl == "" {
info.BaseUrl = common.ChannelBaseURLs[channelType]
}
2024-02-29 16:21:25 +08:00
if info.ChannelType == common.ChannelTypeAzure {
2024-03-08 20:29:04 +08:00
info.ApiVersion = GetAPIVersion(c)
2024-02-29 16:21:25 +08:00
}
2024-08-27 20:19:51 +08:00
if info.ChannelType == common.ChannelTypeVertexAi {
info.ApiVersion = c.GetString("region")
}
if streamSupportedChannels[info.ChannelType] {
2024-07-08 01:27:57 +08:00
info.SupportStreamOptions = true
}
2024-02-29 01:08:18 +08:00
return info
}
func (info *RelayInfo) SetPromptTokens(promptTokens int) {
info.PromptTokens = promptTokens
}
func (info *RelayInfo) SetIsStream(isStream bool) {
info.IsStream = isStream
}
2024-07-15 18:14:07 +08:00
func (info *RelayInfo) SetFirstResponseTime() {
if info.IsFirstResponse {
2024-07-15 18:14:07 +08:00
info.FirstResponseTime = time.Now()
info.IsFirstResponse = false
2024-07-15 18:14:07 +08:00
}
}
type TaskRelayInfo struct {
ChannelType int
ChannelId int
TokenId int
UserId int
Group string
StartTime time.Time
ApiType int
RelayMode int
UpstreamModelName string
RequestURLPath string
ApiKey string
BaseUrl string
Action string
OriginTaskID string
ConsumeQuota bool
}
func GenTaskRelayInfo(c *gin.Context) *TaskRelayInfo {
2024-08-03 22:41:47 +08:00
channelType := c.GetInt("channel_type")
channelId := c.GetInt("channel_id")
tokenId := c.GetInt("token_id")
userId := c.GetInt("id")
group := c.GetString("group")
startTime := time.Now()
apiType, _ := relayconstant.ChannelType2APIType(channelType)
info := &TaskRelayInfo{
RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
BaseUrl: c.GetString("base_url"),
RequestURLPath: c.Request.URL.String(),
ChannelType: channelType,
ChannelId: channelId,
TokenId: tokenId,
UserId: userId,
Group: group,
StartTime: startTime,
ApiType: apiType,
ApiKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
}
if info.BaseUrl == "" {
info.BaseUrl = common.ChannelBaseURLs[channelType]
}
return info
}
2024-09-26 00:59:09 +08:00
func (info *TaskRelayInfo) ToRelayInfo() *RelayInfo {
return &RelayInfo{
ChannelType: info.ChannelType,
ChannelId: info.ChannelId,
TokenId: info.TokenId,
UserId: info.UserId,
Group: info.Group,
StartTime: info.StartTime,
ApiType: info.ApiType,
RelayMode: info.RelayMode,
UpstreamModelName: info.UpstreamModelName,
RequestURLPath: info.RequestURLPath,
ApiKey: info.ApiKey,
BaseUrl: info.BaseUrl,
}
}