hook-langchain.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # ------------------------------------------------------------------
  2. # Copyright (c) 2024 PyInstaller Development Team.
  3. #
  4. # This file is distributed under the terms of the GNU General Public
  5. # License (version 2.0 or later).
  6. #
  7. # The full license is available in LICENSE, distributed with
  8. # this software.
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-or-later
  11. # ------------------------------------------------------------------
  12. from PyInstaller.utils.hooks import collect_data_files, is_module_satisfies
  13. # This was required with langchain < 1.0.0; in contemporary versions, the package does not contain any data files,
  14. # so this should be effectively a no-op.
  15. datas = collect_data_files('langchain')
  16. # In langchain 1.2.1 the import logic for optional add-on packages was refactored, and direct imports in conditional
  17. # blocks were replaced with dictionary-based look-up; see https://github.com/langchain-ai/langchain/pull/32813
  18. # So now we need to use that same dictionary to explicitly collect the optional packages that happen to be installed
  19. # in the build environment.
  20. if is_module_satisfies('langchain >= 1.2.1'):
  21. from PyInstaller import isolated
  22. @isolated.decorate
  23. def get_optional_packages(var_name):
  24. packages = set()
  25. try:
  26. import langchain.chat_models.base
  27. providers = getattr(langchain.chat_models.base, var_name)
  28. packages.update(package_name for package_name, *_ in providers.values())
  29. except Exception:
  30. pass
  31. try:
  32. import langchain.embeddings.base
  33. providers = getattr(langchain.embeddings.base, var_name)
  34. packages.update(package_name for package_name, *_ in providers.values())
  35. except Exception:
  36. pass
  37. return sorted(packages)
  38. # langchain 1.2.10 renamed the `_SUPPORTED_PROVIDERS` into `_BUILTIN_PROVIDERS`.
  39. if is_module_satisfies('langchain >= 1.2.10'):
  40. var_name = '_BUILTIN_PROVIDERS'
  41. else:
  42. var_name = '_SUPPORTED_PROVIDERS'
  43. hiddenimports = get_optional_packages(var_name)
  44. warn_on_missing_hiddenimports = False