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

template.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. import json
  17. import re
  18. from jinja2 import StrictUndefined
  19. from jinja2.sandbox import SandboxedEnvironment
  20. from agent.component.base import ComponentBase, ComponentParamBase
  21. class TemplateParam(ComponentParamBase):
  22. """
  23. Define the Generate component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.content = ""
  28. self.parameters = []
  29. def check(self):
  30. self.check_empty(self.content, "[Template] Content")
  31. return True
  32. class Template(ComponentBase):
  33. component_name = "Template"
  34. def get_dependent_components(self):
  35. inputs = self.get_input_elements()
  36. cpnts = set([i["key"] for i in inputs if i["key"].lower().find("answer") < 0 and i["key"].lower().find("begin") < 0])
  37. return list(cpnts)
  38. def get_input_elements(self):
  39. key_set = set([])
  40. res = []
  41. for r in re.finditer(r"\{([a-z]+[:@][a-z0-9_-]+)\}", self._param.content, flags=re.IGNORECASE):
  42. cpn_id = r.group(1)
  43. if cpn_id in key_set:
  44. continue
  45. if cpn_id.lower().find("begin@") == 0:
  46. cpn_id, key = cpn_id.split("@")
  47. for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
  48. if p["key"] != key:
  49. continue
  50. res.append({"key": r.group(1), "name": p["name"]})
  51. key_set.add(r.group(1))
  52. continue
  53. cpn_nm = self._canvas.get_component_name(cpn_id)
  54. if not cpn_nm:
  55. continue
  56. res.append({"key": cpn_id, "name": cpn_nm})
  57. key_set.add(cpn_id)
  58. return res
  59. def _run(self, history, **kwargs):
  60. content = self._param.content
  61. self._param.inputs = []
  62. for para in self.get_input_elements():
  63. if para["key"].lower().find("begin@") == 0:
  64. cpn_id, key = para["key"].split("@")
  65. for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
  66. if p["key"] == key:
  67. value = p.get("value", "")
  68. self.make_kwargs(para, kwargs, value)
  69. origin_pattern = "{begin@" + key + "}"
  70. new_pattern = "begin_" + key
  71. content = content.replace(origin_pattern, new_pattern)
  72. kwargs[new_pattern] = kwargs.pop(origin_pattern, "")
  73. break
  74. else:
  75. assert False, f"Can't find parameter '{key}' for {cpn_id}"
  76. continue
  77. component_id = para["key"]
  78. cpn = self._canvas.get_component(component_id)["obj"]
  79. if cpn.component_name.lower() == "answer":
  80. hist = self._canvas.get_history(1)
  81. if hist:
  82. hist = hist[0]["content"]
  83. else:
  84. hist = ""
  85. self.make_kwargs(para, kwargs, hist)
  86. if ":" in component_id:
  87. origin_pattern = "{" + component_id + "}"
  88. new_pattern = component_id.replace(":", "_")
  89. content = content.replace(origin_pattern, new_pattern)
  90. kwargs[new_pattern] = kwargs.pop(component_id, "")
  91. continue
  92. _, out = cpn.output(allow_partial=False)
  93. result = ""
  94. if "content" in out.columns:
  95. result = "\n".join([o if isinstance(o, str) else str(o) for o in out["content"]])
  96. self.make_kwargs(para, kwargs, result)
  97. env = SandboxedEnvironment(
  98. autoescape=True,
  99. undefined=StrictUndefined,
  100. )
  101. template = env.from_string(content)
  102. try:
  103. content = template.render(kwargs)
  104. except Exception:
  105. pass
  106. for n, v in kwargs.items():
  107. if not isinstance(v, str):
  108. try:
  109. v = json.dumps(v, ensure_ascii=False)
  110. except Exception:
  111. pass
  112. # Process backslashes in strings, Use Lambda function to avoid escape issues
  113. if isinstance(v, str):
  114. v = v.replace("\\", "\\\\")
  115. content = re.sub(r"\{%s\}" % re.escape(n), lambda match: v, content)
  116. content = re.sub(r"(#+)", r" \1 ", content)
  117. return Template.be_output(content)
  118. def make_kwargs(self, para, kwargs, value):
  119. self._param.inputs.append({"component_id": para["key"], "content": value})
  120. try:
  121. value = json.loads(value)
  122. except Exception:
  123. pass
  124. kwargs[para["key"]] = value