feat: add aliyun rerank

This commit is contained in:
wizardchen
2025-09-09 20:58:16 +08:00
committed by lyingbug
parent 4214e6782b
commit 2b6cbee1b6
8 changed files with 68 additions and 200 deletions
+3
View File
@@ -129,6 +129,9 @@ services:
- MINIO_BUCKET_NAME=${MINIO_BUCKET_NAME}
- MINIO_USE_SSL=${MINIO_USE_SSL}
- WEB_PROXY=${WEB_PROXY}
- GRPC_ENABLE_FORK_SUPPORT=1
- GRPC_WORKER_PROCESSES=1
- GRPC_MAX_WORKERS=4
networks:
- WeKnora-network
restart: unless-stopped
-2
View File
@@ -16,7 +16,6 @@ export interface InitializationConfig {
dimension?: number; // 添加embedding维度字段
};
rerank: {
source: string;
modelName: string;
baseUrl: string;
apiKey?: string;
@@ -236,7 +235,6 @@ export function testEmbeddingModel(modelConfig: {
export function checkRerankModel(modelConfig: {
source: string;
modelName: string;
baseUrl: string;
apiKey?: string;
@@ -334,20 +334,10 @@
<!-- 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="formData.rerank.source === 'aliyun' ? '例如: text-rerank' : '例如: bge-reranker-v2-m3'"
<t-input v-model="formData.rerank.modelName" placeholder="例如: bge-reranker-v2-m3"
@blur="onRerankConfigChange"
@input="onRerankConfigInput" />
<div class="model-status-icon">
@@ -371,8 +361,7 @@
<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="formData.rerank.source === 'aliyun' ? '例如: https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank' : '例如: http://localhost:11434, 去除末尾/rerank路径后的URL的前面部分'"
<t-input v-model="formData.rerank.baseUrl" placeholder="例如: http://localhost:11434, 去除末尾/rerank路径后的URL的前面部分"
@blur="onRerankConfigChange"
@input="onRerankConfigInput" />
<div v-if="formData.rerank.modelName && formData.rerank.baseUrl" class="check-action">
@@ -879,7 +868,6 @@ const formData = reactive({
},
rerank: {
enabled: false,
source: 'remote',
modelName: '',
baseUrl: '',
apiKey: ''
@@ -1356,7 +1344,7 @@ onUnmounted(() => {
});
// 事件处理
const onModelSourceChange = async (type: 'llm' | 'embedding' | 'rerank') => {
const onModelSourceChange = async (type: 'llm' | 'embedding') => {
// 重置模型状态
modelStatus[type].checked = false;
modelStatus[type].available = false;
@@ -1366,15 +1354,6 @@ const onModelSourceChange = async (type: 'llm' | 'embedding' | 'rerank') => {
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') => {
@@ -1775,7 +1754,6 @@ const handleSubmit = async () => {
// 监听表单变化
watch(() => formData.llm.source, () => onModelSourceChange('llm'));
watch(() => formData.embedding.source, () => onModelSourceChange('embedding'));
watch(() => formData.rerank.source, () => onModelSourceChange('rerank'));
// 组件挂载时检查Ollama状态
onMounted(async () => {
@@ -1848,7 +1826,6 @@ 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.apiKey
+23 -77
View File
@@ -18,6 +18,7 @@ import (
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/embedding"
"github.com/Tencent/WeKnora/internal/models/rerank"
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -1410,93 +1411,38 @@ func (h *InitializationHandler) checkRemoteModelConnection(ctx context.Context,
// checkRerankModelConnection 检查Rerank模型连接和功能的内部方法
func (h *InitializationHandler) checkRerankModelConnection(ctx context.Context,
modelName, baseURL, apiKey string) (bool, string) {
client := &http.Client{
Timeout: 15 * time.Second,
// 创建Reranker配置
config := &rerank.RerankerConfig{
APIKey: apiKey,
BaseURL: baseURL,
ModelName: modelName,
Source: types.ModelSourceRemote, // 默认值,实际会根据URL判断
}
// 构造重排API端点
rerankEndpoint := baseURL + "/rerank"
// Mock测试数据
testQuery := "什么是人工智能?"
testPassages := []string{
"机器学习是人工智能的一个子领域,专注于算法和统计模型,使计算机系统能够通过经验自动改进。",
"深度学习是机器学习的一个子集,使用人工神经网络来模拟人脑的工作方式。",
}
// 构造重排请求
rerankRequest := map[string]interface{}{
"model": modelName,
"query": testQuery,
"documents": testPassages,
"truncate_prompt_tokens": 512,
}
jsonData, err := json.Marshal(rerankRequest)
// 创建Reranker实例
reranker, err := rerank.NewReranker(config)
if err != nil {
return false, fmt.Sprintf("构造请求失败: %v", err)
return false, fmt.Sprintf("创建Reranker失败: %v", err)
}
logger.Infof(ctx, "Rerank request: %s, modelName=%s, baseURL=%s, apiKey=%s",
string(jsonData), modelName, baseURL, apiKey)
// 简化的测试数据
testQuery := "ping"
testDocuments := []string{
"pong",
}
req, err := http.NewRequestWithContext(
ctx, "POST", rerankEndpoint, strings.NewReader(string(jsonData)),
)
// 使用Reranker进行测试
results, err := reranker.Rerank(ctx, testQuery, testDocuments)
if err != nil {
return false, fmt.Sprintf("创建请求失败: %v", err)
return false, fmt.Sprintf("重排测试失败: %v", err)
}
// 添加认证头
if apiKey != "" {
req.Header.Set("Authorization", "Bearer "+apiKey)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return false, fmt.Sprintf("连接失败: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Sprintf("读取响应失败: %v", err)
}
// 检查响应状态
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// 尝试解析重排响应
var rerankResp struct {
Results []struct {
Index int `json:"index"`
Document string `json:"document"`
RelevanceScore float64 `json:"relevance_score"`
} `json:"results"`
}
if err := json.Unmarshal(body, &rerankResp); err != nil {
// 如果无法解析标准重排响应,检查是否有其他格式
return true, "连接正常,但响应格式非标准"
}
// 检查是否返回了重排结果
if len(rerankResp.Results) > 0 {
return true, fmt.Sprintf("重排功能正常,返回%d个结果", len(rerankResp.Results))
} else {
return false, "重排接口连接成功,但未返回重排结果"
}
} else if resp.StatusCode == 401 {
return false, "认证失败,请检查API Key"
} else if resp.StatusCode == 403 {
return false, "权限不足,请检查API Key权限"
} else if resp.StatusCode == 404 {
return false, "重排API端点不存在,请检查Base URL"
} else if resp.StatusCode == 422 {
return false, fmt.Sprintf("请求参数错误: %s", string(body))
// 检查结果
if len(results) > 0 {
return true, fmt.Sprintf("重排功能正常,返回%d个结果", len(results))
} else {
return false, fmt.Sprintf("API返回错误状态: %d, 响应: %s", resp.StatusCode, string(body))
return false, "重排接口连接成功,但未返回重排结果"
}
}
+23 -5
View File
@@ -22,11 +22,23 @@ type AliyunReranker struct {
// AliyunRerankRequest represents a request to rerank documents using Aliyun DashScope API
type AliyunRerankRequest struct {
Model string `json:"model"` // Model to use for reranking
Model string `json:"model"` // Model to use for reranking
Input AliyunRerankInput `json:"input"` // Input containing query and documents
Parameters AliyunRerankParameters `json:"parameters"` // Parameters for the reranking
}
// AliyunRerankInput contains the query and documents for reranking
type AliyunRerankInput struct {
Query string `json:"query"` // Query text to compare documents against
Documents []string `json:"documents"` // List of document texts to rerank
}
// AliyunRerankParameters contains parameters for the reranking request
type AliyunRerankParameters struct {
ReturnDocuments bool `json:"return_documents"` // Whether to return documents in response
TopN int `json:"top_n"` // Number of top results to return
}
// AliyunRerankResponse represents the response from Aliyun DashScope reranking request
type AliyunRerankResponse struct {
Output AliyunOutput `json:"output"` // Output containing results
@@ -58,7 +70,7 @@ type AliyunUsage struct {
// 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"
baseURL := "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
if url := config.BaseURL; url != "" {
baseURL = url
}
@@ -76,9 +88,15 @@ func NewAliyunReranker(config *RerankerConfig) (*AliyunReranker, error) {
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,
Model: r.modelName,
Input: AliyunRerankInput{
Query: query,
Documents: documents,
},
Parameters: AliyunRerankParameters{
ReturnDocuments: true,
TopN: len(documents), // Return all documents
},
}
jsonData, err := json.Marshal(requestBody)
@@ -1,80 +0,0 @@
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())
}
}
+4 -6
View File
@@ -88,12 +88,10 @@ type RerankerConfig struct {
// NewReranker creates a reranker
func NewReranker(config *RerankerConfig) (Reranker, error) {
switch strings.ToLower(string(config.Source)) {
case string(types.ModelSourceRemote):
return NewOpenAIReranker(config)
case string(types.ModelSourceAliyun):
// 根据URL判断模型来源,而不是依赖Source字段
if strings.Contains(config.BaseURL, "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank") {
return NewAliyunReranker(config)
default:
return nil, fmt.Errorf("unsupported rerank model source: %s", config.Source)
} else {
return NewOpenAIReranker(config)
}
}
+12 -4
View File
@@ -6,6 +6,9 @@ import traceback
import grpc
import uuid
# Enable gRPC fork support to avoid multiprocessing issues
os.environ.setdefault('GRPC_ENABLE_FORK_SUPPORT', '1')
# Add parent directory to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
@@ -330,15 +333,16 @@ def serve():
})
# Set max number of worker threads and processes
max_workers = int(os.environ.get("GRPC_MAX_WORKERS", "4"))
worker_processes = int(os.environ.get("GRPC_WORKER_PROCESSES", str(os.cpu_count() or 1)))
# Force single process mode to avoid gRPC multiprocessing issues
worker_processes = 1
logger.info(f"Starting DocReader service, max worker threads per process: {max_workers}, "
f"processes: {worker_processes}")
f"processes: {worker_processes} (forced single process mode)")
# Get port number
port = os.environ.get("GRPC_PORT", "50051")
# Multi-process mode
if worker_processes > 1:
# Multi-process mode (disabled due to gRPC fork issues)
if False and worker_processes > 1:
import multiprocessing
processes = []
@@ -349,6 +353,8 @@ def serve():
options=[
('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
('grpc.enable_fork_support', 1),
('grpc.so_reuseport', 1),
],
)
@@ -396,6 +402,8 @@ def serve():
options=[
('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
('grpc.enable_fork_support', 1),
('grpc.so_reuseport', 1),
],
)