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

TransformersZeroShotTextRouter

使用此组件可根据用户定义的分类标签将文本输入路由到各种输出连接。

pipeline 中的最常见位置灵活
必需的初始化变量"labels": 用于分类的标签列表

"token": Hugging Face API 令牌。可以通过HF_API_TOKENHF_TOKEN 环境变量设置。
强制运行变量“text”: 要根据其所属类别路由到指定输出之一的文本
输出变量“documents”: 一个字典,其中键为标签,值为文本
API 参考Routers (路由器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/routers/zero_shot_text_router.py

概述

TransformersZeroShotTextRouter 根据其分类标签将文本输入路由到各种输出连接。此功能尤其有利于根据查询的特定类别,将其引导到管道内的相应组件。用户可以定义此分类过程的标签。

TransformersZeroShotTextRouter 默认使用MoritzLaurer/deberta-v3-base-zeroshot-v1.1-all-33 零样本文本分类模型。您可以使用model 参数设置您选择的其他模型。

要使用TransformersZeroShotTextRouter,您需要提供强制性的labels 参数——一个字符串列表,包含每个序列可被分类的可能类别标签。

要查看完整的参数列表,请查看我们的 API 参考

用法

单独使用

TransformersZeroShotTextRouter 本身的效果并不显著,因为它的主要优势在于管道内工作。当该组件集成到管道中时,其真正的潜力才能得到释放,它可以有效地将文本路由到最合适的组件。请参阅以下部分以获取完整的用法示例。

在 pipeline 中

下面是一个简单的管道示例,它将输入文本路由到管道中的适当路由。

我们首先创建一个InMemoryDocumentStore 并用关于德国和法国的文档填充它,使用SentenceTransformersDocumentEmbedder.

然后,我们创建一个检索管道,其中包含TransformersZeroShotTextRouter,根据预定义的标签将传入文本分类为“passage”或“query”。根据分类,文本随后由适合 passages 和 queries 的 Embedders 进行处理。这些 Embedders 生成的嵌入由InMemoryEmbeddingRetriever 使用,以在 Document Store 中查找相关文档。

最后,使用示例文本执行管道:“德国的首都是哪里?” 这个文本被分类为“query”,并路由到 Query Embedder 和随后的 Query Retriever,以返回相关结果。

from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.core.pipeline import Pipeline
from haystack.components.routers import TransformersZeroShotTextRouter
from haystack.components.embedders import SentenceTransformersTextEmbedder, SentenceTransformersDocumentEmbedder
from haystack.components.retrievers import InMemoryEmbeddingRetriever

document_store = InMemoryDocumentStore()
doc_embedder = SentenceTransformersDocumentEmbedder(model="intfloat/e5-base-v2")
doc_embedder.warm_up()
docs = [
    Document(
        content="Germany, officially the Federal Republic of Germany, is a country in the western region of "
                "Central Europe. The nation's capital and most populous city is Berlin and its main financial centre "
                "is Frankfurt; the largest urban area is the Ruhr."
    ),
    Document(
        content="France, officially the French Republic, is a country located primarily in Western Europe. "
                "France is a unitary semi-presidential republic with its capital in Paris, the country's largest city "
                "and main cultural and commercial centre; other major urban areas include Marseille, Lyon, Toulouse, "
                "Lille, Bordeaux, Strasbourg, Nantes and Nice."
    )
]
docs_with_embeddings = doc_embedder.run(docs)
document_store.write_documents(docs_with_embeddings["documents"])

p = Pipeline()
p.add_component(instance=TransformersZeroShotTextRouter(labels=["passage", "query"]), name="text_router")
p.add_component(
    instance=SentenceTransformersTextEmbedder(model="intfloat/e5-base-v2", prefix="passage: "),
    name="passage_embedder"
)
p.add_component(
    instance=SentenceTransformersTextEmbedder(model="intfloat/e5-base-v2", prefix="query: "),
    name="query_embedder"
)
p.add_component(
    instance=InMemoryEmbeddingRetriever(document_store=document_store),
    name="query_retriever"
)
p.add_component(
    instance=InMemoryEmbeddingRetriever(document_store=document_store),
    name="passage_retriever"
)

p.connect("text_router.passage", "passage_embedder.text")
p.connect("passage_embedder.embedding", "passage_retriever.query_embedding")
p.connect("text_router.query", "query_embedder.text")
p.connect("query_embedder.embedding", "query_retriever.query_embedding")

# Query Example
result = p.run({"text_router": {"text": "What is the capital of Germany?"}})
print(result)

>>{'query_retriever': {'documents': [Document(id=32d393dd8ee60648ae7e630cfe34b1922e747812ddf9a2c8b3650e66e0ecdb5a, 
content: 'Germany, officially the Federal Republic of Germany, is a country in the western region of Central E...', 
score: 0.8625669285150891), Document(id=c17102d8d818ce5cdfee0288488c518f5c9df238a9739a080142090e8c4cb3ba, 
content: 'France, officially the French Republic, is a country located primarily in Western Europe. France is ...', 
score: 0.7637571978602222)]}}

其他参考资料

📓 教程: 使用 TransformersTextRouter 和 TransformersZeroShotTextRouter 进行查询分类