Skip to content

Commit 0e5e95b

Browse files
committed
add the browser package
1 parent 84264e9 commit 0e5e95b

7 files changed

Lines changed: 470 additions & 0 deletions

File tree

packages/browser/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 synw
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/browser/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# LocalLm Browser
2+
3+
Run models in the browser using [Wllama](https://github.com/ngxson/wllama)
4+
5+
## Install
6+
7+
```bash
8+
npm i @locallm/browser
9+
```
10+
11+
## Usage
12+
13+
Vuejs example:
14+
15+
```vue
16+
<template>
17+
<div>
18+
{{ output }}
19+
</div>
20+
</template>
21+
22+
<script setup lang="ts">
23+
import { onMounted, ref } from 'vue';
24+
import { PromptTemplate } from 'modprompt';
25+
import { LmBrowserProviderParams, OnLoadProgress, WllamaProvider } from '@locallm/browser';
26+
27+
const output = ref("");
28+
29+
const lm = WllamaProvider.init({
30+
onToken: (t) => { output.value = t },
31+
} as LmBrowserProviderParams);
32+
const model = {
33+
name: "Qween 0.5b",
34+
url: "https://huggingface.co/Qwen/Qwen2-0.5B-Instruct-GGUF/resolve/main/qwen2-0_5b-instruct-q5_k_m.gguf",
35+
ctx: 32768,
36+
}
37+
38+
const onModelLoading: OnLoadProgress = (st) => {
39+
console.log(st.percent, "%")
40+
}
41+
42+
async function init() {
43+
await lm.loadBrowsermodel(model.name, model.url, model.ctx, onModelLoading);
44+
const p = new PromptTemplate("chatml")
45+
.replaceSystem("You are an AI assistant. Important: always use json to respond")
46+
.prompt("List the planets of the solar system.")
47+
const res = await lm.infer(
48+
p,
49+
{ temperature: 0, min_p: 0.05 }
50+
);
51+
console.log(res.stats)
52+
}
53+
54+
onMounted(() => init())
55+
</script>
56+
```

packages/browser/package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@agent-smith/browser",
3+
"version": "0.0.1",
4+
"description": "Run language models in the browser",
5+
"repository": "https://github.com/synw/locallm",
6+
"scripts": {
7+
"build": "rm -rf dist/* && tsc --skipLibCheck",
8+
"docs": "typedoc --entryPointStrategy expand"
9+
},
10+
"dependencies": {
11+
"@agent-smith/types": "^0.0.7",
12+
"@wllama/wllama": "^3.5.1",
13+
"openai": "^6.45.0",
14+
"restmix": "^0.6.1",
15+
"vite": "^8.1.3"
16+
},
17+
"devDependencies": {
18+
"@agent-smith/types": "^0.7.3",
19+
"@types/node": "^26.1.0",
20+
"tslib": "^2.8.1",
21+
"typedoc": "^0.28.20",
22+
"typedoc-plugin-markdown": "^4.12.0",
23+
"typedoc-plugin-rename-defaults": "^0.7.3",
24+
"typescript": "^6.0.3",
25+
"vue": "^3.5.39"
26+
},
27+
"type": "module",
28+
"files": [
29+
"dist"
30+
],
31+
"module": "./dist/main.js",
32+
"types": "./dist/main.d.ts",
33+
"exports": {
34+
".": {
35+
"import": "./dist/main.js"
36+
}
37+
},
38+
"publishConfig": {
39+
"access": "public",
40+
"registry": "https://registry.npmjs.org/"
41+
},
42+
"license": "MIT"
43+
}

packages/browser/src/interfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface LmBrowserProviderParams {
2+
name: string;
3+
onToken?: (t: string) => void;
4+
onStartEmit?: (data?: any) => void;
5+
onError?: (err: string) => void;
6+
}
7+
8+
export {
9+
LmBrowserProviderParams,
10+
}

packages/browser/src/main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {
2+
LmBrowserProviderParams,
3+
} from "./interfaces.js";
4+
import { WllamaProvider } from "./wllama.js";
5+
6+
export {
7+
WllamaProvider,
8+
LmBrowserProviderParams,
9+
}

0 commit comments

Comments
 (0)