跳到主要内容
版本:v3.0.x

TEI RankerCompatible with Milvus 2.6.x

TEI Ranker 利用 Hugging Face 提供的文本嵌入推理(TEI)服务,通过语义重排来提高搜索相关性。它代表了一种先进的搜索结果排序方法,超越了传统的向量相似性。

前提条件

在 Milvus 中实施 TEI Ranker 之前,请确保您拥有

  • 具有VARCHAR 字段的 Milvus Collection,其中包含要 Reranking 的文本

  • 运行中的具有 Reranker 功能的 TEI 服务。有关设置 TEI 服务的详细说明,请参阅TEI 官方文档

创建 TEI 排序器函数

要在你的 Milvus 应用程序中使用 TEI Ranker,请创建一个函数对象,指定重排应该如何操作。该函数将传递给 Milvus 搜索操作符,以增强结果排名。

from pymilvus import MilvusClient, Function, FunctionType

# Connect to your Milvus server
client = MilvusClient(
uri="http://localhost:19530" # Replace with your Milvus server URI
)

# Configure TEI Ranker
tei_ranker = Function(
name="tei_semantic_ranker", # Unique identifier for your ranker
input_field_names=["document"], # VARCHAR field containing text to rerank
function_type=FunctionType.RERANK, # Must be RERANK for reranking functions
params={
"reranker": "model", # Enables model-based reranking
"provider": "tei", # Specifies TEI as the service provider
"queries": ["renewable energy developments"], # Query text for relevance evaluation
"endpoint": "http://localhost:8080", # Your TEI service URL
"max_client_batch_size": 32, # Optional: batch size for processing (default: 32)
"truncate": True, # Optional: Truncate the inputs that are longer than the maximum supported size
"truncation_direction": "Right", # Optional: Direction to truncate the inputs
}
)

TEI 排序器专用参数

以下参数是 TEI 排序器的特定参数:

| 参数

|

是否需要?

|

说明

|

值/示例

| | --- | --- | --- | --- | |

reranker

|

|

必须设置为"model" 才能启用模型重排。

|

"model"

| |

provider

|

|

用于重排的模型服务提供商。

|

"tei"

| |

queries

|

|

Reranker 模型用于计算相关性得分的查询字符串列表。查询字符串的数量必须与搜索操作中的查询数量完全匹配(即使使用查询向量代替文本),否则将报错。

|

["搜索查询"]

| |

endpoint

|

|

您的 TEI 服务 URL。

|

"http://localhost:8080"

| |

max_client_batch_size

|

|

由于模型服务可能无法一次性处理所有数据,因此此处设置了在多个请求中访问模型服务的批量大小。

|

32 (默认值)

| |

truncate

|

|

是否截断超过最大序列长度的输入。如果False ,过长的输入将导致错误。

|

TrueFalse

| |

truncation_direction

|

|

当输入过长时截断的方向:

  • "Right" (默认): 从序列末尾删除标记,直到符合最大支持大小。

  • "Left":从序列的开头开始删除标记。

|

"Right""Left"

|

说明

有关所有模型排序器共享的一般参数(如provider,queries ),请参阅创建模型排序器

将 TEI Ranker 应用于标准向量搜索:

# Execute search with vLLM reranking
results = client.search(
collection_name="your_collection",
data=[your_query_vector], # Replace with your query vector
anns_field="dense_vector", # Vector field to search
limit=5, # Number of results to return
output_fields=["document"], # Include text field for reranking
ranker=tei_ranker, # Apply tei reranking
consistency_level="Bounded"
)