-
Notifications
You must be signed in to change notification settings - Fork 800
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i.e., non Jinja statements/expressions
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]>
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
cc @OlivierDehaene (original requester of the feature)
TODOs
.strip()
)loop.last
)messages[1:]
)trim_blocks=True, lstrip_blocks=True
(needed by HuggingFaceH4/zephyr-7b-beta)