Local.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import annotations
  2. from ...locals.models import get_models
  3. try:
  4. from ...locals.provider import LocalProvider
  5. has_requirements = True
  6. except ImportError:
  7. has_requirements = False
  8. from ...typing import Messages, CreateResult
  9. from ...providers.base_provider import AbstractProvider, ProviderModelMixin
  10. from ...errors import MissingRequirementsError
  11. class Local(AbstractProvider, ProviderModelMixin):
  12. label = "GPT4All"
  13. working = True
  14. supports_message_history = True
  15. supports_system_message = True
  16. supports_stream = True
  17. @classmethod
  18. def get_models(cls):
  19. if not cls.models:
  20. cls.models = list(get_models())
  21. cls.default_model = cls.models[0]
  22. return cls.models
  23. @classmethod
  24. def create_completion(
  25. cls,
  26. model: str,
  27. messages: Messages,
  28. stream: bool,
  29. **kwargs
  30. ) -> CreateResult:
  31. if not has_requirements:
  32. raise MissingRequirementsError('Install "gpt4all" package | pip install -U g4f[local]')
  33. return LocalProvider.create_completion(
  34. cls.get_model(model),
  35. messages,
  36. stream,
  37. **kwargs
  38. )