add_config.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import builtins
  2. from importlib import import_module
  3. from typing import Callable, Optional
  4. from embedchain.config.base_config import BaseConfig
  5. from embedchain.helper.json_serializable import register_deserializable
  6. @register_deserializable
  7. class ChunkerConfig(BaseConfig):
  8. """
  9. Config for the chunker used in `add` method
  10. """
  11. def __init__(
  12. self,
  13. chunk_size: Optional[int] = None,
  14. chunk_overlap: Optional[int] = None,
  15. length_function: Optional[Callable[[str], int]] = None,
  16. ):
  17. self.chunk_size = chunk_size if chunk_size else 2000
  18. self.chunk_overlap = chunk_overlap if chunk_overlap else 0
  19. if isinstance(length_function, str):
  20. self.length_function = self.load_func(length_function)
  21. else:
  22. self.length_function = length_function if length_function else len
  23. def load_func(self, dotpath: str):
  24. if "." not in dotpath:
  25. return getattr(builtins, dotpath)
  26. else:
  27. module_, func = dotpath.rsplit(".", maxsplit=1)
  28. m = import_module(module_)
  29. return getattr(m, func)
  30. @register_deserializable
  31. class LoaderConfig(BaseConfig):
  32. """
  33. Config for the chunker used in `add` method
  34. """
  35. def __init__(self):
  36. pass
  37. @register_deserializable
  38. class AddConfig(BaseConfig):
  39. """
  40. Config for the `add` method.
  41. """
  42. def __init__(
  43. self,
  44. chunker: Optional[ChunkerConfig] = None,
  45. loader: Optional[LoaderConfig] = None,
  46. ):
  47. """
  48. Initializes a configuration class instance for the `add` method.
  49. :param chunker: Chunker config, defaults to None
  50. :type chunker: Optional[ChunkerConfig], optional
  51. :param loader: Loader config, defaults to None
  52. :type loader: Optional[LoaderConfig], optional
  53. """
  54. self.loader = loader
  55. self.chunker = chunker