在前面的很多示例中,我们创建集合使用的是 get_or_create_collection() 方法,并且仅传递 name(集合名)、embedding_function(嵌套函数),没有传递更多参数。
实际上,创建集合时,我们可以通过传递 configuration 参数来配置集合,例如:
col = client.create_collection(
name="kb",
embedding_function=None,
configuration={
"hnsw": {
"space": "cosine",
"ef_construction": 200,
"ef_search": 100,
"max_neighbors": 32,
}
},
)上面代码将在 Chroma 中创建一个名为 "kb" 的集合(Collection),并且明确指定了底层的 HNSW 向量索引算法及一系列调优参数。
其中的 HNSW 中的参数含义如下:
当集合创建完成后,可以使用如下代码进行读取配置:
import chromadb
client = chromadb.EphemeralClient()
col = client.create_collection(
name="kb_demo",
embedding_function=None,
configuration={
"hnsw": {
"space": "cosine",
"ef_construction": 200,
"ef_search": 100,
"max_neighbors": 32,
}
},
)
print(col.configuration)我本机的实际输出:
{'hnsw': {'space': 'cosine', 'ef_construction': 200, 'ef_search': 100,
'max_neighbors': 32, 'resize_factor': 1.2, 'sync_threshold': 1000},
'spann': None, 'embedding_function': None}顶层有三个 key:
| key | 含义 |
| hnsw | HNSW 索引(单机版默认,全内存) |
| spann | SPANN 索引(磁盘友好,主要给 Cloud 用) |
| embedding_function | 嵌入函数配置 |
hnsw 和 spann 只能选一个,同时传会报,例如:
import chromadb
client = chromadb.EphemeralClient()
col = client.create_collection(
name="kb_demo",
embedding_function=None,
configuration={
"spann": {
"space": "cosine",
"search_nprobe": 64
},
"hnsw": {
"space": "cosine",
"ef_construction": 200,
"ef_search": 100,
"max_neighbors": 32,
}
},
)运行将抛出如下错误信息:
ValueError: hnsw and spann cannot both be provided