Created
March 4, 2025 03:03
-
-
Save wassname/8f6a0cefbb98a8fcc82b067a325baadd to your computer and use it in GitHub Desktop.
IMO the nicest prompt format is prompt.md.j2. Here we make the messages explicit, and the markdown and jinja syntax obvious
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split_frontmatter(fm_md_split :str): | |
"""Load prompt in md.jinja2 format | |
In this format we have multiple frontmatters and content sections, each defining a message. The idea here is to use jinja formatting in a promt.md.jinja file to make the markdown, and jinja formatting obvious | |
e.g. | |
--- | |
role: system | |
--- | |
Be good | |
--- | |
role: user | |
--- | |
What is the meaning of life? | |
usage: | |
from jinja2 import FileSystemLoader, FileSystemLoader | |
env = Environment(loader=FileSystemLoader("../src/prompts/")) | |
fm_md_split = env.get_template("isekai.md.j2") | |
split_frontmatter(fm_md_split.render(content1="Be good", sys="What is the meaning of life?")) | |
""" | |
sections = fm_md_split.split("---\n")[1:] | |
# pairs | |
messages = [] | |
for i in range(0, len(sections), 2): | |
fm = yaml.safe_load(sections[i]) | |
# since the only required role is user, make it the default | |
if fm is None: | |
fm = {'role': 'user'} | |
message = dict(content=sections[i+1], **fm) | |
messages.append(message) | |
return messages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
file: prompt.md.jinja2