Pular para conteúdo

Historic messages model

Base

Bases: DeclarativeBase

Base class for SQLAlchemy models, defining default table configurations.

Attributes:

Name Type Description
__table_args__ dict

Specifies the default schema ('public') for all tables.

Source code in api/models/historic_messages_model.py
 9
10
11
12
13
14
15
16
17
18
class Base(DeclarativeBase):
    """
    Base class for SQLAlchemy models, defining default table configurations.

    Attributes:
        __table_args__ (dict):
            Specifies the default schema ('public') for all tables.
    """
    __table_args__ = {'schema': 'public'}
    pass

MessageDB

Bases: Base

Represents an individual chat message stored in the database.

Attributes:

Name Type Description
__tablename__ str

The name of the database table ("message").

id int

The primary key identifier for the message.

role str

The role of the sender (e.g., "human", "assistant", "system").

content str

The actual message content.

message_history_id int

The foreign key linking this message to a chat history.

message_history MessageHistoryDB

The associated chat history.

Methods:

Name Description
to_dict

Converts the message into a dictionary format.

Source code in api/models/historic_messages_model.py
 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
110
111
112
113
114
115
116
117
118
119
120
121
122
class MessageDB(Base):
    """
    Represents an individual chat message stored in the database.

    Attributes:
        __tablename__ (str): 
            The name of the database table ("message").
        id (int): 
            The primary key identifier for the message.
        role (str): 
            The role of the sender (e.g., "human", "assistant", "system").
        content (str): 
            The actual message content.
        message_history_id (int): 
            The foreign key linking this message to a chat history.
        message_history (MessageHistoryDB): 
            The associated chat history.

    Methods:
        to_dict() -> dict:
            Converts the message into a dictionary format.
    """

    __tablename__ = "message"

    id: Mapped[int] = mapped_column(primary_key=True)
    role: Mapped[str] = mapped_column(String(20))
    content: Mapped[str] = mapped_column(String(2000))
    message_history_id: Mapped[int] = mapped_column(
        ForeignKey("public.message_history.id"))
    message_history: Mapped["MessageHistoryDB"] = relationship(
        back_populates="messages")

    def to_dict(self) -> dict:
        """
        Converts the message into a dictionary format.

        Returns:
            dict:
                A dictionary containing the `role` and `content` of the message.
        """
        return {'role': self.role, 'content': self.content}

to_dict()

Converts the message into a dictionary format.

Returns:

Name Type Description
dict dict

A dictionary containing the role and content of the message.

Source code in api/models/historic_messages_model.py
114
115
116
117
118
119
120
121
122
def to_dict(self) -> dict:
    """
    Converts the message into a dictionary format.

    Returns:
        dict:
            A dictionary containing the `role` and `content` of the message.
    """
    return {'role': self.role, 'content': self.content}

MessageHistoryDB

Bases: Base

Represents a chat history stored in the database.

Attributes:

Name Type Description
__tablename__ str

The name of the database table ("message_history").

id int

The primary key identifier for the chat history.

messages List[MessageDB]

A list of messages related to this chat history.

Methods:

Name Description
to_message_history

Converts the database object into a MessageHistory instance.

to_list

Converts the stored messages into a list of dictionaries.

Source code in api/models/historic_messages_model.py
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
class MessageHistoryDB(Base):
    """
    Represents a chat history stored in the database.

    Attributes:
        __tablename__ (str): 
            The name of the database table ("message_history").
        id (int): 
            The primary key identifier for the chat history.
        messages (List[MessageDB]): 
            A list of messages related to this chat history.

    Methods:
        to_message_history() -> MessageHistory:
            Converts the database object into a `MessageHistory` instance.
        to_list() -> list:
            Converts the stored messages into a list of dictionaries.
    """

    __tablename__ = "message_history"

    id: Mapped[int] = mapped_column(primary_key=True)
    messages: Mapped[List["MessageDB"]] = relationship(
        back_populates="message_history",
        cascade="all, delete-orphan"
    )

    def to_message_history(self) -> MessageHistory:
        """
        Converts the stored messages into a `MessageHistory` object.

        Returns:
            MessageHistory:
                A `MessageHistory` instance containing all messages.
        """
        message_history = MessageHistory()
        for msg in self.messages:
            message_history.add_message(
                Message(
                    role=Role.get(msg.role),
                    content=msg.content
                )
            )
        return message_history

    def to_list(self) -> list:
        """
        Converts the stored messages into a list of dictionaries.

        Returns:
            list:
                A list containing message dictionaries with `role` and `content`.
        """
        msg_list = []
        for msg in self.messages:
            msg_list.append(msg.to_dict())

        return msg_list

to_list()

Converts the stored messages into a list of dictionaries.

Returns:

Name Type Description
list list

A list containing message dictionaries with role and content.

Source code in api/models/historic_messages_model.py
66
67
68
69
70
71
72
73
74
75
76
77
78
def to_list(self) -> list:
    """
    Converts the stored messages into a list of dictionaries.

    Returns:
        list:
            A list containing message dictionaries with `role` and `content`.
    """
    msg_list = []
    for msg in self.messages:
        msg_list.append(msg.to_dict())

    return msg_list

to_message_history()

Converts the stored messages into a MessageHistory object.

Returns:

Name Type Description
MessageHistory MessageHistory

A MessageHistory instance containing all messages.

Source code in api/models/historic_messages_model.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def to_message_history(self) -> MessageHistory:
    """
    Converts the stored messages into a `MessageHistory` object.

    Returns:
        MessageHistory:
            A `MessageHistory` instance containing all messages.
    """
    message_history = MessageHistory()
    for msg in self.messages:
        message_history.add_message(
            Message(
                role=Role.get(msg.role),
                content=msg.content
            )
        )
    return message_history