base.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. """Initializes an instance of base retriever."""
  24. super().__init__()
  25. @abstractmethod
  26. def generate_vector_database(self):
  27. """
  28. Declaration of an abstract method. Subclasses are expected to
  29. provide a concrete implementation of generate_vector_database.
  30. """
  31. raise NotImplementedError(
  32. "The method `generate_vector_database` has not been implemented yet."
  33. )
  34. @abstractmethod
  35. def similarity_retrieval(self):
  36. """
  37. Declaration of an abstract method. Subclasses are expected to
  38. provide a concrete implementation of similarity_retrieval.
  39. """
  40. raise NotImplementedError(
  41. "The method `similarity_retrieval` has not been implemented yet."
  42. )
  43. def is_vector_store(self, s: str) -> bool:
  44. """
  45. Check if the given string starts with the vector store prefix.
  46. Args:
  47. s (str): The input string to check.
  48. Returns:
  49. bool: True if the string starts with the vector store prefix, False otherwise.
  50. """
  51. return s.startswith(self.VECTOR_STORE_PREFIX)
  52. def encode_vector_store(self, vector_store_bytes: bytes) -> str:
  53. """
  54. Encode the vector store bytes into a base64 string prefixed with a specific prefix.
  55. Args:
  56. vector_store_bytes (bytes): The bytes to encode.
  57. Returns:
  58. str: The encoded string with the prefix.
  59. """
  60. return self.VECTOR_STORE_PREFIX + base64.b64encode(vector_store_bytes).decode(
  61. "ascii"
  62. )
  63. def decode_vector_store(self, vector_store_str: str) -> bytes:
  64. """
  65. Decodes the vector store string by removing the prefix and decoding the base64 encoded string.
  66. Args:
  67. vector_store_str (str): The vector store string with a prefix.
  68. Returns:
  69. bytes: The decoded vector store data.
  70. """
  71. return base64.b64decode(vector_store_str[len(self.VECTOR_STORE_PREFIX) :])