From 7725a877d68a03ae6823803b83b9209539baaec7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Jul 2024 00:37:21 -0700 Subject: [PATCH] enh: get_last_user_message_item --- backend/utils/misc.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/utils/misc.py b/backend/utils/misc.py index b4e499df8..5a05f167d 100644 --- a/backend/utils/misc.py +++ b/backend/utils/misc.py @@ -8,14 +8,22 @@ import uuid import time -def get_last_user_message(messages: List[dict]) -> str: +def get_last_user_message_item(messages: List[dict]) -> str: for message in reversed(messages): if message["role"] == "user": - if isinstance(message["content"], list): - for item in message["content"]: - if item["type"] == "text": - return item["text"] - return message["content"] + return message + return None + + +def get_last_user_message(messages: List[dict]) -> str: + message = get_last_user_message_item(messages) + + if message is not None: + if isinstance(message["content"], list): + for item in message["content"]: + if item["type"] == "text": + return item["text"] + return message["content"] return None