encode_documents()
此操作接收文档并将其编码为向量嵌入。
请求语法
python
encode_documents(
documents: List[str],
) -> List[np.array]
参数:
-
documents (List[str])
字符串值列表,其中每个字符串表示一个将传递给嵌入模型进行编码的文档。模型会为列表中的每个字符串生成一个嵌入向量。
返回类型:
List[np.array]
返回值:
一个列表,其中每个元素都是一个 NumPy 数组。
异常:
-
ImportError
未安装 model2vec 模块时,将引发此异常。
示例
python
from pymilvus import model
gemini_ef = model.dense.GeminiEmbeddingFunction(
model_name="gemini-embedding-exp-03-07",
api_key="YOUR_API_KEY",
)
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 = gemini_ef.encode_documents(docs)
# Print embeddings
print("Embeddings:", docs_embeddings)
# Print dimension and shape of embeddings
print("Dim:", gemini_ef.dim, docs_embeddings[0].shape)
# Embeddings: [array([-0.00894029, 0.00573813, 0.013351 , ..., -0.00042766,
# -0.00603091, -0.00341043], shape=(3072,)), array([ 0.00222347, 0.03725113, 0.01152256, ..., 0.01047272,
# -0.01701597, 0.00565377], shape=(3072,)), array([ 0.00661134, 0.00232328, -0.01342973, ..., -0.00514429,
# -0.02374139, -0.00701721], shape=(3072,))]
# Dim: 3072 (3072,)