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

Add support for chat templates #408

Merged
merged 60 commits into from
Dec 18, 2023
Merged

Add support for chat templates #408

merged 60 commits into from
Dec 18, 2023

Conversation

xenova
Copy link
Collaborator

@xenova xenova commented Nov 21, 2023

This PR adds support for chat templates, making it possible to generate well-formatted prompts in a generic fashion. This will also be useful to users of other libraries, like TGI, as this means they can generating prompts on the client-side (link to some discussion).

Since the python library's implementation uses Jinja, it required a minimalistic JavaScript reimplementation of the templating engine. Special thanks to Tyler Laceby for his amazing "Guide to Interpreters" tutorial series, which provided the basis for this implementation.

Example: Applying a chat template to a chat history

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 = await 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 = await 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]

cc @OlivierDehaene (original requester of the feature)

TODOs

  • Add support for built-in string functions (e.g., .strip())
  • Add support for logical operators
  • Add all loop variables (like loop.last)
  • Add full support for nested for-loops (currently, the loop index is reused)
  • Auto-generate unit tests
  • Add support for array slicing (e.g., messages[1:])
  • Do trim_blocks=True, lstrip_blocks=True (needed by HuggingFaceH4/zephyr-7b-beta)
  • Add test cases for invalid templates

xenova added a commit to huggingface/huggingface.js that referenced this pull request Dec 14, 2023
…lates (#352)

This PR introduces the `@huggingface/jinja` library, which is a
minimalistic JavaScript implementation of the Jinja templating engine,
specifically designed for parsing ML chat templates.

Although it was [originally
created](huggingface/transformers.js#408) for (and
integrated into) transformers.js, it became clear that others can use
this functionality too, without the overhead of the transformers.js
library.


**Example usage:** Loading a `tokenizer_config.json` from the HF hub and
render a list of messages

```js
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]"
```

---------

Co-authored-by: coyotte508 <[email protected]>
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@xenova xenova merged commit d4f7cd5 into main Dec 18, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants