adding coverage for openrouter in exception mapping

This commit is contained in:
Krrish Dholakia 2023-08-29 15:30:24 -07:00
parent 436e8eadb2
commit 5bcf612724
6 changed files with 41 additions and 15 deletions

View file

@ -592,6 +592,7 @@ def completion(
model_response["choices"][0]["message"]["content"] = str(completion_response) model_response["choices"][0]["message"]["content"] = str(completion_response)
model_response["created"] = time.time() model_response["created"] = time.time()
model_response["model"] = model model_response["model"] = model
response = model_response
elif model in litellm.vertex_text_models: elif model in litellm.vertex_text_models:
import vertexai import vertexai
from vertexai.language_models import TextGenerationModel from vertexai.language_models import TextGenerationModel

View file

@ -12,6 +12,9 @@ import pytest
import litellm import litellm
from litellm import embedding, completion, text_completion from litellm import embedding, completion, text_completion
litellm.vertex_project = "pathrise-convert-1606954137718"
litellm.vertex_location = "us-central1"
litellm.use_client = True
# from infisical import InfisicalClient # from infisical import InfisicalClient
# litellm.set_verbose = True # litellm.set_verbose = True
@ -189,13 +192,13 @@ def test_completion_openrouter():
messages=messages, messages=messages,
temperature=0.5, temperature=0.5,
top_p=0.1, top_p=0.1,
user="ishaan_dev@berri.ai",
) )
# Add any assertions here to check the response # Add any assertions here to check the response
print(response) print(response)
except Exception as e: except Exception as e:
pytest.fail(f"Error occurred: {e}") pytest.fail(f"Error occurred: {e}")
test_completion_openrouter()
def test_completion_openai_with_more_optional_params(): def test_completion_openai_with_more_optional_params():
try: try:
@ -347,6 +350,14 @@ def test_completion_together_ai():
pytest.fail(f"Error occurred: {e}") pytest.fail(f"Error occurred: {e}")
def test_vertex_ai():
model_name = "chat-bison"
try:
response = completion(model=model_name, messages=messages, logger_fn=logger_fn)
print(response)
except Exception as e:
pytest.fail(f"Error occurred: {e}")
def test_petals(): def test_petals():
model_name = "stabilityai/StableBeluga2" model_name = "stabilityai/StableBeluga2"
try: try:

View file

