跳至主要内容
前往文档
⌘U
Weaviate 数据库

使用 Weaviate 的 APIs 和工具开发 AI 应用

部署

部署、配置和维护 Weaviate 数据库

Weaviate Agents

使用 Weaviate 构建和部署智能代理

Weaviate Cloud

在云端管理和扩展 Weaviate

更多资源

集成
贡献者指南
活动 & 工作坊
Weaviate Academy

需要帮助?

Weaviate Logo询问 AI 助手⌘K
社区论坛

向量化和向量索引配置

Python 和 JS/TS 客户端 - 向量化器配置 API 更改

从 Weaviate Python 客户端 v4.16.0 开始,向量化器配置 API 已更新
从 Weaviate JS/TS 客户端 v3.8.0 开始,向量化器配置 API 已更新

操作要求:更新到最新客户端版本并迁移您的代码以使用 新的向量化器配置 API

指定一个向量化器

为集合指定一个vectorizer

其他信息

集合级别设置会覆盖默认值和常规配置参数,例如环境变量

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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),
],
)

指定向量化器设置

.Vectors.text2vec_xxx 与 AutoSchema

使用 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 中出现无效属性错误

要配置向量化器的工作方式(即使用哪个模型),请为特定集合设置向量化器参数。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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

可以为每个集合定义多个命名向量。这允许每个对象由多个向量嵌入表示,每个向量嵌入都有自己的向量索引。

因此,每个命名向量配置都可以包含自己的向量化器和向量索引设置。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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 中添加

可以将命名向量添加到具有命名向量的现有集合定义中。(这对于没有命名向量的集合来说是不可能的。)

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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.29v1.30 中添加

多向量嵌入,也称为多向量,用多个向量表示单个对象,即二维矩阵。多向量目前仅适用于命名向量的 HNSW 索引。要使用多向量,请为适当的命名向量启用它。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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
)
使用量化和编码来压缩您的向量

多向量嵌入比单向量嵌入占用更多的内存。您可以使用向量量化编码来压缩它们并减少内存使用。

设置向量索引类型

向量索引类型可以在创建时为每个集合设置,介于hnswflatdynamic 索引类型之间。

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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),
],
)
其他信息

设置向量索引参数

通过集合配置设置向量索引参数,例如压缩过滤策略。某些参数可以在集合创建后稍后更新

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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,
),
),
)
其他信息

属性级别设置

配置集合中的各个属性。每个属性都可以有自己的配置。以下是一些常见设置

  • 向量化该属性
  • 向量化属性名称
  • 设置分词类型
py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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
),
],
)

指定距离度量

如果您选择自带向量,则应指定距离度量

py docs  API 文档
更多信息文档中的代码片段反映了最新的客户端库和 Weaviate 数据库版本。请查看 发行说明 以获取特定版本。

如果某个片段无法工作或您有任何反馈,请打开一个 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
),
),
)

其他信息

有关配置参数的详细信息,请参阅以下内容

更多资源

问题和反馈

如果您有任何问题或反馈,请在 用户论坛 中告诉我们。