Dynamically load and create an instance of a provider based on the naming convention.
(cls, provider_key, config)
| 26 | |
| 27 | @classmethod |
| 28 | def create_provider(cls, provider_key, config): |
| 29 | """Dynamically load and create an instance of a provider based on the naming convention.""" |
| 30 | # Convert provider_key to the expected module and class names |
| 31 | provider_class_name = f"{provider_key.capitalize()}Provider" |
| 32 | provider_module_name = f"{provider_key}_provider" |
| 33 | |
| 34 | module_path = f"aisuite.providers.{provider_module_name}" |
| 35 | |
| 36 | # Lazily load the module |
| 37 | try: |
| 38 | module = importlib.import_module(module_path) |
| 39 | except ImportError as e: |
| 40 | raise ImportError( |
| 41 | f"Could not import module {module_path}: {str(e)}. Please ensure the provider is supported by doing ProviderFactory.get_supported_providers()" |
| 42 | ) |
| 43 | |
| 44 | # Instantiate the provider class |
| 45 | provider_class = getattr(module, provider_class_name) |
| 46 | return provider_class(**config) |
| 47 | |
| 48 | @classmethod |
| 49 | @functools.cache |
no outgoing calls
no test coverage detected