File size: 1,917 Bytes
2162545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# backend/session_utils.py

from backend.qdrant_client import qdrant_client, KB_COLLECTION, CHAT_HISTORY_COLLECTION
# Import FilterSelector from qdrant_client.models to correctly structure delete requests
from qdrant_client.models import Filter, FieldCondition, MatchValue, FilterSelector

def delete_session_data(session_id: str) -> bool:
    """
    Deletes all data associated with a given session_id from both the knowledge base
    and chat history collections in Qdrant.
    Returns True on success, False on failure.
    """
    print(f"πŸ—‘οΈ Deleting session data for session_id={session_id}")

    # Define the filter to target points that belong to the specified session_id
    session_filter_condition = Filter(
        must=[
            FieldCondition(
                key="session_id", # The field in your payload storing the session ID
                match=MatchValue(value=session_id) # The value to match
            )
        ]
    )

    try:
        # Delete points from the chat history collection
        qdrant_client.delete(
            collection_name=CHAT_HISTORY_COLLECTION,
            # points_selector must be a PointsSelector object (like FilterSelector)
            points_selector=FilterSelector(filter=session_filter_condition)
        )
        print(f"πŸ—‘οΈ Deleted chat history for session '{session_id}'.")

        # Delete points from the knowledge base (RAG) collection
        qdrant_client.delete(
            collection_name=KB_COLLECTION,
            points_selector=FilterSelector(filter=session_filter_condition)
        )
        print(f"πŸ—‘οΈ Deleted knowledge base data for session '{session_id}'.")

        print(f"βœ… All session data for '{session_id}' successfully deleted. Game Over (for this session)!")
        return True
    except Exception as e:
        print(f"❌ Error deleting session data for session '{session_id}': {e}")
        return False