-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding StringJoiner * Release notes * Remove typing * Remove unused import * Try to fix header * Fix one test * Add to docs, move test to behavioral pipeline test * Undo changes * Fix test * Update haystack/components/joiners/string_joiner.py Co-authored-by: Stefano Fiorucci <[email protected]> * Update haystack/components/joiners/string_joiner.py Co-authored-by: Stefano Fiorucci <[email protected]> * Provide usage example * Apply suggestions from code review Co-authored-by: Stefano Fiorucci <[email protected]> --------- Co-authored-by: Stefano Fiorucci <[email protected]> Co-authored-by: Silvano Cerza <[email protected]>
- Loading branch information
1 parent
ab2eb8e
commit 2fd1d78
Showing
8 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import List | ||
|
||
from haystack import component, logging | ||
from haystack.core.component.types import Variadic | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@component | ||
class StringJoiner: | ||
""" | ||
Component to join strings from different components to a list of strings. | ||
### Usage example | ||
```python | ||
from haystack.components.joiners import StringJoiner | ||
from haystack.components.builders import PromptBuilder | ||
from haystack.core.pipeline import Pipeline | ||
from haystack.components.generators.chat import OpenAIChatGenerator | ||
from haystack.dataclasses import ChatMessage | ||
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") | ||
print(pipeline.run(data={"prompt_builder_1": {"query": string_1}, "prompt_builder_2": {"query": string_2}})) | ||
>> {"string_joiner": {"strings": ["Builder 1: What's Natural Language Processing?", "Builder 2: What is life?"]}} | ||
``` | ||
""" | ||
|
||
@component.output_types(strings=List[str]) | ||
def run(self, strings: Variadic[str]): | ||
""" | ||
Joins strings into a list of strings | ||
:param strings: | ||
strings from different components | ||
:returns: | ||
A dictionary with the following keys: | ||
- `strings`: Merged list of strings | ||
""" | ||
|
||
out_strings = list(strings) | ||
return {"strings": out_strings} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
- | | ||
Added component StringJoiner to join strings from different components to a list of strings. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from haystack.core.serialization import component_from_dict, component_to_dict | ||
from haystack.components.joiners.string_joiner import StringJoiner | ||
|
||
|
||
class TestStringJoiner: | ||
def test_init(self): | ||
joiner = StringJoiner() | ||
assert isinstance(joiner, StringJoiner) | ||
|
||
def test_to_dict(self): | ||
joiner = StringJoiner() | ||
data = component_to_dict(joiner, name="string_joiner") | ||
assert data == {"type": "haystack.components.joiners.string_joiner.StringJoiner", "init_parameters": {}} | ||
|
||
def test_from_dict(self): | ||
data = {"type": "haystack.components.joiners.string_joiner.StringJoiner", "init_parameters": {}} | ||
string_joiner = component_from_dict(StringJoiner, data=data, name="string_joiner") | ||
assert isinstance(string_joiner, StringJoiner) | ||
|
||
def test_empty_list(self): | ||
joiner = StringJoiner() | ||
result = joiner.run([]) | ||
assert result == {"strings": []} | ||
|
||
def test_single_string(self): | ||
joiner = StringJoiner() | ||
result = joiner.run("a") | ||
assert result == {"strings": ["a"]} | ||
|
||
def test_two_strings(self): | ||
joiner = StringJoiner() | ||
result = joiner.run(["a", "b"]) | ||
assert result == {"strings": ["a", "b"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters