From e5940d3020fdcdd53cceacd54efd6f5a12020e42 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sat, 22 Jun 2024 09:47:04 -0400 Subject: [PATCH] Fix Claude consecutive roles issue This commit addresses the issue where the Claude model was throwing an error due to multiple consecutive "user" roles in the chat history. The following changes have been made: - Implemented a new function `group_consecutive_roles` to group consecutive messages of the same role into one. - Updated the `chat_claude`, function to use the `group_consecutive_roles` function before sending the chat history to the respective models. By grouping consecutive messages of the same role, the chat history now alternates between "user" and "assistant" roles, resolving the "roles must alternate between 'user' and 'assistant'" error from the Claude model. --- app.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app.py b/app.py index 247e02b..2538c1b 100644 --- a/app.py +++ b/app.py @@ -37,6 +37,7 @@ cancellation_requests = {} system_users = [ "anthropic.claude-3-haiku-20240307-v1:0", "anthropic.claude-3-sonnet-20240229-v1:0", + "anthropic.claude-3-5-sonnet-20240620-v1:0", "gpt-3.5-turbo", "gpt-4", "gpt-4o", @@ -456,7 +457,36 @@ def handle_update_message(data): ) +def group_consecutive_roles(messages): + if not messages: + return [] + + grouped_messages = [] + current_role = messages[0]['role'] + current_content = [] + + for message in messages: + if message['role'] == current_role: + current_content.append(message['content']) + else: + grouped_messages.append({ + 'role': current_role, + 'content': ' '.join(current_content) + }) + current_role = message['role'] + current_content = [message['content']] + + # Append the last grouped message + grouped_messages.append({ + 'role': current_role, + 'content': ' '.join(current_content) + }) + + return grouped_messages + + def chat_claude( + #username, room_name, model_name="anthropic.claude-3-5-sonnet-20240620-v1:0" username, room_name, model_name="anthropic.claude-3-sonnet-20240229-v1:0" ): with app.app_context(): @@ -473,6 +503,9 @@ def chat_claude( role = "assistant" if msg.username in system_users else "user" chat_history.append({"role": role, "content": msg.content}) + # only claude cares about this constrant. + chat_history = group_consecutive_roles(chat_history) + # Initialize the Bedrock client using boto3 and profile name. if app.config.get("PROFILE_NAME"): session = boto3.Session(profile_name=app.config["PROFILE_NAME"])