跳到主要内容

使用自定义嵌入函数

创建自定义嵌入函数后,在创建或获取 collections 时使用它。

示例如下:

import pyseekdb
from pyseekdb import HNSWConfiguration

# Create a client
client = pyseekdb.Client()

# Create collection with custom embedding function
ef = SentenceTransformerCustomEmbeddingFunction()
collection = client.create_collection(
name="my_collection",
configuration=HNSWConfiguration(dimension=ef.dimension, distance='cosine'),
embedding_function=ef
)

# Get collection with custom embedding function
collection = client.get_collection("my_collection", embedding_function=ef)

# Use the collection - documents will be automatically embedded
collection.add(
ids=["doc1", "doc2"],
documents=["Document 1", "Document 2"], # Vectors auto-generated
metadatas=[{"tag": "A"}, {"tag": "B"}]
)

# Query with texts - query vectors auto-generated
results = collection.query(
query_texts=["my query"],
n_results=10
)