Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

answer.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 random
  17. from abc import ABC
  18. from functools import partial
  19. import pandas as pd
  20. from graph.component.base import ComponentBase, ComponentParamBase
  21. class AnswerParam(ComponentParamBase):
  22. """
  23. Define the Answer component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.post_answers = []
  28. def check(self):
  29. return True
  30. class Answer(ComponentBase, ABC):
  31. component_name = "Answer"
  32. def _run(self, history, **kwargs):
  33. if kwargs.get("stream"):
  34. return partial(self.stream_output)
  35. ans = self.get_input()
  36. if self._param.post_answers:
  37. ans = pd.concat([ans, pd.DataFrame([{"content": random.choice(self._param.post_answers)}])], ignore_index=False)
  38. return ans
  39. def stream_output(self):
  40. res = None
  41. if hasattr(self, "exception") and self.exception:
  42. res = {"content": str(self.exception)}
  43. self.exception = None
  44. yield res
  45. self.set_output(res)
  46. return
  47. stream = self.get_stream_input()
  48. if isinstance(stream, pd.DataFrame):
  49. res = stream
  50. for ii, row in stream.iterrows():
  51. yield row.to_dict()
  52. else:
  53. for st in stream():
  54. res = st
  55. yield st
  56. if self._param.post_answers:
  57. res["content"] += random.choice(self._param.post_answers)
  58. yield res
  59. self.set_output(res)
  60. def set_exception(self, e):
  61. self.exception = e