Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

template.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. break
  70. else:
  71. assert False, f"Can't find parameter '{key}' for {cpn_id}"
  72. continue
  73. component_id = para["key"]
  74. cpn = self._canvas.get_component(component_id)["obj"]
  75. if cpn.component_name.lower() == "answer":
  76. hist = self._canvas.get_history(1)
  77. if hist:
  78. hist = hist[0]["content"]
  79. else:
  80. hist = ""
  81. self.make_kwargs(para, kwargs, hist)
  82. continue
  83. _, out = cpn.output(allow_partial=False)
  84. result = ""
  85. if "content" in out.columns:
  86. result = "\n".join([o if isinstance(o, str) else str(o) for o in out["content"]])
  87. self.make_kwargs(para, kwargs, result)
  88. env = SandboxedEnvironment(
  89. autoescape=True,
  90. undefined=StrictUndefined,
  91. )
  92. template = env.from_string(content)
  93. try:
  94. content = template.render(kwargs)
  95. except Exception:
  96. pass
  97. for n, v in kwargs.items():
  98. if not isinstance(v, str):
  99. try:
  100. v = json.dumps(v, ensure_ascii=False)
  101. except Exception:
  102. pass
  103. # Process backslashes in strings, Use Lambda function to avoid escape issues
  104. if isinstance(v, str):
  105. v = v.replace("\\", "\\\\")
  106. content = re.sub(r"\{%s\}" % re.escape(n), lambda match: v, content)
  107. content = re.sub(r"(#+)", r" \1 ", content)
  108. return Template.be_output(content)
  109. def make_kwargs(self, para, kwargs, value):
  110. self._param.inputs.append({"component_id": para["key"], "content": value})
  111. try:
  112. value = json.loads(value)
  113. except Exception:
  114. pass
  115. kwargs[para["key"]] = value