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

AnswerJoiner

将来自不同生成器的多个答案合并到一个列表中。

pipeline 中的最常见位置在查询管道中,在 Generators 之后,以及随后返回答案列表的组件(例如 AnswerBuilder)之后。
强制运行变量“answers”: 要合并的答案的嵌套列表,来自 Generator。此输入是variadic,这意味着您可以将其连接到可变数量的组件。
输出变量“answers”: 合并后的答案列表
API 参考Joiners (连接器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/answer_joiner.py

概述

AnswerJoiner 将来自多个连接的 Answer 对象的输入列表合并,并将它们作为一个列表返回。

您可以选择设置top_k 参数,它指定要返回的最大答案数量。如果您不设置此参数,该组件将返回它接收到的所有答案。

用法

在这个简单的示例管道中,AnswerJoiner 合并了两个 Generator 实例的答案。

from haystack.components.builders import AnswerBuilder
from haystack.components.joiners import AnswerJoiner

from haystack.core.pipeline import Pipeline

from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage


query = "What's Natural Language Processing?"
messages = [ChatMessage.from_system("You are a helpful, respectful and honest assistant. Be super concise."),
            ChatMessage.from_user(query)]

pipe = Pipeline()
pipe.add_component("gpt-4o", OpenAIChatGenerator(model="gpt-4o"))
pipe.add_component("llama", OpenAIChatGenerator(model="gpt-3.5-turbo"))
pipe.add_component("aba", AnswerBuilder())
pipe.add_component("abb", AnswerBuilder())
pipe.add_component("joiner", AnswerJoiner())

pipe.connect("gpt-4o.replies", "aba")
pipe.connect("llama.replies", "abb")
pipe.connect("aba.answers", "joiner")
pipe.connect("abb.answers", "joiner")

results = pipe.run(data={"gpt-4o": {"messages": messages},
                            "llama": {"messages": messages},
                            "aba": {"query": query},
                            "abb": {"query": query}})