Skip to content

Commit

Permalink
fix: skip faulty merge logic if we didn't ask for modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Mar 23, 2024
1 parent 04f392f commit dc6ff8b
Showing 1 changed file with 65 additions and 59 deletions.
124 changes: 65 additions & 59 deletions litellm/llms/prompt_templates/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,69 +643,75 @@ def anthropic_messages_pt(messages: list):
"""
# add role=tool support to allow function call result/error submission
user_message_types = {"user", "tool"}
# reformat messages to ensure user/assistant are alternating, if there's either 2 consecutive 'user' messages or 2 consecutive 'assistant' message, merge them.
new_messages = []
msg_i = 0
while msg_i < len(messages):
user_content = []
## MERGE CONSECUTIVE USER CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] in user_message_types:
if isinstance(messages[msg_i]["content"], list):
for m in messages[msg_i]["content"]:
if m.get("type", "") == "image_url":
user_content.append(
{
"type": "image",
"source": convert_to_anthropic_image_obj(
m["image_url"]["url"]
),
}
)
elif m.get("type", "") == "text":
user_content.append({"type": "text", "text": m["text"]})
else:
# Tool message content will always be a string
user_content.append(
{
"type": "text",
"text": (
convert_to_anthropic_tool_result(messages[msg_i])
if messages[msg_i]["role"] == "tool"
else messages[msg_i]["content"]
),
}
)

msg_i += 1

if user_content:
new_messages.append({"role": "user", "content": user_content})

assistant_content = []
## MERGE CONSECUTIVE ASSISTANT CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] == "assistant":
assistant_text = (
messages[msg_i].get("content") or ""
) # either string or none
if messages[msg_i].get(
"tool_calls", []
): # support assistant tool invoke convertion
assistant_text += convert_to_anthropic_tool_invoke(
messages[msg_i]["tool_calls"]
)
if not litellm.modify_params:
print("SKIPPING NONSENSE!!!")
new_messages = messages
else:
# reformat messages to ensure user/assistant are alternating, if there's either 2 consecutive 'user' messages or 2 consecutive 'assistant' message, merge them.
new_messages = []
msg_i = 0
while msg_i < len(messages):
user_content = []
# MERGE CONSECUTIVE USER CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] in user_message_types:
if isinstance(messages[msg_i]["content"], list):
for m in messages[msg_i]["content"]:
if m.get("type", "") == "image_url":
user_content.append(
{
"type": "image",
"source": convert_to_anthropic_image_obj(
m["image_url"]["url"]
),
}
)
elif m.get("type", "") == "text":
user_content.append({"type": "text", "text": m["text"]})
else:
# Tool message content will always be a string
user_content.append(
{
"type": "text",
"text": (
convert_to_anthropic_tool_result(messages[msg_i])
if messages[msg_i]["role"] == "tool"
else messages[msg_i]["content"]
),
}
)

assistant_content.append({"type": "text", "text": assistant_text})
msg_i += 1
msg_i += 1

if user_content:
new_messages.append({"role": "user", "content": user_content})

assistant_content = []
## MERGE CONSECUTIVE ASSISTANT CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] == "assistant":
assistant_text = (
messages[msg_i].get("content") or ""
) # either string or none
if messages[msg_i].get(
"tool_calls", []
): # support assistant tool invoke convertion
assistant_text += convert_to_anthropic_tool_invoke(
messages[msg_i]["tool_calls"]
)

if assistant_content:
new_messages.append({"role": "assistant", "content": assistant_content})
assistant_content.append({"type": "text", "text": assistant_text})
msg_i += 1

if (
msg_i < len(messages)
and messages[msg_i]["role"] != user_message_types
and messages[msg_i]["role"] != "assistant"
):
raise Exception(f"Invalid role passed in - {messages[msg_i]}")
if assistant_content:
new_messages.append({"role": "assistant", "content": assistant_content})


if (
msg_i < len(messages)
and messages[msg_i]["role"] != user_message_types
and messages[msg_i]["role"] != "assistant"
):
raise Exception(f"Invalid role passed in - {messages[msg_i]}")

if new_messages[0]["role"] != "user":
if litellm.modify_params:
Expand Down

0 comments on commit dc6ff8b

Please sign in to comment.