From 9e801930087170bb24628e680ad4cbd4f6a5b098 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sat, 31 May 2025 22:39:07 -0700 Subject: [PATCH] Add the model name to each message's metadata --- modules/chat.py | 2 +- modules/html_generator.py | 47 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/modules/chat.py b/modules/chat.py index ba61c7a9..1222d2bb 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -710,7 +710,7 @@ def chatbot_wrapper(text, state, regenerate=False, _continue=False, loading_mess # Add timestamp for assistant's response at the start of generation row_idx = len(output['internal']) - 1 - update_message_metadata(output['metadata'], "assistant", row_idx, timestamp=get_current_timestamp()) + update_message_metadata(output['metadata'], "assistant", row_idx, timestamp=get_current_timestamp(), model_name=shared.model_name) # Generate reply = None diff --git a/modules/html_generator.py b/modules/html_generator.py index cbf3e19c..03b5d485 100644 --- a/modules/html_generator.py +++ b/modules/html_generator.py @@ -350,12 +350,14 @@ remove_button = f'' -def format_message_timestamp(history, role, index): +def format_message_timestamp(history, role, index, tooltip_include_timestamp=True): """Get a formatted timestamp HTML span for a message if available""" key = f"{role}_{index}" if 'metadata' in history and key in history['metadata'] and history['metadata'][key].get('timestamp'): timestamp = history['metadata'][key]['timestamp'] - return f"{timestamp}" + tooltip_text = get_message_tooltip(history, role, index, include_timestamp=tooltip_include_timestamp) + title_attr = f' title="{html.escape(tooltip_text)}"' if tooltip_text else '' + return f"{timestamp}" return "" @@ -388,6 +390,23 @@ def format_message_attachments(history, role, index): return "" +def get_message_tooltip(history, role, index, include_timestamp=True): + """Get tooltip text combining timestamp and model name for a message""" + key = f"{role}_{index}" + if 'metadata' not in history or key not in history['metadata']: + return "" + + meta = history['metadata'][key] + tooltip_parts = [] + + if include_timestamp and meta.get('timestamp'): + tooltip_parts.append(meta['timestamp']) + if meta.get('model_name'): + tooltip_parts.append(f"Model: {meta['model_name']}") + + return " | ".join(tooltip_parts) + + def get_version_navigation_html(history, i, role): """Generate simple navigation arrows for message versions""" key = f"{role}_{i}" @@ -462,15 +481,13 @@ def generate_instruct_html(history): # Create info buttons for timestamps if they exist info_message_user = "" if user_timestamp != "": - # Extract the timestamp value from the span - user_timestamp_value = user_timestamp.split('>', 1)[1].split('<', 1)[0] - info_message_user = info_button.replace("message", user_timestamp_value) + tooltip_text = get_message_tooltip(history, "user", i) + info_message_user = info_button.replace('title="message"', f'title="{html.escape(tooltip_text)}"') info_message_assistant = "" if assistant_timestamp != "": - # Extract the timestamp value from the span - assistant_timestamp_value = assistant_timestamp.split('>', 1)[1].split('<', 1)[0] - info_message_assistant = info_button.replace("message", assistant_timestamp_value) + tooltip_text = get_message_tooltip(history, "assistant", i) + info_message_assistant = info_button.replace('title="message"', f'title="{html.escape(tooltip_text)}"') if converted_visible[0]: # Don't display empty user messages output += ( @@ -521,8 +538,8 @@ def generate_cai_chat_html(history, name1, name2, style, character, reset_cache= converted_visible = [convert_to_markdown_wrapped(entry, message_id=i, use_cache=i != len(history['visible']) - 1) for entry in row_visible] # Get timestamps - user_timestamp = format_message_timestamp(history, "user", i) - assistant_timestamp = format_message_timestamp(history, "assistant", i) + user_timestamp = format_message_timestamp(history, "user", i, tooltip_include_timestamp=False) + assistant_timestamp = format_message_timestamp(history, "assistant", i, tooltip_include_timestamp=False) # Get attachments user_attachments = format_message_attachments(history, "user", i) @@ -580,15 +597,13 @@ def generate_chat_html(history, name1, name2, reset_cache=False): # Create info buttons for timestamps if they exist info_message_user = "" if user_timestamp != "": - # Extract the timestamp value from the span - user_timestamp_value = user_timestamp.split('>', 1)[1].split('<', 1)[0] - info_message_user = info_button.replace("message", user_timestamp_value) + tooltip_text = get_message_tooltip(history, "user", i) + info_message_user = info_button.replace('title="message"', f'title="{html.escape(tooltip_text)}"') info_message_assistant = "" if assistant_timestamp != "": - # Extract the timestamp value from the span - assistant_timestamp_value = assistant_timestamp.split('>', 1)[1].split('<', 1)[0] - info_message_assistant = info_button.replace("message", assistant_timestamp_value) + tooltip_text = get_message_tooltip(history, "assistant", i) + info_message_assistant = info_button.replace('title="message"', f'title="{html.escape(tooltip_text)}"') if converted_visible[0]: # Don't display empty user messages output += (