kjarni-wasm 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/package.json +1 -1
- package/pkg/kjarni_wasm.d.ts +2 -2
- package/pkg/kjarni_wasm.js +7 -7
- package/pkg/kjarni_wasm_bg.wasm +0 -0
- package/pkg/kjarni_wasm_bg.wasm.d.ts +2 -2
package/README.md
CHANGED
|
@@ -15,6 +15,59 @@ Or without a bundler, straight from a CDN:
|
|
|
15
15
|
</script>
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
## Copy this and it runs
|
|
19
|
+
|
|
20
|
+
One file, nothing installed. Save it as `quickstart.html`, serve the directory,
|
|
21
|
+
open it: a real model downloads and ranks three sentences by meaning.
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
curl -sO https://raw.githubusercontent.com/olafurjohannsson/kjarni/main/crates/kjarni-wasm/examples/quickstart.html
|
|
25
|
+
python3 -m http.server 8000
|
|
26
|
+
# then open http://localhost:8000/quickstart.html
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
A server is needed because ES modules do not load over `file://`. Any static
|
|
30
|
+
server will do.
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<pre id="out">starting…</pre>
|
|
34
|
+
<script type="module">
|
|
35
|
+
import init, { WasmModel } from "https://cdn.jsdelivr.net/npm/kjarni-wasm@0.1.4/pkg/kjarni_wasm.js";
|
|
36
|
+
await init();
|
|
37
|
+
|
|
38
|
+
const url = "https://huggingface.co/olafuraron/all-MiniLM-L6-v2-q8/resolve/main/all-MiniLM-L6-v2-q8.kjq";
|
|
39
|
+
const model = WasmModel.from_quantized(new Uint8Array(await (await fetch(url)).arrayBuffer()));
|
|
40
|
+
|
|
41
|
+
const texts = [
|
|
42
|
+
"How do I get my money back?",
|
|
43
|
+
"What is your refund policy?",
|
|
44
|
+
"The weather in Reykjavik is unpredictable.",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
// Normalised, so a dot product is the cosine similarity.
|
|
48
|
+
const flat = model.encode(texts, true);
|
|
49
|
+
const dim = flat.length / texts.length;
|
|
50
|
+
const vec = (i) => flat.subarray(i * dim, (i + 1) * dim);
|
|
51
|
+
const cosine = (a, b) => a.reduce((sum, x, i) => sum + x * b[i], 0);
|
|
52
|
+
|
|
53
|
+
out.textContent = `related: ${cosine(vec(0), vec(1)).toFixed(4)}\n`
|
|
54
|
+
+ `unrelated: ${cosine(vec(0), vec(2)).toFixed(4)}`;
|
|
55
|
+
</script>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
related: 0.5268
|
|
60
|
+
unrelated: -0.0470
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This imports the wasm module directly rather than the `Kjarni` wrapper, so it runs
|
|
64
|
+
on the main thread with no Worker: fine for a page that encodes a few sentences,
|
|
65
|
+
not for chat. Use `Kjarni` for anything larger, and see the Chat section.
|
|
66
|
+
|
|
67
|
+
Scores differ slightly from the native library (0.5510 / -0.0630) because the
|
|
68
|
+
browser model is int8-quantised. The ranking is the same, which is what a search
|
|
69
|
+
uses.
|
|
70
|
+
|
|
18
71
|
## Quick tour
|
|
19
72
|
|
|
20
73
|
```ts
|
package/package.json
CHANGED
package/pkg/kjarni_wasm.d.ts
CHANGED
|
@@ -248,8 +248,8 @@ export interface InitOutput {
|
|
|
248
248
|
readonly wasmsearch_add_chunk: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
|
|
249
249
|
readonly __wbg_wasmreranker_free: (a: number, b: number) => void;
|
|
250
250
|
readonly __wbg_wasmsearch_free: (a: number, b: number) => void;
|
|
251
|
-
readonly
|
|
252
|
-
readonly
|
|
251
|
+
readonly __wasm_bindgen_func_elem_5494: (a: number, b: number, c: number, d: number) => void;
|
|
252
|
+
readonly __wasm_bindgen_func_elem_5510: (a: number, b: number, c: number, d: number) => void;
|
|
253
253
|
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
254
254
|
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
255
255
|
readonly __wbindgen_export3: (a: number) => void;
|
package/pkg/kjarni_wasm.js
CHANGED
|
@@ -1092,7 +1092,7 @@ function __wbg_get_imports() {
|
|
|
1092
1092
|
const a = state0.a;
|
|
1093
1093
|
state0.a = 0;
|
|
1094
1094
|
try {
|
|
1095
|
-
return
|
|
1095
|
+
return __wasm_bindgen_func_elem_5510(a, state0.b, arg0, arg1);
|
|
1096
1096
|
} finally {
|
|
1097
1097
|
state0.a = a;
|
|
1098
1098
|
}
|
|
@@ -1206,8 +1206,8 @@ function __wbg_get_imports() {
|
|
|
1206
1206
|
return addHeapObject(ret);
|
|
1207
1207
|
},
|
|
1208
1208
|
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
1209
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
1210
|
-
const ret = makeMutClosure(arg0, arg1,
|
|
1209
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 2058, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
1210
|
+
const ret = makeMutClosure(arg0, arg1, __wasm_bindgen_func_elem_5494);
|
|
1211
1211
|
return addHeapObject(ret);
|
|
1212
1212
|
},
|
|
1213
1213
|
__wbindgen_cast_0000000000000002: function(arg0) {
|
|
@@ -1244,10 +1244,10 @@ function __wbg_get_imports() {
|
|
|
1244
1244
|
};
|
|
1245
1245
|
}
|
|
1246
1246
|
|
|
1247
|
-
function
|
|
1247
|
+
function __wasm_bindgen_func_elem_5494(arg0, arg1, arg2) {
|
|
1248
1248
|
try {
|
|
1249
1249
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
1250
|
-
wasm.
|
|
1250
|
+
wasm.__wasm_bindgen_func_elem_5494(retptr, arg0, arg1, addHeapObject(arg2));
|
|
1251
1251
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
1252
1252
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
1253
1253
|
if (r1) {
|
|
@@ -1258,8 +1258,8 @@ function __wasm_bindgen_func_elem_5449(arg0, arg1, arg2) {
|
|
|
1258
1258
|
}
|
|
1259
1259
|
}
|
|
1260
1260
|
|
|
1261
|
-
function
|
|
1262
|
-
wasm.
|
|
1261
|
+
function __wasm_bindgen_func_elem_5510(arg0, arg1, arg2, arg3) {
|
|
1262
|
+
wasm.__wasm_bindgen_func_elem_5510(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
|
|
1263
1263
|
}
|
|
1264
1264
|
|
|
1265
1265
|
const WasmChatFinalization = (typeof FinalizationRegistry === 'undefined')
|
package/pkg/kjarni_wasm_bg.wasm
CHANGED
|
Binary file
|
|
@@ -45,8 +45,8 @@ export const wasmsearch_doc_count: (a: number) => number;
|
|
|
45
45
|
export const wasmsearch_add_chunk: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
|
|
46
46
|
export const __wbg_wasmreranker_free: (a: number, b: number) => void;
|
|
47
47
|
export const __wbg_wasmsearch_free: (a: number, b: number) => void;
|
|
48
|
-
export const
|
|
49
|
-
export const
|
|
48
|
+
export const __wasm_bindgen_func_elem_5494: (a: number, b: number, c: number, d: number) => void;
|
|
49
|
+
export const __wasm_bindgen_func_elem_5510: (a: number, b: number, c: number, d: number) => void;
|
|
50
50
|
export const __wbindgen_export: (a: number, b: number) => number;
|
|
51
51
|
export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
52
52
|
export const __wbindgen_export3: (a: number) => void;
|