Chat Template of LLM with Jinja2
A chat_template.jinja is to format messages, tools, and responses into a prompt sent to the LLM. It uses the Jinja2, a Python packaging widely used in Flask, Huggingface, etc.
Table of Contents
chat_template.jinja is a Jinja2 template. Jinja2 is a template language based on Python that allows to dynamically generate content from a template.
This file is usually served as a prompt template that transforms conversation lists, tools, contexts, etc. into a single prompt fed to LLMs.
Here, we briefly introduce the basics of Jinja2.
1. Three Kinds of Notations
The {{ xxx }} tells processor to replace this expression with the result of xxx; {% xxx %} is the execution block, like for-loop, if-conditioning in Jinja; {# comments #} are comments, which will not be evaluated.