encode_documents()
此操作接收文档并将其编码为向量嵌入。
请求语法
python
encode_documents(
documents: List[str],
) -> csr_array
参数:
-
documents (List[str])
字符串值列表,其中每个字符串表示一个将传递给嵌入模型进行编码的文档。模型将为列表中的每个字符串生成一个嵌入向量。
返回类型:
csr_array
返回值:
表示文档嵌入的压缩稀疏行矩阵。
异常:
-
ImportError
当未安装 transformers 库时,将引发此异常。
示例
python
from pymilvus import model
splade_ef = model.sparse.SpladeEmbeddingFunction(
model_name="naver/splade-cocondenser-selfdistil",
device="cpu"
)
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]
docs_embeddings = splade_ef.encode_documents(docs)
# Print embeddings
print("Embeddings:", docs_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(docs_embeddings)[0].shape)
# Embeddings: (0, 2001) 0.6392706036567688
# (0, 2034) 0.024093208834528923
# (0, 2082) 0.3230178654193878
# ...
# (2, 23602) 0.5671860575675964
# (2, 26757) 0.5770265460014343
# (2, 28639) 3.1990697383880615
# Sparse dim: 30522 (1, 30522)