-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for returning intermediate outputs of pipeline comp…
…onents (#7504) * feat: Add support for returning intermediate outputs of pipeline components The `pipeline.run` method has been extended to accept a set of component names whose inputs are returned in addition to the outputs of leaf components. * Add reno * Lint --------- Co-authored-by: Stefano Fiorucci <[email protected]>
- Loading branch information
Showing
3 changed files
with
93 additions
and
3 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
5 changes: 5 additions & 0 deletions
5
releasenotes/notes/pipeline-intermediate-outputs-7cb8e71f79532ec1.yaml
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,5 @@ | ||
--- | ||
enhancements: | ||
- | | ||
`pipeline.run` accepts a set of component names whose intermediate outputs are returned in the final | ||
pipeline output dictionary. |
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,61 @@ | ||
import logging | ||
|
||
from haystack.components.others import Multiplexer | ||
from haystack.core.pipeline import Pipeline | ||
from haystack.testing.sample_components import Accumulate, AddFixedValue, Double, Threshold | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
def test_pipeline_intermediate_outputs(): | ||
pipeline = Pipeline() | ||
pipeline.add_component("first_addition", AddFixedValue(add=2)) | ||
pipeline.add_component("second_addition", AddFixedValue()) | ||
pipeline.add_component("double", Double()) | ||
pipeline.connect("first_addition", "double") | ||
pipeline.connect("double", "second_addition") | ||
|
||
results = pipeline.run( | ||
{"first_addition": {"value": 1}}, include_outputs_from={"first_addition", "second_addition", "double"} | ||
) | ||
assert results == {"second_addition": {"result": 7}, "first_addition": {"result": 3}, "double": {"value": 6}} | ||
|
||
results = pipeline.run({"first_addition": {"value": 1}}, include_outputs_from={"double"}) | ||
assert results == {"second_addition": {"result": 7}, "double": {"value": 6}} | ||
|
||
|
||
def test_pipeline_with_loops_intermediate_outputs(): | ||
accumulator = Accumulate() | ||
|
||
pipeline = Pipeline(max_loops_allowed=10) | ||
pipeline.add_component("add_one", AddFixedValue(add=1)) | ||
pipeline.add_component("multiplexer", Multiplexer(type_=int)) | ||
pipeline.add_component("below_10", Threshold(threshold=10)) | ||
pipeline.add_component("below_5", Threshold(threshold=5)) | ||
pipeline.add_component("add_three", AddFixedValue(add=3)) | ||
pipeline.add_component("accumulator", accumulator) | ||
pipeline.add_component("add_two", AddFixedValue(add=2)) | ||
|
||
pipeline.connect("add_one.result", "multiplexer") | ||
pipeline.connect("multiplexer.value", "below_10.value") | ||
pipeline.connect("below_10.below", "accumulator.value") | ||
pipeline.connect("accumulator.value", "below_5.value") | ||
pipeline.connect("below_5.above", "add_three.value") | ||
pipeline.connect("below_5.below", "multiplexer") | ||
pipeline.connect("add_three.result", "multiplexer") | ||
pipeline.connect("below_10.above", "add_two.value") | ||
|
||
results = pipeline.run( | ||
{"add_one": {"value": 3}}, | ||
include_outputs_from={"add_two", "add_one", "multiplexer", "below_10", "accumulator", "below_5", "add_three"}, | ||
) | ||
|
||
assert results == { | ||
"add_two": {"result": 13}, | ||
"add_one": {"result": 4}, | ||
"multiplexer": {"value": 11}, | ||
"below_10": {"above": 11}, | ||
"accumulator": {"value": 8}, | ||
"below_5": {"above": 8}, | ||
"add_three": {"result": 11}, | ||
} |