base.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. from abc import ABC, abstractmethod
  15. from .....utils.subclass_register import AutoRegisterABCMetaClass
  16. import inspect
  17. import base64
  18. class BaseRetriever(ABC, metaclass=AutoRegisterABCMetaClass):
  19. """Base Retriever"""
  20. __is_base = True
  21. VECTOR_STORE_PREFIX = "PADDLEX_VECTOR_STORE"
  22. def __init__(self):
  23. super().__init__()
  24. @abstractmethod
  25. def generate_vector_database(self):
  26. raise NotImplementedError(
  27. "The method `generate_vector_database` has not been implemented yet.")
  28. @abstractmethod
  29. def similarity_retrieval(self):
  30. raise NotImplementedError(
  31. "The method `similarity_retrieval` has not been implemented yet.")
  32. def is_vector_store(self, s):
  33. return s.startswith(self.VECTOR_STORE_PREFIX)
  34. def encode_vector_store(self, vector_store_bytes):
  35. return self.VECTOR_STORE_PREFIX + base64.b64encode(vector_store_bytes).decode(
  36. "ascii"
  37. )
  38. def decode_vector_store(self, vector_store_str):
  39. return base64.b64decode(vector_store_str[len(self.VECTOR_STORE_PREFIX):])