refactor(models): delete the internal/models/provider façade (#3506)

main
lyingbug 2026-09-21 14:41:48 +08:00 committed by GitHub
parent 2d37f8fd7d
commit 41dde3b05f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 35 additions and 95 deletions

View File

@ -11,9 +11,9 @@ import (
"github.com/Tencent/WeKnora/internal/models/asr"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/models/rerank"
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
"github.com/Tencent/WeKnora/internal/models/vendors/weknoracloud"
"github.com/Tencent/WeKnora/internal/models/vlm"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@ -70,7 +70,7 @@ func (s *modelService) resolveWeKnoraCloudCredentials(ctx context.Context, param
appID = params.AppID
appSecret = s.decryptAppSecret(params.AppSecret)
if provider.ProviderName(params.Provider) != provider.ProviderWeKnoraCloud {
if params.Provider != weknoracloud.ID {
return
}
if appID != "" && appSecret != "" {

View File

@ -8,8 +8,8 @@ import (
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/provider"
modelsutils "github.com/Tencent/WeKnora/internal/models/utils"
"github.com/Tencent/WeKnora/internal/models/vendors/weknoracloud"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
@ -30,7 +30,8 @@ func NewWeKnoraCloudService(
}
func IsWeKnoraCloudDocReaderAddr(addr string) bool {
return strings.TrimSuffix(strings.TrimSpace(addr), "/") == strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")+"/api/v1/doc/reader"
readerURL := strings.TrimRight(weknoracloud.BaseURL, "/") + "/api/v1/doc/reader"
return strings.TrimSuffix(strings.TrimSpace(addr), "/") == readerURL
}
// SaveCredentials 仅保存 APPID/APPSECRET 凭证,不自动创建模型
@ -55,7 +56,7 @@ func (s *weKnoraCloudService) SaveCredentials(ctx context.Context, appID, appSec
// 注意:health 一般为探活接口,远端常不校验 APPID/SECRET 或签名;HTTP 200 通常只表示
// 「网关/服务可达」,不能严格证明凭证有效。若需强校验,应改为调用必须鉴权的业务接口。
func (s *weKnoraCloudService) verifyCredentials(ctx context.Context, appID, appSecret string) error {
baseURL := strings.TrimRight(provider.WeKnoraCloudBaseURL, "/")
baseURL := strings.TrimRight(weknoracloud.BaseURL, "/")
healthURL := baseURL + "/api/v1/health"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)

View File

@ -25,9 +25,10 @@ import (
"github.com/Tencent/WeKnora/internal/models/asr"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/models/rerank"
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
"github.com/Tencent/WeKnora/internal/models/vendors/lkeap"
"github.com/Tencent/WeKnora/internal/models/vendors/volcengine"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
@ -2167,7 +2168,9 @@ func (h *InitializationHandler) CheckRerankModel(c *gin.Context) {
}
model := h.buildTestModel(&req, types.ModelTypeRerank, types.ModelSourceRemote)
if providerName := provider.ProviderName(model.Parameters.Provider); providerName == provider.ProviderLKEAP || providerName == provider.ProviderVolcengine {
// LKEAP and Volcengine rerank sign with a key pair stored on the row
// itself, not with the tenant's WeKnora Cloud credentials.
if p := model.Parameters.Provider; p == lkeap.ID || p == volcengine.ID {
appID = ""
appSecret = decryptModelAppSecret(model.Parameters.AppSecret)
}

View File

@ -1,58 +0,0 @@
// Package provider is the legacy façade over internal/models/catalog. It keeps
// the ProviderName constants and the two helpers older call sites still use
// (DetectProvider and the base URL constants) so those sites keep compiling
// while the catalog is the single source of vendor facts. New code should
// import the catalog directly.
package provider
import "github.com/Tencent/WeKnora/internal/models/catalog"
// ProviderName 模型服务商名称(catalog vendor id)。
//
//nolint:revive // historical name used across the code base
type ProviderName string
// Vendor ids; the values match the catalog registrations.
const (
ProviderOpenAI ProviderName = "openai"
ProviderAnthropic ProviderName = "anthropic"
ProviderAliyun ProviderName = "aliyun"
ProviderZhipu ProviderName = "zhipu"
ProviderOpenRouter ProviderName = "openrouter"
ProviderLiteLLM ProviderName = "litellm"
ProviderRequesty ProviderName = "requesty"
ProviderSiliconFlow ProviderName = "siliconflow"
ProviderJina ProviderName = "jina"
ProviderGeneric ProviderName = "generic"
ProviderDeepSeek ProviderName = "deepseek"
ProviderGemini ProviderName = "gemini"
ProviderVolcengine ProviderName = "volcengine"
ProviderHunyuan ProviderName = "hunyuan"
ProviderMiniMax ProviderName = "minimax"
ProviderMimo ProviderName = "mimo"
ProviderGPUStack ProviderName = "gpustack"
ProviderMoonshot ProviderName = "moonshot"
ProviderModelScope ProviderName = "modelscope"
ProviderQianfan ProviderName = "qianfan"
ProviderQiniu ProviderName = "qiniu"
ProviderLongCat ProviderName = "longcat"
ProviderLKEAP ProviderName = "lkeap"
ProviderNvidia ProviderName = "nvidia"
ProviderNovita ProviderName = "novita"
ProviderAzureOpenAI ProviderName = "azure_openai"
ProviderWeKnoraCloud ProviderName = "weknoracloud"
)
// Base URLs still referenced by embedding / rerank / service code.
const (
WeKnoraCloudBaseURL = "https://weknora.weixin.qq.com"
ZhipuEmbeddingBaseURL = "https://open.bigmodel.cn/api/paas/v4"
VolcengineRerankBaseURL = "https://api-knowledgebase.mlp.cn-beijing.volces.com"
DeepSeekBaseURL = "https://api.deepseek.com/v1"
)
// DetectProvider identifies a vendor from a base URL (legacy rows without a
// stored provider id).
func DetectProvider(baseURL string) ProviderName {
return ProviderName(catalog.DetectByURL(baseURL))
}

View File

@ -3,30 +3,28 @@ package rerank
import (
"testing"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/models/catalog"
)
// TestDetectProviderSeesVendorCatalog guards the blank import of
// TestDetectByURLSeesVendorCatalog guards the blank import of
// internal/models/vendors in reranker.go.
//
// provider.DetectProvider is a thin wrapper over catalog.DetectByURL, which
// returns "generic" for every URL while the catalog is empty. Without the
// vendor packages linked in, a stored rerank row that carries no explicit
// provider id (the legacy shape DetectProvider exists for) would silently be
// built as a plain OpenAI-compatible reranker: LKEAP and Volcengine rows would
// stop being TC3/AK-SK signed and fail at call time instead.
func TestDetectProviderSeesVendorCatalog(t *testing.T) {
cases := map[string]provider.ProviderName{
"https://api.lkeap.cloud.tencent.com/v1": provider.ProviderLKEAP,
"https://api-knowledgebase.mlp.cn-beijing.volces.com": provider.ProviderVolcengine,
"https://api.jina.ai/v1": provider.ProviderJina,
"https://open.bigmodel.cn/api/paas/v4": provider.ProviderZhipu,
"https://dashscope.aliyuncs.com/compatible-mode/v1": provider.ProviderAliyun,
"https://some-self-hosted-gateway.example.internal/v1": provider.ProviderGeneric,
// catalog.DetectByURL returns "generic" for every URL while the catalog is
// empty. Without the vendor packages linked in, a stored rerank row that
// carries no provider id resolves to the generic Cohere client: LKEAP and
// Volcengine lose their signed SDK clients and Aliyun its native protocol.
func TestDetectByURLSeesVendorCatalog(t *testing.T) {
cases := map[string]string{
"https://api.lkeap.cloud.tencent.com/v1": "lkeap",
"https://api-knowledgebase.mlp.cn-beijing.volces.com": "volcengine",
"https://api.jina.ai/v1": "jina",
"https://open.bigmodel.cn/api/paas/v4": "zhipu",
"https://dashscope.aliyuncs.com/compatible-mode/v1": "aliyun",
"https://some-self-hosted-gateway.example.internal/v1": catalog.GenericID,
}
for baseURL, want := range cases {
if got := provider.DetectProvider(baseURL); got != want {
t.Errorf("DetectProvider(%q) = %q, want %q", baseURL, got, want)
if got := catalog.DetectByURL(baseURL); got != want {
t.Errorf("DetectByURL(%q) = %q, want %q", baseURL, got, want)
}
}
}

View File

@ -8,15 +8,11 @@ import (
"github.com/Tencent/WeKnora/internal/models/api"
"github.com/Tencent/WeKnora/internal/models/catalog"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/volcengine/vikingdb-go-sdk/knowledge"
knowledgemodel "github.com/volcengine/vikingdb-go-sdk/knowledge/model"
)
const (
// VolcengineRerankBaseURL is the managed Knowledge Service host.
VolcengineRerankBaseURL = provider.VolcengineRerankBaseURL
volcengineRerankDefaultModel = "doubao-seed-rerank"
volcengineRerankDefaultRegion = "cn-beijing"
volcengineRerankDefaultInstruction = "Whether the Document answers the Query or matches the content retrieval intent"
@ -42,10 +38,9 @@ func newVolcengineClient(config *RerankerConfig, resolved *catalog.Resolved) (ap
return nil, fmt.Errorf("access key and secret key are required for Volcengine rerank")
}
// The catalog supplies the vendor's Knowledge Service host when the row
// names none.
baseURL := resolved.BaseURL
if baseURL == "" {
baseURL = VolcengineRerankBaseURL
}
modelName := strings.TrimSpace(resolved.RemoteModel)
if modelName == "" {

View File

@ -6,8 +6,9 @@ import (
"strings"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/models/catalog"
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
"github.com/Tencent/WeKnora/internal/models/vendors/weknoracloud"
"github.com/Tencent/WeKnora/internal/types"
)
@ -110,11 +111,11 @@ func newVLM(config *Config, ollamaService *ollama.OllamaService) (VLM, error) {
return NewOllamaVLM(config, ollamaService)
}
providerName := provider.ProviderName(config.Provider)
if providerName == "" {
providerName = provider.DetectProvider(config.BaseURL)
providerID := config.Provider
if providerID == "" {
providerID = catalog.DetectByURL(config.BaseURL)
}
if providerName == provider.ProviderWeKnoraCloud {
if providerID == weknoracloud.ID {
return NewWeKnoraCloudVLM(config)
}