File size: 1,245 Bytes
913f308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# 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