threshold.py
1 # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> 2 # 3 # SPDX-License-Identifier: Apache-2.0 4 5 from haystack.core.component import component 6 7 8 @component 9 class Threshold: 10 """ 11 Redirects the value, along a different connection whether the value is above or below the given threshold. 12 13 :param threshold: the number to compare the input value against. This is also a parameter. 14 """ 15 16 def __init__(self, threshold: int = 10) -> None: 17 """ 18 :param threshold: the number to compare the input value against. 19 """ 20 self.threshold = threshold 21 22 @component.output_types(above=int, below=int) 23 def run(self, value: int, threshold: int | None = None): 24 """ 25 Redirects the value, along a different connection whether the value is above or below the given threshold. 26 27 :param threshold: the number to compare the input value against. This is also a parameter. 28 """ 29 if threshold is None: 30 threshold = self.threshold 31 32 if value < threshold: 33 return {"below": value} 34 return {"above": value}