File size: 3,539 Bytes
7b7cab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import uuid
from pymongo import MongoClient
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory


class ChatManagement:
    def __init__(self, cluster_url, database_name, collection_name):
        """
        Initializes the ChatManagement class with MongoDB connection details.
        
        Args:
            cluster_url (str): MongoDB cluster URL.
            database_name (str): Name of the database.
            collection_name (str): Name of the collection.
        """
        self.connection_string = cluster_url
        self.database_name = database_name
        self.collection_name = collection_name
        self.chat_sessions = {}  # Dictionary to store chat history objects for each session

    def create_new_chat(self):
        """
        Creates a new chat session by initializing a MongoDBChatMessageHistory object.
        
        Returns:
            str: The unique chat ID.
        """
        # Generate a unique chat ID
        chat_id = str(uuid.uuid4())

        # Initialize MongoDBChatMessageHistory for the chat session
        chat_message_history = MongoDBChatMessageHistory(
            session_id=chat_id,
            connection_string=self.connection_string,
            database_name=self.database_name,
            collection_name=self.collection_name,
        )

        # Store the chat_message_history object in the session dictionary
        self.chat_sessions[chat_id] = chat_message_history
        return chat_id

    def get_chat_history(self, chat_id):
        """
        Retrieves the MongoDBChatMessageHistory object for a given chat session by its chat ID.
        
        Args:
            chat_id (str): The unique ID of the chat session.
        
        Returns:
            MongoDBChatMessageHistory or None: The chat history object of the chat session, or None if not found.
        """
        # Check if the chat session is already in memory
        if chat_id in self.chat_sessions:
            return self.chat_sessions[chat_id]
        
        # If not in memory, try to fetch from the database
        chat_message_history = MongoDBChatMessageHistory(
            session_id=chat_id,
            connection_string=self.connection_string,
            database_name=self.database_name,
            collection_name=self.collection_name,
        )
        if chat_message_history.messages:  # Check if the session exists in the database
            self.chat_sessions[chat_id] = chat_message_history
            return chat_message_history
        
        return None  # Chat session not found

    def initialize_chat_history(self, chat_id):
        """
        Initializes a new chat history for the given chat ID if it does not already exist.
        
        Args:
            chat_id (str): The unique ID of the chat session.
        
        Returns:
            MongoDBChatMessageHistory: The initialized chat history object.
        """
        # If the chat history already exists, return it
        if chat_id in self.chat_sessions:
            return self.chat_sessions[chat_id]

        # Otherwise, create a new chat history
        chat_message_history = MongoDBChatMessageHistory(
            session_id=chat_id,
            connection_string=self.connection_string,
            database_name=self.database_name,
            collection_name=self.collection_name,
        )

        # Save the new chat session to the session dictionary
        self.chat_sessions[chat_id] = chat_message_history
        return chat_message_history