从ChromaDB集合中删除数据


文档摘要

从ChromaDB集合中删除数据 本教程将引导您完成从ChromaDB集合中删除数据的过程。 创建集合 我们首先初始化ChromaDB客户端,并创建一个名为 的集合。 添加原始文档 我们使用 方法向集合中添加了六份原始文档。每份文档都关联有一个唯一ID,并包含元数据,包括类别和发言人。 删除符合where过滤器条件的条目 ChromaDB允许您使用 method. In this example, we delete all items from the collection where the speaker is "Agent Smith".

从ChromaDB集合中删除数据

本教程将引导您完成从ChromaDB集合中删除数据的过程。

1. 创建集合

import chromadb # Initialize ChromaDB client client = chromadb.Client() # Create the collection matrix_collection = client.create_collection(name="matrix")

我们首先初始化ChromaDB客户端,并创建一个名为matrix的集合。

2. 添加原始文档

matrix_collection.add( documents=[ "The Matrix is everywhere, it is all around us.", "You can see it when you look out your window or when you turn on your television.", "You can feel it when you go to work, when you go to church, when you pay your taxes.", "It seems that you've been living two lives.", "I believe that, as a species, human beings define their reality through misery and suffering", "Human beings are a disease, a cancer of this planet.", ], metadatas=[ {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Agent Smith"}, {"category": "quote", "speaker": "Agent Smith"}, {"category": "quote", "speaker": "Agent Smith"}, ], ids=["quote_1", "quote_2", "quote_3", "quote_4", "quote_5", "quote_6"], )

我们使用add方法向集合中添加了六份原始文档。每份文档都关联有一个唯一ID,并包含元数据,包括类别和发言人。

3. 删除符合where过滤器条件的条目

matrix_collection.delete(where={"speaker": "Agent Smith"})

ChromaDB允许您使用delete method. In this example, we delete all items from the collection where the speaker is "Agent Smith".

The delete method takes a where parameter, which is a dictionary specifying the metadata field and its corresponding value for filtering. All items that match the specified criteria will be deleted from the collection.

Use cases for deleting data from a collection include:

  • Data Cleanup: You can delete specific items or subsets of data from a collection to keep your data clean and up-to-date.
  • Data Privacy: In cases where certain data needs to be removed for privacy or compliance reasons, the delete method allows you to selectively remove sensitive information from the collection.

Feel free to modify the where filter in the delete方法,根据不同的元数据条件从集合中删除符合条件的条目。

# Counting items in the collection after deletion item_count = matrix_collection.count() print(f"Count of items in collection: {item_count}")

删除后,您可以使用count方法统计集合中剩余的条目数量,以验证更改是否成功。

免责声明
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。


作者与出处
原作者: neo-con
来源:neo-con
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: neo-con 转发
评论区 (0)
U