Skip to content

Session

Message

Class to define the message. Message with contain the role and content.And further can we wrap in the session.

Source code in src/project/database/Session.py
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
class Message:
    """Class to define the message.
    Message with contain the role and content.And further can we wrap in the session.
    """

    def __init__(self, role: Role = Role.MODEL, content: str = ''):
        """Constructor to initialize the message.

        Args:
            role (Role, optional): Role of the message. Defaults to Role.MODEL.
            content (str, optional): Content of the message. Defaults to ''.
        """
        self.__role = role
        self.__content = content
        self.__time_stamp = current_milli_time()

    def get_role(self) -> Role:
        """Get the role of the message.

        Returns:
            Role: Role of the message
        """
        return self.__role

    def get_content(self) -> str:
        """Get the content of the message.

        Returns:
            str: Content of the message
        """
        return self.__content
__init__(role=Role.MODEL, content='')

Constructor to initialize the message.

Parameters:

Source code in src/project/database/Session.py
35
36
37
38
39
40
41
42
43
44
def __init__(self, role: Role = Role.MODEL, content: str = ''):
    """Constructor to initialize the message.

    Args:
        role (Role, optional): Role of the message. Defaults to Role.MODEL.
        content (str, optional): Content of the message. Defaults to ''.
    """
    self.__role = role
    self.__content = content
    self.__time_stamp = current_milli_time()
get_content()

Get the content of the message.

Returns:

  • str ( str ) –

    Content of the message

Source code in src/project/database/Session.py
54
55
56
57
58
59
60
def get_content(self) -> str:
    """Get the content of the message.

    Returns:
        str: Content of the message
    """
    return self.__content
get_role()

Get the role of the message.

Returns:

Source code in src/project/database/Session.py
46
47
48
49
50
51
52
def get_role(self) -> Role:
    """Get the role of the message.

    Returns:
        Role: Role of the message
    """
    return self.__role

Role

Bases: enum.Enum

Enum to define the role of the message.

Source code in src/project/database/Session.py
22
23
24
25
26
27
class Role(Enum):
    """
    Enum to define the role of the message.
    """
    MODEL = 0
    USER = 1

Session

Class to define the session. Session act as a container for the messages. A session will have a unique id and name.

Source code in src/project/database/Session.py
 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
 98
 99
100
101
102
103
104
105
106
107
108
109
class Session:
    """
    Class to define the session.
    Session act as a container for the messages. A session will have a unique id and name.
    """

    def __init__(self):
        """
        Constructor to initialize the session.
        """
        self.__session_id = current_milli_time()
        self.__session_name = formate_time(self.__session_id)
        self.__messages = []

    def get_session_id(self) -> int:
        """Get the session id.

        Returns:
            int: Session id
        """
        return self.__session_id

    def get_session_name(self) -> str:
        """Get the session name.

        Returns:
            str: Session name
        """
        return self.__session_name

    def update_message(self, message=None):
        """Update the messages.

        Args:
            message (list, optional): List of messages. Defaults to None.
        """
        if message is None:
            message = []
        self.__messages = message

    def get_messages(self) -> list[Message]:
        """Get the session name.

        Returns:
            list[Message]: Get the list of messages
        """
        return self.__messages
__init__()

Constructor to initialize the session.

Source code in src/project/database/Session.py
69
70
71
72
73
74
75
def __init__(self):
    """
    Constructor to initialize the session.
    """
    self.__session_id = current_milli_time()
    self.__session_name = formate_time(self.__session_id)
    self.__messages = []
get_messages()

Get the session name.

Returns:

Source code in src/project/database/Session.py
103
104
105
106
107
108
109
def get_messages(self) -> list[Message]:
    """Get the session name.

    Returns:
        list[Message]: Get the list of messages
    """
    return self.__messages
get_session_id()

Get the session id.

Returns:

  • int ( int ) –

    Session id

Source code in src/project/database/Session.py
77
78
79
80
81
82
83
def get_session_id(self) -> int:
    """Get the session id.

    Returns:
        int: Session id
    """
    return self.__session_id
get_session_name()

Get the session name.

Returns:

  • str ( str ) –

    Session name

Source code in src/project/database/Session.py
85
86
87
88
89
90
91
def get_session_name(self) -> str:
    """Get the session name.

    Returns:
        str: Session name
    """
    return self.__session_name
update_message(message=None)

Update the messages.

Parameters:

  • message (list, default: None ) –

    List of messages. Defaults to None.

Source code in src/project/database/Session.py
 93
 94
 95
 96
 97
 98
 99
100
101
def update_message(self, message=None):
    """Update the messages.

    Args:
        message (list, optional): List of messages. Defaults to None.
    """
    if message is None:
        message = []
    self.__messages = message

convert_to_markdown(session)

Function to convert the session to markdown.

Parameters:

Returns:

  • str ( str ) –

    Markdown text

Source code in src/project/database/Session.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def convert_to_markdown(session: Session) -> str:
    """Function to convert the session to markdown.

    Args:
        session (Session): Session to be converted to markdown.

    Returns:
        str: Markdown text
    """
    markdown = '# ' + 'Tutor Talk\n\n'
    markdown += f'## {session.get_session_name()}\n\n'
    for message in session.get_messages():
        if message.get_role() == Role.MODEL:
            markdown += message.get_content() + '\n'
        else:
            markdown += '### ' + message.get_content() + '\n'
    markdown += '\n\n'
    markdown += '---'
    markdown += '\n\n'
    # markdown += '\n\n'
    # markdown += 'Developed by [Ayaan](https://www.github.com/aiyu-ayaan)'
    # markdown += '\n\n'
    markdown += 'Get the code on [GitHub](https://github.com/aiyu-ayaan/Gemini-Ai-Streamlit)'

    return to_markdown(markdown).data

map_message_list_to_history(messages)

Function to map the message list to history. Give a list of messages and return the history mentioned in https://ai.google.dev/tutorials/python_quickstart .

Parameters:

Returns:

  • list[dict]

    list[dict]: converted history

Source code in src/project/database/Session.py
139
140
141
142
143
144
145
146
147
148
149
def map_message_list_to_history(messages: list[Message]) -> list[dict]:
    """Function to map the message list to history.
    Give a list of messages and return the history mentioned in https://ai.google.dev/tutorials/python_quickstart .

    Args:
        messages (list[Message]): List of messages

    Returns:
        list[dict]: converted history
    """
    return [{'role': message.get_role().name.lower(), 'parts': [message.get_content()]} for message in messages]

to_markdown(text)

Convert the text to markdown.

Parameters:

  • text (str) –

    Text to be converted to markdown.

Returns:

  • Markdown ( IPython.core.display.Markdown ) –

    Markdown text

Source code in src/project/database/Session.py
 9
10
11
12
13
14
15
16
17
18
19
def to_markdown(text: str) -> Markdown:
    """Convert the text to markdown.

    Args:
        text (str): Text to be converted to markdown.

    Returns:
        Markdown: Markdown text
    """
    text = text.replace('•', '  *')
    return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))