mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-09-08 07:41:43 +00:00

* 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 <stefanofiorucci@gmail.com>
62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
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},
|
|
}
|