* test(relayconvert): add golden snapshot matrix and relaykit boundary guard Phase 0 of the relaykit extraction plan: pin byte-level output of every registered (from,to) request/response/stream conversion route, and forbid kit-bound packages from growing host-only imports. * wip(relayconvert): drop gin.Context from converter signatures; add convmeta draft Phase 1 in progress: relayconvert now takes context.Context; host media resolver adapts gin.Context back at the service boundary. * refactor(relayconvert): decouple converters from RelayInfo, gin, and settings Phase 1 of the relaykit extraction plan: - converters now depend on convmeta.Meta (implemented by RelayInfo) instead of *relaycommon.RelayInfo; ClaudeConvertInfo and the format guesser move to convmeta with aliases left behind - host settings reach converters via a convmeta.Options snapshot built in RelayInfo.ConvOptions; no more model_setting/reasoning global reads inside the conversion layer - effort-suffix helpers move to service/relayconvert/reasoning (old package forwards); chat-to-responses upgrade policy moves to service (host routing logic, not conversion) - golden conversion matrix unchanged * test(relayconvert): tighten boundary — kit packages now free of gin/setting imports * refactor(dto): drop gin and logger dependencies Phase 2 (part 1): dto.Request.IsStream now takes *http.Request instead of *gin.Context (Gemini's impl reads query/path off the std request); dto's three logger calls become common.SysError. Boundary test allowlist is now empty — kit-bound packages import no gin/setting/logger/model. * refactor(kit): extract dependency-free kitutil; dto/types/relayconvert stop importing common Phase 2 of the relaykit extraction plan: - new service/relayconvert/kitutil holds the pure helpers the kit needs (JSON wrappers, pointer/string/uuid/timestamp utils, MaskSensitiveInfo, pluggable LogInfo/LogError hooks, Debug flag) - dto, types, and all relayconvert packages now use kitutil; their only remaining internal deps are dto/types/constant - common keeps every original symbol (MaskSensitiveInfo delegates to kitutil) so host code is untouched; main.go routes kit logging into common.SysLog/SysError and mirrors DebugEnabled - golden conversion matrix unchanged * refactor(kit): move EndpointType/FinishReason to types; OpenRouter dialect via Options Kit packages (dto/types/relayconvert/reasonmap) no longer import constant: - EndpointType and finish-reason values live in types; constant re-exports - the OpenRouter special-case in claude->openai request conversion reads Options.OpenRouterDialect, set by the host from the channel type; InitChannelMeta invalidates the cached snapshot on channel switch * refactor: extract relaykit submodule (dto/types/relayconvert/reasonmap) Phase 3 of the relaykit extraction plan: - new go module github.com/QuantumNous/new-api/relaykit containing dto (minus task family), types, relayconvert (with convmeta/kitutil/reasoning), and reasonmap; host consumes it via require + replace, go.work for dev - task-family dto (task/suno/midjourney/video) stays in the host dto package; dual-consumer host files alias it as taskdto - relaykit builds and tests standalone (GOWORK=off): no host imports, no gin, no DB, no settings - golden conversion matrix unchanged * build(docker): copy relaykit/go.mod before go mod download The local-replace submodule's go.mod must exist inside the build context for the main module graph to resolve. * fix: address relaykit extraction regressions * fix: address relaykit review regressions * docs: document Meta nil receiver contract * fix(relaykit): fail OpenAI→Claude conversion without max_tokens; reject negative default_max_tokens The Claude Messages API requires max_tokens (omitting it is a 400 "Field required"), but with a nil Options.Claude.DefaultMaxTokens hook the converters silently emitted a request the upstream is guaranteed to reject. Both OpenAI Chat and Responses → Claude conversions now return sharedclaude.ErrMissingMaxTokens when no path (client value, default hook, thinking-adapter floor) supplied one. Unreachable in the host, which always configures the hook. Host side, claude.default_max_tokens now rejects negative values at the option API before persisting — they would wrap into huge unsigned values during conversion. Zero stays allowed: the current API treats max_tokens: 0 as cache pre-warming. * fix: make Gemini safety settings read path race-free
394 lines
13 KiB
Go
394 lines
13 KiB
Go
package vertex
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/relay/channel"
|
|
"github.com/QuantumNous/new-api/relay/channel/claude"
|
|
"github.com/QuantumNous/new-api/relay/channel/gemini"
|
|
"github.com/QuantumNous/new-api/relay/channel/openai"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/QuantumNous/new-api/relay/constant"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/QuantumNous/new-api/relaykit/types"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/setting/model_setting"
|
|
"github.com/QuantumNous/new-api/setting/reasoning"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
const (
|
|
RequestModeClaude = 1
|
|
RequestModeGemini = 2
|
|
RequestModeOpenSource = 3
|
|
)
|
|
|
|
var claudeModelMap = map[string]string{
|
|
"claude-3-sonnet-20240229": "claude-3-sonnet@20240229",
|
|
"claude-3-opus-20240229": "claude-3-opus@20240229",
|
|
"claude-3-haiku-20240307": "claude-3-haiku@20240307",
|
|
"claude-3-5-sonnet-20240620": "claude-3-5-sonnet@20240620",
|
|
"claude-3-5-sonnet-20241022": "claude-3-5-sonnet-v2@20241022",
|
|
"claude-3-7-sonnet-20250219": "claude-3-7-sonnet@20250219",
|
|
"claude-sonnet-4-20250514": "claude-sonnet-4@20250514",
|
|
"claude-opus-4-20250514": "claude-opus-4@20250514",
|
|
"claude-opus-4-1-20250805": "claude-opus-4-1@20250805",
|
|
"claude-sonnet-4-5-20250929": "claude-sonnet-4-5@20250929",
|
|
"claude-haiku-4-5-20251001": "claude-haiku-4-5@20251001",
|
|
"claude-opus-4-5-20251101": "claude-opus-4-5@20251101",
|
|
"claude-opus-4-6": "claude-opus-4-6",
|
|
"claude-opus-4-7": "claude-opus-4-7",
|
|
"claude-opus-4-8": "claude-opus-4-8",
|
|
}
|
|
|
|
const anthropicVersion = "vertex-2023-10-16"
|
|
|
|
type Adaptor struct {
|
|
RequestMode int
|
|
AccountCredentials Credentials
|
|
}
|
|
|
|
func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
|
|
// Vertex AI does not support functionResponse.id; keep it stripped here for consistency.
|
|
if model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
|
|
removeFunctionResponseID(request)
|
|
}
|
|
geminiAdaptor := gemini.Adaptor{}
|
|
return geminiAdaptor.ConvertGeminiRequest(c, info, request)
|
|
}
|
|
|
|
func removeFunctionResponseID(request *dto.GeminiChatRequest) {
|
|
if request == nil {
|
|
return
|
|
}
|
|
|
|
if len(request.Contents) > 0 {
|
|
for i := range request.Contents {
|
|
if len(request.Contents[i].Parts) == 0 {
|
|
continue
|
|
}
|
|
for j := range request.Contents[i].Parts {
|
|
part := &request.Contents[i].Parts[j]
|
|
if part.FunctionResponse == nil {
|
|
continue
|
|
}
|
|
if len(part.FunctionResponse.ID) > 0 {
|
|
part.FunctionResponse.ID = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(request.Requests) > 0 {
|
|
for i := range request.Requests {
|
|
removeFunctionResponseID(&request.Requests[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
|
if v, ok := claudeModelMap[info.UpstreamModelName]; ok {
|
|
c.Set("request_model", v)
|
|
} else {
|
|
c.Set("request_model", request.Model)
|
|
}
|
|
vertexClaudeReq := copyRequest(request, anthropicVersion)
|
|
return vertexClaudeReq, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
|
geminiAdaptor := gemini.Adaptor{}
|
|
return geminiAdaptor.ConvertImageRequest(c, info, request)
|
|
}
|
|
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|
if strings.HasPrefix(info.UpstreamModelName, "claude") {
|
|
a.RequestMode = RequestModeClaude
|
|
} else if strings.Contains(info.UpstreamModelName, "llama") ||
|
|
// open source models
|
|
strings.Contains(info.UpstreamModelName, "-maas") {
|
|
a.RequestMode = RequestModeOpenSource
|
|
} else {
|
|
a.RequestMode = RequestModeGemini
|
|
}
|
|
}
|
|
|
|
func (a *Adaptor) getRequestUrl(info *relaycommon.RelayInfo, modelName, suffix string) (string, error) {
|
|
region := GetModelRegion(info.ApiVersion, info.OriginModelName)
|
|
if info.ChannelOtherSettings.VertexKeyType != dto.VertexKeyTypeAPIKey {
|
|
adc := &Credentials{}
|
|
if err := common.Unmarshal([]byte(info.ApiKey), adc); err != nil {
|
|
return "", fmt.Errorf("failed to decode credentials file: %w", err)
|
|
}
|
|
a.AccountCredentials = *adc
|
|
|
|
if a.RequestMode == RequestModeGemini {
|
|
return BuildGoogleModelURL(info.ChannelBaseUrl, DefaultAPIVersion, adc.ProjectID, region, modelName, suffix), nil
|
|
} else if a.RequestMode == RequestModeClaude {
|
|
return BuildAnthropicModelURL(info.ChannelBaseUrl, DefaultAPIVersion, adc.ProjectID, region, modelName, suffix), nil
|
|
} else if a.RequestMode == RequestModeOpenSource {
|
|
return BuildOpenSourceChatCompletionsURL(info.ChannelBaseUrl, adc.ProjectID, region), nil
|
|
}
|
|
} else {
|
|
var keyPrefix string
|
|
if strings.HasSuffix(suffix, "?alt=sse") {
|
|
keyPrefix = "&"
|
|
} else {
|
|
keyPrefix = "?"
|
|
}
|
|
if a.RequestMode == RequestModeGemini {
|
|
return fmt.Sprintf(
|
|
"%s%skey=%s",
|
|
BuildGoogleModelURL(info.ChannelBaseUrl, DefaultAPIVersion, "", region, modelName, suffix),
|
|
keyPrefix,
|
|
info.ApiKey,
|
|
), nil
|
|
} else if a.RequestMode == RequestModeClaude {
|
|
return fmt.Sprintf(
|
|
"%s%skey=%s",
|
|
BuildAnthropicModelURL(info.ChannelBaseUrl, DefaultAPIVersion, "", region, modelName, suffix),
|
|
keyPrefix,
|
|
info.ApiKey,
|
|
), nil
|
|
}
|
|
}
|
|
return "", errors.New("unsupported request mode")
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|
suffix := ""
|
|
if a.RequestMode == RequestModeGemini {
|
|
if model_setting.GetGeminiSettings().ThinkingAdapterEnabled &&
|
|
!model_setting.ShouldPreserveThinkingSuffix(info.OriginModelName) {
|
|
// 新增逻辑:处理 -thinking-<budget> 格式
|
|
if strings.Contains(info.UpstreamModelName, "-thinking-") {
|
|
parts := strings.Split(info.UpstreamModelName, "-thinking-")
|
|
info.UpstreamModelName = parts[0]
|
|
} else if strings.HasSuffix(info.UpstreamModelName, "-thinking") { // 旧的适配
|
|
info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-thinking")
|
|
} else if strings.HasSuffix(info.UpstreamModelName, "-nothinking") {
|
|
info.UpstreamModelName = strings.TrimSuffix(info.UpstreamModelName, "-nothinking")
|
|
} else if baseModel, level, ok := reasoning.TrimEffortSuffix(info.UpstreamModelName); ok && level != "" {
|
|
info.UpstreamModelName = baseModel
|
|
}
|
|
}
|
|
|
|
if info.IsStream {
|
|
suffix = "streamGenerateContent?alt=sse"
|
|
} else {
|
|
suffix = "generateContent"
|
|
}
|
|
|
|
if strings.HasPrefix(info.UpstreamModelName, "imagen") {
|
|
suffix = "predict"
|
|
}
|
|
return a.getRequestUrl(info, info.UpstreamModelName, suffix)
|
|
} else if a.RequestMode == RequestModeClaude {
|
|
if info.IsStream {
|
|
suffix = "streamRawPredict?alt=sse"
|
|
} else {
|
|
suffix = "rawPredict"
|
|
}
|
|
model := info.UpstreamModelName
|
|
if v, ok := claudeModelMap[info.UpstreamModelName]; ok {
|
|
model = v
|
|
}
|
|
return a.getRequestUrl(info, model, suffix)
|
|
} else if a.RequestMode == RequestModeOpenSource {
|
|
return a.getRequestUrl(info, "", "")
|
|
}
|
|
return "", errors.New("unsupported request mode")
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
|
channel.SetupApiRequestHeader(info, c, req)
|
|
if info.ChannelOtherSettings.VertexKeyType != dto.VertexKeyTypeAPIKey {
|
|
accessToken, err := getAccessToken(a, info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Set("Authorization", "Bearer "+accessToken)
|
|
}
|
|
if a.AccountCredentials.ProjectID != "" {
|
|
req.Set("x-goog-user-project", a.AccountCredentials.ProjectID)
|
|
}
|
|
if strings.Contains(info.UpstreamModelName, "claude") {
|
|
claude.CommonClaudeHeadersOperation(c, req, info)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
if a.RequestMode == RequestModeGemini && strings.HasPrefix(info.UpstreamModelName, "imagen") {
|
|
prompt := ""
|
|
for _, m := range request.Messages {
|
|
if m.Role == "user" {
|
|
prompt = m.StringContent()
|
|
if prompt != "" {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if prompt == "" {
|
|
if p, ok := request.Prompt.(string); ok {
|
|
prompt = p
|
|
}
|
|
}
|
|
if prompt == "" {
|
|
return nil, errors.New("prompt is required for image generation")
|
|
}
|
|
|
|
imgReq := dto.ImageRequest{
|
|
Model: request.Model,
|
|
Prompt: prompt,
|
|
N: lo.ToPtr(uint(1)),
|
|
Size: "1024x1024",
|
|
}
|
|
if request.N != nil && *request.N > 0 {
|
|
imgReq.N = lo.ToPtr(uint(*request.N))
|
|
}
|
|
if request.Size != "" {
|
|
imgReq.Size = request.Size
|
|
}
|
|
if len(request.ExtraBody) > 0 {
|
|
var extra map[string]any
|
|
if err := common.Unmarshal(request.ExtraBody, &extra); err == nil {
|
|
if n, ok := extra["n"].(float64); ok && n > 0 {
|
|
imgReq.N = lo.ToPtr(uint(n))
|
|
}
|
|
if size, ok := extra["size"].(string); ok {
|
|
imgReq.Size = size
|
|
}
|
|
// accept aspectRatio in extra body (top-level or under parameters)
|
|
if ar, ok := extra["aspectRatio"].(string); ok && ar != "" {
|
|
imgReq.Size = ar
|
|
}
|
|
if params, ok := extra["parameters"].(map[string]any); ok {
|
|
if ar, ok := params["aspectRatio"].(string); ok && ar != "" {
|
|
imgReq.Size = ar
|
|
}
|
|
}
|
|
}
|
|
}
|
|
c.Set("request_model", request.Model)
|
|
return a.ConvertImageRequest(c, info, imgReq)
|
|
}
|
|
if a.RequestMode == RequestModeClaude {
|
|
result, err := service.ConvertRequest(c, info, types.RelayFormatClaude, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claudeReq, ok := result.Value.(*dto.ClaudeRequest)
|
|
if !ok {
|
|
return nil, fmt.Errorf("expected Anthropic Messages request, got %T", result.Value)
|
|
}
|
|
vertexClaudeReq := copyRequest(claudeReq, anthropicVersion)
|
|
c.Set("request_model", claudeReq.Model)
|
|
info.UpstreamModelName = claudeReq.Model
|
|
return vertexClaudeReq, nil
|
|
} else if a.RequestMode == RequestModeGemini {
|
|
result, err := service.ConvertRequest(c, info, types.RelayFormatGemini, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
geminiRequest, ok := result.Value.(*dto.GeminiChatRequest)
|
|
if !ok {
|
|
return nil, fmt.Errorf("expected Gemini generateContent request, got %T", result.Value)
|
|
}
|
|
c.Set("request_model", request.Model)
|
|
return geminiRequest, nil
|
|
} else if a.RequestMode == RequestModeOpenSource {
|
|
return request, nil
|
|
}
|
|
return nil, errors.New("unsupported request mode")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
|
//TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
|
// TODO implement me
|
|
return nil, errors.New("not implemented")
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
|
return channel.DoApiRequest(a, c, info, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
|
claudeAdaptor := claude.Adaptor{}
|
|
if info.IsStream {
|
|
switch a.RequestMode {
|
|
case RequestModeClaude:
|
|
return claudeAdaptor.DoResponse(c, resp, info)
|
|
case RequestModeGemini:
|
|
if info.RelayMode == constant.RelayModeGemini {
|
|
return gemini.GeminiTextGenerationStreamHandler(c, info, resp)
|
|
} else {
|
|
return gemini.GeminiChatStreamHandler(c, info, resp)
|
|
}
|
|
case RequestModeOpenSource:
|
|
return openai.OaiStreamHandler(c, info, resp)
|
|
}
|
|
} else {
|
|
switch a.RequestMode {
|
|
case RequestModeClaude:
|
|
return claudeAdaptor.DoResponse(c, resp, info)
|
|
case RequestModeGemini:
|
|
if info.RelayMode == constant.RelayModeGemini {
|
|
return gemini.GeminiTextGenerationHandler(c, info, resp)
|
|
} else {
|
|
if strings.HasPrefix(info.UpstreamModelName, "imagen") {
|
|
return gemini.GeminiImageHandler(c, info, resp)
|
|
}
|
|
return gemini.GeminiChatHandler(c, info, resp)
|
|
}
|
|
case RequestModeOpenSource:
|
|
return openai.OpenaiHandler(c, info, resp)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
var modelList []string
|
|
for i, s := range ModelList {
|
|
modelList = append(modelList, s)
|
|
ModelList[i] = s
|
|
}
|
|
for i, s := range claude.ModelList {
|
|
modelList = append(modelList, s)
|
|
claude.ModelList[i] = s
|
|
}
|
|
for i, s := range gemini.ModelList {
|
|
modelList = append(modelList, s)
|
|
gemini.ModelList[i] = s
|
|
}
|
|
return modelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return ChannelName
|
|
}
|