Overview
QwenReRanker leverages DashScope’s TextReRank service to perform cross-encoder style reranking. It sends query and document pairs to the API and receives relevance scores based on deep semantic understanding.
This reranker is suitable for single-vector or multi-vector search scenarios where semantic relevance to a specific query is required.
Requirements:
dashscopePython package must be installed- Valid DashScope API key
- API rate limits and quotas apply per DashScope subscription
Constructor
Parameters
Query text for semantic re-ranking. Cannot be empty or
None.Maximum number of documents to return after re-ranking.
Document field name to use as re-ranking input text (e.g.,
"content", "title", "body").DashScope re-ranking model identifier. Available models:
"gte-rerank-v2"(default)- Other models supported by DashScope TextReRank API
DashScope API authentication key. If not provided, reads from
DASHSCOPE_API_KEY environment variable.Properties
Query text used for semantic re-ranking.
Maximum number of documents to return.
Document field used for re-ranking input.
DashScope model name.
Methods
rerank()
Mapping from vector field names to lists of retrieved documents. Documents from all fields are deduplicated and re-ranked together.
Re-ranked documents (up to
topn) with updated score fields containing relevance scores from the API.Behavior:
- Duplicate documents (same ID) across fields are processed once
- Documents with empty/missing
rerank_fieldcontent are skipped - Returned scores are relevance scores from the cross-encoder model
Usage Examples
Basic Semantic Reranking
Multi-Vector Search with Semantic Reranking
Different Reranking Fields
Passing API Key Explicitly
How It Works
- Collect Documents: Gather and deduplicate documents from all vector field queries
- Extract Text: Extract text content from the specified
rerank_field - API Call: Send query and document texts to DashScope TextReRank API
- Score Update: Update document scores with relevance scores from the model
- Rank: Return top
ndocuments by relevance score
API Request Format
The reranker sends requests to DashScope in this format:API Response Format
When to Use Qwen Reranking
Use QwenReRanker when:
- You need semantic relevance beyond vector similarity
- Query intent requires deep understanding
- Precision is more important than speed
- Working with natural language queries
- Combining diverse document types