chatlog#

Attributes#

Classes#

ChatLogItem

Represents a chat log entry for a request and response

ChatLogger

Handles chat logging functionality

ChatSocketAPI

An instance of this class is created for each chat request.

ChatSocketIOHandler

A handler class which writes logging records, appropriately formatted,

LangchainLoggingHandler

Functions#

initialize_json_log()

log_to_json(context, prompt, prompt_template, response)

log_chat_item(project, question, output, ...[, error])

Log a chat interaction to the chat log file.

Module Contents#

chatlog.logger[source]#
chatlog.log[source]#
class chatlog.ChatLogItem[source]#

Represents a chat log entry for a request and response

context: str[source]#
query: str[source]#
prompt_template: str[source]#
response: str[source]#
timestamp: str[source]#
conversation_id: str | None = None[source]#
view_name: str | None = None[source]#
error: bool | None = None[source]#
to_dict() Dict[str, Any][source]#

Convert to dictionary for JSON serialization

classmethod from_dict(data: Dict[str, Any]) ChatLogItem[source]#

Create from dictionary (e.g. from JSON)

class chatlog.ChatLogger(log_file_path: str)[source]#

Handles chat logging functionality

log_file_path[source]#
_ensure_log_file_exists()[source]#

Ensure the log file exists and is properly initialized

log_chat(item: ChatLogItem)[source]#

Log a chat item to the JSON file

get_logs() List[ChatLogItem][source]#

Get all chat logs

get_conversation_logs(conversation_id: str) List[ChatLogItem][source]#

Get logs for a specific conversation

class chatlog.ChatSocketAPI(project: mdvtools.mdvproject.MDVProject, id: str, room: str, conversation_id: str)[source]#

An instance of this class is created for each chat request. It will instantiate a Logger instance & SocketIOHandler which should be GCed when the request is finished.

project[source]#
socketio = None[source]#
progress_name = 'chat_progress'[source]#
project_namespace = '/project/Uninferable'[source]#
room[source]#
logger[source]#
log(msg: str)[source]#
update_chat_progress(message: str, id: str, progress: int, delta: int)[source]#

Send a message to the chat log and also update the progress bar.

Parameters:
  • message (str) – the message to send to the chat log

  • id (str) – the id of the associated chat request

  • progress (int) – the progress value (%) to update the progress bar with

  • delta (int) – the expected cost of the current operation (%)

class chatlog.ChatSocketIOHandler(socketio: flask_socketio.SocketIO, event_name: str, namespace: str, id: str, room: str)[source]#

Bases: logging.StreamHandler

A handler class which writes logging records, appropriately formatted, to a stream. Note that this class does not close the stream, as sys.stdout or sys.stderr may be used.

socketio[source]#
event_name[source]#
namespace[source]#
id[source]#
room[source]#
emit(record)[source]#

Emit a record - send it via socketio & also print it to the console. subject to change.

chatlog.mypath[source]#
chatlog.json_keyfile_path[source]#
chatlog.initialize_json_log()[source]#
chatlog.log_to_json(context, prompt, prompt_template, response)[source]#
chatlog.file = False[source]#
class chatlog.LangchainLoggingHandler(logger: logging.Logger)[source]#

Bases: langchain_core.callbacks.BaseCallbackHandler

logger[source]#
log[source]#
on_chat_model_start(serialized: Dict[str, Any], messages: List[List[langchain_core.messages.BaseMessage]], **kwargs) None[source]#
on_llm_end(response: langchain_core.outputs.LLMResult, **kwargs) None[source]#
on_chain_start(serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs) None[source]#
on_chain_end(outputs: Dict[str, Any], **kwargs) None[source]#
chatlog.log_chat_item(project, question, output, prompt_template, response, conversation_id, view_name: str | None, error: bool = False)[source]#

Log a chat interaction to the chat log file. :param project: The MDVProject instance :param output: Result of invoke ‘from langchain.chains import RetrievalQA’ (can be None for errors) :param prompt_template: The template used for the prompt (can be empty for errors) :param response: The response generated (error message if error) :param conversation_id: ID to group messages from the same conversation :param error: Whether this log is for an error