Spaces:
Paused
Paused
| # | |
| # SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org> | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # | |
| # Utility function to convert conversation history format for AI model consumption | |
| def convert_history(history): # Convert history to message format | |
| """ | |
| Converts a list of [user_msg, assistant_msg] pairs into a flat list of role-content dictionaries. | |
| This format is required for AI model input. | |
| """ | |
| new_history = [] # Initialize new history list | |
| for entry in history: # Iterate over each entry in history | |
| # Ensure the entry is a list with exactly two elements: user message and assistant message | |
| if isinstance(entry, list) and len(entry) == 2: # Check entry structure | |
| user_msg, assistant_msg = entry # Unpack user and assistant messages | |
| if user_msg is not None: # Check if user message is not None | |
| new_history.append({"role": "user", "content": user_msg}) # Add user message to new history | |
| if assistant_msg is not None: # Check if assistant message is not None | |
| new_history.append({"role": "assistant", "content": assistant_msg}) # Add assistant message to new history | |
| return new_history # Return the converted history list |