文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

ChromaQueryTextRetriever

这是与 Chroma Document Store 兼容的 Retriever。

pipeline 中的最常见位置1. 在 Text Embedder 之后,在 RAG pipeline 的 PromptBuilder 之前 2. 语义搜索 pipeline 中的最后一个组件 3. 在 Text Embedder 之后,在 extractive QA pipeline 的 ExtractiveReader 之前
必需的初始化变量"document_store": ChromaDocumentStore 的一个实例
强制运行变量“query”:一个纯文本格式的单个查询,将由 Retriever 处理
输出变量“documents”:文档列表
API 参考Chroma
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/chroma

概述

ChromaQueryTextRetriever 是一个兼容ChromaDocumentStore 的基于 embedding 的 Retriever,它使用 Chroma 的 query API
该组件接收纯文本查询字符串作为输入,并返回匹配的文档。
Chroma 将使用其 embedding function 为查询创建 embedding;如果您不想使用默认的 embedding function,则必须在ChromaDocumentStore 初始化时指定。

用法

单独使用

该 Retriever 需要 ChromaDocumentStore 和已索引的文档才能运行。

from haystack_integrations.document_stores.chroma import ChromaDocumentStore
from haystack_integrations.components.retrievers.chroma import ChromaQueryTextRetriever

document_store = ChromaDocumentStore()

retriever = ChromaQueryTextRetriever(document_store=document_store)

# example run query
retriever.run(query = "How does Chroma Retriever work?")

在 pipeline 中

以下是如何在 Pipeline 中使用ChromaQueryTextRetriever。在此示例中,您将创建两个 pipeline:一个索引 pipeline 和一个查询 pipeline。

在索引 pipeline 中,文档被写入 Document Store。

然后,在查询 pipeline 中,ChromaQueryTextRetriever 根据提供的查询从 Document Store 获取答案。

import os
from pathlib import Path

from haystack import Pipeline
from haystack.dataclasses import Document
from haystack.components.writers import DocumentWriter

from haystack_integrations.document_stores.chroma import ChromaDocumentStore
from haystack_integrations.components.retrievers.chroma import ChromaQueryTextRetriever

# Chroma is used in-memory so we use the same instances in the two pipelines below
document_store = ChromaDocumentStore()

documents = [
    Document(content="This contains variable declarations", meta={"title": "one"}),
    Document(content="This contains another sort of variable declarations", meta={"title": "two"}),
    Document(content="This has nothing to do with variable declarations", meta={"title": "three"}),
    Document(content="A random doc", meta={"title": "four"}),
]

indexing = Pipeline()
indexing.add_component("writer", DocumentWriter(document_store))
indexing.run({"writer": {"documents": documents}})

querying = Pipeline()
querying.add_component("retriever", ChromaQueryTextRetriever(document_store))
results = querying.run({"retriever": {"query": "Variable declarations", "top_k": 3}})

for d in results["retriever"]["documents"]:
    print(d.meta, d.score)

其他参考资料

🧑‍🍳 食谱:使用 Chroma 进行 RAG 和索引