mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-27 10:18:37 +00:00
✨ feat(architecture): Core+Plugin
This commit is contained in:
114
plugins/channels/base_plugin.go
Normal file
114
plugins/channels/base_plugin.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"github.com/QuantumNous/new-api/relay/channel"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// BaseChannelPlugin 基础Channel插件
|
||||
// 包装现有的Adaptor实现,使其符合ChannelPlugin接口
|
||||
type BaseChannelPlugin struct {
|
||||
adaptor channel.Adaptor
|
||||
name string
|
||||
version string
|
||||
priority int
|
||||
}
|
||||
|
||||
// NewBaseChannelPlugin 创建基础Channel插件
|
||||
func NewBaseChannelPlugin(adaptor channel.Adaptor, name, version string, priority int) *BaseChannelPlugin {
|
||||
return &BaseChannelPlugin{
|
||||
adaptor: adaptor,
|
||||
name: name,
|
||||
version: version,
|
||||
priority: priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Name 返回插件名称
|
||||
func (p *BaseChannelPlugin) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
// Version 返回插件版本
|
||||
func (p *BaseChannelPlugin) Version() string {
|
||||
return p.version
|
||||
}
|
||||
|
||||
// Priority 返回优先级
|
||||
func (p *BaseChannelPlugin) Priority() int {
|
||||
return p.priority
|
||||
}
|
||||
|
||||
// 以下方法直接委托给内部的Adaptor
|
||||
|
||||
func (p *BaseChannelPlugin) Init(info *relaycommon.RelayInfo) {
|
||||
p.adaptor.Init(info)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
||||
return p.adaptor.GetRequestURL(info)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
||||
return p.adaptor.SetupRequestHeader(c, req, info)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
|
||||
return p.adaptor.ConvertOpenAIRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
||||
return p.adaptor.ConvertRerankRequest(c, relayMode, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
|
||||
return p.adaptor.ConvertEmbeddingRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||
return p.adaptor.ConvertAudioRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
|
||||
return p.adaptor.ConvertImageRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
|
||||
return p.adaptor.ConvertOpenAIResponsesRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
|
||||
return p.adaptor.DoRequest(c, info, requestBody)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||
return p.adaptor.DoResponse(c, resp, info)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) GetModelList() []string {
|
||||
return p.adaptor.GetModelList()
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) GetChannelName() string {
|
||||
return p.adaptor.GetChannelName()
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
|
||||
return p.adaptor.ConvertClaudeRequest(c, info, request)
|
||||
}
|
||||
|
||||
func (p *BaseChannelPlugin) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
|
||||
return p.adaptor.ConvertGeminiRequest(c, info, request)
|
||||
}
|
||||
|
||||
// GetAdaptor 获取内部的Adaptor(用于向后兼容)
|
||||
func (p *BaseChannelPlugin) GetAdaptor() channel.Adaptor {
|
||||
return p.adaptor
|
||||
}
|
||||
|
||||
106
plugins/channels/registry.go
Normal file
106
plugins/channels/registry.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/core/registry"
|
||||
"github.com/QuantumNous/new-api/relay/channel"
|
||||
"github.com/QuantumNous/new-api/relay/channel/ali"
|
||||
"github.com/QuantumNous/new-api/relay/channel/aws"
|
||||
"github.com/QuantumNous/new-api/relay/channel/baidu"
|
||||
"github.com/QuantumNous/new-api/relay/channel/baidu_v2"
|
||||
"github.com/QuantumNous/new-api/relay/channel/claude"
|
||||
"github.com/QuantumNous/new-api/relay/channel/cloudflare"
|
||||
"github.com/QuantumNous/new-api/relay/channel/cohere"
|
||||
"github.com/QuantumNous/new-api/relay/channel/coze"
|
||||
"github.com/QuantumNous/new-api/relay/channel/deepseek"
|
||||
"github.com/QuantumNous/new-api/relay/channel/dify"
|
||||
"github.com/QuantumNous/new-api/relay/channel/gemini"
|
||||
"github.com/QuantumNous/new-api/relay/channel/jimeng"
|
||||
"github.com/QuantumNous/new-api/relay/channel/jina"
|
||||
"github.com/QuantumNous/new-api/relay/channel/mistral"
|
||||
"github.com/QuantumNous/new-api/relay/channel/mokaai"
|
||||
"github.com/QuantumNous/new-api/relay/channel/moonshot"
|
||||
"github.com/QuantumNous/new-api/relay/channel/ollama"
|
||||
"github.com/QuantumNous/new-api/relay/channel/openai"
|
||||
"github.com/QuantumNous/new-api/relay/channel/palm"
|
||||
"github.com/QuantumNous/new-api/relay/channel/perplexity"
|
||||
"github.com/QuantumNous/new-api/relay/channel/siliconflow"
|
||||
"github.com/QuantumNous/new-api/relay/channel/submodel"
|
||||
"github.com/QuantumNous/new-api/relay/channel/tencent"
|
||||
"github.com/QuantumNous/new-api/relay/channel/vertex"
|
||||
"github.com/QuantumNous/new-api/relay/channel/volcengine"
|
||||
"github.com/QuantumNous/new-api/relay/channel/xai"
|
||||
"github.com/QuantumNous/new-api/relay/channel/xunfei"
|
||||
"github.com/QuantumNous/new-api/relay/channel/zhipu"
|
||||
"github.com/QuantumNous/new-api/relay/channel/zhipu_4v"
|
||||
)
|
||||
|
||||
// init 包初始化时自动注册所有Channel插件
|
||||
func init() {
|
||||
RegisterAllChannels()
|
||||
}
|
||||
|
||||
// RegisterAllChannels 注册所有Channel插件
|
||||
func RegisterAllChannels() {
|
||||
// 包装现有的Adaptor并注册为插件
|
||||
channels := []struct {
|
||||
channelType int
|
||||
adaptor channel.Adaptor
|
||||
name string
|
||||
}{
|
||||
{constant.APITypeOpenAI, &openai.Adaptor{}, "openai"},
|
||||
{constant.APITypeAnthropic, &claude.Adaptor{}, "claude"},
|
||||
{constant.APITypeGemini, &gemini.Adaptor{}, "gemini"},
|
||||
{constant.APITypeAli, &ali.Adaptor{}, "ali"},
|
||||
{constant.APITypeBaidu, &baidu.Adaptor{}, "baidu"},
|
||||
{constant.APITypeBaiduV2, &baidu_v2.Adaptor{}, "baidu_v2"},
|
||||
{constant.APITypeTencent, &tencent.Adaptor{}, "tencent"},
|
||||
{constant.APITypeXunfei, &xunfei.Adaptor{}, "xunfei"},
|
||||
{constant.APITypeZhipu, &zhipu.Adaptor{}, "zhipu"},
|
||||
{constant.APITypeZhipuV4, &zhipu_4v.Adaptor{}, "zhipu_v4"},
|
||||
{constant.APITypeOllama, &ollama.Adaptor{}, "ollama"},
|
||||
{constant.APITypePerplexity, &perplexity.Adaptor{}, "perplexity"},
|
||||
{constant.APITypeAws, &aws.Adaptor{}, "aws"},
|
||||
{constant.APITypeCohere, &cohere.Adaptor{}, "cohere"},
|
||||
{constant.APITypeDify, &dify.Adaptor{}, "dify"},
|
||||
{constant.APITypeJina, &jina.Adaptor{}, "jina"},
|
||||
{constant.APITypeCloudflare, &cloudflare.Adaptor{}, "cloudflare"},
|
||||
{constant.APITypeSiliconFlow, &siliconflow.Adaptor{}, "siliconflow"},
|
||||
{constant.APITypeVertexAi, &vertex.Adaptor{}, "vertex"},
|
||||
{constant.APITypeMistral, &mistral.Adaptor{}, "mistral"},
|
||||
{constant.APITypeDeepSeek, &deepseek.Adaptor{}, "deepseek"},
|
||||
{constant.APITypeMokaAI, &mokaai.Adaptor{}, "mokaai"},
|
||||
{constant.APITypeVolcEngine, &volcengine.Adaptor{}, "volcengine"},
|
||||
{constant.APITypeXai, &xai.Adaptor{}, "xai"},
|
||||
{constant.APITypeCoze, &coze.Adaptor{}, "coze"},
|
||||
{constant.APITypeJimeng, &jimeng.Adaptor{}, "jimeng"},
|
||||
{constant.APITypeMoonshot, &moonshot.Adaptor{}, "moonshot"},
|
||||
{constant.APITypeSubmodel, &submodel.Adaptor{}, "submodel"},
|
||||
{constant.APITypePaLM, &palm.Adaptor{}, "palm"},
|
||||
// OpenRouter 和 Xinference 使用 OpenAI adaptor
|
||||
{constant.APITypeOpenRouter, &openai.Adaptor{}, "openrouter"},
|
||||
{constant.APITypeXinference, &openai.Adaptor{}, "xinference"},
|
||||
}
|
||||
|
||||
registeredCount := 0
|
||||
for _, ch := range channels {
|
||||
plugin := NewBaseChannelPlugin(
|
||||
ch.adaptor,
|
||||
ch.name,
|
||||
"1.0.0",
|
||||
100, // 默认优先级
|
||||
)
|
||||
|
||||
if err := registry.RegisterChannel(ch.channelType, plugin); err != nil {
|
||||
common.SysError("Failed to register channel plugin: " + ch.name + ", error: " + err.Error())
|
||||
} else {
|
||||
registeredCount++
|
||||
}
|
||||
}
|
||||
|
||||
common.SysLog(fmt.Sprintf("Registered %d channel plugins", registeredCount))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user