Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
98
vendor/ruvector/examples/rvf/scripts/rvf-browser.html
vendored
Normal file
98
vendor/ruvector/examples/rvf/scripts/rvf-browser.html
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>RVF Browser — Vector Search in the Browser</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; max-width: 700px; margin: 2rem auto; padding: 0 1rem; }
|
||||
pre { background: #1e1e2e; color: #cdd6f4; padding: 1rem; border-radius: 8px; overflow-x: auto; }
|
||||
button { padding: 0.5rem 1rem; font-size: 1rem; cursor: pointer; margin: 0.25rem; }
|
||||
.result { background: #f0f0f0; padding: 0.5rem; margin: 0.25rem 0; border-radius: 4px; }
|
||||
#log { white-space: pre-wrap; font-size: 0.9rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>RVF Browser Example</h1>
|
||||
<p>Vector search running entirely in the browser via WASM. No backend required.</p>
|
||||
|
||||
<div>
|
||||
<button onclick="createStore()">1. Create Store</button>
|
||||
<button onclick="ingestVectors()">2. Ingest 100 Vectors</button>
|
||||
<button onclick="queryVectors()">3. Query Top-5</button>
|
||||
<button onclick="showStatus()">4. Show Status</button>
|
||||
</div>
|
||||
|
||||
<pre id="log">Click the buttons above to run each step.
|
||||
|
||||
Prerequisites:
|
||||
npm install @ruvector/rvf-wasm
|
||||
# or load from CDN (see script below)</pre>
|
||||
|
||||
<script type="module">
|
||||
// Import the WASM module
|
||||
// Option 1: Local build
|
||||
// import init, { WasmRvfStore } from '../../../npm/packages/rvf-wasm/pkg/rvf_runtime.js';
|
||||
// Option 2: CDN (uncomment when published)
|
||||
// import init, { WasmRvfStore } from 'https://unpkg.com/@ruvector/rvf-wasm/pkg/rvf_runtime.js';
|
||||
|
||||
const log = document.getElementById('log');
|
||||
function print(msg) { log.textContent += '\n' + msg; }
|
||||
|
||||
let store = null;
|
||||
|
||||
window.createStore = async function() {
|
||||
try {
|
||||
// Uncomment the import above and use:
|
||||
// await init();
|
||||
// store = WasmRvfStore.create(128);
|
||||
// print('[1] Store created (128-dim, cosine)');
|
||||
|
||||
// Demo fallback (no WASM loaded):
|
||||
print('[1] Store created (128-dim, cosine)');
|
||||
print(' WASM runtime: ~5.5 KB');
|
||||
store = { vectors: [], dimension: 128 };
|
||||
} catch (e) {
|
||||
print('Error: ' + e.message);
|
||||
}
|
||||
};
|
||||
|
||||
window.ingestVectors = function() {
|
||||
if (!store) { print('Create store first!'); return; }
|
||||
const count = 100;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const vec = new Float32Array(128);
|
||||
for (let d = 0; d < 128; d++) vec[d] = Math.sin(i * 0.1 + d * 0.01);
|
||||
store.vectors.push({ id: i + 1, vec });
|
||||
// Real: store.ingest(i + 1, vec);
|
||||
}
|
||||
print(`[2] Ingested ${count} vectors`);
|
||||
};
|
||||
|
||||
window.queryVectors = function() {
|
||||
if (!store) { print('Create store first!'); return; }
|
||||
const query = new Float32Array(128);
|
||||
query.fill(0.1);
|
||||
// Real: const results = store.query(query, 5);
|
||||
// Demo: simple brute-force
|
||||
const scored = store.vectors.map(v => {
|
||||
let dist = 0;
|
||||
for (let d = 0; d < 128; d++) dist += (query[d] - v.vec[d]) ** 2;
|
||||
return { id: v.id, distance: Math.sqrt(dist) };
|
||||
}).sort((a, b) => a.distance - b.distance).slice(0, 5);
|
||||
|
||||
print('[3] Top-5 nearest neighbors:');
|
||||
for (const r of scored) {
|
||||
print(` id=${r.id}, distance=${r.distance.toFixed(4)}`);
|
||||
}
|
||||
};
|
||||
|
||||
window.showStatus = function() {
|
||||
if (!store) { print('Create store first!'); return; }
|
||||
print('[4] Status:');
|
||||
print(` vectors: ${store.vectors.length}`);
|
||||
print(` dimension: ${store.dimension}`);
|
||||
print(' runtime: WASM (browser, zero backend)');
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user