You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 re
  17. from agent.component.base import ComponentBase, ComponentParamBase
  18. class TemplateParam(ComponentParamBase):
  19. """
  20. Define the Generate component parameters.
  21. """
  22. def __init__(self):
  23. super().__init__()
  24. self.content = ""
  25. self.parameters = []
  26. def check(self):
  27. self.check_empty(self.content, "[Template] Content")
  28. return True
  29. class Template(ComponentBase):
  30. component_name = "Template"
  31. def get_dependent_components(self):
  32. cpnts = set([para["component_id"].split("@")[0] for para in self._param.parameters \
  33. if para.get("component_id") \
  34. and para["component_id"].lower().find("answer") < 0 \
  35. and para["component_id"].lower().find("begin") < 0])
  36. return list(cpnts)
  37. def _run(self, history, **kwargs):
  38. content = self._param.content
  39. self._param.inputs = []
  40. for para in self._param.parameters:
  41. if not para.get("component_id"): continue
  42. component_id = para["component_id"].split("@")[0]
  43. if para["component_id"].lower().find("@") >= 0:
  44. cpn_id, key = para["component_id"].split("@")
  45. for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
  46. if p["key"] == key:
  47. kwargs[para["key"]] = p.get("value", "")
  48. self._param.inputs.append(
  49. {"component_id": para["component_id"], "content": kwargs[para["key"]]})
  50. break
  51. else:
  52. assert False, f"Can't find parameter '{key}' for {cpn_id}"
  53. continue
  54. cpn = self._canvas.get_component(component_id)["obj"]
  55. if cpn.component_name.lower() == "answer":
  56. hist = self._canvas.get_history(1)
  57. if hist:
  58. hist = hist[0]["content"]
  59. else:
  60. hist = ""
  61. kwargs[para["key"]] = hist
  62. continue
  63. _, out = cpn.output(allow_partial=False)
  64. if "content" not in out.columns:
  65. kwargs[para["key"]] = ""
  66. else:
  67. kwargs[para["key"]] = " - "+"\n - ".join([o if isinstance(o, str) else str(o) for o in out["content"]])
  68. self._param.inputs.append({"component_id": para["component_id"], "content": kwargs[para["key"]]})
  69. for n, v in kwargs.items():
  70. content = re.sub(r"\{%s\}" % re.escape(n), str(v).replace("\\", " "), content)
  71. return Template.be_output(content)