lazy_import#

Lazy import a module and class.

Functions

safe_import(module_name, install_message)

Safely import a module and raise an ImportError with the install message if the module is not found.

Classes

LazyImport(import_path, optional_package, ...)

Lazy import a module and class.

OptionalPackages(value[, names, module, ...])

Enum for optional packages that can be used in the library.

class OptionalPackages(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

Enum for optional packages that can be used in the library.

The package name and error message are defined for each optional package as a tuple.

GROQ = ('groq', 'Please install groq with: pip install groq')#
OPENAI = ('openai', 'Please install openai with: pip install openai')#
ANTHROPIC = ('anthropic', 'Please install anthropic with: pip install anthropic')#
GOOGLE_GENERATIVEAI = ('google.generativeai', 'Please install google-generativeai with: pip install google-generativeai')#
TRANSFORMERS = ('transformers', 'Please install transformers with: pip install transformers')#
COHERE = ('cohere', 'Please install cohere with: pip install cohere')#
OLLAMA = ('ollama', 'Please install ollama with: pip install ollama')#
TORCH = ('torch', 'Please install torch with: pip install torch')#
FAISS = ('faiss', 'Please install faiss with: pip install faiss-cpu (or faiss if you use GPU)')#
SQLALCHEMY = ('sqlalchemy', 'Please install sqlalchemy with: pip install sqlalchemy')#
PGVECTOR = ('pgvector', 'Please install pgvector with: pip install pgvector')#
DATASETS = ('datasets', 'Please install datasets with: pip install datasets')#
QDRANT = ('qdrant-client', 'Please install qdrant-client with: pip install qdrant-client')#
class LazyImport(import_path: str, optional_package: OptionalPackages, *args, **kwargs)[source]#

Bases: object

Lazy import a module and class.

It is a proxy. The class/func will be created only when the class is instantiated or called.

Note

Do not subclass a lazy imported class.

Mainly internal library use to import optional packages only when needed.

Parameters:
  • import_path (str) – The import path of the module and class, eg. “adalflow.components.model_client.openai_client.OpenAIClient”.

  • optional_package (OptionalPackages) – The optional package to import, it helps define the package name and error message.

load_class()[source]#
safe_import(module_name: str, install_message: str) ModuleType[source]#

Safely import a module and raise an ImportError with the install message if the module is not found.

Mainly used internally to import optional packages only when needed.

Example:

  1. Tests

try:
    numpy = safe_import("numpy", "Please install numpy with: pip install numpy")
    print(numpy.__version__)
except ImportError as e:
    print(e)

When numpy is not installed, it will raise an ImportError with the install message. When numpy is installed, it will print the numpy version.

  1. Use it to delay the import of optional packages in the library.

from adalflow.utils.lazy_import import safe_import, OptionalPackages

numpy = safe_import(OptionalPackages.NUMPY.value[0], OptionalPackages.NUMPY.value[1])