在ChromaDB中向集合添加原始文档


文档摘要

在ChromaDB中向集合添加原始文档 本教程将指导您如何在ChromaDB中向集合添加原始文档。 创建集合 我们首先初始化ChromaDB客户端,并创建一个名为 的集合。 添加原始文档 我们使用 method. Each document is associated with a unique id. Importantly: When embeddings are not passed to the 方法向集合中添加了两段来自《黑客帝国》的著名台词。ChromaDB会自动为我们嵌入这些文档。 默认情况下,如果在创建集合时未指定特定的嵌入函数,ChromaDB会使用句子转换器进行嵌入。 统计集合中的项目数量 我们可以使用 method.

在ChromaDB中向集合添加原始文档

本教程将指导您如何在ChromaDB中向集合添加原始文档。

1. 创建集合

client = chromadb.Client() neo_collection = client.create_collection(name="neo")

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

2. 添加原始文档

neo_collection.add( documents=["I know kung fu.", "There is no spoon."], ids=["quote_1", "quote_2"] )

我们使用add method. Each document is associated with a unique id. Importantly:

  • When embeddings are not passed to the add方法向集合中添加了两段来自《黑客帝国》的著名台词。ChromaDB会自动为我们嵌入这些文档。
  • 默认情况下,如果在创建集合时未指定特定的嵌入函数,ChromaDB会使用句子转换器进行嵌入。

3. 统计集合中的项目数量

item_count = neo_collection.count() print(f"Count of items in collection: {item_count}")

我们可以使用count method. Since we have added two documents to our collection, the count returns 2方法统计集合中的项目数量。

4. 从集合中检索项目

items = neo_collection.get() print(items)

我们可以使用get方法检索集合中的所有项目。默认情况下,这将返回一个字典,其中包含集合中项目的ID、元数据(如果已提供)以及文档。

5. 预览集合内容

neo_collection.peek(limit=5)

我们也可以使用peek method to return the first 10 items from a collection. By default, this will return a dictionary with the ids, metadatas (if provided) and documents of the items in the collection.

Note: The main difference between peek and get methods is that the get method allows for more arguments, whereas the peek method only takes limit, which is simply the number of results to return.

Please refer to the adding_raw_docs.py文件获取本教程中使用的完整Python代码。

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


发布者: 作者: 转发
评论区 (0)
U