@ -1,4 +1,4 @@
# from openai.error import AuthenticationError, InvalidRequestError, RateLimitError, OpenAIError from openai.error import AuthenticationError, InvalidRequestError, RateLimitError, OpenAIError
import os import os
import sys import sys
import traceback import traceback
@ -10,15 +10,17 @@ import litellm
from litellm import ( from litellm import (
embedding, embedding,
completion, completion,
AuthenticationError, # AuthenticationError,
InvalidRequestError, # InvalidRequestError,
ContextWindowExceededError, ContextWindowExceededError,
RateLimitError, # RateLimitError,
ServiceUnavailableError, # ServiceUnavailableError,
OpenAIError, # OpenAIError,
) )
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import pytest import pytest
litellm.vertex_project = "pathrise-convert-1606954137718"
litellm.vertex_location = "us-central1"
litellm.failure_callback = ["sentry"] litellm.failure_callback = ["sentry"]
# litellm.set_verbose = True # litellm.set_verbose = True
@ -33,8 +35,8 @@ litellm.failure_callback = ["sentry"]
# Approach: Run each model through the test -> assert if the correct error (always the same one) is triggered # Approach: Run each model through the test -> assert if the correct error (always the same one) is triggered
# models = ["gpt-3.5-turbo", "chatgpt-test", "claude-instant-1", "command-nightly"] # models = ["gpt-3.5-turbo", "chatgpt-test", "claude-instant-1", "command-nightly"]
test_model = "j2-light" test_model = "claude-instant-1"
models = ["j2-light"] models = ["claude-instant-1"]
def logging_fn(model_call_dict): def logging_fn(model_call_dict):
@ -48,7 +50,7 @@ def logging_fn(model_call_dict):
# Test 1: Context Window Errors # Test 1: Context Window Errors
@pytest.mark.parametrize("model", models) @pytest.mark.parametrize("model", models)
def test_context_window(model): def test_context_window(model):
sample_text = "how does a court case get to the Supreme Court?" * 5000 sample_text = "how does a court case get to the Supreme Court?" * 1000000
messages = [{"content": sample_text, "role": "user"}] messages = [{"content": sample_text, "role": "user"}]
try: try:
print(f"model: {model}") print(f"model: {model}")
@ -84,14 +86,12 @@ def invalid_auth(model): # set the model key to an invalid key, depending on th
messages = [{"content": "Hello, how are you?", "role": "user"}] messages = [{"content": "Hello, how are you?", "role": "user"}]
temporary_key = None temporary_key = None
try: try:
custom_llm_provider = None
if model == "gpt-3.5-turbo": if model == "gpt-3.5-turbo":
temporary_key = os.environ["OPENAI_API_KEY"] temporary_key = os.environ["OPENAI_API_KEY"]
os.environ["OPENAI_API_KEY"] = "bad-key" os.environ["OPENAI_API_KEY"] = "bad-key"
elif model == "chatgpt-test": elif model == "chatgpt-test":
temporary_key = os.environ["AZURE_API_KEY"] temporary_key = os.environ["AZURE_API_KEY"]
os.environ["AZURE_API_KEY"] = "bad-key" os.environ["AZURE_API_KEY"] = "bad-key"
custom_llm_provider = "azure"
elif model == "claude-instant-1": elif model == "claude-instant-1":
temporary_key = os.environ["ANTHROPIC_API_KEY"] temporary_key = os.environ["ANTHROPIC_API_KEY"]
os.environ["ANTHROPIC_API_KEY"] = "bad-key" os.environ["ANTHROPIC_API_KEY"] = "bad-key"
@ -104,6 +104,9 @@ def invalid_auth(model): # set the model key to an invalid key, depending on th
elif "togethercomputer" in model: elif "togethercomputer" in model:
temporary_key = os.environ["TOGETHERAI_API_KEY"] temporary_key = os.environ["TOGETHERAI_API_KEY"]
os.environ["TOGETHERAI_API_KEY"] = "84060c79880fc49df126d3e87b53f8a463ff6e1c6d27fe64207cde25cdfcd1f24a" os.environ["TOGETHERAI_API_KEY"] = "84060c79880fc49df126d3e87b53f8a463ff6e1c6d27fe64207cde25cdfcd1f24a"
elif model in litellm.openrouter_models:
temporary_key = os.environ["OPENROUTER_API_KEY"]
os.environ["OPENROUTER_API_KEY"] = "bad-key"
elif ( elif (
model model
== "replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1" == "replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1"
@ -112,7 +115,7 @@ def invalid_auth(model): # set the model key to an invalid key, depending on th
os.environ["REPLICATE_API_KEY"] = "bad-key" os.environ["REPLICATE_API_KEY"] = "bad-key"
print(f"model: {model}") print(f"model: {model}")
response = completion( response = completion(
model=model, messages=messages, custom_llm_provider=custom_llm_provider model=model, messages=messages
) )
print(f"response: {response}") print(f"response: {response}")
except AuthenticationError as e: except AuthenticationError as e:
@ -123,6 +126,7 @@ def invalid_auth(model): # set the model key to an invalid key, depending on th
print(f"OpenAIError Caught Exception - {e}") print(f"OpenAIError Caught Exception - {e}")
except Exception as e: except Exception as e:
print(type(e)) print(type(e))
print(type(AuthenticationError))
print(e.__class__.__name__) print(e.__class__.__name__)
print(f"Uncaught Exception - {e}") print(f"Uncaught Exception - {e}")
pytest.fail(f"Error occurred: {e}") pytest.fail(f"Error occurred: {e}")

View file

@ -1331,7 +1331,17 @@ def exception_type(model, original_exception, custom_llm_provider):
if isinstance(original_exception, OriginalError): if isinstance(original_exception, OriginalError):
# Handle the OpenAIError # Handle the OpenAIError
exception_mapping_worked = True exception_mapping_worked = True
if custom_llm_provider == "azure": if model in litellm.openrouter_models:
print(f"e: {original_exception}")
print(f"original_exception.http_status: {original_exception.http_status}")
if original_exception.http_status == 413:
raise ContextWindowExceededError(
message=str(original_exception),
model=model,
llm_provider="openrouter"
)
original_exception.llm_provider = "openrouter"
elif custom_llm_provider == "azure":
original_exception.llm_provider = "azure" original_exception.llm_provider = "azure"
else: else:
original_exception.llm_provider = "openai" original_exception.llm_provider = "openai"
@ -1400,7 +1410,7 @@ def exception_type(model, original_exception, custom_llm_provider):
message=f"ReplicateException - {error_str}", message=f"ReplicateException - {error_str}",
llm_provider="replicate", llm_provider="replicate",
) )
elif model == "command-nightly": # Cohere elif model in litellm.cohere_models: # Cohere
if ( if (
"invalid api token" in error_str "invalid api token" in error_str
or "No API key provided." in error_str or "No API key provided." in error_str