webllm : chat interface
This commit is contained in:
parent
724bcd8572
commit
d74a2cfb4f
14 changed files with 1034 additions and 0 deletions
24
sandbox-webllm-bun/sandbox-webllm-bun/.gitignore
vendored
Normal file
24
sandbox-webllm-bun/sandbox-webllm-bun/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
100
sandbox-webllm-bun/sandbox-webllm-bun/bun.lock
Normal file
100
sandbox-webllm-bun/sandbox-webllm-bun/bun.lock
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
{
|
||||
"lockfileVersion": 1,
|
||||
"configVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "sandbox-webllm-bun",
|
||||
"dependencies": {
|
||||
"@mlc-ai/web-llm": "^0.2.85",
|
||||
},
|
||||
"devDependencies": {
|
||||
"vite": "^8.3.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@mlc-ai/web-llm": ["@mlc-ai/web-llm@0.2.85", "", { "dependencies": { "loglevel": "^1.9.1" } }, "sha512-CwUiSAvDCYnmAZlpPMkurPHDfBLq6fvdRB3vRgoA894hRG/V/BSeY9bmMm6iIIU5VpjJxfb/dLPpJX2at0L9rw=="],
|
||||
|
||||
"@oxc-project/types": ["@oxc-project/types@0.150.0", "", {}, "sha512-rDS5/31E9HfPl/CIzGrn0DOlvBbXFseQ5URJ9sYMfstbKLD/c6Gm9vmRzRGDdAXyOIL4zmO37lc9RIwYqVruZw=="],
|
||||
|
||||
"@rolldown/binding-android-arm-eabi": ["@rolldown/binding-android-arm-eabi@1.2.9", "", { "os": "android", "cpu": "arm" }, "sha512-tNISae1QEf/vkb3xkRcjV5SEdzPE97We5IVaa2Z8jSszQPZ8U60B/YCYpw4QI7VidYsBtKavczXf+DyDs9WGxw=="],
|
||||
|
||||
"@rolldown/binding-android-arm64": ["@rolldown/binding-android-arm64@1.2.9", "", { "os": "android", "cpu": "arm64" }, "sha512-YC8YsI30o606GTZi0VyzYlsDKFP8W61i/QzayHDkLbNEz/IShqAmTa+hsJRj13xTHA0H+6fk4b2UmGn+Q/cMlg=="],
|
||||
|
||||
"@rolldown/binding-darwin-arm64": ["@rolldown/binding-darwin-arm64@1.2.9", "", { "os": "darwin", "cpu": "arm64" }, "sha512-IwhlH3qK5urrY8hZiEgGkHKEFN901p/p2bjxCxJlr4GyNnF7wYpUvK+Y43uaRYuC4hpfjzbR3SJC3arX1jGvmw=="],
|
||||
|
||||
"@rolldown/binding-darwin-x64": ["@rolldown/binding-darwin-x64@1.2.9", "", { "os": "darwin", "cpu": "x64" }, "sha512-XxpJfVzFh+jilRxIXUqcfYAYcunIc/XEzIizsOL1fcJee5Sf7H3mH8WlLmfHfluz5amqR88QQo9izKtmMlavAw=="],
|
||||
|
||||
"@rolldown/binding-freebsd-x64": ["@rolldown/binding-freebsd-x64@1.2.9", "", { "os": "freebsd", "cpu": "x64" }, "sha512-kSfvhmgeWyfkbT3p/1s5vSgboogoah2zkm9fX2zjg2hHxSV7T4KhMWRUUaRk4OXNqoD3QAUeRqLcs1aZOK4U1g=="],
|
||||
|
||||
"@rolldown/binding-linux-arm-gnueabihf": ["@rolldown/binding-linux-arm-gnueabihf@1.2.9", "", { "os": "linux", "cpu": "arm" }, "sha512-1RVzG17pxqbTfYLC352JlLt6kKLG+6Hr30n8DlIJqsnV5luUDd2Qdx9Ayw1Cabfyb1K9k0jXEZ7evxkRoT+uiw=="],
|
||||
|
||||
"@rolldown/binding-linux-arm64-gnu": ["@rolldown/binding-linux-arm64-gnu@1.2.9", "", { "os": "linux", "cpu": "arm64" }, "sha512-BXqPvZ2drqVD+/Z8UpKwcs4Mp7grM+eGFku4CAEKrEtcbAsUpzREphK1sogCRZGreVPiMkiiBtw0n3TPteuqvw=="],
|
||||
|
||||
"@rolldown/binding-linux-arm64-musl": ["@rolldown/binding-linux-arm64-musl@1.2.9", "", { "os": "linux", "cpu": "arm64" }, "sha512-11vWvo8YDwLzukt27J3aYDWU+gg2P7J+ZOmiJ0hkF5BXZDW7pVya7r40MXDy6ya0i9KamoENSVKIugvJNgFXIA=="],
|
||||
|
||||
"@rolldown/binding-linux-ppc64-gnu": ["@rolldown/binding-linux-ppc64-gnu@1.2.9", "", { "os": "linux", "cpu": "ppc64" }, "sha512-a1tijMkdwsIARtc0F39ApURROkf3NwqinI6TOiSSWCTR7dT96dffNvMUtDHnq64wKNTIZOIlzKrFvvFUznJiyw=="],
|
||||
|
||||
"@rolldown/binding-linux-s390x-gnu": ["@rolldown/binding-linux-s390x-gnu@1.2.9", "", { "os": "linux", "cpu": "s390x" }, "sha512-x6SQNdAvv4c3hWqTMaWuawzMX9myaCs/yEmlGsxJzkdClnHW7FbrjQuSiRDhuSYzEYoEMhsaJy9qHG/XNemJPQ=="],
|
||||
|
||||
"@rolldown/binding-linux-x64-gnu": ["@rolldown/binding-linux-x64-gnu@1.2.9", "", { "os": "linux", "cpu": "x64" }, "sha512-9s0AZ8BFK5/n7B/TBoa2yJE3gI3KURrbXcPBlsAsvjU4VeJKgE90y1YtNxyEUIcHPQkg6/yfF3qihUrcM/Kf0Q=="],
|
||||
|
||||
"@rolldown/binding-linux-x64-musl": ["@rolldown/binding-linux-x64-musl@1.2.9", "", { "os": "linux", "cpu": "x64" }, "sha512-P7VWAmV+WdJluH7ovnRGoiv2i8To7GAZ+kGzfGup635cyL7SyYl3lSUaA3Gp5THf0n/Co5EyEqb2zbqq+nMOHQ=="],
|
||||
|
||||
"@rolldown/binding-openharmony-arm64": ["@rolldown/binding-openharmony-arm64@1.2.9", "", { "os": "none", "cpu": "arm64" }, "sha512-1qixtsE4BK8h+yS3BfmZ09UhA7O/N4IACva6YBr7EBvCJraByTuRcgOTaiA62Tm0vey3UcKXLOaoGHtYmNGEVg=="],
|
||||
|
||||
"@rolldown/binding-win32-arm64-msvc": ["@rolldown/binding-win32-arm64-msvc@1.2.9", "", { "os": "win32", "cpu": "arm64" }, "sha512-ok8IQjcEPs1AKZfuEUznVBrJw+gK4soq+bx8b1X2XoMqVClarc1q5JDmVtWXY1xfr6ZuHTAsPXHTgTrqKTZeww=="],
|
||||
|
||||
"@rolldown/binding-win32-x64-msvc": ["@rolldown/binding-win32-x64-msvc@1.2.9", "", { "os": "win32", "cpu": "x64" }, "sha512-Ip2mXoU0hM0boq3Rf+ekuT653OROSo6aSYcPT1VHE4q52KvyxgFkQgrgb/IEsxOuvQ2fZZbs8khJAyCEPM24/g=="],
|
||||
|
||||
"@rolldown/pluginutils": ["@rolldown/pluginutils@1.0.1", "", {}, "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw=="],
|
||||
|
||||
"detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="],
|
||||
|
||||
"fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="],
|
||||
|
||||
"fsevents": ["fsevents@2.3.3", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="],
|
||||
|
||||
"lightningcss": ["lightningcss@1.33.0", "", { "dependencies": { "detect-libc": "^2.0.3" }, "optionalDependencies": { "lightningcss-android-arm64": "1.33.0", "lightningcss-darwin-arm64": "1.33.0", "lightningcss-darwin-x64": "1.33.0", "lightningcss-freebsd-x64": "1.33.0", "lightningcss-linux-arm-gnueabihf": "1.33.0", "lightningcss-linux-arm64-gnu": "1.33.0", "lightningcss-linux-arm64-musl": "1.33.0", "lightningcss-linux-x64-gnu": "1.33.0", "lightningcss-linux-x64-musl": "1.33.0", "lightningcss-win32-arm64-msvc": "1.33.0", "lightningcss-win32-x64-msvc": "1.33.0" } }, "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA=="],
|
||||
|
||||
"lightningcss-android-arm64": ["lightningcss-android-arm64@1.33.0", "", { "os": "android", "cpu": "arm64" }, "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg=="],
|
||||
|
||||
"lightningcss-darwin-arm64": ["lightningcss-darwin-arm64@1.33.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg=="],
|
||||
|
||||
"lightningcss-darwin-x64": ["lightningcss-darwin-x64@1.33.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ=="],
|
||||
|
||||
"lightningcss-freebsd-x64": ["lightningcss-freebsd-x64@1.33.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg=="],
|
||||
|
||||
"lightningcss-linux-arm-gnueabihf": ["lightningcss-linux-arm-gnueabihf@1.33.0", "", { "os": "linux", "cpu": "arm" }, "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ=="],
|
||||
|
||||
"lightningcss-linux-arm64-gnu": ["lightningcss-linux-arm64-gnu@1.33.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg=="],
|
||||
|
||||
"lightningcss-linux-arm64-musl": ["lightningcss-linux-arm64-musl@1.33.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ=="],
|
||||
|
||||
"lightningcss-linux-x64-gnu": ["lightningcss-linux-x64-gnu@1.33.0", "", { "os": "linux", "cpu": "x64" }, "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg=="],
|
||||
|
||||
"lightningcss-linux-x64-musl": ["lightningcss-linux-x64-musl@1.33.0", "", { "os": "linux", "cpu": "x64" }, "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw=="],
|
||||
|
||||
"lightningcss-win32-arm64-msvc": ["lightningcss-win32-arm64-msvc@1.33.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA=="],
|
||||
|
||||
"lightningcss-win32-x64-msvc": ["lightningcss-win32-x64-msvc@1.33.0", "", { "os": "win32", "cpu": "x64" }, "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA=="],
|
||||
|
||||
"loglevel": ["loglevel@1.9.2", "", {}, "sha512-HgMmCqIJSAKqo68l0rS2AanEWfkxaZ5wNiEFb5ggm08lDs9Xl2KxBlX3PTcaD2chBM1gXAYf491/M2Rv8Jwayg=="],
|
||||
|
||||
"nanoid": ["nanoid@3.3.19", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-Y2tUNy4ouw6tq5oDSKeQYGOyhkUBhNOcGV/02KC+6kd9eDGqdZd++mjMiIDilrBYvjEnCYvVtsuHCuP+okSfug=="],
|
||||
|
||||
"picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="],
|
||||
|
||||
"picomatch": ["picomatch@4.0.7", "", {}, "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA=="],
|
||||
|
||||
"postcss": ["postcss@8.5.28", "", { "dependencies": { "nanoid": "^3.3.18", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-RRuzqDtt5Y9h3quz5hWhK+TPnsmVs6WwSU6LkJMeY4HstUEDuYTG8UJSdawMRzmzAtV+KEoG8N3Qg2qLy5vM/A=="],
|
||||
|
||||
"rolldown": ["rolldown@1.2.9", "", { "dependencies": { "@oxc-project/types": "=0.150.0", "@rolldown/pluginutils": "^1.0.0" }, "optionalDependencies": { "@rolldown/binding-android-arm-eabi": "1.2.9", "@rolldown/binding-android-arm64": "1.2.9", "@rolldown/binding-darwin-arm64": "1.2.9", "@rolldown/binding-darwin-x64": "1.2.9", "@rolldown/binding-freebsd-x64": "1.2.9", "@rolldown/binding-linux-arm-gnueabihf": "1.2.9", "@rolldown/binding-linux-arm64-gnu": "1.2.9", "@rolldown/binding-linux-arm64-musl": "1.2.9", "@rolldown/binding-linux-ppc64-gnu": "1.2.9", "@rolldown/binding-linux-s390x-gnu": "1.2.9", "@rolldown/binding-linux-x64-gnu": "1.2.9", "@rolldown/binding-linux-x64-musl": "1.2.9", "@rolldown/binding-openharmony-arm64": "1.2.9", "@rolldown/binding-win32-arm64-msvc": "1.2.9", "@rolldown/binding-win32-x64-msvc": "1.2.9" }, "bin": { "rolldown": "./bin/cli.mjs" } }, "sha512-hx/Pv0N1haXRb11qkfnK5MXB/iqr7i0yjWQqmO9uHqZpBgQSqzc8UsSnEpalsh+j1I8qQ2CkXAkJC8Br3dKSlg=="],
|
||||
|
||||
"source-map-js": ["source-map-js@1.2.1", "", {}, "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA=="],
|
||||
|
||||
"tinyglobby": ["tinyglobby@0.2.17", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.4" } }, "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g=="],
|
||||
|
||||
"vite": ["vite@8.3.0", "", { "dependencies": { "lightningcss": "^1.33.0", "picomatch": "^4.0.7", "postcss": "^8.5.28", "rolldown": "~1.2.6", "tinyglobby": "^0.2.17" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^20.19.0 || >=22.12.0", "@vitejs/devtools": "^0.7.1", "esbuild": "^0.27.0 || ^0.28.0", "jiti": ">=1.21.0", "less": "^4.0.0", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "@vitejs/devtools", "esbuild", "jiti", "less", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-lhZBVvEHefgE+HQZC9O7EBJgCU/nVzFNl7vkS4RE0APtWLP02/8QVIkQtzBxPquh7lq5/78NHipTj7ODQ6XuyQ=="],
|
||||
}
|
||||
}
|
||||
21
sandbox-webllm-bun/sandbox-webllm-bun/index.html
Normal file
21
sandbox-webllm-bun/sandbox-webllm-bun/index.html
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WebLLM x Bun</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; max-width: 600px; margin: 2rem auto; line-height: 1.5; background: #000; color: #fff;}
|
||||
#output { background: #111; border: 1px solid #333; padding: 1.5rem; border-radius: 8px; min-height: 100px; margin-top: 1rem; }
|
||||
.status { color: #facc15; font-weight: bold; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Bac à sable WebLLM ⚡️ Bun</h1>
|
||||
<input type="text" id="user-input" placeholder="Pose ta question à l'IA..." style="width: 100%; padding: 12px; margin-bottom: 10px; box-sizing: border-box; background: #222; border: 1px solid #444; color: white; border-radius: 6px; font-size: 16px;">
|
||||
<button id="start-btn">Générer la réponse</button>
|
||||
<p id="status" class="status">Prêt.</p>
|
||||
<div id="output">La réponse s'affichera ici en direct.</div>
|
||||
<script type="module" src="/src/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
17
sandbox-webllm-bun/sandbox-webllm-bun/package.json
Normal file
17
sandbox-webllm-bun/sandbox-webllm-bun/package.json
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "sandbox-webllm-bun",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vite": "^8.3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mlc-ai/web-llm": "^0.2.85"
|
||||
}
|
||||
}
|
||||
1
sandbox-webllm-bun/sandbox-webllm-bun/public/favicon.svg
Normal file
1
sandbox-webllm-bun/sandbox-webllm-bun/public/favicon.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 9.3 KiB |
24
sandbox-webllm-bun/sandbox-webllm-bun/public/icons.svg
Normal file
24
sandbox-webllm-bun/sandbox-webllm-bun/public/icons.svg
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<symbol id="bluesky-icon" viewBox="0 0 16 17">
|
||||
<g clip-path="url(#bluesky-clip)"><path fill="#08060d" d="M7.75 7.735c-.693-1.348-2.58-3.86-4.334-5.097-1.68-1.187-2.32-.981-2.74-.79C.188 2.065.1 2.812.1 3.251s.241 3.602.398 4.13c.52 1.744 2.367 2.333 4.07 2.145-2.495.37-4.71 1.278-1.805 4.512 3.196 3.309 4.38-.71 4.987-2.746.608 2.036 1.307 5.91 4.93 2.746 2.72-2.746.747-4.143-1.747-4.512 1.702.189 3.55-.4 4.07-2.145.156-.528.397-3.691.397-4.13s-.088-1.186-.575-1.406c-.42-.19-1.06-.395-2.741.79-1.755 1.24-3.64 3.752-4.334 5.099"/></g>
|
||||
<defs><clipPath id="bluesky-clip"><path fill="#fff" d="M.1.85h15.3v15.3H.1z"/></clipPath></defs>
|
||||
</symbol>
|
||||
<symbol id="discord-icon" viewBox="0 0 20 19">
|
||||
<path fill="#08060d" d="M16.224 3.768a14.5 14.5 0 0 0-3.67-1.153c-.158.286-.343.67-.47.976a13.5 13.5 0 0 0-4.067 0c-.128-.306-.317-.69-.476-.976A14.4 14.4 0 0 0 3.868 3.77C1.546 7.28.916 10.703 1.231 14.077a14.7 14.7 0 0 0 4.5 2.306q.545-.748.965-1.587a9.5 9.5 0 0 1-1.518-.74q.191-.14.372-.293c2.927 1.369 6.107 1.369 8.999 0q.183.152.372.294-.723.437-1.52.74.418.838.963 1.588a14.6 14.6 0 0 0 4.504-2.308c.37-3.911-.63-7.302-2.644-10.309m-9.13 8.234c-.878 0-1.599-.82-1.599-1.82 0-.998.705-1.82 1.6-1.82.894 0 1.614.82 1.599 1.82.001 1-.705 1.82-1.6 1.82m5.91 0c-.878 0-1.599-.82-1.599-1.82 0-.998.705-1.82 1.6-1.82.893 0 1.614.82 1.599 1.82 0 1-.706 1.82-1.6 1.82"/>
|
||||
</symbol>
|
||||
<symbol id="documentation-icon" viewBox="0 0 21 20">
|
||||
<path fill="none" stroke="#aa3bff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.35" d="m15.5 13.333 1.533 1.322c.645.555.967.833.967 1.178s-.322.623-.967 1.179L15.5 18.333m-3.333-5-1.534 1.322c-.644.555-.966.833-.966 1.178s.322.623.966 1.179l1.534 1.321"/>
|
||||
<path fill="none" stroke="#aa3bff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.35" d="M17.167 10.836v-4.32c0-1.41 0-2.117-.224-2.68-.359-.906-1.118-1.621-2.08-1.96-.599-.21-1.349-.21-2.848-.21-2.623 0-3.935 0-4.983.369-1.684.591-3.013 1.842-3.641 3.428C3 6.449 3 7.684 3 10.154v2.122c0 2.558 0 3.838.706 4.726q.306.383.713.671c.76.536 1.79.64 3.581.66"/>
|
||||
<path fill="none" stroke="#aa3bff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.35" d="M3 10a2.78 2.78 0 0 1 2.778-2.778c.555 0 1.209.097 1.748-.047.48-.129.854-.503.982-.982.145-.54.048-1.194.048-1.749a2.78 2.78 0 0 1 2.777-2.777"/>
|
||||
</symbol>
|
||||
<symbol id="github-icon" viewBox="0 0 19 19">
|
||||
<path fill="#08060d" fill-rule="evenodd" d="M9.356 1.85C5.05 1.85 1.57 5.356 1.57 9.694a7.84 7.84 0 0 0 5.324 7.44c.387.079.528-.168.528-.376 0-.182-.013-.805-.013-1.454-2.165.467-2.616-.935-2.616-.935-.349-.91-.864-1.143-.864-1.143-.71-.48.051-.48.051-.48.787.051 1.2.805 1.2.805.695 1.194 1.817.857 2.268.649.064-.507.27-.857.49-1.052-1.728-.182-3.545-.857-3.545-3.87 0-.857.31-1.558.8-2.104-.078-.195-.349-1 .077-2.078 0 0 .657-.208 2.14.805a7.5 7.5 0 0 1 1.946-.26c.657 0 1.328.092 1.946.26 1.483-1.013 2.14-.805 2.14-.805.426 1.078.155 1.883.078 2.078.502.546.799 1.247.799 2.104 0 3.013-1.818 3.675-3.558 3.87.284.247.528.714.528 1.454 0 1.052-.012 1.896-.012 2.156 0 .208.142.455.528.377a7.84 7.84 0 0 0 5.324-7.441c.013-4.338-3.48-7.844-7.773-7.844" clip-rule="evenodd"/>
|
||||
</symbol>
|
||||
<symbol id="social-icon" viewBox="0 0 20 20">
|
||||
<path fill="none" stroke="#aa3bff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.35" d="M12.5 6.667a4.167 4.167 0 1 0-8.334 0 4.167 4.167 0 0 0 8.334 0"/>
|
||||
<path fill="none" stroke="#aa3bff" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.35" d="M2.5 16.667a5.833 5.833 0 0 1 8.75-5.053m3.837.474.513 1.035c.07.144.257.282.414.309l.93.155c.596.1.736.536.307.965l-.723.73a.64.64 0 0 0-.152.531l.207.903c.164.715-.213.991-.84.618l-.872-.52a.63.63 0 0 0-.577 0l-.872.52c-.624.373-1.003.094-.84-.618l.207-.903a.64.64 0 0 0-.152-.532l-.723-.729c-.426-.43-.289-.864.306-.964l.93-.156a.64.64 0 0 0 .412-.31l.513-1.034c.28-.562.735-.562 1.012 0"/>
|
||||
</symbol>
|
||||
<symbol id="x-icon" viewBox="0 0 19 19">
|
||||
<path fill="#08060d" fill-rule="evenodd" d="M1.893 1.98c.052.072 1.245 1.769 2.653 3.77l2.892 4.114c.183.261.333.48.333.486s-.068.089-.152.183l-.522.593-.765.867-3.597 4.087c-.375.426-.734.834-.798.905a1 1 0 0 0-.118.148c0 .01.236.017.664.017h.663l.729-.83c.4-.457.796-.906.879-.999a692 692 0 0 0 1.794-2.038c.034-.037.301-.34.594-.675l.551-.624.345-.392a7 7 0 0 1 .34-.374c.006 0 .93 1.306 2.052 2.903l2.084 2.965.045.063h2.275c1.87 0 2.273-.003 2.266-.021-.008-.02-1.098-1.572-3.894-5.547-2.013-2.862-2.28-3.246-2.273-3.266.008-.019.282-.332 2.085-2.38l2-2.274 1.567-1.782c.022-.028-.016-.03-.65-.03h-.674l-.3.342a871 871 0 0 1-1.782 2.025c-.067.075-.405.458-.75.852a100 100 0 0 1-.803.91c-.148.172-.299.344-.99 1.127-.304.343-.32.358-.345.327-.015-.019-.904-1.282-1.976-2.808L6.365 1.85H1.8zm1.782.91 8.078 11.294c.772 1.08 1.413 1.973 1.425 1.984.016.017.241.02 1.05.017l1.03-.004-2.694-3.766L7.796 5.75 5.722 2.852l-1.039-.004-1.039-.004z" clip-rule="evenodd"/>
|
||||
</symbol>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.9 KiB |
BIN
sandbox-webllm-bun/sandbox-webllm-bun/src/assets/hero.png
Normal file
BIN
sandbox-webllm-bun/sandbox-webllm-bun/src/assets/hero.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="32" height="32" viewBox="0 0 256 256"><path fill="#F7DF1E" d="M0 0h256v256H0V0Z"/><path d="m67.312 213.932l19.59-11.856c3.78 6.701 7.218 12.371 15.465 12.371c7.905 0 12.89-3.092 12.89-15.12v-81.798h24.057v82.138c0 24.917-14.606 36.259-35.916 36.259c-19.245 0-30.416-9.967-36.087-21.996m85.07-2.576l19.588-11.341c5.157 8.421 11.859 14.607 23.715 14.607c9.969 0 16.325-4.984 16.325-11.858c0-8.248-6.53-11.17-17.528-15.98l-6.013-2.58c-17.357-7.387-28.87-16.667-28.87-36.257c0-18.044 13.747-31.792 35.228-31.792c15.294 0 26.292 5.328 34.196 19.247l-18.732 12.03c-4.125-7.389-8.591-10.31-15.465-10.31c-7.046 0-11.514 4.468-11.514 10.31c0 7.217 4.468 10.14 14.778 14.608l6.014 2.577c20.45 8.765 31.963 17.7 31.963 37.804c0 21.654-17.012 33.51-39.867 33.51c-22.339 0-36.774-10.654-43.819-24.574"/></svg>
|
||||
|
After Width: | Height: | Size: 863 B |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 8.5 KiB |
9
sandbox-webllm-bun/sandbox-webllm-bun/src/counter.js
Normal file
9
sandbox-webllm-bun/sandbox-webllm-bun/src/counter.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export function setupCounter(element) {
|
||||
let counter = 0
|
||||
const setCounter = (count) => {
|
||||
counter = count
|
||||
element.innerHTML = `Count is ${counter}`
|
||||
}
|
||||
element.addEventListener('click', () => setCounter(counter + 1))
|
||||
setCounter(0)
|
||||
}
|
||||
55
sandbox-webllm-bun/sandbox-webllm-bun/src/main.js
Normal file
55
sandbox-webllm-bun/sandbox-webllm-bun/src/main.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import { CreateMLCEngine } from "@mlc-ai/web-llm";
|
||||
|
||||
const statusEl = document.getElementById("status");
|
||||
const outputEl = document.getElementById("output");
|
||||
const startBtn = document.getElementById("start-btn");
|
||||
const inputEl = document.getElementById("user-input"); // Nouveau
|
||||
|
||||
// On peut initialiser le moteur une seule fois au chargement de la page pour aller plus vite ensuite
|
||||
let engine;
|
||||
|
||||
async function generateResponse() {
|
||||
const userText = inputEl.value;
|
||||
if (!userText.trim()) return; // On ne fait rien si le champ est vide
|
||||
|
||||
startBtn.disabled = true;
|
||||
outputEl.textContent = "";
|
||||
|
||||
try {
|
||||
// Initialisation paresseuse (lazy load) du modèle au premier clic
|
||||
if (!engine) {
|
||||
statusEl.textContent = "⏳ Vérification du cache du modèle...";
|
||||
engine = await CreateMLCEngine("Phi-3-mini-4k-instruct-q4f16_1-MLC", {
|
||||
initProgressCallback: (progress) => {
|
||||
statusEl.textContent = `⏳ ${progress.text}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
statusEl.textContent = "🚀 Génération en cours...";
|
||||
|
||||
const chunks = await engine.chat.completions.create({
|
||||
messages: [
|
||||
{ role: "system", content: "Tu es un expert technique concis." },
|
||||
{ role: "user", content: userText } // On utilise le texte tapé par l'utilisateur
|
||||
],
|
||||
temperature: 0.7,
|
||||
stream: true,
|
||||
});
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
const content = chunk.choices[0]?.delta?.content || "";
|
||||
outputEl.textContent += content;
|
||||
}
|
||||
|
||||
statusEl.textContent = "✅ Génération terminée";
|
||||
|
||||
} catch (error) {
|
||||
statusEl.textContent = "❌ Erreur (voir console).";
|
||||
console.error(error);
|
||||
} finally {
|
||||
startBtn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
startBtn.addEventListener("click", generateResponse);
|
||||
296
sandbox-webllm-bun/sandbox-webllm-bun/src/style.css
Normal file
296
sandbox-webllm-bun/sandbox-webllm-bun/src/style.css
Normal file
|
|
@ -0,0 +1,296 @@
|
|||
:root {
|
||||
--text: #6b6375;
|
||||
--text-h: #08060d;
|
||||
--bg: #fff;
|
||||
--border: #e5e4e7;
|
||||
--code-bg: #f4f3ec;
|
||||
--accent: #aa3bff;
|
||||
--accent-bg: rgba(170, 59, 255, 0.1);
|
||||
--accent-border: rgba(170, 59, 255, 0.5);
|
||||
--social-bg: rgba(244, 243, 236, 0.5);
|
||||
--shadow:
|
||||
rgba(0, 0, 0, 0.1) 0 10px 15px -3px, rgba(0, 0, 0, 0.05) 0 4px 6px -2px;
|
||||
|
||||
--sans: system-ui, 'Segoe UI', Roboto, sans-serif;
|
||||
--heading: system-ui, 'Segoe UI', Roboto, sans-serif;
|
||||
--mono: ui-monospace, Consolas, monospace;
|
||||
|
||||
font: 18px/145% var(--sans);
|
||||
letter-spacing: 0.18px;
|
||||
color-scheme: light dark;
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
font-size: 16px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--text: #9ca3af;
|
||||
--text-h: #f3f4f6;
|
||||
--bg: #16171d;
|
||||
--border: #2e303a;
|
||||
--code-bg: #1f2028;
|
||||
--accent: #c084fc;
|
||||
--accent-bg: rgba(192, 132, 252, 0.15);
|
||||
--accent-border: rgba(192, 132, 252, 0.5);
|
||||
--social-bg: rgba(47, 48, 58, 0.5);
|
||||
--shadow:
|
||||
rgba(0, 0, 0, 0.4) 0 10px 15px -3px, rgba(0, 0, 0, 0.25) 0 4px 6px -2px;
|
||||
}
|
||||
|
||||
#social .button-icon {
|
||||
filter: invert(1) brightness(2);
|
||||
}
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2 {
|
||||
font-family: var(--heading);
|
||||
font-weight: 500;
|
||||
color: var(--text-h);
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 56px;
|
||||
letter-spacing: -1.68px;
|
||||
margin: 32px 0;
|
||||
@media (max-width: 1024px) {
|
||||
font-size: 36px;
|
||||
margin: 20px 0;
|
||||
}
|
||||
}
|
||||
h2 {
|
||||
font-size: 24px;
|
||||
line-height: 118%;
|
||||
letter-spacing: -0.24px;
|
||||
margin: 0 0 8px;
|
||||
@media (max-width: 1024px) {
|
||||
font-size: 20px;
|
||||
}
|
||||
}
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
code,
|
||||
.counter {
|
||||
font-family: var(--mono);
|
||||
display: inline-flex;
|
||||
border-radius: 4px;
|
||||
color: var(--text-h);
|
||||
}
|
||||
|
||||
code {
|
||||
font-size: 15px;
|
||||
line-height: 135%;
|
||||
padding: 4px 8px;
|
||||
background: var(--code-bg);
|
||||
}
|
||||
|
||||
.counter {
|
||||
font-size: 16px;
|
||||
padding: 5px 10px;
|
||||
border-radius: 5px;
|
||||
color: var(--accent);
|
||||
background: var(--accent-bg);
|
||||
border: 2px solid transparent;
|
||||
transition: border-color 0.3s;
|
||||
margin-bottom: 24px;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--accent-border);
|
||||
}
|
||||
&:focus-visible {
|
||||
outline: 2px solid var(--accent);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.hero {
|
||||
position: relative;
|
||||
|
||||
.base,
|
||||
.framework,
|
||||
.vite {
|
||||
inset-inline: 0;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.base {
|
||||
width: 170px;
|
||||
position: relative;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.framework,
|
||||
.vite {
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.framework {
|
||||
z-index: 1;
|
||||
top: 34px;
|
||||
height: 28px;
|
||||
transform: perspective(2000px) rotateZ(300deg) rotateX(44deg) rotateY(39deg)
|
||||
scale(1.4);
|
||||
}
|
||||
|
||||
.vite {
|
||||
z-index: 0;
|
||||
top: 107px;
|
||||
height: 26px;
|
||||
width: auto;
|
||||
transform: perspective(2000px) rotateZ(300deg) rotateX(40deg) rotateY(39deg)
|
||||
scale(0.8);
|
||||
}
|
||||
}
|
||||
|
||||
#app {
|
||||
width: 1126px;
|
||||
max-width: 100%;
|
||||
margin: 0 auto;
|
||||
text-align: center;
|
||||
border-inline: 1px solid var(--border);
|
||||
min-height: 100svh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#center {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 25px;
|
||||
place-content: center;
|
||||
place-items: center;
|
||||
flex-grow: 1;
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
padding: 32px 20px 24px;
|
||||
gap: 18px;
|
||||
}
|
||||
}
|
||||
|
||||
#next-steps {
|
||||
display: flex;
|
||||
border-top: 1px solid var(--border);
|
||||
text-align: left;
|
||||
|
||||
& > div {
|
||||
flex: 1 1 0;
|
||||
padding: 32px;
|
||||
@media (max-width: 1024px) {
|
||||
padding: 24px 20px;
|
||||
}
|
||||
}
|
||||
|
||||
.icon {
|
||||
margin-bottom: 16px;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
flex-direction: column;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
#docs {
|
||||
border-right: 1px solid var(--border);
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
border-right: none;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
}
|
||||
|
||||
#next-steps ul {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin: 32px 0 0;
|
||||
|
||||
.logo {
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
a {
|
||||
color: var(--text-h);
|
||||
font-size: 16px;
|
||||
border-radius: 6px;
|
||||
background: var(--social-bg);
|
||||
display: flex;
|
||||
padding: 6px 12px;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
text-decoration: none;
|
||||
transition: box-shadow 0.3s;
|
||||
|
||||
&:hover {
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.button-icon {
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
margin-top: 20px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
|
||||
li {
|
||||
flex: 1 1 calc(50% - 8px);
|
||||
}
|
||||
|
||||
a {
|
||||
width: 100%;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#spacer {
|
||||
height: 88px;
|
||||
border-top: 1px solid var(--border);
|
||||
@media (max-width: 1024px) {
|
||||
height: 48px;
|
||||
}
|
||||
}
|
||||
|
||||
.ticks {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
|
||||
&::before,
|
||||
&::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: -4.5px;
|
||||
border: 5px solid transparent;
|
||||
}
|
||||
|
||||
&::before {
|
||||
left: 0;
|
||||
border-left-color: var(--border);
|
||||
}
|
||||
&::after {
|
||||
right: 0;
|
||||
border-right-color: var(--border);
|
||||
}
|
||||
}
|
||||
25
webllm/SupportFirefox.md
Normal file
25
webllm/SupportFirefox.md
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Pour activer WebGPU dans Firefox, la méthode dépend de votre système d'exploitation et de la version de Firefox que vous utilisez. Sur les versions récentes (Windows et macOS Apple Silicon), c'est souvent activé par défaut. Sinon, il faut passer par les flags expérimentaux.
|
||||
|
||||
### 🚀 Méthode rapide : via `about:config`
|
||||
|
||||
C'est la méthode qui fonctionne pour toutes les versions récentes de Firefox (stable, beta, etc.), quelle que soit votre plateforme.
|
||||
|
||||
1. Dans la barre d'adresse de Firefox, tapez `about:config` et appuyez sur Entrée.
|
||||
2. Acceptez l'avertissement "Risque de danger" si c'est la première fois.
|
||||
3. Dans la barre de recherche, tapez : `dom.webgpu.enabled`.
|
||||
4. Si la valeur est sur `false`, double-cliquez dessus pour la passer à `true`.
|
||||
|
||||
### 💡 À savoir selon votre système
|
||||
|
||||
La situation a évolué récemment, il y a donc des cas particuliers :
|
||||
|
||||
* **Windows (Firefox 141+)** : WebGPU est **activé par défaut** depuis la version 141. Normalement, vous n'avez rien à faire.
|
||||
* **macOS Apple Silicon (Firefox 147+)** : WebGPU est également **activé par défaut** sur les Mac équipés de puces M1/M2/M3/etc..
|
||||
* **macOS Intel et Linux** : Le support est soit absent, soit désactivé par défaut pour des raisons de sécurité ou de pilotes instables. Vous devez passer par `about:config` pour forcer l'activation, en gardant en tête que cela peut provoquer des plantages ou des comportements instables selon votre carte graphique et vos pilotes.
|
||||
* **Firefox Nightly** : Cette version de développement a souvent WebGPU activé par défaut, même sur des plateformes où la version stable ne l'a pas encore.
|
||||
|
||||
### ✅ Vérifier que ça marche
|
||||
|
||||
Une fois l'option activée, redémarrez Firefox, puis ouvrez la console de développement (F12) et tapez `navigator.gpu`. Si la console vous retourne un objet `GPU` (et non `undefined`), c'est bon.
|
||||
|
||||
Vous pouvez ensuite relancer votre projet (`bun start`) et ouvrir `http://localhost:3000`. Si le modèle se charge sans erreur, c'est que tout fonctionne.
|
||||
460
webllm/todo.md
Normal file
460
webllm/todo.md
Normal file
|
|
@ -0,0 +1,460 @@
|
|||
# Projet WebLLM avec interface de chat et serveur Bun
|
||||
|
||||
Voici un projet complet et fonctionnel. Le point clé que vous aviez manqué : **WebLLM nécessite d'être servi via HTTP** (pas `file://`), et les modules ES doivent être correctement résolus. Bun sert à la fois de serveur statique et de gestionnaire de dépendances.
|
||||
|
||||
## Structure du projet
|
||||
|
||||
```
|
||||
webllm-chat/
|
||||
├── package.json
|
||||
├── server.js
|
||||
├── persona.txt
|
||||
├── public/
|
||||
│ ├── index.html
|
||||
│ ├── index.js
|
||||
│ └── personaLoader.js
|
||||
```
|
||||
|
||||
## 1. `package.json`
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "webllm-chat",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "bun run server.js",
|
||||
"dev": "bun --watch server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mlc-ai/web-llm": "^0.2.85"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. `server.js` (serveur Bun)
|
||||
|
||||
Bun sait servir des fichiers statiques très simplement. On expose aussi le dossier `node_modules` pour que le navigateur puisse importer `@mlc-ai/web-llm` en ESM.
|
||||
|
||||
```js
|
||||
// server.js
|
||||
import { serve } from "bun";
|
||||
import { join, extname } from "path";
|
||||
import { stat } from "fs/promises";
|
||||
|
||||
const ROOT = import.meta.dir;
|
||||
const PORT = 3000;
|
||||
|
||||
const MIME_TYPES = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".js": "application/javascript; charset=utf-8",
|
||||
".mjs": "application/javascript; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
".json": "application/json; charset=utf-8",
|
||||
".txt": "text/plain; charset=utf-8",
|
||||
".wasm": "application/wasm",
|
||||
".map": "application/json; charset=utf-8",
|
||||
};
|
||||
|
||||
async function tryFile(path) {
|
||||
try {
|
||||
const s = await stat(path);
|
||||
if (!s.isFile()) return null;
|
||||
return Bun.file(path);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const server = serve({
|
||||
port: PORT,
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
let pathname = decodeURIComponent(url.pathname);
|
||||
|
||||
// Route API pour la persona
|
||||
if (pathname === "/api/persona") {
|
||||
const file = await tryFile(join(ROOT, "persona.txt"));
|
||||
if (!file) {
|
||||
return new Response("Persona introuvable", { status: 404 });
|
||||
}
|
||||
return new Response(file, {
|
||||
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
||||
});
|
||||
}
|
||||
|
||||
// Résolution des chemins
|
||||
let filePath;
|
||||
|
||||
if (pathname.startsWith("/node_modules/")) {
|
||||
// Modules NPM (web-llm, etc.)
|
||||
filePath = join(ROOT, pathname);
|
||||
} else if (pathname === "/" || pathname === "/index.html") {
|
||||
filePath = join(ROOT, "public", "index.html");
|
||||
} else {
|
||||
// Fichiers du dossier public
|
||||
filePath = join(ROOT, "public", pathname);
|
||||
}
|
||||
|
||||
const file = await tryFile(filePath);
|
||||
if (file) {
|
||||
const ext = extname(filePath);
|
||||
const headers = {
|
||||
"Content-Type": MIME_TYPES[ext] || "application/octet-stream",
|
||||
// Important pour WebLLM (SharedArrayBuffer / WebGPU workers)
|
||||
"Cross-Origin-Opener-Policy": "same-origin",
|
||||
"Cross-Origin-Embedder-Policy": "require-corp",
|
||||
};
|
||||
return new Response(file, { headers });
|
||||
}
|
||||
|
||||
return new Response("404 — Not Found", { status: 404 });
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`🚀 Serveur prêt sur http://localhost:${server.port}`);
|
||||
```
|
||||
|
||||
> ⚠️ **COOP/COEP** : WebLLM utilise des Web Workers et potentiellement `SharedArrayBuffer`. Ces en-têtes sont recommandés. Si vous rencontrez des erreurs CORS avec un CDN, on peut les retirer — mais avec tout servi localement, ils ne posent pas de problème.
|
||||
|
||||
## 3. `persona.txt`
|
||||
|
||||
```
|
||||
Tu es un assistant culinaire expert, spécialisé dans la cuisine française.
|
||||
Tu réponds toujours avec enthousiasme et proposes des alternatives aux ingrédients difficiles à trouver.
|
||||
Tu ne parles jamais de sujets hors de la cuisine.
|
||||
```
|
||||
|
||||
## 4. `public/index.html`
|
||||
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Chat WebLLM</title>
|
||||
<style>
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: system-ui, -apple-system, sans-serif;
|
||||
background: #0f1115;
|
||||
color: #e6e6e6;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
header {
|
||||
padding: 1rem;
|
||||
background: #1a1d23;
|
||||
border-bottom: 1px solid #2a2e36;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
header h1 { font-size: 1.1rem; font-weight: 600; }
|
||||
#status {
|
||||
font-size: 0.85rem;
|
||||
color: #8b93a7;
|
||||
}
|
||||
#status.ready { color: #4ade80; }
|
||||
#status.error { color: #f87171; }
|
||||
|
||||
#progress {
|
||||
height: 4px;
|
||||
background: #1a1d23;
|
||||
overflow: hidden;
|
||||
}
|
||||
#progress-bar {
|
||||
height: 100%;
|
||||
width: 0%;
|
||||
background: linear-gradient(90deg, #6366f1, #8b5cf6);
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
|
||||
#chat {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1.5rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
max-width: 900px;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.msg {
|
||||
max-width: 80%;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
.msg.user {
|
||||
align-self: flex-end;
|
||||
background: #6366f1;
|
||||
color: white;
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
.msg.assistant {
|
||||
align-self: flex-start;
|
||||
background: #1e2128;
|
||||
border: 1px solid #2a2e36;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
.msg.system {
|
||||
align-self: center;
|
||||
background: transparent;
|
||||
color: #8b93a7;
|
||||
font-size: 0.85rem;
|
||||
font-style: italic;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
footer {
|
||||
padding: 1rem;
|
||||
border-top: 1px solid #2a2e36;
|
||||
background: #1a1d23;
|
||||
}
|
||||
form {
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
input[type="text"] {
|
||||
flex: 1;
|
||||
padding: 0.75rem 1rem;
|
||||
background: #0f1115;
|
||||
border: 1px solid #2a2e36;
|
||||
border-radius: 8px;
|
||||
color: #e6e6e6;
|
||||
font-size: 1rem;
|
||||
outline: none;
|
||||
}
|
||||
input[type="text"]:focus { border-color: #6366f1; }
|
||||
input[type="text"]:disabled { opacity: 0.5; }
|
||||
button {
|
||||
padding: 0.75rem 1.25rem;
|
||||
background: #6366f1;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
button:hover:not(:disabled) { background: #4f46e5; }
|
||||
button:disabled { opacity: 0.5; cursor: not-allowed; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>🤖 Chat WebLLM</h1>
|
||||
<div id="status">Initialisation…</div>
|
||||
</header>
|
||||
<div id="progress"><div id="progress-bar"></div></div>
|
||||
|
||||
<div id="chat">
|
||||
<div class="msg system">Le modèle se charge dans votre navigateur (première fois ~1-2 min).</div>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
<form id="form">
|
||||
<input
|
||||
type="text"
|
||||
id="input"
|
||||
placeholder="Chargement du modèle…"
|
||||
autocomplete="off"
|
||||
disabled
|
||||
/>
|
||||
<button type="submit" id="send" disabled>Envoyer</button>
|
||||
</form>
|
||||
</footer>
|
||||
|
||||
<script type="module" src="/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## 5. `public/personaLoader.js`
|
||||
|
||||
```js
|
||||
// personaLoader.js
|
||||
export async function loadPersona() {
|
||||
const response = await fetch("/api/persona");
|
||||
if (!response.ok) {
|
||||
throw new Error(`Erreur ${response.status}: impossible de charger la persona`);
|
||||
}
|
||||
const systemPrompt = await response.text();
|
||||
return systemPrompt.trim();
|
||||
}
|
||||
```
|
||||
|
||||
## 6. `public/index.js`
|
||||
|
||||
```js
|
||||
import { CreateMLCEngine } from "/node_modules/@mlc-ai/web-llm/lib/index.js";
|
||||
import { loadPersona } from "./personaLoader.js";
|
||||
|
||||
// ---- Références DOM ----
|
||||
const chatEl = document.getElementById("chat");
|
||||
const formEl = document.getElementById("form");
|
||||
const inputEl = document.getElementById("input");
|
||||
const sendEl = document.getElementById("send");
|
||||
const statusEl = document.getElementById("status");
|
||||
const progressEl = document.getElementById("progress-bar");
|
||||
|
||||
// ---- État ----
|
||||
const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC";
|
||||
let engine = null;
|
||||
const messages = []; // historique de conversation
|
||||
|
||||
// ---- Helpers UI ----
|
||||
function addMessage(role, content) {
|
||||
const div = document.createElement("div");
|
||||
div.className = `msg ${role}`;
|
||||
div.textContent = content;
|
||||
chatEl.appendChild(div);
|
||||
chatEl.scrollTop = chatEl.scrollHeight;
|
||||
return div;
|
||||
}
|
||||
|
||||
function updateStatus(text, cls = "") {
|
||||
statusEl.textContent = text;
|
||||
statusEl.className = cls;
|
||||
}
|
||||
|
||||
// ---- Chargement du modèle ----
|
||||
const initProgressCallback = (report) => {
|
||||
// report.text : "Fetching param cache[1/2]…" etc.
|
||||
// report.progress : 0..1
|
||||
if (typeof report.progress === "number") {
|
||||
progressEl.style.width = `${(report.progress * 100).toFixed(1)}%`;
|
||||
}
|
||||
if (report.text) updateStatus(report.text);
|
||||
};
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
// 1) Charger la persona depuis le serveur
|
||||
updateStatus("Chargement de la persona…");
|
||||
const systemPrompt = await loadPersona();
|
||||
messages.push({ role: "system", content: systemPrompt });
|
||||
addMessage("system", `Persona chargée : « ${systemPrompt.slice(0, 80)}… »`);
|
||||
|
||||
// 2) Créer le moteur WebLLM
|
||||
updateStatus("Chargement du modèle…");
|
||||
engine = await CreateMLCEngine(selectedModel, { initProgressCallback });
|
||||
|
||||
progressEl.style.width = "100%";
|
||||
updateStatus("Prêt ✅", "ready");
|
||||
inputEl.disabled = false;
|
||||
sendEl.disabled = false;
|
||||
inputEl.placeholder = "Posez votre question…";
|
||||
inputEl.focus();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
updateStatus(`Erreur : ${err.message}`, "error");
|
||||
addMessage("system", `❌ ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Envoi d'un message ----
|
||||
async function sendMessage(userText) {
|
||||
addMessage("user", userText);
|
||||
messages.push({ role: "user", content: userText });
|
||||
|
||||
inputEl.disabled = true;
|
||||
sendEl.disabled = true;
|
||||
|
||||
// Créer la bulle assistant vide qu'on va remplir en streaming
|
||||
const assistantEl = addMessage("assistant", "");
|
||||
let reply = "";
|
||||
|
||||
try {
|
||||
const chunks = await engine.chat.completions.create({
|
||||
messages,
|
||||
temperature: 0.7,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
});
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
const delta = chunk.choices[0]?.delta?.content || "";
|
||||
reply += delta;
|
||||
assistantEl.textContent = reply;
|
||||
chatEl.scrollTop = chatEl.scrollHeight;
|
||||
|
||||
if (chunk.usage) {
|
||||
console.log("Usage:", chunk.usage);
|
||||
}
|
||||
}
|
||||
|
||||
messages.push({ role: "assistant", content: reply });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
assistantEl.textContent = `❌ Erreur : ${err.message}`;
|
||||
} finally {
|
||||
inputEl.disabled = false;
|
||||
sendEl.disabled = false;
|
||||
inputEl.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Événements ----
|
||||
formEl.addEventListener("submit", (e) => {
|
||||
e.preventDefault();
|
||||
const text = inputEl.value.trim();
|
||||
if (!text || !engine) return;
|
||||
inputEl.value = "";
|
||||
sendMessage(text);
|
||||
});
|
||||
|
||||
// ---- Démarrage ----
|
||||
init();
|
||||
```
|
||||
|
||||
## Installation & lancement
|
||||
|
||||
```bash
|
||||
# 1. Créer le dossier et les fichiers ci-dessus
|
||||
|
||||
# 2. Installer les dépendances avec Bun
|
||||
bun install
|
||||
|
||||
# 3. Lancer le serveur
|
||||
bun start
|
||||
```
|
||||
|
||||
Puis ouvrez **http://localhost:3000** dans **Chrome / Edge récent** (WebGPU requis).
|
||||
|
||||
---
|
||||
|
||||
## Points clés qui expliquent pourquoi ça marchait pas avant
|
||||
|
||||
| Problème initial | Solution |
|
||||
|---|---|
|
||||
| `file://` bloque les imports ESM et `fetch()` | Servir via HTTP (Bun static) |
|
||||
| Import `./node_modules/@mlc-ai/web-llm/lib/index.js` depuis `public/` | Servir `/node_modules/*` via le serveur, et importer avec chemin absolu `/node_modules/...` |
|
||||
| Persona lue avec `fetch('./persona.txt')` | Route dédiée `/api/persona` qui lit le fichier côté serveur |
|
||||
| Pas de streaming → l'utilisateur attend sans feedback | `stream: true` + mise à jour progressive de la bulle |
|
||||
| Modèle q4f32 lourd (~5 Go) | Patientez au premier chargement — c'est mis en cache par le navigateur |
|
||||
|
||||
## À propos du modèle
|
||||
|
||||
`Llama-3.1-8B-Instruct-q4f32_1-MLC` fait ~**4.5 Go**. Le premier chargement prend 1-3 minutes selon la connexion. Ensuite c'est mis en cache (IndexedDB / CacheStorage).
|
||||
|
||||
Pour tester plus vite, remplacez par un modèle plus petit :
|
||||
- `"Llama-3.2-1B-Instruct-q4f32_1-MLC"` (~700 Mo)
|
||||
- `"Qwen2.5-1.5B-Instruct-q4f32_1-MLC"` (~1 Go)
|
||||
- `"Phi-3.5-mini-instruct-q4f32_1-MLC"` (~2.2 Go)
|
||||
|
||||
Liste complète : https://mlc.ai/models
|
||||
|
||||
## Notes
|
||||
|
||||
- **WebGPU obligatoire** → Chrome/Edge 113+, ou Safari 18+. Firefox : support partiel.
|
||||
- Si vous voyez une erreur `SharedArrayBuffer is not defined`, c'est que les en-têtes COOP/COEP ne sont pas appliqués → vérifiez le `server.js`.
|
||||
- Le `persona.txt` est lu **côté serveur**, donc plus de souci de CORS ni de chemins relatifs.
|
||||
Loading…
Reference in a new issue