File size: 1,556 Bytes
af4f56f
 
840ed42
af4f56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Initialize_db as db
import json
from langchain_core.messages import messages_from_dict


def get_user_conversation(user_name: str):
    query = f"""
    SELECT * FROM (
        SELECT *
        FROM u816628190_booking.MovieLogs   
        WHERE user_id = '{user_name}'
        ORDER BY response_timestamp DESC LIMIT 5
    ) AS tmp
    ORDER BY response_timestamp
    """
    df = db.get_results_as_dframe(query)
    messages = []
    if len(df) > 0:
        for ind, val in df.iterrows():
            if val['history']:
                messages.extend(messages_from_dict(json.loads(val['history'])))
    return messages

def update_conversation(user_id, user_message, request_timestamp, ai_message, response_timestamp, history):
    """
    Updates conversation in database with serialized history
    
    Args:
        user_id (str): User identifier
        user_message (str): Message from user
        request_timestamp (datetime): Time of user message
        ai_message (str): Response from AI
        response_timestamp (datetime): Time of AI response
        history (list): Conversation history
    """

    
    insert_query = """
    INSERT INTO u816628190_booking.MovieLogs 
    (user_id, user_message, request_timestamp, ai_message, response_timestamp, history)
    VALUES (%s, %s, %s, %s, %s, %s)
    """
    db.update_record_in_db(
        insert_query, 
        (user_id, user_message, request_timestamp, ai_message, response_timestamp, history)
    )

if __name__ == "__main__":
    df = get_user_conversation("Yash")
    print(df)