在ChromaDB中选择从集合中返回哪些数据


文档摘要

在ChromaDB中选择从集合中返回哪些数据 本教程将指导您如何在ChromaDB中选择从集合中返回哪些数据。 创建集合 我们首先初始化ChromaDB客户端,并创建一个名为 的集合。 添加原始文档 我们使用 方法向集合中添加了三个原始文档。每个文档都与一个唯一的ID相关联。 根据文本查询集合并选择要返回的数据 我们可以使用 method. In this example, we search for the phrase "make a choice" and ask for one result.

在ChromaDB中选择从集合中返回哪些数据

本教程将指导您如何在ChromaDB中选择从集合中返回哪些数据。

1. 创建集合

client = chromadb.Client() morpheus_collection = client.create_collection(name="morpheus", metadata={"hnsw:space": "cosine"})

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

2. 添加原始文档

morpheus_collection.add( documents=[ "This is your last chance. After this, there is no turning back.", "You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe.", "You take the red pill, you stay in Wonderland, and I show you how deep the rabbit hole goes.", ], ids=["quote_1", "quote_2", "quote_3"], )

我们使用add方法向集合中添加了三个原始文档。每个文档都与一个唯一的ID相关联。

3. 根据文本查询集合并选择要返回的数据

results = morpheus_collection.query( query_texts=["make a choice"], n_results=1, include=["distances", "embeddings"] )

我们可以使用query method. In this example, we search for the phrase "make a choice" and ask for one result. Additionally, we specify that we only want the distances and embeddings to be returned by including them in the include parameter.

The query method will return a dictionary with the selected data (distances and embeddings) for the closest match to the query text.

Use cases for choosing which data is returned:

  1. Selective Data Retrieval:

    • Retrieve only document IDs for quick reference checks
    • Fetch embeddings for further processing or visualization
    • Get distances to analyze similarity scores without full content
  2. Data Privacy and Security:

    • Return only non-sensitive metadata while excluding document content
    • Provide aggregated results without exposing individual data points
    • Implement role-based access control by returning different data based on user permissions
  3. Efficiency and Resource Optimization:

    • Retrieve only document IDs for initial filtering, then fetch full content as needed
    • Return summarized data for large-scale analytics without transferring entire documents
    • Optimize mobile app performance by requesting minimal data for display
  4. Custom Application Needs:

    • Fetch only specific metadata fields relevant to the current user interface
    • Retrieve embeddings for local similarity comparisons or clustering
    • Return document snippets for search result previews without full content
  5. Debugging and Monitoring:

    • Include internal scores or rankings for system performance analysis
    • Return processing timestamps to monitor query latency
    • Fetch version information to ensure data consistency across updates

Remember to adjust the include参数来根据文本查询集合,以满足您的具体用例需求。

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


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