24 lines
643 B
Python
24 lines
643 B
Python
# 获取已经存在的集合
|
|
|
|
# 如果集合已经存在,可以使用get_collection() 或者 get_or_create_collection() 方法
|
|
|
|
import chromadb
|
|
|
|
# 创建持久化客户端
|
|
client = chromadb.PersistentClient(path="./chromadb_store")
|
|
|
|
# 方法1:获取已存在的集合
|
|
try:
|
|
existring_collection = client.get_collection(name="notes")
|
|
print("集合已经存在", existring_collection.name)
|
|
except Exception as e:
|
|
print("集合不存在", e)
|
|
|
|
|
|
# 方法2:获取或者创建集合(推荐使用)
|
|
|
|
collection = client.get_or_create_collection(
|
|
name="notes", metadata={"description": "笔记集合"}
|
|
)
|
|
print(collection.name)
|