From 6ec91227cd40a444ab64a17bd51a88e0c236fb9a Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Wed, 9 Aug 2023 14:15:19 -0700 Subject: [PATCH] fix install error --- litellm/tests/test_completion.py | 6 ++++-- litellm/utils.py | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index b4f3866b8..28ebba367 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -7,10 +7,10 @@ sys.path.insert(0, os.path.abspath('../..')) # Adds the parent directory to the import pytest import litellm from litellm import embedding, completion -from infisical import InfisicalClient +# from infisical import InfisicalClient # litellm.set_verbose = True -litellm.secret_manager_client = InfisicalClient(token=os.environ["INFISICAL_TOKEN"]) +# litellm.secret_manager_client = InfisicalClient(token=os.environ["INFISICAL_TOKEN"]) user_message = "Hello, whats the weather in San Francisco??" messages = [{ "content": user_message,"role": "user"}] @@ -26,6 +26,7 @@ def test_completion_claude(): except Exception as e: pytest.fail(f"Error occurred: {e}") +test_completion_claude() def test_completion_claude_stream(): try: messages = [ @@ -39,6 +40,7 @@ def test_completion_claude_stream(): except Exception as e: pytest.fail(f"Error occurred: {e}") +test_completion_claude_stream() def test_completion_hf_api(): try: user_message = "write some code to find the sum of two numbers" diff --git a/litellm/utils.py b/litellm/utils.py index b961fe812..04bef0709 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -5,6 +5,8 @@ import litellm, openai import random, uuid, requests import datetime, time import tiktoken +import pkg_resources +from pkg_resources import DistributionNotFound, VersionConflict encoding = tiktoken.get_encoding("cl100k_base") from .integrations.helicone import HeliconeLogger from .integrations.aispend import AISpendLogger @@ -36,14 +38,33 @@ def print_verbose(print_statement): ####### Package Import Handler ################### import importlib import subprocess -def install_and_import(package): +def install_and_import(package: str): try: - importlib.import_module(package) - except ImportError: + # Import the module + module = importlib.import_module(package) + + # Get the package's information + dist = pkg_resources.get_distribution(package) + required = [req for req in pkg_resources.get_distribution(package).requires()] + + # Check if there are dependencies + if required: + for req in required: + install_and_import(req.project_name) + else: + print(f"{package} has been successfully installed with no dependencies.") + + except (DistributionNotFound, ImportError): print(f"{package} is not installed. Installing...") - subprocess.call([sys.executable, '-m', 'pip', 'install', package]) - finally: + subprocess.call([sys.executable, "-m", "pip", "install", package]) globals()[package] = importlib.import_module(package) + except VersionConflict as vc: + print(f"Detected version conflict for {package}. Upgrading...") + subprocess.call([sys.executable, "-m", "pip", "install", "--upgrade", package]) + globals()[package] = importlib.import_module(package) + finally: + if package not in globals().keys(): + globals()[package] = importlib.import_module(package) ################################################## ####### LOGGING ###################