A package for loading promptdown files, which are a special type of markdown file for defining structured LLM prompts
Project description
Promptdown
Promptdown is a Python package that allows you to express structured prompts for language models in a markdown format. It provides a simple and intuitive way to define and manage prompts, making it easier to work with language models in your projects.
Installation
Using PDM
Promptdown can be installed using PDM:
pdm add promptdown
Using pip
Alternatively, you can install Promptdown using pip:
pip install promptdown
Usage
Basic Usage
To use Promptdown, simply create a Promptdown file (.prompt.md
) with the following format:
# My Prompt
## System Message
You are a helpful assistant.
## Conversation
| Role | Content |
|-----------|---------------------------------------------|
| User | Hi, can you help me? |
| Assistant | Of course! What do you need assistance with?|
| User | I'm having trouble with my code. |
| Assistant | I'd be happy to help. What seems to be the problem? |
Then, you can parse this file into a StructuredPrompt
object using Promptdown:
from promptdown import StructuredPrompt
structured_prompt = StructuredPrompt.from_promptdown_file('path/to/your_prompt_file.prompt.md')
print(structured_prompt)
Please note that the Conversation
section can be omitted, but the System Message
section is always required.
Parsing a Prompt from a String
For scenarios where you have the prompt data as a string (perhaps dynamically generated or retrieved from an external source), you can parse it directly:
from promptdown import StructuredPrompt
promptdown_string = """
# My Prompt
## System Message
You are a helpful assistant.
## Conversation
| Role | Content |
|-----------|---------------------------------------------|
| User | Hi, can you help me? |
| Assistant | Of course! What do you need assistance with?|
| User | I'm having trouble with my code. |
| Assistant | I'd be happy to help. What seems to be the problem? |
"""
structured_prompt = StructuredPrompt.from_promptdown_string(promptdown_string)
print(structured_prompt)
Loading Prompts from Package Resources
For applications where prompts are bundled within Python packages, Promptdown can load prompts directly from these resources. This approach is useful for distributing prompts alongside Python libraries or applications:
from promptdown import StructuredPrompt
structured_prompt = StructuredPrompt.from_package_resource('your_package', 'your_prompt_file.prompt.md')
print(structured_prompt)
This method facilitates easy management of prompts within a package, ensuring that they can be versioned, shared, and reused effectively.
Using Template Strings
Promptdown supports the use of template strings within your prompts, allowing for dynamic customization of both system messages and conversation content. This feature is particularly useful when you need to tailor prompts based on specific contexts or user data.
Defining Template Strings
To incorporate template strings in your Promptdown files, use double curly braces {{variable}}
around placeholders that you intend to replace dynamically. Here is an example of how to use template strings in a prompt:
# My Prompt
## System Message
You are a helpful assistant in {{topic}}.
## Conversation
| Role | Content |
|-----------|-------------------------------------------------|
| User | Hi, can you help me with {{topic}}? |
| Assistant | Of course! What specifically do you need help with in {{topic}}?|
| User | I'm having trouble understanding {{concept}}. |
| Assistant | No problem! Let's dive into {{concept}} together. |
Applying Template Values
Once you have defined a prompt with placeholders, you can replace these placeholders by passing a dictionary of template values to the apply_template_values
method. Here's how you can apply template values to your prompt:
from promptdown import StructuredPrompt
# Load your structured prompt from a file or string that contains template placeholders
structured_prompt = StructuredPrompt.from_promptdown_string(promptdown_string)
# Define the template values to apply
template_values = {
"topic": "Python programming",
"concept": "decorators"
}
# Apply the template values
structured_prompt.apply_template_values(template_values)
# Output the updated prompt
print(structured_prompt)
This will replace {{topic}}
with "Python programming" and {{concept}}
with "decorators" in the system message and conversation content. Using template strings in Promptdown allows for more flexible and context-sensitive interactions with language models.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
License
Promptdown is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file promptdown-0.5.0.tar.gz
.
File metadata
- Download URL: promptdown-0.5.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d18c1406c4a823cc80aa8dd6f0bad7d453bd41999cc1c493371f7693c0658a51 |
|
MD5 | dab33fa6182a37862d0f2fe7c6235cbc |
|
BLAKE2b-256 | 62b9a60fb63bc3726f6b1a19ca841385cfc0ac839faa0232e80e62c3400136d5 |
File details
Details for the file promptdown-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: promptdown-0.5.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da4eb1897d56739aa0966a2cccf55b7c9f90730f10f9335f81d3414ae8fe95a7 |
|
MD5 | f0a828070c0a0043bc53c36b3dc523af |
|
BLAKE2b-256 | 74e2e0c0913b04b18f8f306abb530a10ed3bf5c33fb072acdfc0ca0cbdb0f3a6 |