⚪Managing Prompts in Production
The Prompt
class provides a comprehensive solution for managing prompts, including advanced features like version control, autosaving, and logging. This guide will walk you through how to effectively use this class in a production environment, focusing on its core features, use cases, and best practices.
Table of Contents
Getting Started
Installation and Setup
Creating a New Prompt
Managing Prompt Content
Editing Prompts
Retrieving Prompt Content
Version Control
Tracking Edits and History
Rolling Back to Previous Versions
Autosaving Prompts
Enabling and Configuring Autosave
Manually Triggering Autosave
Logging and Telemetry
Handling Errors
Extending the Prompt Class
Customizing the Save Mechanism
Integrating with Databases
1. Getting Started
Installation and Setup
Before diving into how to use the Prompt
class, ensure that you have the required dependencies installed:
Creating a New Prompt
To create a new instance of a Prompt
, simply initialize it with the required attributes such as content
:
This creates a new prompt with the current timestamp and a unique identifier.
2. Managing Prompt Content
Editing Prompts
Once you have initialized a prompt, you can edit its content using the edit_prompt
method. Each time the content is edited, a new version is stored in the edit_history
, and the last_modified_at
timestamp is updated.
Note: If the new content is identical to the current content, an error will be raised to prevent unnecessary edits:
Retrieving Prompt Content
You can retrieve the current prompt content using the get_prompt
method:
This method also logs telemetry data, which includes both system information and prompt metadata.
3. Version Control
Tracking Edits and History
The Prompt
class automatically tracks every change made to the prompt. This is stored in the edit_history
attribute as a list of previous versions.
The number of edits is also tracked using the edit_count
attribute:
Rolling Back to Previous Versions
If you want to revert a prompt to a previous version, you can use the rollback
method, passing the version index you want to revert to:
The rollback operation is thread-safe, and any rollback also triggers a telemetry log.
4. Autosaving Prompts
Enabling and Configuring Autosave
To automatically save prompts to storage after every change, you can enable the autosave
feature when initializing the prompt:
This will ensure that every edit or rollback action triggers an autosave to the specified folder.
Manually Triggering Autosave
You can also manually trigger an autosave by calling the _autosave
method (which is a private method typically used internally):
Autosaves are stored as JSON files in the folder specified by autosave_folder
under the workspace directory (WORKSPACE_DIR
environment variable).
5. Logging and Telemetry
The Prompt
class integrates with the loguru
logging library to provide detailed logs for every major action, such as editing, rolling back, and saving. The log_telemetry
method captures and logs system data, including prompt metadata, for each operation.
Here's an example of a log when editing a prompt:
You can extend logging by integrating the log_telemetry
method with your own telemetry systems or databases:
6. Handling Errors
Error handling in the Prompt
class is robust and prevents common mistakes, such as editing with identical content or rolling back to an invalid version. Here's a common scenario:
Editing with Identical Content
Invalid Rollback Version
Always ensure that version numbers passed to rollback
are within the valid range of existing versions.
7. Extending the Prompt Class
Customizing the Save Mechanism
The Prompt
class currently includes a placeholder for saving and loading prompts from persistent storage. You can override the save_to_storage
and load_from_storage
methods to integrate with databases, cloud storage, or other persistent layers.
Here's how you can implement the save functionality:
Similarly, you can implement a load_from_storage
function to load the prompt from a storage location using its unique identifier (id
).
Full Example code with all methods
Last updated