Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat: add aliyun rerank model
wizardchen4214e6782bad
diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.tsindex fb5718ba5..cc55eb340 100644--- a/frontend/src/api/initialization/index.ts+++ b/frontend/src/api/initialization/index.ts@@ -16,6 +16,7 @@ export interface InitializationConfig {dimension?: number; // 添加embedding维度字段};rerank: {+ source: string;modelName: string;baseUrl: string;apiKey?: string;@@ -235,6 +236,7 @@ export function testEmbeddingModel(modelConfig: {export function checkRerankModel(modelConfig: {+ source: string;modelName: string;baseUrl: string;apiKey?: string;diff --git a/frontend/src/views/initialization/InitializationConfig.vue b/frontend/src/views/initialization/InitializationConfig.vueindex 3bc0a0977..09c5ec207 100644--- a/frontend/src/views/initialization/InitializationConfig.vue+++ b/frontend/src/views/initialization/InitializationConfig.vue@@ -334,10 +334,20 @@<!-- Rerank 详细配置 --><div v-if="formData.rerank.enabled" class="rerank-config">+ <div class="form-row">+ <t-form-item label="模型来源" name="rerank.source">+ <t-radio-group v-model="formData.rerank.source" @change="onModelSourceChange('rerank')">+ <t-radio value="remote">Remote API (远程)</t-radio>+ <t-radio value="aliyun">Aliyun DashScope (阿里云)</t-radio>+ </t-radio-group>+ </t-form-item>+ </div>+<div class="form-row"><t-form-item label="模型名称" name="rerank.modelName"><div class="model-input-with-status">- <t-input v-model="formData.rerank.modelName" placeholder="例如: bge-reranker-v2-m3"+ <t-input v-model="formData.rerank.modelName"+ :placeholder="formData.rerank.source === 'aliyun' ? '例如: text-rerank' : '例如: bge-reranker-v2-m3'"@blur="onRerankConfigChange"@input="onRerankConfigInput" /><div class="model-status-icon">@@ -361,7 +371,8 @@<div class="form-row"><t-form-item label="Base URL" name="rerank.baseUrl"><div class="url-input-with-check">- <t-input v-model="formData.rerank.baseUrl" placeholder="例如: http://localhost:11434, 去除末尾/rerank路径后的URL的前面部分"+ <t-input v-model="formData.rerank.baseUrl"+ :placeholder="formData.rerank.source === 'aliyun' ? '例如: https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank' : '例如: http://localhost:11434, 去除末尾/rerank路径后的URL的前面部分'"@blur="onRerankConfigChange"@input="onRerankConfigInput" /><div v-if="formData.rerank.modelName && formData.rerank.baseUrl" class="check-action">@@ -868,6 +879,7 @@ const formData = reactive({},rerank: {enabled: false,+ source: 'remote',modelName: '',baseUrl: '',apiKey: ''@@ -1344,7 +1356,7 @@ onUnmounted(() => {});// 事件处理-const onModelSourceChange = async (type: 'llm' | 'embedding') => {+const onModelSourceChange = async (type: 'llm' | 'embedding' | 'rerank') => {// 重置模型状态modelStatus[type].checked = false;modelStatus[type].available = false;@@ -1354,6 +1366,15 @@ const onModelSourceChange = async (type: 'llm' | 'embedding') => {if (formData[type].source === 'local' && !ollamaStatus.checked) {await checkOllama();}++ // 对于rerank,根据source设置默认的baseUrl+ if (type === 'rerank') {+ if (formData.rerank.source === 'aliyun') {+ formData.rerank.baseUrl = 'https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank';+ } else if (formData.rerank.source === 'remote') {+ formData.rerank.baseUrl = '';+ }+ }};const onModelNameChange = async (type: 'llm' | 'embedding' | 'vlm') => {@@ -1754,6 +1775,7 @@ const handleSubmit = async () => {// 监听表单变化watch(() => formData.llm.source, () => onModelSourceChange('llm'));watch(() => formData.embedding.source, () => onModelSourceChange('embedding'));+watch(() => formData.rerank.source, () => onModelSourceChange('rerank'));// 组件挂载时检查Ollama状态onMounted(async () => {@@ -1826,6 +1848,7 @@ const checkRerankModelStatus = async () => {modelStatus.rerank.message = '';const result = await checkRerankModel({+ source: formData.rerank.source,modelName: formData.rerank.modelName,baseUrl: formData.rerank.baseUrl,apiKey: formData.rerank.apiKeydiff --git a/internal/models/rerank/aliyun_reranker.go b/internal/models/rerank/aliyun_reranker.gonew file mode 100644index 000000000..fa47b5894--- /dev/null+++ b/internal/models/rerank/aliyun_reranker.go@@ -0,0 +1,146 @@+package rerank++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "io"+ "net/http"++ "github.com/Tencent/WeKnora/internal/logger"+)++// AliyunReranker implements a reranking system based on Aliyun DashScope models+type AliyunReranker struct {+ modelName string // Name of the model used for reranking+ modelID string // Unique identifier of the model+ apiKey string // API key for authentication+ baseURL string // Base URL for API requests+ client *http.Client // HTTP client for making API requests+}++// AliyunRerankRequest represents a request to rerank documents using Aliyun DashScope API+type AliyunRerankRequest struct {+ Model string `json:"model"` // Model to use for reranking+ Query string `json:"query"` // Query text to compare documents against+ Documents []string `json:"documents"` // List of document texts to rerank+}++// AliyunRerankResponse represents the response from Aliyun DashScope reranking request+type AliyunRerankResponse struct {+ Output AliyunOutput `json:"output"` // Output containing results+ Usage AliyunUsage `json:"usage"` // Token usage information+}++// AliyunOutput contains the reranking results+type AliyunOutput struct {+ Results []AliyunRankResult `json:"results"` // Ranked results with relevance scores+}++// AliyunRankResult represents a single reranking result from Aliyun+type AliyunRankResult struct {+ Document AliyunDocument `json:"document"` // Document information+ Index int `json:"index"` // Original index of the document+ RelevanceScore float64 `json:"relevance_score"` // Relevance score+}++// AliyunDocument represents document information in Aliyun response+type AliyunDocument struct {+ Text string `json:"text"` // Document text+}++// AliyunUsage contains information about token usage in the Aliyun API request+type AliyunUsage struct {+ TotalTokens int `json:"total_tokens"` // Total tokens consumed+}++// NewAliyunReranker creates a new instance of Aliyun reranker with the provided configuration+func NewAliyunReranker(config *RerankerConfig) (*AliyunReranker, error) {+ apiKey := config.APIKey+ baseURL := "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank"+ if url := config.BaseURL; url != "" {+ baseURL = url+ }++ return &AliyunReranker{+ modelName: config.ModelName,+ modelID: config.ModelID,+ apiKey: apiKey,+ baseURL: baseURL,+ client: &http.Client{},+ }, nil+}++// Rerank performs document reranking based on relevance to the query using Aliyun DashScope API+func (r *AliyunReranker) Rerank(ctx context.Context, query string, documents []string) ([]RankResult, error) {+ // Build the request body+ requestBody := &AliyunRerankRequest{+ Model: r.modelName,+ Query: query,+ Documents: documents,+ }++ jsonData, err := json.Marshal(requestBody)+ if err != nil {+ return nil, fmt.Errorf("marshal request body: %w", err)+ }++ // Send the request+ req, err := http.NewRequestWithContext(ctx, "POST", r.baseURL, bytes.NewBuffer(jsonData))+ if err != nil {+ return nil, fmt.Errorf("create request: %w", err)+ }+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))++ // Log the curl equivalent for debugging+ logger.Debugf(ctx, "curl -X POST %s -H \"Content-Type: application/json\" -H \"Authorization: Bearer %s\" -d '%s'",+ r.baseURL, r.apiKey, string(jsonData),+ )++ resp, err := r.client.Do(req)+ if err != nil {+ return nil, fmt.Errorf("do request: %w", err)+ }+ defer resp.Body.Close()++ // Read the response+ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return nil, fmt.Errorf("read response body: %w", err)+ }++ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("aliyun rerank API error: Http Status: %s, Body: %s", resp.Status, string(body))+ }++ var response AliyunRerankResponse+ if err := json.Unmarshal(body, &response); err != nil {+ return nil, fmt.Errorf("unmarshal response: %w", err)+ }++ // Convert Aliyun results to standard RankResult format+ results := make([]RankResult, len(response.Output.Results))+ for i, aliyunResult := range response.Output.Results {+ results[i] = RankResult{+ Index: aliyunResult.Index,+ Document: DocumentInfo{+ Text: aliyunResult.Document.Text,+ },+ RelevanceScore: aliyunResult.RelevanceScore,+ }+ }++ return results, nil+}++// GetModelName returns the name of the reranking model+func (r *AliyunReranker) GetModelName() string {+ return r.modelName+}++// GetModelID returns the unique identifier of the reranking model+func (r *AliyunReranker) GetModelID() string {+ return r.modelID+}diff --git a/internal/models/rerank/aliyun_reranker_test.go b/internal/models/rerank/aliyun_reranker_test.gonew file mode 100644index 000000000..9990b03be--- /dev/null+++ b/internal/models/rerank/aliyun_reranker_test.go@@ -0,0 +1,80 @@+package rerank++import (+ "testing"+)++// TestAliyunRerankerCreation tests creating an Aliyun reranker+func TestAliyunRerankerCreation(t *testing.T) {+ config := &RerankerConfig{+ APIKey: "test-api-key",+ BaseURL: "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank",+ ModelName: "text-rerank",+ Source: "aliyun",+ ModelID: "aliyun-text-rerank",+ }++ reranker, err := NewAliyunReranker(config)+ if err != nil {+ t.Fatalf("Failed to create Aliyun reranker: %v", err)+ }++ if reranker.GetModelName() != config.ModelName {+ t.Errorf("Expected model name %s, got %s", config.ModelName, reranker.GetModelName())+ }++ if reranker.GetModelID() != config.ModelID {+ t.Errorf("Expected model ID %s, got %s", config.ModelID, reranker.GetModelID())+ }+}++// TestAliyunRerankerViaNewReranker tests creating an Aliyun reranker through NewReranker+func TestAliyunRerankerViaNewReranker(t *testing.T) {+ config := &RerankerConfig{+ APIKey: "test-api-key",+ BaseURL: "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank",+ ModelName: "text-rerank",+ Source: "aliyun",+ ModelID: "aliyun-text-rerank",+ }++ reranker, err := NewReranker(config)+ if err != nil {+ t.Fatalf("Failed to create reranker: %v", err)+ }++ // Verify it's an AliyunReranker+ _, ok := reranker.(*AliyunReranker)+ if !ok {+ t.Errorf("Expected AliyunReranker, got %T", reranker)+ }++ if reranker.GetModelName() != config.ModelName {+ t.Errorf("Expected model name %s, got %s", config.ModelName, reranker.GetModelName())+ }++ if reranker.GetModelID() != config.ModelID {+ t.Errorf("Expected model ID %s, got %s", config.ModelID, reranker.GetModelID())+ }+}++// TestAliyunRerankerUnsupportedSource tests error handling for unsupported sources+func TestAliyunRerankerUnsupportedSource(t *testing.T) {+ config := &RerankerConfig{+ APIKey: "test-api-key",+ BaseURL: "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank",+ ModelName: "text-rerank",+ Source: "unsupported",+ ModelID: "aliyun-text-rerank",+ }++ _, err := NewReranker(config)+ if err == nil {+ t.Error("Expected error for unsupported source, got nil")+ }++ expectedError := "unsupported rerank model source: unsupported"+ if err.Error() != expectedError {+ t.Errorf("Expected error %s, got %s", expectedError, err.Error())+ }+}diff --git a/internal/models/rerank/reranker.go b/internal/models/rerank/reranker.goindex 01f9b078f..bf8283aa8 100644--- a/internal/models/rerank/reranker.go+++ b/internal/models/rerank/reranker.go@@ -91,6 +91,8 @@ func NewReranker(config *RerankerConfig) (Reranker, error) {switch strings.ToLower(string(config.Source)) {case string(types.ModelSourceRemote):return NewOpenAIReranker(config)+ case string(types.ModelSourceAliyun):+ return NewAliyunReranker(config)default:return nil, fmt.Errorf("unsupported rerank model source: %s", config.Source)}diff --git a/internal/types/model.go b/internal/types/model.goindex 433665eb4..d70fada00 100644--- a/internal/types/model.go+++ b/internal/types/model.go@@ -33,6 +33,7 @@ type ModelSource stringconst (ModelSourceLocal ModelSource = "local" // Local modelModelSourceRemote ModelSource = "remote" // Remote model+ ModelSourceAliyun ModelSource = "aliyun" // Aliyun DashScope model)type EmbeddingParameters struct {