diff --git a/ui/bun.lockb b/ui/bun.lockb index c727e416..b9271d95 100644 Binary files a/ui/bun.lockb and b/ui/bun.lockb differ diff --git a/ui/package.json b/ui/package.json index 60388c0b..06aa0392 100644 --- a/ui/package.json +++ b/ui/package.json @@ -16,12 +16,14 @@ "postcss-load-config": "^5.0.2", "svelte": "^4.2.7", "tailwindcss": "^3.3.6", - "vite": "^5.0.3" + "vite": "^5.0.3", + "vite-plugin-wasm": "^3.3.0" }, "type": "module", "dependencies": { - "socket.io-client": "^4.7.5", "clsx": "^2.1.0", + "socket.io-client": "^4.7.5", + "tiktoken": "^1.0.13", "xterm": "^5.3.0", "xterm-addon-fit": "^0.8.0" } diff --git a/ui/src/lib/components/MessageInput.svelte b/ui/src/lib/components/MessageInput.svelte index a454d4fb..dd1a2b8b 100644 --- a/ui/src/lib/components/MessageInput.svelte +++ b/ui/src/lib/components/MessageInput.svelte @@ -1,6 +1,7 @@
@@ -73,7 +59,7 @@ class="w-full p-2 border-2 rounded-lg pr-20" placeholder="Type your message..." bind:value={messageInput} - on:input={calculateTokens} + on:input={setTokenSize} on:keydown={(e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); diff --git a/ui/src/lib/token.js b/ui/src/lib/token.js new file mode 100644 index 00000000..426f74cf --- /dev/null +++ b/ui/src/lib/token.js @@ -0,0 +1,13 @@ +import { Tiktoken } from "tiktoken/lite"; +import cl100k_base from "tiktoken/encoders/cl100k_base.json"; + +const encoding = new Tiktoken( + cl100k_base.bpe_ranks, + cl100k_base.special_tokens, + cl100k_base.pat_str +); + +export function calculateTokens(text) { + const tokens = encoding.encode(text); + return tokens.length; +} diff --git a/ui/vite.config.js b/ui/vite.config.js index 576a4862..8a41207c 100644 --- a/ui/vite.config.js +++ b/ui/vite.config.js @@ -1,9 +1,10 @@ -import { sveltekit } from '@sveltejs/kit/vite'; -import { defineConfig } from 'vite'; +import { sveltekit } from "@sveltejs/kit/vite"; +import { defineConfig } from "vite"; +import wasm from "vite-plugin-wasm"; export default defineConfig({ - plugins: [sveltekit()], - server: { - port: 3000, - }, + plugins: [sveltekit(), wasm()], + server: { + port: 3000, + }, });