fix exception raised in factory.py

This commit is contained in:
Ishaan Jaff 2024-07-13 09:55:04 -07:00
parent 66cedccd6b
commit d0dbc0742b
7 changed files with 85 additions and 23 deletions

View file

@ -1845,7 +1845,9 @@ class BedrockConverseLLM(BaseLLM):
inference_params.pop(key, None)
bedrock_messages: List[MessageBlock] = _bedrock_converse_messages_pt(
messages=messages
messages=messages,
model=model,
llm_provider="bedrock_converse",
)
bedrock_tools: List[ToolBlock] = _bedrock_tools_pt(
inference_params.pop("tools", [])

View file

@ -212,7 +212,9 @@ def completion(
headers = validate_environment(api_key)
completion_url = api_base
model = model
most_recent_message, chat_history = cohere_messages_pt_v2(messages=messages)
most_recent_message, chat_history = cohere_messages_pt_v2(
messages=messages, model=model, llm_provider="cohere_chat"
)
## Load Config
config = litellm.CohereConfig.get_config()

View file

@ -35,6 +35,9 @@ def prompt_injection_detection_default_pt():
return """Detect if a prompt is safe to run. Return 'UNSAFE' if not."""
BAD_MESSAGE_ERROR_STR = "Invalid Message "
def map_system_message_pt(messages: list) -> list:
"""
Convert 'system' message to 'user' message if provider doesn't support 'system' role.
@ -1205,7 +1208,11 @@ def convert_to_anthropic_tool_invoke(tool_calls: list) -> list:
return anthropic_tool_invoke
def anthropic_messages_pt(messages: list):
def anthropic_messages_pt(
messages: list,
model: str,
llm_provider: str,
):
"""
format messages for anthropic
1. Anthropic supports roles like "user" and "assistant" (system prompt sent separately)
@ -1284,10 +1291,10 @@ def anthropic_messages_pt(messages: list):
new_messages.append({"role": "assistant", "content": assistant_content})
if msg_i == init_msg_i: # prevent infinite loops
raise Exception(
"Invalid Message passed in - {}. File an issue https://github.com/BerriAI/litellm/issues".format(
messages[msg_i]
)
raise litellm.BadRequestError(
message=BAD_MESSAGE_ERROR_STR + f"passed in {messages[msg_i]}",
model=model,
llm_provider=llm_provider,
)
if not new_messages or new_messages[0]["role"] != "user":
if litellm.modify_params:
@ -1567,6 +1574,8 @@ def convert_to_cohere_tool_invoke(tool_calls: list) -> List[ToolCallObject]:
def cohere_messages_pt_v2(
messages: List,
model: str,
llm_provider: str,
) -> Tuple[Union[str, ToolResultObject], ChatHistory]:
"""
Returns a tuple(Union[tool_result, message], chat_history)
@ -1691,10 +1700,10 @@ def cohere_messages_pt_v2(
)
if msg_i == init_msg_i: # prevent infinite loops
raise Exception(
"Invalid Message passed in - {}. File an issue https://github.com/BerriAI/litellm/issues".format(
messages[msg_i]
)
raise litellm.BadRequestError(
message=BAD_MESSAGE_ERROR_STR + f"passed in {messages[msg_i]}",
model=model,
llm_provider=llm_provider,
)
return returned_message, new_messages
@ -2144,7 +2153,11 @@ def _convert_to_bedrock_tool_call_result(
return BedrockMessageBlock(role="user", content=[content_block])
def _bedrock_converse_messages_pt(messages: List) -> List[BedrockMessageBlock]:
def _bedrock_converse_messages_pt(
messages: List,
model: str,
llm_provider: str,
) -> List[BedrockMessageBlock]:
"""
Converts given messages from OpenAI format to Bedrock format
@ -2228,12 +2241,11 @@ def _bedrock_converse_messages_pt(messages: List) -> List[BedrockMessageBlock]:
contents.append(tool_call_result)
msg_i += 1
if msg_i == init_msg_i: # prevent infinite loops
raise Exception(
"Invalid Message passed in - {}. File an issue https://github.com/BerriAI/litellm/issues".format(
messages[msg_i]
)
raise litellm.BadRequestError(
message=BAD_MESSAGE_ERROR_STR + f"passed in {messages[msg_i]}",
model=model,
llm_provider=llm_provider,
)
return contents
@ -2425,7 +2437,9 @@ def prompt_factory(
elif custom_llm_provider == "anthropic":
if model == "claude-instant-1" or model == "claude-2":
return anthropic_pt(messages=messages)
return anthropic_messages_pt(messages=messages)
return anthropic_messages_pt(
messages=messages, model=model, llm_provider=custom_llm_provider
)
elif custom_llm_provider == "anthropic_xml":
return anthropic_messages_pt_xml(messages=messages)
elif custom_llm_provider == "together_ai":

View file

@ -3265,7 +3265,9 @@ def test_completion_anthropic_hanging():
{"role": "function", "name": "get_capital", "content": "Kokoko"},
]
converted_messages = anthropic_messages_pt(messages)
converted_messages = anthropic_messages_pt(
messages, model="claude-3-sonnet-20240229", llm_provider="anthropic"
)
print(f"converted_messages: {converted_messages}")

View file

@ -414,6 +414,35 @@ def test_completion_mistral_exception():
# test_completion_mistral_exception()
def test_completion_bedrock_invalid_role_exception():
"""
Test if litellm raises a BadRequestError for an invalid role on Bedrock
"""
try:
litellm.set_verbose = True
response = completion(
model="bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "very-bad-role", "content": "hello"}],
)
print(f"response: {response}")
print(response)
except Exception as e:
assert isinstance(
e, litellm.BadRequestError
), "Expected BadRequestError but got {}".format(type(e))
print("str(e) = {}".format(str(e)))
# This is important - We we previously returning a poorly formatted error string. Which was
# litellm.BadRequestError: litellm.BadRequestError: Invalid Message passed in {'role': 'very-bad-role', 'content': 'hello'}
# IMPORTANT ASSERTION
assert (
(str(e))
== "litellm.BadRequestError: Invalid Message passed in {'role': 'very-bad-role', 'content': 'hello'}"
)
def test_content_policy_exceptionimage_generation_openai():
try:
# this is ony a test - we needed some way to invoke the exception :(

View file

@ -121,13 +121,20 @@ def test_anthropic_messages_pt():
litellm.modify_params = True
messages = []
expected_messages = [{"role": "user", "content": [{"type": "text", "text": "."}]}]
assert anthropic_messages_pt(messages) == expected_messages
assert (
anthropic_messages_pt(
messages, model="claude-3-sonnet-20240229", llm_provider="anthropic"
)
== expected_messages
)
# Test case: No messages (filtered system messages only) when modify_params is False should raise error
litellm.modify_params = False
messages = []
with pytest.raises(Exception) as err:
anthropic_messages_pt(messages)
anthropic_messages_pt(
messages, model="claude-3-sonnet-20240229", llm_provider="anthropic"
)
assert "Invalid first message" in str(err.value)

View file

@ -5961,6 +5961,12 @@ def exception_type(
extra_kwargs={},
):
global user_logger_fn, liteDebuggerClient
if any(
isinstance(original_exception, exc_type)
for exc_type in litellm.LITELLM_EXCEPTION_TYPES
):
return original_exception
exception_mapping_worked = False
if litellm.suppress_debug_info is False:
print() # noqa
@ -6131,7 +6137,7 @@ def exception_type(
):
exception_mapping_worked = True
raise BadRequestError(
message=f"BadRequestError: {exception_provider} - {message}",
message=f"{exception_provider} - {message}",
llm_provider=custom_llm_provider,
model=model,
response=original_exception.response,
@ -6218,7 +6224,7 @@ def exception_type(
elif original_exception.status_code == 422:
exception_mapping_worked = True
raise BadRequestError(
message=f"BadRequestError: {exception_provider} - {message}",
message=f"{exception_provider} - {message}",
model=model,
llm_provider=custom_llm_provider,
response=original_exception.response,