Skip to main content

Crate tpt_jinja_chat

Crate tpt_jinja_chat 

Source
Expand description

§tpt-jinja-chat

A lightweight, dependency-free parser for the subset of Jinja2 used by LLM tokenizer_config.json chat templates (e.g. Llama 3, Mistral).

It supports:

  • {{ expression }} output statements
  • {% set name = expr %} variable binding, including {% set ns.attr = .. %}
  • {% for item in iterable %} … {% endfor %}, including tuple unpacking ({% for k, v in d.items() %})
  • {% if %} / {% elif %} / {% else %} / {% endif %}
  • expressions: variables, member access (.), indexing ([..]), string, number, boolean, none and list literals, + / - / * / /, ~ (string concat), the comparison operators, in / not in, and and / or / not
  • filters via |: tojson, trim, default, join, upper, lower, length, first, last, selectattr, rejectattr, map, select, reject, string, replace, int, float, and more
  • is tests: defined, undefined, none, string, number, mapping, iterable, equalto, in, …
  • function/method calls: raise_exception(...), namespace(...), range(...), dict.items(), str.startswith(...), …

§Example

use tpt_jinja_chat::{ChatTemplate, Context, Value};

let tmpl = ChatTemplate::parse(
    "{% for m in messages %}{{ m.role }}: {{ m.content }}\n{% endfor %}",
).unwrap();

let mut ctx = Context::new();
ctx.insert("messages", Value::Array(vec![
    Value::object(vec![
        ("role".into(), Value::String("user".into())),
        ("content".into(), Value::String("Hello!".into())),
    ]),
]));

let rendered = tmpl.render(&ctx).unwrap();
assert_eq!(rendered, "user: Hello!\n");

Re-exports§

pub use error::TemplateError;
pub use value::Context;
pub use value::Value;

Modules§

error
Error types for parsing and rendering Jinja chat templates.
value
Dynamic values used inside chat-template contexts.

Structs§

ChatTemplate
A parsed chat template ready to be rendered against a Context.