Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Count sysmte prompt tokens * show error only once * fix * fix css * simplify * fix * simplify * Revert "simplify" This reverts commit 61c0a22. * `model.tokenizer` config & fix reactivity issues * rm gated tokenizer * use `truncate` --------- Co-authored-by: Nathan Sarrazin <[email protected]>
- Loading branch information
Showing
8 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<script lang="ts"> | ||
import type { Model } from "$lib/types/Model"; | ||
import { AutoTokenizer, PreTrainedTokenizer } from "@xenova/transformers"; | ||
export let classNames = ""; | ||
export let prompt = ""; | ||
export let modelTokenizer: Exclude<Model["tokenizer"], undefined>; | ||
export let truncate: number | undefined = undefined; | ||
let tokenizer: PreTrainedTokenizer | undefined = undefined; | ||
async function getTokenizer(_modelTokenizer: Exclude<Model["tokenizer"], undefined>) { | ||
if (typeof _modelTokenizer === "string") { | ||
// return auto tokenizer | ||
return await AutoTokenizer.from_pretrained(_modelTokenizer); | ||
} | ||
{ | ||
// construct & return pretrained tokenizer | ||
const { tokenizerUrl, tokenizerConfigUrl } = _modelTokenizer satisfies { | ||
tokenizerUrl: string; | ||
tokenizerConfigUrl: string; | ||
}; | ||
const tokenizerJSON = await (await fetch(tokenizerUrl)).json(); | ||
const tokenizerConfig = await (await fetch(tokenizerConfigUrl)).json(); | ||
return new PreTrainedTokenizer(tokenizerJSON, tokenizerConfig); | ||
} | ||
} | ||
async function tokenizeText(_prompt: string) { | ||
if (!tokenizer) { | ||
return; | ||
} | ||
const { input_ids } = await tokenizer(_prompt); | ||
return input_ids.size; | ||
} | ||
$: (async () => { | ||
tokenizer = await getTokenizer(modelTokenizer); | ||
})(); | ||
</script> | ||
|
||
{#if tokenizer} | ||
{#await tokenizeText(prompt) then nTokens} | ||
<p class="text-sm opacity-60 hover:opacity-80 {classNames}"> | ||
{nTokens}{truncate ? `/${truncate}` : ""} | ||
</p> | ||
{/await} | ||
{/if} |
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
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
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
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
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