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