Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare versions with packaging.version #332

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/chainlit/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
from chainlit.logger import logger


def is_langchain_installed():
from chainlit.langchain import LANGCHAIN_INSTALLED

return LANGCHAIN_INSTALLED


def init_lc_cache():
use_cache = config.run.no_cache is False and config.run.ci is False

if use_cache and is_langchain_installed():
import langchain
if use_cache:
try:
import langchain
except ImportError:
return
from langchain.cache import SQLiteCache

if config.project.lc_cache_path is not None:
Expand Down
15 changes: 5 additions & 10 deletions src/chainlit/haystack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
try:
import haystack
from chainlit.utils import check_module_version

if haystack.__version__ < "1.18.0":
raise ValueError(
"Haystack version is too old, expected >= 1.18.0. Run `pip install farm-haystack --upgrade`"
)

HAYSTACK_INSTALLED = True
except ImportError:
HAYSTACK_INSTALLED = False
if not check_module_version("haystack", "1.18.0"):
raise ValueError(
"Haystack version is too old, expected >= 1.18.0. Run `pip install farm-haystack --upgrade`"
)
15 changes: 5 additions & 10 deletions src/chainlit/langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
try:
import langchain
from chainlit.utils import check_module_version

if langchain.__version__ < "0.0.198":
raise ValueError(
"LangChain version is too old, expected >= 0.0.198. Run `pip install langchain --upgrade`"
)

LANGCHAIN_INSTALLED = True
except ImportError:
LANGCHAIN_INSTALLED = False
if not check_module_version("langchain", "0.0.198"):
raise ValueError(
"LangChain version is too old, expected >= 0.0.198. Run `pip install langchain --upgrade`"
)
15 changes: 5 additions & 10 deletions src/chainlit/langflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
try:
import langflow
from chainlit.utils import check_module_version

if langflow.__version__ < "0.1.4":
raise ValueError(
"Langflow version is too old, expected >= 0.1.4. Run `pip install langflow --upgrade`"
)

LANGFLOW_INSTALLED = True
except ImportError:
LANGFLOW_INSTALLED = False
if not check_module_version("langflow", "0.1.4"):
raise ValueError(
"Langflow version is too old, expected >= 0.1.4. Run `pip install langflow --upgrade`"
)

from typing import Dict, Optional, Union

Expand Down
15 changes: 5 additions & 10 deletions src/chainlit/llama_index/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
try:
import llama_index
from chainlit.utils import check_module_version

if llama_index.__version__ < "0.8.3":
raise ValueError(
"LlamaIndex version is too old, expected >= 0.8.3. Run `pip install llama_index --upgrade`"
)

LLAMA_INDEX_INSTALLED = True
except ImportError:
LLAMA_INDEX_INSTALLED = False
if not check_module_version("llama_index", "0.8.3"):
raise ValueError(
"LlamaIndex version is too old, expected >= 0.8.3. Run `pip install llama_index --upgrade`"
)
21 changes: 21 additions & 0 deletions src/chainlit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import inspect
from typing import Callable

from packaging import version

from chainlit.context import context
from chainlit.logger import logger
from chainlit.message import ErrorMessage
Expand Down Expand Up @@ -63,3 +65,22 @@ def __getattr__(name):
return getattr(module, name)

return __getattr__


def check_module_version(name, required_version):
"""
Check the version of a module.

Args:
name (str): A module name.
version (str): Minimum version.

Returns:
(bool): Return False if the module is installed and the version
is lower than the minimum version required.
"""
try:
module = importlib.import_module(name)
except ModuleNotFoundError:
return True
xsyann marked this conversation as resolved.
Show resolved Hide resolved
return version.parse(module.__version__) >= version.parse(required_version)
1 change: 1 addition & 0 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ watchfiles="^0.19.0"
prisma="^0.9.0"
filetype = "^1.2.0"
lazify = "^0.4.0"
packaging = "^23.1"

[tool.poetry.group.tests]
optional = true
Expand Down