Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Here's how OpenAI Token count is computed in Tiktokenizer - Part 3

In this article, we will review how OpenAI Token count is computed in Tiktokenizer — Part 3. We will look at: OpenSourceTokenizer class For more context, read part 2. OpenSourceTokenizer class In tikto…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In this article, we will review how OpenAI Token count is computed in Tiktokenizer — Part 3. We will look at:




  1. OpenSourceTokenizer class






For more context, read part 2.







OpenSourceTokenizer class



In tiktokenizer/src/models/tokenizer.ts, at line 82, you will find the following code:




export class OpenSourceTokenizer implements Tokenizer {
constructor(private tokenizer: PreTrainedTokenizer, name?: string) {
this.name = name ?? tokenizer.name;
}

name: string;

static async load(
model: z.infer<typeof openSourceModels>
): Promise<PreTrainedTokenizer> {
// use current host as proxy if we're running on the client
if (typeof window !== "undefined") {
env.remoteHost = window.location.origin;
}
env.remotePathTemplate = "/hf/{model}";
// Set to false for testing!
// env.useBrowserCache = false;
const t = await PreTrainedTokenizer.from_pretrained(model, {
progress_callback: (progress: any) =>
console.log(`loading "${model}"`, progress),
});
console.log("loaded tokenizer", model, t.name);
return t;
}






This class also implements tokenizer. Tokenizer is an interface defined in the same file




export interface Tokenizer {
name: string;
tokenize(text: string): TokenizerResult;
free?(): void;
}









constructor



constructor had the following code



 



 constructor(private tokenizer: PreTrainedTokenizer, name?: string) {
this.name = name ?? tokenizer.name;
}





This constructor only sets this.name.



The type of tokenizer is PreTrainedTokenizer, it is imported as shown below:




import { PreTrainedTokenizer, env } from "@xenova/transformers";









static load



This OpenSourceTokenizer class has a static method named load and it contains the following code.





static async load(
model: z.infer<typeof openSourceModels>
): Promise<PreTrainedTokenizer> {
// use current host as proxy if we're running on the client
if (typeof window !== "undefined") {
env.remoteHost = window.location.origin;
}
env.remotePathTemplate = "/hf/{model}";
// Set to false for testing!
// env.useBrowserCache = false;
const t = await PreTrainedTokenizer.from_pretrained(model, {
progress_callback: (progress: any) =>
console.log(`loading "${model}"`, progress),
});
console.log("loaded tokenizer", model, t.name);
return t;
}






This function returns a variable name t and this t is assigned a value returned by the PreTrainedTokenizer.from_pretrained as shown below



 




const t = await PreTrainedTokenizer.from_pretrained(model, {
progress_callback: (progress: any) =>
console.log(`loading "${model}"`, progress),
});









tokenize



tokenize has the following code.



 




tokenize(text: string): TokenizerResult {
// const tokens = this.tokenizer(text);
const tokens = this.tokenizer.encode(text);
const removeFirstToken = (
hackModelsRemoveFirstToken.options as string[]
).includes(this.name);
return {
name: this.name,
tokens,
segments: getHuggingfaceSegments(this.tokenizer, text, removeFirstToken),
count: tokens.length,
};
}






It returns the object that contains name, tokens, segments and count which is same as the object returned by the TiktokenTokenizer at line 26.






About me:



Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.



Email: [email protected]




Want to learn from open-source projects? Solve challenges inspired by open-source projects.







References:




  1. https://github.com/dqbd/tiktokenizer/blob/master/src/models/tokenizer.ts#L82


  2. https://github.com/dqbd/tiktokenizer/blob/master/src/models/tokenizer.ts#L26


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Here's how OpenAI Token count is computed in Tiktokenizer - Part 3

Thematisch verwandte Begriffe: Heres, OpenAI, Token, count · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-17636 | IBM Financial Transaction Manager (FTM) for RedHat OpenShift could allow…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick