参考:
https://docs.trychroma.com/usage-guide
https://blog.csdn.net/weixin_46515328/article/details/131855650
##安装
pip install chromadb
import chromadb
##这句可以数据库默认时刻保存,启动自动加载
chroma_client = chromadb.PersistentClient(path=".")
# chroma_client = chromadb.Client()
#创建Chroma数据集
collection = chroma_client.create_collection(name="my_collection2")
# 插入向量:
collection.add(
embeddings=[[1.2, 2.3, 4.5], [6.7, 8.2, 9.2]],
# documents=["This is a document", "This is another document"],
metadatas=[{"source": "my_source"}, {"source": "my_source"}],
ids=["id3", "id5"]
)
# 查询Chroma中的数据
results = collection.query(
# query_texts=["This is a query document"],
query_embeddings = [[2.3, 4.5,6.8]],
n_results=2,
include = ["embeddings",'distances',"metadatas"] ##include使用embeddings结果展示出来,不加结果为None
)
print(results)
##具体id查询
collection.get(
ids=["id3"],
where={"source": "my_source"},
include = ["embeddings"]
)
##更新具体id
collection.update(
ids=["id3"],
embeddings=[[1.1, 2.3, 3.2]],
metadatas=[{"source": "my_source"}],
)
collection.get(
ids=["id3"],
where={"source": "my_source"},
include = ["embeddings"]
)
##插入新数据
collection.upsert(
ids=["id18"],
embeddings=[[1.5, 2.3, 3.2]],
metadatas=[{"source": "my_source"}],
)
collection.get(
ids=["id18"],
where={"source": "my_source"},
include = ["embeddings"]
)