在ChromaDB中使用Where过滤器


文档摘要

在ChromaDB中使用Where过滤器 本教程将引导您了解如何在ChromaDB中使用where过滤器,以根据特定元数据条件筛选和查询项目。 创建集合 我们首先初始化ChromaDB客户端,并创建一个名为 的集合。 添加原始文档 我们使用 方法向集合中添加五条原始文档(来自《黑客帝国》的引言)。每条文档都关联有一个唯一ID,并包含元数据,包括类别和发言人。 使用Where过滤器 ChromaDB支持通过where过滤器按元数据进行查询过滤。在本示例中,我们使用查询文本“什么是矩阵?”来查询集合,并根据发言人是“墨菲斯”这一条件对结果进行过滤。

在ChromaDB中使用Where过滤器

本教程将引导您了解如何在ChromaDB中使用where过滤器,以根据特定元数据条件筛选和查询项目。

1. 创建集合

import chromadb # Initialize ChromaDB client client = chromadb.Client() # Create the collection matrix_collection = client.create_collection(name="matrix", metadata={"hnsw:space": "cosine"})

我们首先初始化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.", "Unfortunately, no one can be told what the Matrix is", "You hear that Mr. Anderson?... That is the sound of inevitability...", "You are a plague, Mr. Anderson. You and your kind are 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"}, ], ids=["quote_1", "quote_2", "quote_3", "quote_4", "quote_5"], )

我们使用add方法向集合中添加五条原始文档(来自《黑客帝国》的引言)。每条文档都关联有一个唯一ID,并包含元数据,包括类别和发言人。

3. 使用Where过滤器

results = matrix_collection.query( query_texts=["What is the Matrix?"], where={"speaker": "Morpheus"}, n_results=2, ) print(results)

ChromaDB支持通过where过滤器按元数据进行查询过滤。在本示例中,我们使用查询文本“什么是矩阵?”来查询集合,并根据发言人是“墨菲斯”这一条件对结果进行过滤。

query method takes a where parameter, which is a dictionary specifying the metadata field and its corresponding value for filtering. The results will include only the items that match the specified criteria.

Use cases for using where filters include:

  • Metadata-based Filtering: You can filter items based on specific metadata fields, such as speaker, category, or any other relevant criteria.
  • Targeted Analysis: By filtering the results using where filters, you can perform targeted analysis or operations on specific subsets of the data that meet certain metadata criteria.

Feel free to modify the where filters in the query方法可帮助您探索不同的过滤选项。请尝试使用不同的元数据字段和值,看看ChromaDB如何让您根据特定元数据条件筛选和查询项目。

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


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