Skip to content

Commit 1427125

Browse files
authored
Update jinja dependency (#459)
* Make `@huggingface/jinja` a dependency * Update package-lock.json * Update JSDoc
1 parent 61cb4f5 commit 1427125

File tree

4 files changed

+11
-25
lines changed

4 files changed

+11
-25
lines changed

package-lock.json

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@
3939
"homepage": "https://github.com/xenova/transformers.js#readme",
4040
"dependencies": {
4141
"onnxruntime-web": "1.14.0",
42-
"sharp": "^0.32.0"
42+
"sharp": "^0.32.0",
43+
"@huggingface/jinja": "^0.1.0"
4344
},
4445
"optionalDependencies": {
4546
"onnxruntime-node": "1.14.0"
4647
},
47-
"peerDependencies": {
48-
"@huggingface/jinja": "^0.1.0"
49-
},
5048
"devDependencies": {
5149
"@types/jest": "^29.5.1",
5250
"catharsis": "github:xenova/catharsis",

src/tokenizers.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import {
4141
CharTrie,
4242
} from './utils/data-structures.js';
4343

44+
import { Template } from '@huggingface/jinja';
45+
4446

4547
/**
4648
* @typedef {Object} TokenizerProperties Additional tokenizer-specific properties.
@@ -2785,10 +2787,10 @@ export class PreTrainedTokenizer extends Callable {
27852787
* { "role": "user", "content": "I'd like to show off how chat templating works!" },
27862788
* ]
27872789
*
2788-
* const text = await tokenizer.apply_chat_template(chat, { tokenize: false });
2790+
* const text = tokenizer.apply_chat_template(chat, { tokenize: false });
27892791
* // "<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]"
27902792
*
2791-
* const input_ids = await tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
2793+
* const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
27922794
* // [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]
27932795
* ```
27942796
*
@@ -2806,9 +2808,9 @@ export class PreTrainedTokenizer extends Callable {
28062808
* @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false.
28072809
* If not specified, the tokenizer's `max_length` attribute will be used as a default.
28082810
* @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false.
2809-
* @returns {Promise<string | Tensor | number[]| number[][]>} A promise that resolves to the tokenized output.
2811+
* @returns {string | Tensor | number[]| number[][]} The tokenized output.
28102812
*/
2811-
async apply_chat_template(conversation, {
2813+
apply_chat_template(conversation, {
28122814
chat_template = null,
28132815
add_generation_prompt = false,
28142816
tokenize = true,
@@ -2823,17 +2825,6 @@ export class PreTrainedTokenizer extends Callable {
28232825
// Compilation function uses a cache to avoid recompiling the same template
28242826
let compiledTemplate = this._compiled_template_cache.get(chat_template);
28252827
if (compiledTemplate === undefined) {
2826-
// Dynamically load the `@huggingface/jinja` library. Since this is a peer dependency
2827-
// (i.e., must be installed separately), an error is thrown if it is not installed.
2828-
let Template;
2829-
try {
2830-
Template = (await import( /* webpackMode: "eager" */ '@huggingface/jinja')).Template;
2831-
} catch (e) {
2832-
throw new Error(
2833-
`apply_chat_template requires '@huggingface/jinja' to be installed. ` +
2834-
`You can install it with \`npm install @huggingface/jinja\`.`
2835-
)
2836-
}
28372828
compiledTemplate = new Template(chat_template);
28382829
this._compiled_template_cache.set(chat_template, compiledTemplate);
28392830
}

tests/tokenizers.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ describe('Chat templates', () => {
189189
{ "role": "user", "content": "I'd like to show off how chat templating works!" },
190190
]
191191

192-
const text = await tokenizer.apply_chat_template(chat, { tokenize: false });
192+
const text = tokenizer.apply_chat_template(chat, { tokenize: false });
193193

194194
expect(text).toEqual("<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]");
195195

196-
const input_ids = await tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
196+
const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
197197
compare(input_ids, [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])
198198
});
199199

0 commit comments

Comments
 (0)