Prompt References
Learn how to reference other prompts in PromptL
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.
In this example:
- The parent prompt references
policies.promptl
and passes theassistant_name
variable. - The
assistant_name
variable 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 a resolveFn
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 resolveFn
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 resolveFn
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 resolveFn
Pass your resolveFn
to the render
function:
Tip: Prompts can be stored in files, a database, or any structured format. Adapt resolveFn
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
path
in the<prompt>
tag matches the actual file structure.
- Ensure
- Log Resolved Prompts:
- Add a
console.log()
inresolveFn
to verify the correct prompt is being loaded.
- Add a
- Handle Missing Prompts:
- Add error handling in
resolveFn
to handle missing or unreadable prompts gracefully:
- Add error handling in
- Inspect Variables:
- Confirm that required variables are being passed correctly.
Summary
Prompt references enable modular and maintainable prompt structures by allowing you to reuse and manage shared sections across projects. With features like variable passing, nested references, and customizable resolution logic, PromptL makes it easy to handle even the largest and most complex prompt configurations.