mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-19 22:53:41 +08:00
- 添加Anthropic适配器实现 | Add Anthropic adaptor implementation - 支持Anthropic消息格式转换 | Support Anthropic message format conversion - 添加Vertex AI Claude适配器支持 | Add Vertex AI Claude adapter support - 更新Anthropic的中继模式定义 | Update relay mode definitions for Anthropic - 添加Anthropic控制器和路由 | Add Anthropic controller and routing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: zgdmemail@gmail.com
74 lines
2.1 KiB
Go
74 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/apitype"
|
|
"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
|
|
}
|
|
|
|
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)
|
|
|
|
// Force Anthropic API type for native Anthropic protocol requests
|
|
if meta.Mode == relaymode.AnthropicMessages {
|
|
meta.APIType = apitype.Anthropic
|
|
}
|
|
|
|
return &meta
|
|
}
|