mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
add context window exceeded error for anthropic
This commit is contained in:
parent
4b6922918f
commit
509120bf61
4 changed files with 23 additions and 6 deletions
Binary file not shown.
|
@ -113,10 +113,13 @@ class AnthropicLLM:
|
||||||
)
|
)
|
||||||
print_verbose(f"raw model_response: {response.text}")
|
print_verbose(f"raw model_response: {response.text}")
|
||||||
## RESPONSE OBJECT
|
## RESPONSE OBJECT
|
||||||
|
try:
|
||||||
completion_response = response.json()
|
completion_response = response.json()
|
||||||
|
except:
|
||||||
|
raise AnthropicError(message=response.text, status_code=response.status_code)
|
||||||
if "error" in completion_response:
|
if "error" in completion_response:
|
||||||
raise AnthropicError(
|
raise AnthropicError(
|
||||||
message=completion_response["error"],
|
message=str(completion_response["error"]),
|
||||||
status_code=response.status_code,
|
status_code=response.status_code,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -23,7 +23,6 @@ litellm.vertex_project = "pathrise-convert-1606954137718"
|
||||||
litellm.vertex_location = "us-central1"
|
litellm.vertex_location = "us-central1"
|
||||||
|
|
||||||
litellm.failure_callback = ["sentry"]
|
litellm.failure_callback = ["sentry"]
|
||||||
# litellm.set_verbose = True
|
|
||||||
#### What this tests ####
|
#### What this tests ####
|
||||||
# This tests exception mapping -> trigger an exception from an llm provider -> assert if output is of the expected type
|
# This tests exception mapping -> trigger an exception from an llm provider -> assert if output is of the expected type
|
||||||
|
|
||||||
|
@ -35,8 +34,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 = "gpt-3.5-turbo"
|
test_model = "claude-instant-1"
|
||||||
models = ["gpt-3.5-turbo"]
|
models = ["claude-instant-1"]
|
||||||
|
|
||||||
|
|
||||||
def logging_fn(model_call_dict):
|
def logging_fn(model_call_dict):
|
||||||
|
@ -50,7 +49,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?" * 1000000
|
sample_text = "how does a court case get to the Supreme Court?" * 50000
|
||||||
messages = [{"content": sample_text, "role": "user"}]
|
messages = [{"content": sample_text, "role": "user"}]
|
||||||
try:
|
try:
|
||||||
print(f"model: {model}")
|
print(f"model: {model}")
|
||||||
|
@ -64,6 +63,7 @@ def test_context_window(model):
|
||||||
return
|
return
|
||||||
except InvalidRequestError as e:
|
except InvalidRequestError as e:
|
||||||
print(f"InvalidRequestError: {e.llm_provider}")
|
print(f"InvalidRequestError: {e.llm_provider}")
|
||||||
|
print(f"InvalidRequestError message: {e.message}")
|
||||||
return
|
return
|
||||||
except OpenAIError as e:
|
except OpenAIError as e:
|
||||||
print(f"OpenAIError: {e.llm_provider}")
|
print(f"OpenAIError: {e.llm_provider}")
|
||||||
|
|
|
@ -1357,6 +1357,14 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
else:
|
else:
|
||||||
exception_type = ""
|
exception_type = ""
|
||||||
if "claude" in model: # one of the anthropics
|
if "claude" in model: # one of the anthropics
|
||||||
|
if hasattr(original_exception, "message"):
|
||||||
|
if "prompt is too long" in original_exception.message:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise ContextWindowExceededError(
|
||||||
|
message=original_exception.message,
|
||||||
|
model=model,
|
||||||
|
llm_provider="anthropic"
|
||||||
|
)
|
||||||
if hasattr(original_exception, "status_code"):
|
if hasattr(original_exception, "status_code"):
|
||||||
print_verbose(f"status_code: {original_exception.status_code}")
|
print_verbose(f"status_code: {original_exception.status_code}")
|
||||||
if original_exception.status_code == 401:
|
if original_exception.status_code == 401:
|
||||||
|
@ -1372,6 +1380,12 @@ def exception_type(model, original_exception, custom_llm_provider):
|
||||||
model=model,
|
model=model,
|
||||||
llm_provider="anthropic",
|
llm_provider="anthropic",
|
||||||
)
|
)
|
||||||
|
elif original_exception.status_code == 413:
|
||||||
|
exception_mapping_worked = True
|
||||||
|
raise ContextWindowExceededError(
|
||||||
|
message=f"AnthropicException - {original_exception.message}",
|
||||||
|
llm_provider="anthropic",
|
||||||
|
)
|
||||||
elif original_exception.status_code == 429:
|
elif original_exception.status_code == 429:
|
||||||
exception_mapping_worked = True
|
exception_mapping_worked = True
|
||||||
raise RateLimitError(
|
raise RateLimitError(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue