PromptRecorder
Note
This tutorial was written by GPT4 and edited by a human.
The prompt recorder is a class named PromptRecorder that helps in recording prompts and responses. It works as a context manager, allowing you to record prompts and responses within a specific context. Here's a brief overview of how it works:
-
The
PromptRecorderclass is initialized with an empty list calledpromptsto store the prompts and responses. -
When entering the context manager using the
withstatement, the__enter__()method is called, which sets the current instance of thePromptRecorderas the active recorder in theprompt_recorder_varcontext variable. -
To log a prompt and response, the
log()method is called with the prompt and response as arguments. This method appends the prompt and response as a dictionary to thepromptslist. -
The
autorecord()function is provided to be called within every bot. It checks if there is an activePromptRecorderinstance in the context and logs the prompt and response using thelog()method. -
When exiting the context manager, the
__exit__()method is called, which resets theprompt_recorder_varcontext variable toNoneand prints a message indicating that the recording is complete. -
The
PromptRecorderclass also provides methods to represent the recorded data in different formats, such as a string representation (__repr__()), an HTML representation (_repr_html_()), a pandas DataFrame representation (dataframe()), and a panel representation (panel()).
By using the PromptRecorder class as a context manager, you can easily record prompts and responses within a specific context and then analyze or display the recorded data in various formats.