Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jinja for chat templates #730

Closed
nsarrazin opened this issue Jan 24, 2024 · 4 comments
Closed

Use jinja for chat templates #730

nsarrazin opened this issue Jan 24, 2024 · 4 comments
Assignees
Labels
back This issue is related to the Svelte backend or the DB enhancement New feature or request models This issue is related to model performance/reliability

Comments

@nsarrazin
Copy link
Collaborator

We can use @huggingface/jinja for parsing chat templates and fetch chat templates from the hub directly. This would allow for a single source of truth for model templates.

@nsarrazin nsarrazin added enhancement New feature or request back This issue is related to the Svelte backend or the DB models This issue is related to model performance/reliability labels Jan 24, 2024
@julien-c
Copy link
Member

(cc @xenova for viz)

@xenova
Copy link
Contributor

xenova commented Jan 24, 2024

And luckily, we already have @xenova/transformers as a dependency (which adds tokenizer-specific functionality to @huggingface/jinja), so we can upgrade to >=2.14, and then use it as follows:

import { AutoTokenizer } from "@xenova/transformers";

// Load tokenizer from the Hugging Face Hub
const tokenizer = await AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1");

// Define chat messages
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]

@mishig25
Copy link
Collaborator

mishig25 commented Apr 3, 2024

Thanks to #850, there is a new field in model info called tokenizer which can be used for calling

const tokenizer = await AutoTokenizer.from_pretrained(model.tokenizer);

which is already the case here

async function getTokenizer(_modelTokenizer: Exclude<Model["tokenizer"], undefined>) {
if (typeof _modelTokenizer === "string") {
// return auto tokenizer
return await AutoTokenizer.from_pretrained(_modelTokenizer);

nsarrazin added a commit that referenced this issue Apr 4, 2024
* Use jinja template for chat formatting

* Add support for transformers js chat template

* update to latest transformers version

* Make sure to `add_generation_prompt`

* unindent
@nsarrazin
Copy link
Collaborator Author

Closing this as the feature is now released 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
back This issue is related to the Svelte backend or the DB enhancement New feature or request models This issue is related to model performance/reliability
Projects
None yet
Development

No branches or pull requests

4 participants