您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

switch.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. from agent.component.base import ComponentBase, ComponentParamBase
  18. class SwitchParam(ComponentParamBase):
  19. """
  20. Define the Switch component parameters.
  21. """
  22. def __init__(self):
  23. super().__init__()
  24. """
  25. {
  26. "logical_operator" : "and | or"
  27. "items" : [
  28. {"cpn_id": "categorize:0", "operator": "contains", "value": ""},
  29. {"cpn_id": "categorize:0", "operator": "contains", "value": ""},...],
  30. "to": ""
  31. }
  32. """
  33. self.conditions = []
  34. self.end_cpn_id = "answer:0"
  35. self.operators = ['contains', 'not contains', 'start with', 'end with', 'empty', 'not empty', '=', '≠', '>',
  36. '<', '≥', '≤']
  37. def check(self):
  38. self.check_empty(self.conditions, "[Switch] conditions")
  39. for cond in self.conditions:
  40. if not cond["to"]: raise ValueError(f"[Switch] 'To' can not be empty!")
  41. class Switch(ComponentBase, ABC):
  42. component_name = "Switch"
  43. def _run(self, history, **kwargs):
  44. for cond in self._param.conditions:
  45. res = []
  46. for item in cond["items"]:
  47. out = self._canvas.get_component(item["cpn_id"])["obj"].output()[1]
  48. cpn_input = "" if "content" not in out.columns else " ".join(out["content"])
  49. res.append(self.process_operator(cpn_input, item["operator"], item["value"]))
  50. if cond["logical_operator"] != "and" and any(res):
  51. return Switch.be_output(cond["to"])
  52. if all(res):
  53. return Switch.be_output(cond["to"])
  54. return Switch.be_output(self._param.end_cpn_id)
  55. def process_operator(self, input: str, operator: str, value: str) -> bool:
  56. if not isinstance(input, str) or not isinstance(value, str):
  57. raise ValueError('Invalid input or value type: string')
  58. if operator == "contains":
  59. return True if value.lower() in input.lower() else False
  60. elif operator == "not contains":
  61. return True if value.lower() not in input.lower() else False
  62. elif operator == "start with":
  63. return True if input.lower().startswith(value.lower()) else False
  64. elif operator == "end with":
  65. return True if input.lower().endswith(value.lower()) else False
  66. elif operator == "empty":
  67. return True if not input else False
  68. elif operator == "not empty":
  69. return True if input else False
  70. elif operator == "=":
  71. return True if input == value else False
  72. elif operator == "≠":
  73. return True if input != value else False
  74. elif operator == ">":
  75. try:
  76. return True if float(input) > float(value) else False
  77. except Exception as e:
  78. return True if input > value else False
  79. elif operator == "<":
  80. try:
  81. return True if float(input) < float(value) else False
  82. except Exception as e:
  83. return True if input < value else False
  84. elif operator == "≥":
  85. try:
  86. return True if float(input) >= float(value) else False
  87. except Exception as e:
  88. return True if input >= value else False
  89. elif operator == "≤":
  90. try:
  91. return True if float(input) <= float(value) else False
  92. except Exception as e:
  93. return True if input <= value else False
  94. raise ValueError('Not supported operator' + operator)