Prompts
Learn how to configure and manage prompts in your R2R deployment
Prompt Management in R2R
R2R provides a flexible system for managing prompts, allowing you to create, update, retrieve, and delete prompts dynamically. This system is crucial for customizing the behavior of language models and ensuring consistent interactions across your application.
Default Prompts
R2R comes with a set of default prompts that are loaded from YAML files located in the py/core/providers/database/prompts
directory. These default prompts provide a starting point for various tasks within the R2R system.
For example, the default RAG (Retrieval-Augmented Generation) prompt is defined as follows:
Default Prompt Usage
You can find the full list of default prompts and their contents in the prompts directory.
Prompt Provider
R2R uses a postgres class to manage prompts. This allows for storage, retrieval, and manipulation of prompts, leveraging both a PostgreSQL database and YAML files for flexibility and persistence.
Key features of prompts inside R2R:
- Database Storage: Prompts are stored in a PostgreSQL table, allowing for efficient querying and updates.
- YAML File Support: Prompts can be loaded from YAML files, providing an easy way to version control and distribute default prompts.
- In-Memory Cache: Prompts are kept in memory for fast access during runtime.
Prompt Structure
Each prompt in R2R consists of:
- Name: A unique identifier for the prompt.
- Template: The actual text of the prompt, which may include placeholders for dynamic content.
- Input Types: A dictionary specifying the expected types for any dynamic inputs to the prompt.
Managing Prompts
R2R provides several endpoints and SDK methods for managing prompts:
Adding a Prompt
To add a new prompt:
Updating a Prompt
To update an existing prompt:
Retrieving a Prompt
To get a specific prompt:
Listing All Prompts
To retrieve all prompts:
Deleting a Prompt
To delete a prompt:
Security Considerations
Access to prompt management functions is restricted to superusers to prevent unauthorized modifications to system prompts. Ensure that only trusted administrators have superuser access to your R2R deployment.
Best Practices
- Version Control: Store your prompts in version-controlled YAML files for easy tracking of changes and rollbacks.
- Consistent Naming: Use a consistent naming convention for your prompts to make them easy to identify and manage.
- Input Validation: Always specify input types for your prompts to ensure that they receive the correct data types.
- Regular Audits: Periodically review and update your prompts to ensure they remain relevant and effective.
- Testing: Test prompts thoroughly before deploying them to production, especially if they involve complex logic or multiple input variables.
Advanced Usage
Dynamic Prompt Loading
R2R’s prompt system allows for dynamic loading of prompts from both the database and YAML files. This enables you to:
- Deploy default prompts with your application.
- Override or extend these prompts at runtime.
- Easily update prompts without redeploying your entire application.
Prompt Templating
The prompt template system in R2R supports complex string formatting. You can include conditional logic, loops, and other Python expressions within your prompts using a templating engine.
Example of a more complex prompt template:
This flexibility allows you to create highly dynamic and context-aware prompts that can adapt to various scenarios in your application.
Conclusion
R2R’s prompt management system provides a powerful and flexible way to control the behavior of language models in your application. By leveraging this system effectively, you can create more dynamic, context-aware, and maintainable AI-powered features.
For more detailed information on other aspects of R2R configuration, please refer to the following pages: