-
Notifications
You must be signed in to change notification settings - Fork 302
[JS] Update tokenizer methods #3012
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
884f0ec
fd3bbd7
d886ad1
78f4171
f3e48bf
9b6daa3
03e6a6e
d85604b
3e22f65
3b7b57d
4815ef8
bb60892
77cf065
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Copyright (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Tensor } from "openvino-node"; | ||
| import { ChatHistory } from "./addon.js"; | ||
|
|
||
| /** | ||
| * TokenizedInputs contains input_ids and attention_mask tensors. | ||
| * This is the result of encoding prompts using the Tokenizer. | ||
| */ | ||
| export interface TokenizedInputs { | ||
Retribution98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** Tensor containing token IDs for the encoded input */ | ||
| input_ids: Tensor; | ||
| /** Tensor containing attention mask (1 for real tokens, 0 for padding) */ | ||
| attention_mask: Tensor; | ||
| } | ||
|
|
||
| /** | ||
| * Options for encode method. | ||
| */ | ||
| export interface EncodeOptions { | ||
| /** | ||
| * Whether to add special tokens like BOS, EOS, PAD. | ||
| * @defaultValue true | ||
| */ | ||
| addSpecialTokens?: boolean; | ||
|
|
||
| /** | ||
| * Whether to pad the sequence to the maximum length. | ||
| * @defaultValue false | ||
| */ | ||
| padToMaxLength?: boolean; | ||
|
|
||
| /** | ||
| * Maximum length of the sequence. | ||
| * If undefined, the value will be taken from the IR. | ||
| */ | ||
| maxLength?: number; | ||
|
|
||
| /** | ||
| * Side to pad the sequence, can be 'left' or 'right'. | ||
| * If undefined, the value will be taken from the IR. | ||
| */ | ||
| paddingSide?: "left" | "right"; | ||
apaniukov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * The Tokenizer class is used to encode prompts and decode resulting tokens. | ||
| * | ||
| * Chat template is initialized from sources in the following order, overriding the previous value: | ||
| * 1. chat_template entry from tokenizer_config.json | ||
| * 2. chat_template entry from processor_config.json | ||
| * 3. chat_template entry from chat_template.json | ||
| * 4. chat_template entry from rt_info section of openvino.Model | ||
| * 5. If the template is known to be not supported by GenAI, it's replaced with a simplified supported version. | ||
| */ | ||
| export interface Tokenizer { | ||
| /** | ||
| * Applies a chat template to format chat history into a prompt string. | ||
| * @param chatHistory - chat history as an array of message objects or ChatHistory instance | ||
| * @param addGenerationPrompt - whether to add a generation prompt at the end | ||
| * @param chatTemplate - optional custom chat template to use instead of the default | ||
| * @param tools - optional array of tool definitions for function calling | ||
| * @param extraContext - optional extra context object for custom template variables | ||
| * @returns formatted prompt string | ||
| */ | ||
| applyChatTemplate( | ||
| chatHistory: Record<string, any>[] | ChatHistory, | ||
| addGenerationPrompt: boolean, | ||
| chatTemplate?: string, | ||
| tools?: Record<string, any>[], | ||
| extraContext?: Record<string, any>, | ||
| ): string; | ||
|
Comment on lines
108
to
114
|
||
|
|
||
| /** | ||
| * Encodes a single prompt or a list of prompts into tokenized inputs. | ||
| * @param prompts - single prompt string or array of prompts | ||
| * @param options - encoding options | ||
| * @returns TokenizedInputs object containing input_ids and attention_mask tensors. | ||
| */ | ||
| encode(prompts: string | string[], options?: EncodeOptions): TokenizedInputs; | ||
|
|
||
| /** | ||
| * Encodes two lists of prompts into tokenized inputs (for paired input). | ||
| * The number of strings must be the same, or one of the inputs can contain one string. | ||
| * In the latter case, the single-string input will be broadcast into the shape of the other input, | ||
| * which is more efficient than repeating the string in pairs. | ||
| * @param prompts1 - first list of prompts to encode | ||
| * @param prompts2 - second list of prompts to encode | ||
| * @param options - encoding options | ||
| * @returns TokenizedInputs object containing input_ids and attention_mask tensors. | ||
| */ | ||
| encode(prompts1: string[], prompts2: string[], options?: EncodeOptions): TokenizedInputs; | ||
|
|
||
| /** | ||
| * Encodes a list of paired prompts into tokenized inputs. | ||
| * Input format is same as for HF paired input [[prompt_1, prompt_2], ...]. | ||
| * @param prompts - list of paired prompts to encode | ||
| * @param options - encoding options | ||
| * @returns TokenizedInputs object containing input_ids and attention_mask tensors. | ||
| */ | ||
| encode(prompts: [string, string][], options?: EncodeOptions): TokenizedInputs; | ||
|
|
||
| // TODO: move decode options to another interface | ||
Retribution98 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * Decode a sequence of token IDs into a string prompt. | ||
| * @param tokens - sequence of token IDs to decode | ||
| * @param skipSpecialTokens - whether to skip special tokens. Default is true. | ||
| * @returns decoded string. | ||
| */ | ||
| decode(tokens: number[], skipSpecialTokens?: boolean): string; | ||
|
|
||
| /** | ||
| * Decode a batch of token sequences (as Tensor or array of arrays) into a list of string prompts. | ||
| * @param tokens - tensor containing token IDs or batch of token ID sequences | ||
| * @param skipSpecialTokens - whether to skip special tokens. Default is true. | ||
| * @returns list of decoded strings. | ||
| */ | ||
| decode(tokens: Tensor | number[][], skipSpecialTokens?: boolean): string[]; | ||
|
|
||
| /** | ||
| * Returns the BOS (Beginning of Sequence) token string. | ||
| * @returns BOS token string | ||
| */ | ||
| getBosToken(): string; | ||
|
|
||
| /** | ||
| * Returns the BOS (Beginning of Sequence) token ID. | ||
| * @returns BOS token ID | ||
| */ | ||
| getBosTokenId(): number; | ||
|
|
||
| /** | ||
| * Returns the EOS (End of Sequence) token string. | ||
| * @returns EOS token string | ||
| */ | ||
| getEosToken(): string; | ||
|
|
||
| /** | ||
| * Returns the EOS (End of Sequence) token ID. | ||
| * @returns EOS token ID | ||
| */ | ||
| getEosTokenId(): number; | ||
|
|
||
| /** | ||
| * Returns the PAD (Padding) token string. | ||
| * @returns PAD token string | ||
| */ | ||
| getPadToken(): string; | ||
|
|
||
| /** | ||
| * Returns the PAD (Padding) token ID. | ||
| * @returns PAD token ID | ||
| */ | ||
| getPadTokenId(): number; | ||
|
|
||
| /** | ||
| * Returns the current chat template string. | ||
| * @returns current chat template string | ||
| */ | ||
| getChatTemplate(): string; | ||
|
|
||
| /** | ||
| * Returns the original chat template from the tokenizer configuration. | ||
| * @returns original chat template string | ||
| */ | ||
| getOriginalChatTemplate(): string; | ||
|
|
||
| /** | ||
| * Override a chat template read from tokenizer_config.json. | ||
| * @param chatTemplate - custom chat template string to use | ||
| */ | ||
| setChatTemplate(chatTemplate: string): void; | ||
|
|
||
| /** | ||
| * Returns true if the tokenizer supports paired input, false otherwise. | ||
| * @returns whether the tokenizer supports paired input | ||
| */ | ||
| supportsPairedInput(): boolean; | ||
|
|
||
| /** | ||
| * The current chat template string. | ||
| * Can be used to get or set the chat template. | ||
| */ | ||
| chatTemplate: string; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,28 @@ void init_class(Napi::Env env, | |||||||
| exports.Set(class_name, prototype); | ||||||||
| } | ||||||||
|
|
||||||||
| Napi::Value init_ov_addon(const Napi::CallbackInfo& info) { | ||||||||
|
||||||||
| Napi::Value init_ov_addon(const Napi::CallbackInfo& info) { | |
| void init_ov_addon(const Napi::CallbackInfo& info) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is used as a JavaScript function, so we usually return a Napi::Value even if nothing is returned. I understand your confusion because it is called like init_class, but they have different usages. To be clearer, I will rename this function to set_ov_node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there are functions that return Napi::Value even if they are void, but I do not think we should repeat that pattern. Here it pop_back is used as js function and it's void
| void ChatHistoryWrap::pop_back(const Napi::CallbackInfo& info) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, it has been updated and let's use this approach in the next changes.
Uh oh!
There was an error while loading. Please reload this page.