base.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import base64
  15. from ..base import BaseComponent
  16. from ....utils.subclass_register import AutoRegisterABCMetaClass
  17. __all__ = ["BaseLLM"]
  18. class BaseLLM(BaseComponent, metaclass=AutoRegisterABCMetaClass):
  19. __is_base = True
  20. ERROR_MASSAGE = ""
  21. VECTOR_STORE_PREFIX = "PADDLEX_VECTOR_STORE"
  22. def __init__(self):
  23. super().__init__()
  24. def pre_process(self, inputs):
  25. return inputs
  26. def post_process(self, outputs):
  27. return outputs
  28. def pred(self, inputs):
  29. raise NotImplementedError("The method `pred` has not been implemented yet.")
  30. def get_vector(self):
  31. raise NotImplementedError(
  32. "The method `get_vector` has not been implemented yet."
  33. )
  34. def caculate_similar(self):
  35. raise NotImplementedError(
  36. "The method `caculate_similar` has not been implemented yet."
  37. )
  38. def apply(self, inputs):
  39. pre_process_results = self.pre_process(inputs)
  40. pred_results = self.pred(pre_process_results)
  41. post_process_results = self.post_process(pred_results)
  42. return post_process_results
  43. def is_vector_store(self, s):
  44. return s.startswith(self.VECTOR_STORE_PREFIX)
  45. def encode_vector_store(self, vector_store_bytes):
  46. return self.VECTOR_STORE_PREFIX + base64.b64encode(vector_store_bytes).decode(
  47. "ascii"
  48. )
  49. def decode_vector_store(self, vector_store_str):
  50. return base64.b64decode(vector_store_str[len(self.VECTOR_STORE_PREFIX) :])