Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

message.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 MessageParam(ComponentParamBase):
  22. """
  23. Define the Message component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.messages = []
  28. def check(self):
  29. self.check_empty(self.messages, "[Message]")
  30. return True
  31. class Message(ComponentBase, ABC):
  32. component_name = "Message"
  33. def _run(self, history, **kwargs):
  34. if kwargs.get("stream"):
  35. return partial(self.stream_output)
  36. return Message.be_output(random.choice(self._param.messages))
  37. def stream_output(self):
  38. res = None
  39. if self._param.messages:
  40. res = {"content": random.choice(self._param.messages)}
  41. yield res
  42. self.set_output(res)