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

StringJoiner

将不同组件的字符串合并成一个字符串列表的组件。

pipeline 中的最常见位置在至少两个其他组件之后,将它们的字符串合并。
强制运行变量“strings”: 来自连接组件的多个字符串。
输出变量“strings”: 一个合并后的字符串列表
API 参考Joiners (连接器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/string_joiner.py

概述

StringJoiner 组件收集来自各种管道组件的多个字符串输出,并将它们合并成一个列表。当您需要将管道不同部分的多个字符串合并成一个统一的输出时,这非常有用。

用法

from haystack.components.joiners import StringJoiner
from haystack.components.builders import PromptBuilder
from haystack.core.pipeline import Pipeline

string_1 = "What's Natural Language Processing?"
string_2 = "What is life?"

pipeline = Pipeline()
pipeline.add_component("prompt_builder_1", PromptBuilder("Builder 1: {{query}}"))
pipeline.add_component("prompt_builder_2", PromptBuilder("Builder 2: {{query}}"))
pipeline.add_component("string_joiner", StringJoiner())

pipeline.connect("prompt_builder_1.prompt", "string_joiner.strings")
pipeline.connect("prompt_builder_2.prompt", "string_joiner.strings")

result = pipeline.run(data={
    "prompt_builder_1": {"query": string_1}, 
    "prompt_builder_2": {"query": string_2}
})

print(result)