Skip to content

Instantly share code, notes, and snippets.

@wassname
Created March 4, 2025 03:03
Show Gist options
  • Save wassname/8f6a0cefbb98a8fcc82b067a325baadd to your computer and use it in GitHub Desktop.
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
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
@wassname
Copy link
Author

wassname commented Mar 4, 2025

file: prompt.md.jinja2

---
role: system
---
{{ system_prompt }}
---
role: user
---
What is the meaning of life?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment