Skip to content
jinja / 0.0.3

jinja 0.0.3

Install from the command line:
Learn more about npm packages
$ npm install @huggingface/jinja@0.0.3
Install via package.json:
"@huggingface/jinja": "0.0.3"

About this version

Jinja

A minimalistic JavaScript implementation of the Jinja templating engine, specifically designed for parsing and rendering ML chat templates.

Usage

Load template from a model on the Hugging Face Hub

First, install the templates and hub packages:

npm i @huggingface/templates
npm i @huggingface/hub

You can then load a tokenizer from the Hugging Face Hub and render a list of chat messages, as follows:

import { Template } from "@huggingface/templates";
import { downloadFile } from "@huggingface/hub";

const config = await (
	await downloadFile({
		repo: "mistralai/Mistral-7B-Instruct-v0.1",
		path: "tokenizer_config.json",
	})
).json();

const chat = [
	{ role: "user", content: "Hello, how are you?" },
	{ role: "assistant", content: "I'm doing great. How can I help you today?" },
	{ role: "user", content: "I'd like to show off how chat templating works!" },
];

const template = new Template(config.chat_template);
const result = template.render({
	messages: chat,
	bos_token: config.bos_token,
	eos_token: config.eos_token,
});
// "<s>[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today?</s> [INST] I'd like to show off how chat templating works! [/INST]"

Transformers.js (coming soon)

First, install the @huggingface/templates and @xenova/transformers packages:

npm i @huggingface/templates
npm i @xenova/transformers
import { AutoTokenizer } from "@xenova/transformers";

const tokenizer = await AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1");

const chat = [
	{ role: "user", content: "Hello, how are you?" },
	{ role: "assistant", content: "I'm doing great. How can I help you today?" },
	{ role: "user", content: "I'd like to show off how chat templating works!" },
];

const text = tokenizer.apply_chat_template(chat, { tokenize: false });
// "<s>[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today?</s> [INST] I'd like to show off how chat templating works! [/INST]"

const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
// [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793]

Details


Assets

  • jinja-0.0.3.tgz

Download activity

  • Total downloads 0
  • Last 30 days 0
  • Last week 0
  • Today 0