Overview
Prompt references (Snippets) allow you to modularize your prompts by referencing other prompts within your project. This feature is particularly useful for:- Managing large projects with reusable prompt components.
- Reducing duplication by reusing common sections (e.g., policies, instructions).
- Simplifying maintenance by centralizing updates to shared prompts.
Syntax
To reference another prompt, use the<prompt path="..." /> tag. The path attribute specifies the relative or absolute path to the referenced prompt.
Basic Usage
Referenced prompts are isolated from the parent prompt by default, meaning they don’t inherit variables. However, you can pass variables explicitly using attributes in the<prompt> tag.
- The parent prompt references
policies.promptland passes theassistant_namevariable. - The
assistant_namevariable is interpolated in the referenced prompt.
Setup
Prompt references are not enabled by default. Since PropmtL does not know how your prompts are structured, you must provide areferenceFn function to define how PromptL should locate and load referenced prompts.
You can structure your prompts in any way you like, as long as your referenceFn can find and load them. Some examples include:
- Storing prompts in a file system.
- Using a database to store prompts.
- Fetching prompts from an API.
Basic referenceFn
Create a function that retrieves a prompt based on its path:
Supporting Relative Paths
To resolve paths relative to the current prompt, you can define a second argument with the current prompt’s full path:Using the referenceFn
Pass your referenceFn to the render function:
Tip: Prompts can be stored in files, a database, or any structured format. Adapt
referenceFn to fit your storage solution.Real-World Examples
Modular Prompts for Instructions
Nested References
Prompts can reference other prompts, enabling complex workflows.Best Practices
- Use Descriptive Paths:
- Organize prompts logically (e.g.,
prompts/policies.promptl).
- Organize prompts logically (e.g.,
- Centralize Shared Logic:
- Store common instructions, rules, or templates in reusable prompts.
- Pass Variables Explicitly:
- Always pass required variables to avoid missing or mismatched data.
- Avoid Circular References:
- Ensure prompts don’t reference each other in loops.
Debugging Tips
If your prompt references aren’t working as expected:- Check File Paths:
- Ensure
pathin the<prompt>tag matches the actual file structure.
- Ensure
- Log Resolved Prompts:
- Add a
console.log()inreferenceFnto verify the correct prompt is being loaded.
- Add a
- Handle Missing Prompts:
- Add error handling in
referenceFnto handle missing or unreadable prompts gracefully:
- Add error handling in
- Inspect Variables:
- Confirm that required variables are being passed correctly.