ChromaDB中的更新插入操作


文档摘要

ChromaDB 中的更新插入操作 本教程将指导您在 ChromaDB 中执行更新插入操作,以更新现有条目或向集合中添加新条目。 创建集合 我们首先初始化 ChromaDB 客户端,并创建一个名为 的集合。 添加原始文档 我们使用 方法向集合中添加了三个原始文档。每个文档都关联有一个唯一 ID,并包含元数据,包括类别和发言人。 更新插入操作 ChromaDB 支持更新插入操作,该操作允许我们更新现有条目或向集合中添加新条目。在本示例中,我们对 ID 为“quote2”和“quote4”的两个条目执行了更新插入操作。 方法接受待更新或添加的条目的 ID、嵌入向量、元数据和文档。如果某个 ID 已存在于集合中,则对应的条目将被更新;如果某个 ID 不存在,则会添加一个新的条目。

ChromaDB 中的更新插入操作

本教程将指导您在 ChromaDB 中执行更新插入操作,以更新现有条目或向集合中添加新条目。

1. 创建集合

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

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

2. 添加原始文档

# Add the raw documents 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.", ], metadatas=[ {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Morpheus"}, ], ids=["quote_1", "quote_2", "quote_3"], )

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

3. 更新插入操作

# Upsert operation matrix_collection.upsert( ids=["quote_2", "quote_4"], metadatas=[ {"category": "quote", "speaker": "Morpheus"}, {"category": "quote", "speaker": "Agent Smith"}, ], documents=[ "You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe.", "I'm going to enjoy watching you die, Mr. Anderson.", ], )

ChromaDB 支持更新插入操作,该操作允许我们更新现有条目或向集合中添加新条目。在本示例中,我们对 ID 为“quote_2”和“quote_4”的两个条目执行了更新插入操作。

upsert 方法接受待更新或添加的条目的 ID、嵌入向量、元数据和文档。如果某个 ID 已存在于集合中,则对应的条目将被更新;如果某个 ID 不存在,则会添加一个新的条目。

4. 从集合中检索更新后的条目

# Retrieve updated items from the collection items = matrix_collection.get(ids=["quote_2", "quote_4"]) print(items)

我们可以使用 get method. In this case, we retrieve the newly upserted items with the ID "quote_2" and "quote_4" to see the changes.

Use cases for the upsert operation include:

  • Updating Existing Items: You can update specific items in the collection by providing their IDs along with the updated embeddings, metadatas, and documents.
  • Adding New Items: You can add new items to the collection by providing their IDs along with the corresponding embeddings, metadatas, and documents.

Feel free to modify the IDs, embeddings, metadatas, and documents in the upsert 方法从集合中检索更新后的条目,以更新现有条目或向集合中添加新条目。

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


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