maeser.chat.chat_logs module#

Module for managing chat logs, including logging and retrieving chat history, feedback, and training data.

© 2024 Blaine Freestone, Carson Bush

This file is part of Maeser.

Maeser is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Maeser is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with Maeser. If not, see <https://www.gnu.org/licenses/>.

class maeser.chat.chat_logs.BaseChatLogsManager(chat_log_path: str, user_manager: UserManager | None = None)[source]#

Bases: ABC

abstract get_chat_history(branch_name: str, session_id: str) dict[source]#

Abstract method to get chat history for a session.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

Returns:

The chat history for the session.

Return type:

dict

abstract get_chat_history_overview(user: User | None) list[dict][source]#

Abstract method to get an overview of chat history. This is used to display a list of overviews of previous chat conversations.

Parameters:

user (User | None) – The user to get chat history for.

Returns:

A list of dictionaries containing information about previous chat conversations. Each should have the following keys:
  • ’branch’: The name of the branch.

  • ’session’: The session ID for the conversation.

  • ’modified’: The timestamp of when the chat conversation was last modified.

  • ’header’: The text that will be used as the link text. Usually the first message in the conversation. Could also be a conversation title.

Return type:

list[dict]

abstract get_chat_logs_overview(sort_by: str, order: str, branch_filter: str, feedback_filter: str) tuple[list[dict], int, float][source]#

Abstract method to get an overview of chat logs.

Parameters:
  • sort_by (str) – The field to sort by.

  • order (str) – The order to sort by. Either ‘asc’ or ‘desc’.

  • branch_filter (str) – The branch to filter by.

  • feedback_filter (str) – The feedback to filter by.

Returns:

A tuple containing:
  • list[dict]: A list of dictionaries containing information about chat logs.

  • int: The total number of tokens used.

  • float: The total cost of the chat logs.

Return type:

tuple

abstract get_log_file_template(filename: str, branch: str) str[source]#

Abstract method to get the jinja template for a log file.

Parameters:
  • filename (str) – The name of the log file.

  • branch (str) – The branch the log file is in.

Returns:

The rendered template for the log file.

Return type:

str

abstract log(branch_name: str, session_id: str, log_data: dict) None[source]#

Abstract method to log chat data.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

  • log_data (dict) – The data to be logged.

Returns:

None

abstract log_feedback(branch_name: str, session_id: str, message_index: int, feedback: str) None[source]#

Abstract method to log feedback for a message.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

  • message_index (int) – The index of the message to add feedback to.

  • feedback (str) – The feedback to add to the message.

Returns:

None

abstract save_feedback(feedback: dict) None[source]#

Abstract method to save feedback input to a file.

Parameters:

feedback (dict) – The feedback to save.

Returns:

None

abstract save_training_data(training_data: dict) None[source]#

Abstract method to save training data to a file.

Parameters:

training_data (dict) – The training data to save.

Returns:

None

class maeser.chat.chat_logs.ChatLogsManager(chat_log_path: str)[source]#

Bases: BaseChatLogsManager

get_chat_history(branch_name: str, session_id: str) dict[source]#

Retrieves chat history for a specific session.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

Returns:

The chat history for the session.

Return type:

dict

get_chat_history_overview(user: User | None) list[dict][source]#

Gets an overview of chat history.

Parameters:

user (User | None) – The user to get chat history for.

Returns:

A list of dictionaries containing information about previous chat conversations.

Return type:

list[dict]

get_chat_logs_overview(sort_by: str, order: str, branch_filter: str, feedback_filter: str) tuple[list[dict], int, float][source]#

Gets an overview of chat logs.

Parameters:
  • sort_by (str) – The field to sort by.

  • order (str) – The order to sort by. Either ‘asc’ or ‘desc’.

  • branch_filter (str) – The branch to filter by.

  • feedback_filter (str) – The feedback to filter by.

Returns:

A tuple containing:
  • list[dict]: A list of dictionaries containing information about chat logs.

  • int: The total number of tokens used.

  • float: The total cost of the chat logs.

Return type:

tuple

get_log_file_template(filename: str, branch: str) str[source]#

Gets the Jinja template for a log file.

Parameters:
  • filename (str) – The name of the log file.

  • branch (str) – The branch the log file is in.

Returns:

The rendered template for the log file.

Return type:

str

log(branch_name: str, session_id: str, log_data: dict) None[source]#

Logs chat data to a YAML file.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

  • log_data (dict) – The data to be logged. Should contain the following keys: ‘user’, ‘cost’, ‘tokens’, and ‘message’.

Returns:

None

log_feedback(branch_name: str, session_id: str, message_index: int, feedback: str) None[source]#

Adds feedback to the log for a specific response in a specific session.

Parameters:
  • branch_name (str) – The name of the branch.

  • session_id (str) – The session ID for the conversation.

  • message_index (int) – The index of the message to add feedback to.

  • feedback (str) – The feedback to add to the message.

Returns:

None

save_feedback(feedback: dict) None[source]#

Saves feedback input to a YAML file.

Parameters:

feedback (dict) – The feedback to save.

Returns:

None

save_training_data(training_data: dict) None[source]#

Saves training data to a YAML file.

Parameters:

training_data (dict) – The training data to save.