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.

iterationitem.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import pandas as pd
  18. from agent.component.base import ComponentBase, ComponentParamBase
  19. class IterationItemParam(ComponentParamBase):
  20. """
  21. Define the IterationItem component parameters.
  22. """
  23. def check(self):
  24. return True
  25. class IterationItem(ComponentBase, ABC):
  26. component_name = "IterationItem"
  27. def __init__(self, canvas, id, param: ComponentParamBase):
  28. super().__init__(canvas, id, param)
  29. self._idx = 0
  30. def _run(self, history, **kwargs):
  31. parent = self.get_parent()
  32. ans = parent.get_input()
  33. ans = parent._param.delimiter.join(ans["content"]) if "content" in ans else ""
  34. ans = [a.strip() for a in ans.split(parent._param.delimiter)]
  35. if not ans:
  36. self._idx = -1
  37. return pd.DataFrame()
  38. df = pd.DataFrame([{"content": ans[self._idx]}])
  39. self._idx += 1
  40. if self._idx >= len(ans):
  41. self._idx = -1
  42. return df
  43. def end(self):
  44. return self._idx == -1