DashScope RankerCompatible with Milvus 2.6.x

The DashScope Ranker lets Milvus call Alibaba Cloud DashScope reranking models to reorder search results by semantic relevance.

Prerequisites

Before using the DashScope Ranker, ensure that you have:

  • A Milvus collection with a VARCHAR field that contains the text to rerank.

  • A valid DashScope API key.

  • Access to a DashScope reranking model, such as gte-rerank-v2.

For available rerank models and regional endpoints, refer to the Alibaba Cloud Model Studio Text Rerank API.

Configure credentials

Milvus must know your DashScope API key before it can request reranking from DashScope. You can configure the API key in milvus.yaml or through an environment variable.

Option 1: Configuration file

Store your API key in milvus.yaml and point the DashScope rerank provider to the credential label.

# milvus.yaml
credential:
  dashscope_apikey:
    apikey: <YOUR_DASHSCOPE_API_KEY>

function:
  rerank:
    model:
      providers:
        ali:
          credential: dashscope_apikey
          # url: https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank

Option 2: Environment variable

If no matching credential is configured in milvus.yaml, Milvus can read the DashScope API key from the following environment variable:

Variable

Required?

Description

MILVUS_DASHSCOPE_API_KEY

Yes

DashScope API key used by the Milvus service to call Alibaba Cloud DashScope.

Create a DashScope ranker function

To use the DashScope Ranker, create a Function object that specifies the DashScope reranking model and query text. Use provider: "ali" for DashScope reranking.

from pymilvus import Function, FunctionType

dashscope_ranker = Function(
    name="dashscope_semantic_ranker",
    input_field_names=["document"],
    function_type=FunctionType.RERANK,
    params={
        "reranker": "model",
        "provider": "ali",
        "model_name": "gte-rerank-v2",
        "queries": ["renewable energy developments"],
        "max_client_batch_size": 128,
        "credential": "dashscope_apikey",
    },
)

DashScope ranker-specific parameters

Parameter

Required?

Description

Value / Example

reranker

Yes

Must be set to "model" to enable model reranking.

"model"

provider

Yes

The model service provider to use for reranking. For DashScope, use "ali".

"ali"

model_name

Yes

The DashScope reranking model to use.

"gte-rerank-v2"

queries

Yes

List of query strings used by the rerank model to calculate relevance scores. The number of query strings must match the number of queries in the search request.

["renewable energy developments"]

max_client_batch_size

No

Maximum number of documents to send to the model service per request.

128 (default)

credential

No

The label of a credential defined in the top-level credential: section of milvus.yaml.

"dashscope_apikey"

For general parameters shared across all model rankers, such as provider and queries, refer to Create a model ranker.

To apply DashScope Ranker to a standard vector search, pass the ranker Function to search().

results = client.search(
    collection_name="your_collection",
    data=[your_query_vector],
    anns_field="dense_vector",
    limit=5,
    output_fields=["document"],
    ranker=dashscope_ranker,
    consistency_level="Bounded",
)