跳到主要内容

encode_queries()

此操作接收一个查询字符串列表,并将每个查询编码为向量嵌入。

请求语法​

python
encode_queries(
queries: List[str],
) -> csr_array

参数:

  • queries (List[str])

    字符串值列表,其中每个字符串表示一个将传递给嵌入模型进行编码的查询。模型将为列表中的每个字符串生成一个嵌入向量。

返回类型:

csr_array

返回值:

表示查询嵌入的压缩稀疏行矩阵。

异常:

  • ImportError

    当未安装 transformers 库时,将引发此异常。

示例​

python
from pymilvus import model

splade_ef = model.sparse.SpladeEmbeddingFunction(
model_name="naver/splade-cocondenser-selfdistil",
device="cpu"
)

queries = ["When was artificial intelligence founded",
"Where was Alan Turing born?"]

query_embeddings = splade_ef.encode_queries(queries)

# Print embeddings
print("Embeddings:", query_embeddings)
# since the output embeddings are in a 2D csr_array format, we convert them to a list for easier manipulation.
print("Sparse dim:", splade_ef.dim, list(query_embeddings)[0].shape)

# Embeddings: (0, 2001) 0.6353746056556702
# (0, 2194) 0.015553371049463749
# (0, 2301) 0.2756537199020386
# ...
# (1, 18522) 0.1282549500465393
# (1, 23602) 0.13133203983306885
# (1, 28639) 2.8150033950805664
# Sparse dim: 30522 (1, 30522)
最低 SDK 版本v2.3.x