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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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"):
  42. continue
  43. component_id = para["component_id"].split("@")[0]
  44. if para["component_id"].lower().find("@") >= 0:
  45. cpn_id, key = para["component_id"].split("@")
  46. for p in self._canvas.get_component(cpn_id)["obj"]._param.query:
  47. if p["key"] == key:
  48. kwargs[para["key"]] = p.get("value", "")
  49. self._param.inputs.append(
  50. {"component_id": para["component_id"], "content": kwargs[para["key"]]})
  51. break
  52. else:
  53. assert False, f"Can't find parameter '{key}' for {cpn_id}"
  54. continue
  55. cpn = self._canvas.get_component(component_id)["obj"]
  56. if cpn.component_name.lower() == "answer":
  57. hist = self._canvas.get_history(1)
  58. if hist:
  59. hist = hist[0]["content"]
  60. else:
  61. hist = ""
  62. kwargs[para["key"]] = hist
  63. continue
  64. _, out = cpn.output(allow_partial=False)
  65. if "content" not in out.columns:
  66. kwargs[para["key"]] = ""
  67. else:
  68. kwargs[para["key"]] = " - "+"\n - ".join([o if isinstance(o, str) else str(o) for o in out["content"]])
  69. self._param.inputs.append({"component_id": para["component_id"], "content": kwargs[para["key"]]})
  70. for n, v in kwargs.items():
  71. content = re.sub(r"\{%s\}" % re.escape(n), str(v).replace("\\", " "), content)
  72. return Template.be_output(content)