Pular para conteúdo

Services

chat_history_from_id(message_history_id, session)

Retrieves a chat history from the database based on the given ID.

Parameters:

Name Type Description Default
message_history_id str

The unique identifier of the chat history.

required
session Session

The database session used to execute the query.

required

Returns:

Name Type Description
MessageHistoryDB MessageHistoryDB

The retrieved chat history object. If not found, a new instance is created.

Raises:

Type Description
NoResultFound

If no chat history is found, a new one is instantiated instead.

Source code in api/chat/services.py
 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
def chat_history_from_id(message_history_id: str, session) -> MessageHistoryDB:
    """
    Retrieves a chat history from the database based on the given ID.

    Args:
        message_history_id (str):
            The unique identifier of the chat history.
        session (Session):
            The database session used to execute the query.

    Returns:
        MessageHistoryDB:
            The retrieved chat history object. If not found, a new instance is created.

    Raises:
        NoResultFound:
            If no chat history is found, a new one is instantiated instead.
    """
    stmt = select(MessageHistoryDB).where(
        MessageHistoryDB.id == message_history_id
    )

    try:
        chat = session.scalars(stmt).one()
    except NoResultFound:
        chat = MessageHistoryDB(id=message_history_id)

    return chat

save_assistant_message_in_chat(content, chat)

Saves an assistant message in the chat history.

Parameters:

Name Type Description Default
content str

The message content from the assistant.

required
chat MessageHistoryDB

The chat history object where the message should be stored.

required
Source code in api/chat/services.py
50
51
52
53
54
55
56
57
58
59
60
def save_assistant_message_in_chat(content: str, chat: MessageHistoryDB) -> None:
    """
    Saves an assistant message in the chat history.

    Args:
        content (str):
            The message content from the assistant.
        chat (MessageHistoryDB):
            The chat history object where the message should be stored.
    """
    chat.messages.append(MessageDB(role=Role.assistant.name, content=content))

save_user_message_in_chat(content, chat)

Saves a user message in the chat history.

Parameters:

Name Type Description Default
content str

The message content from the user.

required
chat MessageHistoryDB

The chat history object where the message should be stored.

required
Source code in api/chat/services.py
37
38
39
40
41
42
43
44
45
46
47
def save_user_message_in_chat(content: str, chat: MessageHistoryDB) -> None:
    """
    Saves a user message in the chat history.

    Args:
        content (str):
            The message content from the user.
        chat (MessageHistoryDB):
            The chat history object where the message should be stored.
    """
    chat.messages.append(MessageDB(role=Role.human.name, content=content))