mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-07 01:56:53 +00:00
feat: advanced custom channel (#5590)
This commit is contained in:
@@ -0,0 +1,545 @@
|
||||
package advancedcustom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"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"
|
||||
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
||||
"github.com/QuantumNous/new-api/service"
|
||||
"github.com/QuantumNous/new-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
const ChannelName = "advanced_custom"
|
||||
|
||||
const advancedCustomModelPlaceholder = "{model}"
|
||||
|
||||
type Adaptor struct {
|
||||
openaiAdaptor openai.Adaptor
|
||||
claudeAdaptor claude.Adaptor
|
||||
geminiAdaptor gemini.Adaptor
|
||||
|
||||
resolved bool
|
||||
fallback bool
|
||||
converted bool
|
||||
route dto.AdvancedCustomRoute
|
||||
converter string
|
||||
}
|
||||
|
||||
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
||||
a.openaiAdaptor.Init(info)
|
||||
a.claudeAdaptor.Init(info)
|
||||
a.geminiAdaptor.Init(info)
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.fallback || converter == dto.AdvancedCustomConverterNone {
|
||||
return a.convertOpenAICompatibleRequest(c, info, request)
|
||||
}
|
||||
|
||||
switch converter {
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages:
|
||||
return a.claudeAdaptor.ConvertOpenAIRequest(c, info, request)
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToOpenAIResponses:
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
return service.ChatCompletionsRequestToResponsesRequest(request)
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent:
|
||||
return a.geminiAdaptor.ConvertOpenAIRequest(c, info, request)
|
||||
default:
|
||||
return nil, fmt.Errorf("converter %q does not support OpenAI chat completions requests", converter)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.fallback {
|
||||
return a.convertClaudeToOpenAICompatibleRequest(c, info, request)
|
||||
}
|
||||
|
||||
switch converter {
|
||||
case dto.AdvancedCustomConverterNone:
|
||||
return a.claudeAdaptor.ConvertClaudeRequest(c, info, request)
|
||||
case dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions:
|
||||
return a.convertClaudeToOpenAICompatibleRequest(c, info, request)
|
||||
default:
|
||||
return nil, fmt.Errorf("converter %q does not support Anthropic Messages requests", converter)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.fallback {
|
||||
return a.convertGeminiToOpenAICompatibleRequest(c, info, request)
|
||||
}
|
||||
|
||||
switch converter {
|
||||
case dto.AdvancedCustomConverterNone:
|
||||
return a.geminiAdaptor.ConvertGeminiRequest(c, info, request)
|
||||
case dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions:
|
||||
return a.convertGeminiToOpenAICompatibleRequest(c, info, request)
|
||||
default:
|
||||
return nil, fmt.Errorf("converter %q does not support Gemini generateContent requests", converter)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if converter != dto.AdvancedCustomConverterNone {
|
||||
return nil, fmt.Errorf("converter %q does not support OpenAI Responses requests", converter)
|
||||
}
|
||||
return a.convertOpenAICompatibleResponsesRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if converter != dto.AdvancedCustomConverterNone {
|
||||
return nil, fmt.Errorf("converter %q does not support embedding requests", converter)
|
||||
}
|
||||
return a.convertOpenAICompatibleEmbeddingRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if converter != dto.AdvancedCustomConverterNone {
|
||||
return nil, fmt.Errorf("converter %q does not support audio requests", converter)
|
||||
}
|
||||
return a.convertOpenAICompatibleAudioRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
||||
converter, err := a.resolveForConversion(c, info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if converter != dto.AdvancedCustomConverterNone {
|
||||
return nil, fmt.Errorf("converter %q does not support image requests", converter)
|
||||
}
|
||||
return a.convertOpenAICompatibleImageRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
||||
a.converted = true
|
||||
return a.openaiAdaptor.ConvertRerankRequest(c, relayMode, request)
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
||||
if err := a.resolve(nil, info); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if a.fallback {
|
||||
return a.withTemporaryChannelType(info, constant.ChannelTypeOpenAI, func() (string, error) {
|
||||
return a.openaiAdaptor.GetRequestURL(info)
|
||||
})
|
||||
}
|
||||
return a.routeURL(info)
|
||||
}
|
||||
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, header *http.Header, info *relaycommon.RelayInfo) error {
|
||||
if err := a.resolve(c, info); err != nil {
|
||||
return err
|
||||
}
|
||||
if a.fallback {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
err := a.openaiAdaptor.SetupRequestHeader(c, header, info)
|
||||
info.ChannelType = old
|
||||
return err
|
||||
}
|
||||
|
||||
channel.SetupApiRequestHeader(info, c, header)
|
||||
auth := a.route.Auth
|
||||
if auth == nil {
|
||||
header.Set("Authorization", "Bearer "+info.ApiKey)
|
||||
} else {
|
||||
switch strings.TrimSpace(auth.Type) {
|
||||
case dto.AdvancedCustomAuthTypeNone:
|
||||
case dto.AdvancedCustomAuthTypeHeader:
|
||||
header.Set(strings.TrimSpace(auth.Name), applyAuthTemplate(auth.Value, info.ApiKey))
|
||||
case dto.AdvancedCustomAuthTypeQuery:
|
||||
default:
|
||||
return fmt.Errorf("invalid advanced custom auth type: %s", auth.Type)
|
||||
}
|
||||
}
|
||||
|
||||
if shouldApplyClaudeHeaders(a.converter, info) {
|
||||
applyClaudeHeaders(c, header, info)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
||||
if err := a.resolve(c, info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !a.converted && (a.fallback || a.converter != dto.AdvancedCustomConverterNone) {
|
||||
return nil, errors.New("advanced custom converter routes cannot be used with pass-through request body")
|
||||
}
|
||||
|
||||
if info.RelayMode == relayconstant.RelayModeAudioTranscription ||
|
||||
info.RelayMode == relayconstant.RelayModeAudioTranslation ||
|
||||
(info.RelayMode == relayconstant.RelayModeImagesEdits && !isJSONRequest(c)) {
|
||||
return channel.DoFormRequest(a, c, info, requestBody)
|
||||
}
|
||||
if info.RelayMode == relayconstant.RelayModeRealtime {
|
||||
return channel.DoWssRequest(a, c, info, requestBody)
|
||||
}
|
||||
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) {
|
||||
if err := a.resolve(c, info); err != nil {
|
||||
return nil, types.NewOpenAIError(err, types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||
}
|
||||
if a.fallback {
|
||||
return a.openaiAdaptor.DoResponse(c, resp, info)
|
||||
}
|
||||
|
||||
switch a.converter {
|
||||
case dto.AdvancedCustomConverterNone:
|
||||
return a.doNativeResponse(c, resp, info)
|
||||
case dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
|
||||
dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions:
|
||||
return a.openaiAdaptor.DoResponse(c, resp, info)
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages:
|
||||
return a.claudeAdaptor.DoResponse(c, resp, info)
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent:
|
||||
return a.geminiAdaptor.DoResponse(c, resp, info)
|
||||
case dto.AdvancedCustomConverterOpenAIChatCompletionsToOpenAIResponses:
|
||||
if info.IsStream {
|
||||
return openai.OaiResponsesToChatStreamHandler(c, info, resp)
|
||||
}
|
||||
return openai.OaiResponsesToChatHandler(c, info, resp)
|
||||
default:
|
||||
return nil, types.NewOpenAIError(fmt.Errorf("unsupported advanced custom converter: %s", a.converter), types.ErrorCodeInvalidRequest, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetModelList() []string {
|
||||
models := make([]string, 0, len(openai.ModelList)+len(claude.ModelList)+len(gemini.ModelList))
|
||||
models = append(models, openai.ModelList...)
|
||||
models = append(models, claude.ModelList...)
|
||||
models = append(models, gemini.ModelList...)
|
||||
return lo.Uniq(models)
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetChannelName() string {
|
||||
return ChannelName
|
||||
}
|
||||
|
||||
func (a *Adaptor) doNativeResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (any, *types.NewAPIError) {
|
||||
switch info.RelayFormat {
|
||||
case types.RelayFormatClaude:
|
||||
return a.claudeAdaptor.DoResponse(c, resp, info)
|
||||
case types.RelayFormatGemini:
|
||||
return a.geminiAdaptor.DoResponse(c, resp, info)
|
||||
default:
|
||||
return a.openaiAdaptor.DoResponse(c, resp, info)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Adaptor) resolveForConversion(c *gin.Context, info *relaycommon.RelayInfo) (string, error) {
|
||||
if err := a.resolve(c, info); err != nil {
|
||||
return "", err
|
||||
}
|
||||
a.converted = true
|
||||
return a.converter, nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) resolve(c *gin.Context, info *relaycommon.RelayInfo) error {
|
||||
if a.resolved {
|
||||
return nil
|
||||
}
|
||||
if info == nil {
|
||||
return errors.New("missing relay info")
|
||||
}
|
||||
config := info.ChannelOtherSettings.AdvancedCustom
|
||||
if config == nil {
|
||||
return errors.New("advanced_custom is required")
|
||||
}
|
||||
if err := config.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
incomingPath := incomingRequestPath(c, info)
|
||||
route, ok := lo.Find(config.Routes, func(route dto.AdvancedCustomRoute) bool {
|
||||
return matchIncomingPath(strings.TrimSpace(route.IncomingPath), incomingPath)
|
||||
})
|
||||
if ok {
|
||||
route.Converter = strings.TrimSpace(route.Converter)
|
||||
if route.Converter == "" {
|
||||
route.Converter = dto.AdvancedCustomConverterNone
|
||||
}
|
||||
a.route = route
|
||||
a.converter = route.Converter
|
||||
a.resolved = true
|
||||
return nil
|
||||
}
|
||||
if config.Fallback.Enabled {
|
||||
a.fallback = true
|
||||
a.converter = dto.AdvancedCustomConverterNone
|
||||
a.resolved = true
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("advanced custom route not found for path: %s", incomingPath)
|
||||
}
|
||||
|
||||
func incomingRequestPath(c *gin.Context, info *relaycommon.RelayInfo) string {
|
||||
if c != nil && c.Request != nil && c.Request.URL != nil {
|
||||
return c.Request.URL.Path
|
||||
}
|
||||
if info == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.Split(info.RequestURLPath, "?")[0]
|
||||
}
|
||||
|
||||
func matchIncomingPath(configuredPath string, requestPath string) bool {
|
||||
if matchIncomingPathTemplate(configuredPath, requestPath) {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(configuredPath, ":generateContent") {
|
||||
streamPath := strings.Replace(configuredPath, ":generateContent", ":streamGenerateContent", 1)
|
||||
return matchIncomingPathTemplate(streamPath, requestPath)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func matchIncomingPathTemplate(configuredPath string, requestPath string) bool {
|
||||
if !strings.Contains(configuredPath, advancedCustomModelPlaceholder) {
|
||||
return configuredPath == requestPath
|
||||
}
|
||||
|
||||
parts := strings.Split(configuredPath, advancedCustomModelPlaceholder)
|
||||
if len(parts) != 2 {
|
||||
return false
|
||||
}
|
||||
if !strings.HasPrefix(requestPath, parts[0]) || !strings.HasSuffix(requestPath, parts[1]) {
|
||||
return false
|
||||
}
|
||||
|
||||
model := strings.TrimSuffix(strings.TrimPrefix(requestPath, parts[0]), parts[1])
|
||||
return model != "" && !strings.Contains(model, "/")
|
||||
}
|
||||
|
||||
func (a *Adaptor) routeURL(info *relaycommon.RelayInfo) (string, error) {
|
||||
parsedURL, err := resolveUpstreamTargetURL(applyUpstreamPathTemplate(strings.TrimSpace(a.route.UpstreamPath), info), info)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if shouldUseGeminiStreamURL(a.converter, info) {
|
||||
useGeminiStreamGenerateContentURL(parsedURL)
|
||||
}
|
||||
if info != nil && info.RelayMode == relayconstant.RelayModeRealtime {
|
||||
switch parsedURL.Scheme {
|
||||
case "https":
|
||||
parsedURL.Scheme = "wss"
|
||||
case "http":
|
||||
parsedURL.Scheme = "ws"
|
||||
}
|
||||
}
|
||||
if a.route.Auth != nil && strings.TrimSpace(a.route.Auth.Type) == dto.AdvancedCustomAuthTypeQuery {
|
||||
query := parsedURL.Query()
|
||||
query.Set(strings.TrimSpace(a.route.Auth.Name), applyAuthTemplate(a.route.Auth.Value, info.ApiKey))
|
||||
parsedURL.RawQuery = query.Encode()
|
||||
}
|
||||
return parsedURL.String(), nil
|
||||
}
|
||||
|
||||
func resolveUpstreamTargetURL(upstreamPath string, info *relaycommon.RelayInfo) (*url.URL, error) {
|
||||
if strings.HasPrefix(upstreamPath, "/") {
|
||||
if strings.HasPrefix(upstreamPath, "//") {
|
||||
return nil, errors.New("advanced custom upstream path must be a full URL or a path starting with /")
|
||||
}
|
||||
if info == nil || strings.TrimSpace(info.ChannelBaseUrl) == "" {
|
||||
return nil, errors.New("channel base URL is required when advanced custom upstream path is relative")
|
||||
}
|
||||
return joinBaseURLAndUpstreamPath(info.ChannelBaseUrl, upstreamPath)
|
||||
}
|
||||
|
||||
parsedURL, err := url.Parse(upstreamPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if parsedURL.Scheme == "" || parsedURL.Host == "" {
|
||||
return nil, errors.New("advanced custom upstream path must be a full URL or a path starting with /")
|
||||
}
|
||||
if !strings.EqualFold(parsedURL.Scheme, "http") && !strings.EqualFold(parsedURL.Scheme, "https") {
|
||||
return nil, errors.New("advanced custom upstream path must use http or https")
|
||||
}
|
||||
return parsedURL, nil
|
||||
}
|
||||
|
||||
func joinBaseURLAndUpstreamPath(baseURL string, upstreamPath string) (*url.URL, error) {
|
||||
parsedBaseURL, err := url.Parse(strings.TrimSpace(baseURL))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if parsedBaseURL.Scheme == "" || parsedBaseURL.Host == "" {
|
||||
return nil, errors.New("channel base URL must be a full URL when advanced custom upstream path is relative")
|
||||
}
|
||||
if !strings.EqualFold(parsedBaseURL.Scheme, "http") && !strings.EqualFold(parsedBaseURL.Scheme, "https") {
|
||||
return nil, errors.New("channel base URL must use http or https when advanced custom upstream path is relative")
|
||||
}
|
||||
|
||||
parsedPath, err := url.Parse(upstreamPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsedBaseURL.Path = strings.TrimRight(parsedBaseURL.Path, "/") + "/" + strings.TrimLeft(parsedPath.Path, "/")
|
||||
parsedBaseURL.RawPath = ""
|
||||
parsedBaseURL.RawQuery = parsedPath.RawQuery
|
||||
parsedBaseURL.Fragment = parsedPath.Fragment
|
||||
return parsedBaseURL, nil
|
||||
}
|
||||
|
||||
func applyUpstreamPathTemplate(upstreamPath string, info *relaycommon.RelayInfo) string {
|
||||
if info == nil {
|
||||
return upstreamPath
|
||||
}
|
||||
return strings.ReplaceAll(upstreamPath, advancedCustomModelPlaceholder, info.UpstreamModelName)
|
||||
}
|
||||
|
||||
func shouldUseGeminiStreamURL(converter string, info *relaycommon.RelayInfo) bool {
|
||||
return info != nil &&
|
||||
info.IsStream &&
|
||||
converter == dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent
|
||||
}
|
||||
|
||||
func useGeminiStreamGenerateContentURL(parsedURL *url.URL) {
|
||||
if strings.Contains(parsedURL.Path, ":generateContent") {
|
||||
parsedURL.Path = strings.Replace(parsedURL.Path, ":generateContent", ":streamGenerateContent", 1)
|
||||
}
|
||||
if strings.Contains(parsedURL.Path, ":streamGenerateContent") {
|
||||
query := parsedURL.Query()
|
||||
query.Set("alt", "sse")
|
||||
parsedURL.RawQuery = query.Encode()
|
||||
}
|
||||
}
|
||||
|
||||
func shouldApplyClaudeHeaders(converter string, info *relaycommon.RelayInfo) bool {
|
||||
return converter == dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages ||
|
||||
(converter == dto.AdvancedCustomConverterNone && info != nil && info.RelayFormat == types.RelayFormatClaude)
|
||||
}
|
||||
|
||||
func applyClaudeHeaders(c *gin.Context, header *http.Header, info *relaycommon.RelayInfo) {
|
||||
anthropicVersion := ""
|
||||
if c != nil && c.Request != nil {
|
||||
anthropicVersion = c.Request.Header.Get("anthropic-version")
|
||||
}
|
||||
if anthropicVersion == "" {
|
||||
anthropicVersion = "2023-06-01"
|
||||
}
|
||||
header.Set("anthropic-version", anthropicVersion)
|
||||
if c != nil {
|
||||
claude.CommonClaudeHeadersOperation(c, header, info)
|
||||
}
|
||||
}
|
||||
|
||||
func applyAuthTemplate(template string, apiKey string) string {
|
||||
return strings.ReplaceAll(template, "{api_key}", apiKey)
|
||||
}
|
||||
|
||||
func isJSONRequest(c *gin.Context) bool {
|
||||
if c == nil || c.Request == nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(strings.ToLower(c.Request.Header.Get("Content-Type")), "application/json")
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertOpenAIRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertClaudeToOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertClaudeRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertGeminiToOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertGeminiRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertOpenAIResponsesRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertEmbeddingRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertAudioRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) convertOpenAICompatibleImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = constant.ChannelTypeOpenAI
|
||||
converted, err := a.openaiAdaptor.ConvertImageRequest(c, info, request)
|
||||
info.ChannelType = old
|
||||
return converted, err
|
||||
}
|
||||
|
||||
func (a *Adaptor) withTemporaryChannelType(info *relaycommon.RelayInfo, channelType int, fn func() (string, error)) (string, error) {
|
||||
old := info.ChannelType
|
||||
info.ChannelType = channelType
|
||||
value, err := fn()
|
||||
info.ChannelType = old
|
||||
return value, err
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
package advancedcustom
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
||||
"github.com/QuantumNous/new-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAdaptorUsesExactRouteAndQueryAuth(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "https://upstream.example/v1/chat/completions?existing=1",
|
||||
Converter: dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeQuery,
|
||||
Name: "api_key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
info.RequestURLPath = "/v1/messages?client=1"
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
|
||||
parsedURL, err := url.Parse(requestURL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https", parsedURL.Scheme)
|
||||
assert.Equal(t, "upstream.example", parsedURL.Host)
|
||||
assert.Equal(t, "/v1/chat/completions", parsedURL.Path)
|
||||
assert.Equal(t, "1", parsedURL.Query().Get("existing"))
|
||||
assert.Equal(t, "sk-test", parsedURL.Query().Get("api_key"))
|
||||
}
|
||||
|
||||
func TestAdaptorJoinsUpstreamPathWithChannelBaseURL(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "/proxy/v1/chat/completions?existing=1",
|
||||
Converter: dto.AdvancedCustomConverterNone,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeQuery,
|
||||
Name: "api_key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
info.ChannelBaseUrl = "https://gateway.example/base"
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
|
||||
parsedURL, err := url.Parse(requestURL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https", parsedURL.Scheme)
|
||||
assert.Equal(t, "gateway.example", parsedURL.Host)
|
||||
assert.Equal(t, "/base/proxy/v1/chat/completions", parsedURL.Path)
|
||||
assert.Equal(t, "1", parsedURL.Query().Get("existing"))
|
||||
assert.Equal(t, "sk-test", parsedURL.Query().Get("api_key"))
|
||||
}
|
||||
|
||||
func TestAdaptorReturnsErrorWhenUpstreamPathNeedsMissingBaseURL(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "/v1/chat/completions",
|
||||
Converter: dto.AdvancedCustomConverterNone,
|
||||
},
|
||||
},
|
||||
})
|
||||
info.ChannelBaseUrl = ""
|
||||
|
||||
_, err := adaptor.GetRequestURL(info)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "base URL is required")
|
||||
}
|
||||
|
||||
func TestAdaptorSetupRequestHeaderUsesDefaultBearerAuth(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "https://upstream.example/v1/chat/completions",
|
||||
Converter: dto.AdvancedCustomConverterNone,
|
||||
},
|
||||
},
|
||||
})
|
||||
c := advancedCustomGinContext("/v1/chat/completions")
|
||||
header := http.Header{}
|
||||
|
||||
require.NoError(t, adaptor.SetupRequestHeader(c, &header, info))
|
||||
assert.Equal(t, "Bearer sk-test", header.Get("Authorization"))
|
||||
}
|
||||
|
||||
func TestAdaptorSetupRequestHeaderUsesConfiguredHeaderAuth(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "https://upstream.example/v1/chat/completions",
|
||||
Converter: dto.AdvancedCustomConverterNone,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeHeader,
|
||||
Name: "x-api-key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
c := advancedCustomGinContext("/v1/chat/completions")
|
||||
header := http.Header{}
|
||||
|
||||
require.NoError(t, adaptor.SetupRequestHeader(c, &header, info))
|
||||
assert.Empty(t, header.Get("Authorization"))
|
||||
assert.Equal(t, "sk-test", header.Get("x-api-key"))
|
||||
}
|
||||
|
||||
func TestAdaptorSetupRequestHeaderAddsClaudeDefaultHeaders(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "https://api.anthropic.com/v1/messages",
|
||||
Converter: dto.AdvancedCustomConverterNone,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeHeader,
|
||||
Name: "x-api-key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
info.RelayFormat = types.RelayFormatClaude
|
||||
c := advancedCustomGinContext("/v1/messages")
|
||||
header := http.Header{}
|
||||
|
||||
require.NoError(t, adaptor.SetupRequestHeader(c, &header, info))
|
||||
assert.Equal(t, "sk-test", header.Get("x-api-key"))
|
||||
assert.Equal(t, "2023-06-01", header.Get("anthropic-version"))
|
||||
}
|
||||
|
||||
func TestAdaptorReturnsErrorWhenNoRouteAndFallbackDisabled(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/messages",
|
||||
UpstreamPath: "https://upstream.example/v1/chat/completions",
|
||||
Converter: dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
|
||||
},
|
||||
},
|
||||
})
|
||||
info.RequestURLPath = "/v1/chat/completions"
|
||||
|
||||
_, err := adaptor.GetRequestURL(info)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "route not found")
|
||||
}
|
||||
|
||||
func TestAdaptorFallbackUsesOpenAICompatibleBaseURL(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Fallback: dto.AdvancedCustomFallback{Enabled: true},
|
||||
})
|
||||
info.RequestURLPath = "/v1/messages"
|
||||
info.RelayFormat = types.RelayFormatClaude
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https://fallback.example/v1/chat/completions", requestURL)
|
||||
}
|
||||
|
||||
func TestAdaptorReplacesModelPlaceholderInRouteURL(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent",
|
||||
Converter: dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeQuery,
|
||||
Name: "key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
info.UpstreamModelName = "gemini-2.5-flash"
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
|
||||
parsedURL, err := url.Parse(requestURL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "/v1beta/models/gemini-2.5-flash:generateContent", parsedURL.Path)
|
||||
assert.Equal(t, "sk-test", parsedURL.Query().Get("key"))
|
||||
assert.Empty(t, parsedURL.Query().Get("alt"))
|
||||
}
|
||||
|
||||
func TestAdaptorSwitchesGeminiGenerateContentURLForStream(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1/chat/completions",
|
||||
UpstreamPath: "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?existing=1",
|
||||
Converter: dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent,
|
||||
Auth: &dto.AdvancedCustomRouteAuth{
|
||||
Type: dto.AdvancedCustomAuthTypeQuery,
|
||||
Name: "key",
|
||||
Value: "{api_key}",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
info.UpstreamModelName = "gemini-2.5-pro"
|
||||
info.IsStream = true
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
|
||||
parsedURL, err := url.Parse(requestURL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "/v1beta/models/gemini-2.5-pro:streamGenerateContent", parsedURL.Path)
|
||||
assert.Equal(t, "sse", parsedURL.Query().Get("alt"))
|
||||
assert.Equal(t, "1", parsedURL.Query().Get("existing"))
|
||||
assert.Equal(t, "sk-test", parsedURL.Query().Get("key"))
|
||||
}
|
||||
|
||||
func TestAdaptorMatchesGeminiIncomingPathTemplate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requestURLPath string
|
||||
wantRequestPath string
|
||||
}{
|
||||
{
|
||||
name: "generate content",
|
||||
requestURLPath: "/v1beta/models/gemini-2.5-flash:generateContent",
|
||||
wantRequestPath: "/v1/chat/completions",
|
||||
},
|
||||
{
|
||||
name: "stream generate content",
|
||||
requestURLPath: "/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse",
|
||||
wantRequestPath: "/v1/chat/completions",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
adaptor := &Adaptor{}
|
||||
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
|
||||
Routes: []dto.AdvancedCustomRoute{
|
||||
{
|
||||
IncomingPath: "/v1beta/models/{model}:generateContent",
|
||||
UpstreamPath: "https://upstream.example/v1/chat/completions",
|
||||
Converter: dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions,
|
||||
},
|
||||
},
|
||||
})
|
||||
info.RequestURLPath = tt.requestURLPath
|
||||
|
||||
requestURL, err := adaptor.GetRequestURL(info)
|
||||
require.NoError(t, err)
|
||||
|
||||
parsedURL, err := url.Parse(requestURL)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantRequestPath, parsedURL.Path)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func advancedCustomRelayInfo(config *dto.AdvancedCustomConfig) *relaycommon.RelayInfo {
|
||||
return &relaycommon.RelayInfo{
|
||||
RelayFormat: types.RelayFormatOpenAI,
|
||||
RelayMode: relayconstant.RelayModeChatCompletions,
|
||||
RequestURLPath: "/v1/chat/completions",
|
||||
ChannelMeta: &relaycommon.ChannelMeta{
|
||||
ApiKey: "sk-test",
|
||||
ChannelBaseUrl: "https://fallback.example",
|
||||
ChannelType: constant.ChannelTypeAdvancedCustom,
|
||||
ChannelOtherSettings: dto.ChannelOtherSettings{
|
||||
AdvancedCustom: config,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func advancedCustomGinContext(path string) *gin.Context {
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodPost, path, nil)
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
return c
|
||||
}
|
||||
Reference in New Issue
Block a user