refactor: 重构 Gemini 适配器以支持图片编辑和生成 feat(relay): 添加图片编辑模式支持 feat(controller): 实现 UsageAPIURL 用于获取真实 token 用量 feat(web): 在渠道测试中添加模型选择功能 perf(token): 优化多模态 token 计算逻辑 fix(web): 修复日志分页组件显示问题 docs: 更新渠道配置中的 UsageAPIURL 说明 style: 清理调试日志和注释 feat(gemini): 支持 Imagen 3+ 图片生成模型 feat(openai): 添加生成 ID 捕获和元数据获取功能
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package meta
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
|
"github.com/songquanpeng/one-api/model"
|
|
"github.com/songquanpeng/one-api/relay/channeltype"
|
|
"github.com/songquanpeng/one-api/relay/relaymode"
|
|
)
|
|
|
|
type Meta struct {
|
|
Mode int
|
|
ChannelType int
|
|
ChannelId int
|
|
TokenId int
|
|
TokenName string
|
|
UserId int
|
|
Group string
|
|
ModelMapping map[string]string
|
|
// BaseURL is the proxy url set in the channel config
|
|
BaseURL string
|
|
APIKey string
|
|
APIType int
|
|
Config model.ChannelConfig
|
|
IsStream bool
|
|
// OriginModelName is the model name from the raw user request
|
|
OriginModelName string
|
|
// ActualModelName is the model name after mapping
|
|
ActualModelName string
|
|
RequestURLPath string
|
|
PromptTokens int // only for DoResponse
|
|
ForcedSystemPrompt string
|
|
StartTime time.Time
|
|
// GenerationId is the upstream generation/request ID captured during DoResponse.
|
|
// Adaptors should set this when available; it is used for post-response metadata fetches.
|
|
GenerationId string
|
|
}
|
|
|
|
func GetByContext(c *gin.Context) *Meta {
|
|
meta := Meta{
|
|
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(),
|
|
ForcedSystemPrompt: c.GetString(ctxkey.SystemPrompt),
|
|
StartTime: time.Now(),
|
|
}
|
|
cfg, ok := c.Get(ctxkey.Config)
|
|
if ok {
|
|
meta.Config = cfg.(model.ChannelConfig)
|
|
}
|
|
if meta.BaseURL == "" {
|
|
meta.BaseURL = channeltype.ChannelBaseURLs[meta.ChannelType]
|
|
}
|
|
meta.APIType = channeltype.ToAPIType(meta.ChannelType)
|
|
return &meta
|
|
}
|