greet.py
 1  # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
 2  #
 3  # SPDX-License-Identifier: Apache-2.0
 4  
 5  import logging
 6  
 7  import haystack.logging as haystack_logging
 8  from haystack.core.component import component
 9  
10  logger = haystack_logging.getLogger(__name__)
11  
12  
13  @component
14  class Greet:
15      """
16      Logs a greeting message without affecting the value passing on the connection.
17      """
18  
19      def __init__(
20          self, message: str = "\nGreeting component says: Hi! The value is {value}\n", log_level: str = "INFO"
21      ) -> None:
22          """
23          Class constructor
24  
25          :param message: the message to log. Can use `{value}` to embed the value.
26          :param log_level: the level to log at.
27          """
28          if log_level and not getattr(logging, log_level):
29              raise ValueError(f"This log level does not exist: {log_level}")
30          self.message = message
31          self.log_level = log_level
32  
33      @component.output_types(value=int)
34      def run(self, value: int, message: str | None = None, log_level: str | None = None):
35          """
36          Logs a greeting message without affecting the value passing on the connection.
37          """
38          if not message:
39              message = self.message
40          if not log_level:
41              log_level = self.log_level
42  
43          level = getattr(logging, log_level, None)
44          if not level:
45              raise ValueError(f"This log level does not exist: {log_level}")
46  
47          logger.log(level=level, msg=message.format(value=value))
48          return {"value": value}