import jinja2 from llama_index.core import PromptTemplate def get_prompt_by_jinja2_template(template_string: str, **kwargs) -> PromptTemplate: # use jinja2's template because it support complex render logic # for example: # {% for e in entities %} # {{ e.name }} # {% endfor %} template = ( jinja2.Template(template_string) .render(**kwargs) # llama-index will use f-string to format the template # so we need to escape the curly braces even if we do not use it .replace("{", "{{") .replace("}", "}}") # This is a workaround to bypass above escape, # llama-index will use f-string to format following variables, # maybe we can use regex to replace the variable name to make this more robust .replace("<>", "{query_str}") .replace("<>", "{context_str}") .replace("<>", "{existing_answer}") .replace("<>", "{context_msg}") ) return PromptTemplate(template=template)