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