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
+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)
}
}