向量化和向量索引配置
从 Weaviate Python 客户端 v4.16.0 开始,向量化器配置 API 已更新。
从 Weaviate JS/TS 客户端 v3.8.0 开始,向量化器配置 API 已更新。
操作要求:更新到最新客户端版本并迁移您的代码以使用 新的向量化器配置 API。
指定一个向量化器
为集合指定一个vectorizer。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_openai(),
properties=[
Property(name="title", data_type=DataType.TEXT),
Property(name="body", data_type=DataType.TEXT),
],
)
指定向量化器设置
使用 Python 客户端库 4.16.0-4.16.3 使用 Configure.Vectors.text2vec_xxx() 定义集合,如果在未定义任何属性且未将 vectorize_collection_name 设置为 True 的情况下,将引发错误。
这在 Weaviate Python 客户端的 4.16.4 中得到解决。有关更多详细信息,请参阅此常见问题解答条目:Python 客户端版本 4.16.0 到 4.16.3 中出现无效属性错误。
要配置向量化器的工作方式(即使用哪个模型),请为特定集合设置向量化器参数。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_cohere(
model="embed-multilingual-v2.0", vectorize_collection_name=True
),
)
定义命名向量
v1.24可以为每个集合定义多个命名向量。这允许每个对象由多个向量嵌入表示,每个向量嵌入都有自己的向量索引。
因此,每个命名向量配置都可以包含自己的向量化器和向量索引设置。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
client.collections.create(
"ArticleNV",
vector_config=[
# Set a named vector with the "text2vec-cohere" vectorizer
Configure.Vectors.text2vec_cohere(
name="title",
source_properties=["title"], # (Optional) Set the source property(ies)
vector_index_config=Configure.VectorIndex.hnsw(), # (Optional) Set vector index options
),
# Set another named vector with the "text2vec-openai" vectorizer
Configure.Vectors.text2vec_openai(
name="title_country",
source_properties=[
"title",
"country",
], # (Optional) Set the source property(ies)
vector_index_config=Configure.VectorIndex.hnsw(), # (Optional) Set vector index options
),
# Set a named vector for your own uploaded vectors
Configure.Vectors.self_provided(
name="custom_vector",
vector_index_config=Configure.VectorIndex.hnsw(), # (Optional) Set vector index options
),
],
properties=[ # Define properties
Property(name="title", data_type=DataType.TEXT),
Property(name="country", data_type=DataType.TEXT),
],
)
添加新的命名向量
v1.31 中添加可以将命名向量添加到具有命名向量的现有集合定义中。(这对于没有命名向量的集合来说是不可能的。)
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure
articles = client.collections.use("Article")
articles.config.add_vector(
vector_config=Configure.Vectors.text2vec_cohere(
name="body_vector",
source_properties=["body"],
)
)
将新向量添加到集合定义中不会触发现有对象的向量化。只有在添加向量之后创建的对象才会收到这些新的向量嵌入。
定义多向量嵌入(例如 ColBERT、ColPali)
v1.29 和 v1.30 中添加多向量嵌入,也称为多向量,用多个向量表示单个对象,即二维矩阵。多向量目前仅适用于命名向量的 HNSW 索引。要使用多向量,请为适当的命名向量启用它。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
client.collections.create(
"DemoCollection",
vector_config=[
# Example 1 - Use a model integration
# The factory function will automatically enable multi-vector support for the HNSW index
Configure.MultiVectors.text2vec_jinaai(
name="jina_colbert",
source_properties=["text"],
),
# Example 2 - User-provided multi-vector representations
# Must explicitly enable multi-vector support for the HNSW index
Configure.MultiVectors.self_provided(
name="custom_multi_vector",
),
],
properties=[Property(name="text", data_type=DataType.TEXT)],
# Additional parameters not shown
)
设置向量索引类型
向量索引类型可以在创建时为每个集合设置,介于hnsw、flat 和 dynamic 索引类型之间。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_openai(
name="default",
vector_index_config=Configure.VectorIndex.hnsw(), # Use the HNSW index
# vector_index_config=Configure.VectorIndex.flat(), # Use the FLAT index
# vector_index_config=Configure.VectorIndex.dynamic(), # Use the DYNAMIC index
),
properties=[
Property(name="title", data_type=DataType.TEXT),
Property(name="body", data_type=DataType.TEXT),
],
)
其他信息
- 有关索引类型和压缩的更多信息,请参阅概念:向量索引。
设置向量索引参数
通过集合配置设置向量索引参数,例如压缩和过滤策略。某些参数可以在集合创建后稍后更新。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import (
Configure,
Property,
DataType,
VectorDistances,
VectorFilterStrategy,
)
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_openai(
name="default",
vector_index_config=Configure.VectorIndex.hnsw(
ef_construction=300,
distance_metric=VectorDistances.COSINE,
filter_strategy=VectorFilterStrategy.ACORN,
),
),
)
其他信息
- 有关索引类型和压缩的更多信息,请参阅概念:向量索引。
属性级别设置
配置集合中的各个属性。每个属性都可以有自己的配置。以下是一些常见设置
- 向量化该属性
- 向量化属性名称
- 设置分词类型
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType, Tokenization
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_cohere(),
properties=[
Property(
name="title",
data_type=DataType.TEXT,
vectorize_property_name=True, # Use "title" as part of the value to vectorize
tokenization=Tokenization.LOWERCASE, # Use "lowercase" tokenization
description="The title of the article.", # Optional description
),
Property(
name="body",
data_type=DataType.TEXT,
skip_vectorization=True, # Don't vectorize this property
tokenization=Tokenization.WHITESPACE, # Use "whitespace" tokenization
),
],
)
指定距离度量
如果您选择自带向量,则应指定距离度量。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, VectorDistances
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_openai(
vector_index_config=Configure.VectorIndex.hnsw(
distance_metric=VectorDistances.COSINE
),
),
)
更多资源
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
