From 2a36f067631464a48c92e816a4eb6e33cde25602 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Tue, 5 Sep 2023 15:06:22 -0700 Subject: [PATCH] remove install_and_import remove petals --- litellm/main.py | 47 ++++++++--------------------------------------- litellm/utils.py | 38 +++++++++----------------------------- 2 files changed, 17 insertions(+), 68 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index cf02b1b32..2d8afca9c 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -14,7 +14,6 @@ from litellm import ( # type: ignore ) from litellm.utils import ( get_secret, - install_and_import, CustomStreamWrapper, read_config_args, completion_with_fallbacks, @@ -34,7 +33,6 @@ from typing import Callable, List, Optional, Dict encoding = tiktoken.get_encoding("cl100k_base") from litellm.utils import ( get_secret, - install_and_import, CustomStreamWrapper, ModelResponse, read_config_args, @@ -344,8 +342,10 @@ def completion( response = model_response elif "replicate" in model or custom_llm_provider == "replicate": # import replicate/if it fails then pip install replicate - install_and_import("replicate") - import replicate + try: + import replicate + except: + Exception("Replicate import failed please run `pip install replicate`") # Setting the relevant API KEY for replicate, replicate defaults to using os.environ.get("REPLICATE_API_TOKEN") replicate_key = os.environ.get("REPLICATE_API_TOKEN") @@ -507,8 +507,10 @@ def completion( ) elif model in litellm.cohere_models: # import cohere/if it fails then pip install cohere - install_and_import("cohere") - import cohere + try: + import cohere + except: + Exception("Cohere import failed please run `pip install cohere`") cohere_key = ( api_key @@ -776,39 +778,6 @@ def completion( ) return response response = model_response - elif custom_llm_provider == "petals" or ( - litellm.api_base and "chat.petals.dev" in litellm.api_base - ): - url = "https://chat.petals.dev/api/v1/generate" - import requests - - prompt = " ".join([message["content"] for message in messages]) - - ## LOGGING - logging.pre_call( - input=prompt, - api_key=None, - additional_args={"url": url, "max_new_tokens": 100}, - ) - - response = requests.post( - url, data={"inputs": prompt, "max_new_tokens": 100, "model": model} - ) - ## LOGGING - logging.post_call( - input=prompt, - api_key=None, - original_response=response.text, - additional_args={"url": url, "max_new_tokens": 100}, - ) - - completion_response = response.json()["outputs"] - - # RESPONSE OBJECT - model_response["choices"][0]["message"]["content"] = completion_response - model_response["created"] = time.time() - model_response["model"] = model - response = model_response else: raise ValueError( f"Unable to map your input to a model. Check your input - {args}" diff --git a/litellm/utils.py b/litellm/utils.py index 3f403ce70..1c78e50c4 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -119,33 +119,6 @@ def print_verbose(print_statement): if random.random() <= 0.3: print("Get help - https://discord.com/invite/wuPM9dRgDw") - -####### Package Import Handler ################### - - -def install_and_import(package: str): - if package in globals().keys(): - print_verbose(f"{package} has already been imported.") - return - try: - # Import the module - module = importlib.import_module(package) - except ImportError: - print_verbose(f"{package} is not installed. Installing...") - subprocess.call([sys.executable, "-m", "pip", "install", package]) - globals()[package] = importlib.import_module(package) - # except VersionConflict as vc: - # print_verbose(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 ################### from enum import Enum @@ -599,7 +572,11 @@ def token_counter(model, text): # use tiktoken or anthropic's tokenizer depending on the model num_tokens = 0 if "claude" in model: - install_and_import("anthropic") + try: + import anthropic + except Exception: + Exception("Anthropic import failed please run `pip install anthropic`") + from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT anthropic = Anthropic() @@ -1293,7 +1270,10 @@ def prompt_token_calculator(model, messages): text = " ".join(message["content"] for message in messages) num_tokens = 0 if "claude" in model: - install_and_import("anthropic") + try: + import anthropic + except: + Exception("Anthropic import failed please run `pip install anthropic`") from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT anthropic = Anthropic()