Skip to content

Repository

ChatRepositoryImp

Bases: database.Interface.ChatRepository

Class to implement the ChatRepository interface.

Parameters:

  • ChatRepository (database.Interface.ChatRepository) –

    Parent class

Source code in src/project/database/Repository.py
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
96
97
class ChatRepositoryImp(ChatRepository):
    """Class to implement the ChatRepository interface.

    Args:
        ChatRepository (ChatRepository): Parent class
    """

    def __init__(self):
        self.__session_list = create_or_update_session(State.SESSION_LIST_STATE.value, [Session()])
        self.__current_session: Session = create_or_update_session(State.CURRENT_SESSION.value,
                                                                   get_value_from_state(State.SESSION_LIST_STATE.value)[
                                                                       0])

    def create_new_session(self):
        session = self.__session_list
        session.append(Session())
        create_or_update_session(State.SESSION_LIST_STATE.value, updated_value=session)
        create_or_update_session(State.CURRENT_SESSION.value, updated_value=session[-1])
        print('Session Created', get_value_from_state(State.CURRENT_SESSION.value).get_session_name())
        self.__session_list = session
        self.__current_session = session[-1]
        print('Current Session', self.__current_session.get_session_name())

    def get_all_session(self):
        return self.__session_list

    def get_current_session(self, session_id: int):
        for session in self.__session_list:
            if session.get_session_id() == session_id:
                self.__current_session = session
                create_or_update_session(
                    State.CURRENT_SESSION.value,
                    updated_value=self.__current_session
                )
                return session
        return self.__current_session

    def get_current(self) -> Session:
        return get_value_from_state(State.CURRENT_SESSION.value)

    def add_message(self, message: str, role: Role):
        messages = self.get_current().get_messages()
        messages.append(Message(role, message))
        self.__current_session.update_message(messages)
        session = create_or_update_session(State.CURRENT_SESSION.value, updated_value=self.__current_session)
        self.__current_session = session

State

Bases: enum.Enum

Enum to define the state keys for the session state.

Source code in src/project/database/Repository.py
 9
10
11
12
13
14
15
16
17
18
19
class State(Enum):
    """Enum to define the state keys for the session state.
    """
    CHAT_REPOSITORY = 'ChatRepository'
    GEMINI = 'Gemini'
    EXPORT = 'Export'
    DATABASE_STATE = 'database_state'
    SESSION_LIST_STATE = 'session_list_state'
    CURRENT_SESSION = 'current_session'
    MESSAGES_LIST = 'messages_list'
    SUMMARY_MARKDOWN = 'summary_markdown'

create_or_update_session(key, init_value=None, updated_value=None)

Function to create or update a session state.

Parameters:

  • key (any) –

    description

  • init_value (any, default: None ) –

    Initial value. Defaults to None.

  • updated_value (any, default: None ) –

    Updated value. Defaults to None.

Returns:

  • any

    Value of the session state

Source code in src/project/database/Repository.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def create_or_update_session(key, init_value=None, updated_value=None):
    """Function to create or update a session state.

    Args:
        key (any): _description_
        init_value (any, optional): Initial value. Defaults to None.
        updated_value (any, optional): Updated value. Defaults to None.

    Returns:
        any: Value of the session state
    """
    if key not in st.session_state and init_value is not None:
        st.session_state[key] = init_value
    elif key in st.session_state and updated_value is not None:
        st.session_state[key] = updated_value

    return st.session_state[key] if key in st.session_state else None

get_value_from_state(key)

Function to get the value from the session state. Args: key (any): Key of the session state.

Returns:

  • any

    Value of the session state

Source code in src/project/database/Repository.py
41
42
43
44
45
46
47
48
49
def get_value_from_state(key):
    """Function to get the value from the session state.
    Args:
        key (any): Key of the session state.

    Returns:
        any: Value of the session state
    """
    return st.session_state[key]