Publish browser pgvector classroom demo
Browse files- README.md +16 -6
- app.js +154 -0
- data.json +0 -0
- engine-worker.js +141 -0
- index.html +57 -17
- style.css +75 -22
README.md
CHANGED
|
@@ -1,10 +1,20 @@
|
|
| 1 |
---
|
| 2 |
-
title: Chat
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
-
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Chat pgvector Cloud Services
|
| 3 |
+
emoji: 🔎
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
+
short_description: Praktikum pencarian semantik dengan pgvector
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Chat pgvector untuk Cloud Services
|
| 12 |
+
|
| 13 |
+
1. Klik **Pencarian makna** untuk menjalankan pertanyaan contoh.
|
| 14 |
+
2. Bandingkan tiga **Sumber terdekat**. Klik salah satu judul untuk membaca isi catatan.
|
| 15 |
+
3. Kirim pertanyaan sendiri. Browser mengunduh model MiniLM sekitar 120 MB ketika pertama kali diperlukan.
|
| 16 |
+
4. Buka **Lihat SQL dan cara kerjanya** untuk memeriksa query dan versi ekstensi.
|
| 17 |
+
|
| 18 |
+
Demo memuat 16 catatan latihan fiktif sebagai tabel PostgreSQL di PGlite. pgvector mengurutkan cosine distance secara langsung. Model MiniLM multibahasa membuat embedding pertanyaan bebas di browser. Pertanyaan contoh memakai embedding yang telah dihitung dengan model yang sama. Chat mengutip catatan teratas berdasarkan ambang jarak latihan.
|
| 19 |
+
|
| 20 |
+
[Model MiniLM multibahasa](https://huggingface.co/Xenova/paraphrase-multilingual-MiniLM-L12-v2) · [PGlite dan pgvector](https://pglite.dev/extensions/#pgvector)
|
app.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const input = document.querySelector('#question');
|
| 2 |
+
const form = document.querySelector('#question-form');
|
| 3 |
+
const send = document.querySelector('#send');
|
| 4 |
+
const examples = document.querySelector('#examples');
|
| 5 |
+
const conversation = document.querySelector('#conversation');
|
| 6 |
+
const results = document.querySelector('#results');
|
| 7 |
+
const rankingEmpty = document.querySelector('#ranking-empty');
|
| 8 |
+
const status = document.querySelector('#status');
|
| 9 |
+
const databaseInfo = document.querySelector('#database-info');
|
| 10 |
+
const score = new Intl.NumberFormat('id-ID', { minimumFractionDigits: 4, maximumFractionDigits: 4 });
|
| 11 |
+
const threshold = 0.68;
|
| 12 |
+
const pending = new Map();
|
| 13 |
+
const worker = new Worker(new URL('./engine-worker.js', import.meta.url), { type: 'module' });
|
| 14 |
+
let nextId = 0;
|
| 15 |
+
let ready = false;
|
| 16 |
+
let busy = false;
|
| 17 |
+
let workerFailed = false;
|
| 18 |
+
|
| 19 |
+
function setStatus(message, isError = false) {
|
| 20 |
+
status.textContent = message;
|
| 21 |
+
status.classList.toggle('error', isError);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function updateControls() {
|
| 25 |
+
send.disabled = !ready || busy || workerFailed;
|
| 26 |
+
for (const button of examples.querySelectorAll('button')) {
|
| 27 |
+
button.disabled = send.disabled;
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
worker.addEventListener('message', ({ data: event }) => {
|
| 32 |
+
const job = pending.get(event.id);
|
| 33 |
+
if (!job) return;
|
| 34 |
+
if (event.type === 'progress') {
|
| 35 |
+
setStatus(event.message);
|
| 36 |
+
return;
|
| 37 |
+
}
|
| 38 |
+
pending.delete(event.id);
|
| 39 |
+
if (event.type === 'error') job.reject(new Error(event.message));
|
| 40 |
+
else job.resolve(event.data);
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
worker.addEventListener('error', () => {
|
| 44 |
+
workerFailed = true;
|
| 45 |
+
const error = new Error('Mesin pencarian gagal dibuka. Muat ulang halaman untuk mencoba lagi.');
|
| 46 |
+
for (const job of pending.values()) job.reject(error);
|
| 47 |
+
pending.clear();
|
| 48 |
+
updateControls();
|
| 49 |
+
setStatus(error.message, true);
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
function request(type, query) {
|
| 53 |
+
const id = ++nextId;
|
| 54 |
+
return new Promise((resolve, reject) => {
|
| 55 |
+
pending.set(id, { resolve, reject });
|
| 56 |
+
worker.postMessage({ id, type, query });
|
| 57 |
+
});
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
function element(tag, text, className) {
|
| 61 |
+
const node = document.createElement(tag);
|
| 62 |
+
if (text) node.textContent = text;
|
| 63 |
+
if (className) node.className = className;
|
| 64 |
+
return node;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
function addTurn(query, top) {
|
| 68 |
+
conversation.querySelector('.empty')?.remove();
|
| 69 |
+
const turn = element('article', '', 'turn');
|
| 70 |
+
const question = element('div', '', 'message user-message');
|
| 71 |
+
question.append(element('strong', 'Kamu'), element('p', query));
|
| 72 |
+
const answer = element('div', '', 'message answer');
|
| 73 |
+
if (top && top.distance <= threshold) {
|
| 74 |
+
answer.append(
|
| 75 |
+
element('strong', top.title),
|
| 76 |
+
element('p', top.body),
|
| 77 |
+
element('p', `Sumber ${top.id} · Jarak ${score.format(top.distance)}`, 'source-id'),
|
| 78 |
+
);
|
| 79 |
+
} else {
|
| 80 |
+
answer.append(
|
| 81 |
+
element('strong', 'Belum menemukan catatan yang cukup dekat'),
|
| 82 |
+
element('p', 'Coba tanyakan tentang pgvector, Supabase, atau Hugging Face. Kamu tetap bisa memeriksa catatan yang jaraknya paling kecil.'),
|
| 83 |
+
);
|
| 84 |
+
}
|
| 85 |
+
turn.append(question, answer);
|
| 86 |
+
conversation.append(turn);
|
| 87 |
+
conversation.scrollTop = conversation.scrollHeight;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
function renderRanking(rows) {
|
| 91 |
+
results.replaceChildren();
|
| 92 |
+
rankingEmpty.hidden = rows.length > 0;
|
| 93 |
+
for (const row of rows) {
|
| 94 |
+
const item = element('li');
|
| 95 |
+
const details = element('details');
|
| 96 |
+
const summary = element('summary', row.title);
|
| 97 |
+
const metadata = element('p', '', 'source-metadata');
|
| 98 |
+
metadata.append(
|
| 99 |
+
element('span', `${row.id} · ${row.topic}`),
|
| 100 |
+
element('span', `Jarak ${score.format(row.distance)}`, 'distance'),
|
| 101 |
+
);
|
| 102 |
+
details.append(summary, metadata, element('p', row.body, 'source-body'));
|
| 103 |
+
item.append(details);
|
| 104 |
+
results.append(item);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
async function submit(question) {
|
| 109 |
+
const query = question.trim();
|
| 110 |
+
if (!ready || busy || workerFailed || !query || query.length > 600) return;
|
| 111 |
+
busy = true;
|
| 112 |
+
updateControls();
|
| 113 |
+
setStatus('Mencari catatan yang paling dekat…');
|
| 114 |
+
try {
|
| 115 |
+
const { rows, preset } = await request('search', query);
|
| 116 |
+
addTurn(query, rows[0]);
|
| 117 |
+
renderRanking(rows);
|
| 118 |
+
if (input.value.trim() === query) input.value = '';
|
| 119 |
+
setStatus(preset ? 'Pencarian selesai dengan embedding contoh.' : 'Pencarian selesai dengan embedding dari MiniLM.');
|
| 120 |
+
} catch (error) {
|
| 121 |
+
setStatus(error.message, true);
|
| 122 |
+
} finally {
|
| 123 |
+
busy = false;
|
| 124 |
+
updateControls();
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
form.addEventListener('submit', event => {
|
| 129 |
+
event.preventDefault();
|
| 130 |
+
void submit(input.value);
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
try {
|
| 134 |
+
const [{ extversion, count }, data] = await Promise.all([
|
| 135 |
+
request('init'),
|
| 136 |
+
fetch(new URL('./data.json', import.meta.url)).then(response => {
|
| 137 |
+
if (!response.ok) throw new Error('Catatan latihan gagal dimuat.');
|
| 138 |
+
return response.json();
|
| 139 |
+
}),
|
| 140 |
+
]);
|
| 141 |
+
for (const { label, query } of data.presets) {
|
| 142 |
+
const button = element('button', label, 'example');
|
| 143 |
+
button.type = 'button';
|
| 144 |
+
button.title = query;
|
| 145 |
+
button.addEventListener('click', () => void submit(query));
|
| 146 |
+
examples.append(button);
|
| 147 |
+
}
|
| 148 |
+
databaseInfo.textContent = `pgvector ${extversion} · ${count} catatan latihan fiktif.`;
|
| 149 |
+
ready = true;
|
| 150 |
+
updateControls();
|
| 151 |
+
setStatus('Siap mencari.');
|
| 152 |
+
} catch (error) {
|
| 153 |
+
setStatus(`${error.message} Muat ulang halaman jika perlu.`, true);
|
| 154 |
+
}
|
data.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
engine-worker.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const dimensions = 384;
|
| 2 |
+
const model = 'Xenova/paraphrase-multilingual-MiniLM-L12-v2';
|
| 3 |
+
const modelRevision = '2c4055b12046f11709e9df2c122e59ffbdc2f900';
|
| 4 |
+
const pgliteUrl = 'https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.3.15/dist/';
|
| 5 |
+
const transformersUrl = 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.3.2/dist/transformers.web.min.js';
|
| 6 |
+
|
| 7 |
+
let databasePromise;
|
| 8 |
+
let extractorPromise;
|
| 9 |
+
const downloadListeners = new Set();
|
| 10 |
+
|
| 11 |
+
function vectorLiteral(values) {
|
| 12 |
+
if (!Array.isArray(values) || values.length !== dimensions
|
| 13 |
+
|| !values.every(value => Number.isFinite(Math.fround(value)) && typeof value === 'number')
|
| 14 |
+
|| !values.some(value => Math.fround(value) !== 0)) {
|
| 15 |
+
throw new Error('Embedding harus berisi 384 angka hingga dan memiliki setidaknya satu angka bukan nol.');
|
| 16 |
+
}
|
| 17 |
+
return JSON.stringify(values);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
async function openDatabase() {
|
| 21 |
+
const [response, { PGlite }, { vector }] = await Promise.all([
|
| 22 |
+
fetch(new URL('./data.json', import.meta.url)),
|
| 23 |
+
import(`${pgliteUrl}index.js`),
|
| 24 |
+
import(`${pgliteUrl}vector/index.js`),
|
| 25 |
+
]);
|
| 26 |
+
if (!response.ok) throw new Error(`Gagal memuat data latihan (HTTP ${response.status}).`);
|
| 27 |
+
const { documents, presets } = await response.json();
|
| 28 |
+
const database = await PGlite.create({ extensions: { vector } });
|
| 29 |
+
|
| 30 |
+
try {
|
| 31 |
+
await database.exec(`
|
| 32 |
+
CREATE EXTENSION vector;
|
| 33 |
+
CREATE TABLE documents (
|
| 34 |
+
id text PRIMARY KEY,
|
| 35 |
+
title text NOT NULL,
|
| 36 |
+
body text NOT NULL,
|
| 37 |
+
topic text NOT NULL,
|
| 38 |
+
embedding vector(384) NOT NULL
|
| 39 |
+
);
|
| 40 |
+
`);
|
| 41 |
+
await database.transaction(async tx => {
|
| 42 |
+
for (const { id, title, body, topic, embedding } of documents) {
|
| 43 |
+
await tx.query(
|
| 44 |
+
'INSERT INTO documents (id, title, body, topic, embedding) VALUES ($1, $2, $3, $4, $5::vector)',
|
| 45 |
+
[id, title, body, topic, vectorLiteral(embedding)],
|
| 46 |
+
);
|
| 47 |
+
}
|
| 48 |
+
});
|
| 49 |
+
const { rows: [{ extversion }] } = await database.query(
|
| 50 |
+
'SELECT extversion FROM pg_extension WHERE extname = $1',
|
| 51 |
+
['vector'],
|
| 52 |
+
);
|
| 53 |
+
return {
|
| 54 |
+
database,
|
| 55 |
+
extversion,
|
| 56 |
+
count: documents.length,
|
| 57 |
+
presets: new Map(presets.map(({ query, embedding }) => [query.trim(), embedding])),
|
| 58 |
+
};
|
| 59 |
+
} catch (error) {
|
| 60 |
+
await database.close();
|
| 61 |
+
throw error;
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function getDatabase() {
|
| 66 |
+
databasePromise ??= openDatabase().catch(error => {
|
| 67 |
+
databasePromise = undefined;
|
| 68 |
+
throw error;
|
| 69 |
+
});
|
| 70 |
+
return databasePromise;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
async function loadExtractor() {
|
| 74 |
+
const { env, pipeline } = await import(transformersUrl);
|
| 75 |
+
env.allowLocalModels = false;
|
| 76 |
+
env.useBrowserCache = true;
|
| 77 |
+
env.backends.onnx.wasm.numThreads = 1;
|
| 78 |
+
env.backends.onnx.wasm.proxy = false;
|
| 79 |
+
|
| 80 |
+
let lastReported = -1;
|
| 81 |
+
return pipeline('feature-extraction', model, {
|
| 82 |
+
revision: modelRevision,
|
| 83 |
+
device: 'wasm',
|
| 84 |
+
dtype: 'q8',
|
| 85 |
+
progress_callback(event) {
|
| 86 |
+
if (event.status !== 'progress' || event.file !== 'onnx/model_quantized.onnx') return;
|
| 87 |
+
const percent = Math.floor(event.progress);
|
| 88 |
+
if (percent < lastReported + 5 && percent !== 100) return;
|
| 89 |
+
lastReported = percent;
|
| 90 |
+
for (const notify of downloadListeners) notify(`Mengunduh model embedding… ${percent}%`);
|
| 91 |
+
},
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
async function embed(query, notify) {
|
| 96 |
+
notify('Menyiapkan model embedding. Unduhan pertama sekitar 118 MB.');
|
| 97 |
+
downloadListeners.add(notify);
|
| 98 |
+
try {
|
| 99 |
+
extractorPromise ??= loadExtractor().catch(error => {
|
| 100 |
+
extractorPromise = undefined;
|
| 101 |
+
throw error;
|
| 102 |
+
});
|
| 103 |
+
const extractor = await extractorPromise;
|
| 104 |
+
notify('Mengubah pertanyaan menjadi embedding…');
|
| 105 |
+
const vector = await extractor(query, { pooling: 'mean', normalize: true });
|
| 106 |
+
return vector.tolist()[0];
|
| 107 |
+
} finally {
|
| 108 |
+
downloadListeners.delete(notify);
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
async function search({ database, presets }, query, notify) {
|
| 113 |
+
if (typeof query !== 'string' || !query.trim() || query.length > 600) {
|
| 114 |
+
throw new Error('Masukkan pertanyaan sepanjang 1–600 karakter.');
|
| 115 |
+
}
|
| 116 |
+
const question = query.trim();
|
| 117 |
+
const preset = presets.has(question);
|
| 118 |
+
const embedding = preset ? presets.get(question) : await embed(question, notify);
|
| 119 |
+
const { rows } = await database.query(`
|
| 120 |
+
SELECT id, title, body, topic, embedding <=> $1::vector AS distance
|
| 121 |
+
FROM documents
|
| 122 |
+
ORDER BY distance, id
|
| 123 |
+
LIMIT $2::integer
|
| 124 |
+
`, [vectorLiteral(embedding), 3]);
|
| 125 |
+
return { rows, preset };
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
self.addEventListener('message', async ({ data: { id, type, query } }) => {
|
| 129 |
+
const notify = message => self.postMessage({ id, type: 'progress', message });
|
| 130 |
+
try {
|
| 131 |
+
if (type !== 'init' && type !== 'search') throw new Error('Perintah tidak dikenal.');
|
| 132 |
+
if (type === 'init') notify('Menyiapkan PostgreSQL dan pgvector…');
|
| 133 |
+
const state = await getDatabase();
|
| 134 |
+
const data = type === 'init'
|
| 135 |
+
? { extversion: state.extversion, count: state.count }
|
| 136 |
+
: await search(state, query, notify);
|
| 137 |
+
self.postMessage({ id, type: 'done', data });
|
| 138 |
+
} catch (error) {
|
| 139 |
+
self.postMessage({ id, type: 'error', message: error.message });
|
| 140 |
+
}
|
| 141 |
+
});
|
index.html
CHANGED
|
@@ -1,19 +1,59 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="id">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<meta name="description" content="Praktikum Cloud Services untuk menguji pencarian semantik dengan MiniLM dan pgvector.">
|
| 7 |
+
<meta name="theme-color" content="#112941">
|
| 8 |
+
<link rel="icon" href="data:,">
|
| 9 |
+
<link rel="stylesheet" href="./style.css">
|
| 10 |
+
<title>Chat pgvector · Praktikum Cloud Services</title>
|
| 11 |
+
</head>
|
| 12 |
+
<body>
|
| 13 |
+
<header class="masthead">
|
| 14 |
+
<div class="header-inner">
|
| 15 |
+
<p class="course">Praktikum Cloud Services</p>
|
| 16 |
+
<h1>Chat <span>pgvector</span></h1>
|
| 17 |
+
<p class="subtitle">Coba pencarian semantik pada 16 catatan latihan tentang pgvector, Supabase, dan Hugging Face.</p>
|
| 18 |
+
</div>
|
| 19 |
+
</header>
|
| 20 |
+
<main>
|
| 21 |
+
<div class="lab">
|
| 22 |
+
<section class="panel" aria-labelledby="chat-title">
|
| 23 |
+
<h2 id="chat-title">Ajukan pertanyaan</h2>
|
| 24 |
+
<p class="intro">Klik contoh untuk mulai, lalu coba kalimatmu sendiri.</p>
|
| 25 |
+
<div id="examples" class="examples" role="group" aria-label="Contoh pertanyaan"></div>
|
| 26 |
+
<div id="conversation" class="conversation" role="log" aria-label="Riwayat pertanyaan dan jawaban" aria-relevant="additions">
|
| 27 |
+
<p class="empty">Kutipan dari catatan terdekat akan muncul di sini.</p>
|
| 28 |
+
</div>
|
| 29 |
+
<form id="question-form">
|
| 30 |
+
<label for="question">Pertanyaanmu</label>
|
| 31 |
+
<div class="question-input">
|
| 32 |
+
<input id="question" name="question" type="text" required maxlength="600" autocapitalize="sentences" autocomplete="off" placeholder="Tulis pertanyaan dalam bahasa Indonesia">
|
| 33 |
+
<button type="submit" id="send" disabled>Cari</button>
|
| 34 |
+
</div>
|
| 35 |
+
</form>
|
| 36 |
+
<p id="status" class="status" role="status">Menyiapkan pgvector…</p>
|
| 37 |
+
<p class="note">Pertanyaan bebas perlu unduhan model sekitar 120 MB pada penggunaan pertama. Tombol contoh sudah memiliki embedding siap pakai.</p>
|
| 38 |
+
</section>
|
| 39 |
+
<aside class="panel results-panel" aria-labelledby="ranking-title">
|
| 40 |
+
<h2 id="ranking-title">Sumber terdekat</h2>
|
| 41 |
+
<p class="intro">Angka yang lebih kecil berarti arah vektornya lebih mirip.</p>
|
| 42 |
+
<ol id="results" class="ranking" aria-label="Tiga catatan dengan jarak kosinus terkecil"></ol>
|
| 43 |
+
<p id="ranking-empty" class="empty">Tiga catatan akan diurutkan setelah pencarian pertama.</p>
|
| 44 |
+
</aside>
|
| 45 |
+
</div>
|
| 46 |
+
<details class="sql">
|
| 47 |
+
<summary>Lihat SQL dan cara kerjanya</summary>
|
| 48 |
+
<p>MiniLM mengubah pertanyaan menjadi vektor 384 dimensi. PostgreSQL melalui PGlite menjalankan pgvector di browser. Operator <code><=></code> menghitung jarak kosinus, lalu query mengurutkan dan mengambil tiga hasil pertama.</p>
|
| 49 |
+
<pre><code>SELECT id, title, body, topic, embedding <=> $1::vector AS distance
|
| 50 |
+
FROM documents
|
| 51 |
+
ORDER BY distance, id
|
| 52 |
+
LIMIT $2::integer;</code></pre>
|
| 53 |
+
<p>Chat mengutip catatan teratas jika jaraknya ≤ 0,68. Angka ini ambang latihan. Buka sumber dan baca isinya untuk memastikan jawabannya sesuai.</p>
|
| 54 |
+
<p id="database-info"></p>
|
| 55 |
+
</details>
|
| 56 |
+
</main>
|
| 57 |
+
<script type="module" src="./app.js"></script>
|
| 58 |
+
</body>
|
| 59 |
</html>
|
style.css
CHANGED
|
@@ -1,28 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
margin-top: 5px;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
.card {
|
| 19 |
-
max-width: 620px;
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
|
|
|
| 1 |
+
:root {
|
| 2 |
+
color-scheme: light;
|
| 3 |
+
font-family: "Segoe UI", system-ui, sans-serif;
|
| 4 |
+
color: #162740;
|
| 5 |
+
background: #eef3f8;
|
| 6 |
+
--navy: #112941;
|
| 7 |
+
--blue: #075b9b;
|
| 8 |
+
--muted: #455b72;
|
| 9 |
+
--border: #d6e0ea;
|
| 10 |
}
|
| 11 |
|
| 12 |
+
* { box-sizing: border-box; }
|
| 13 |
+
body { margin: 0; }
|
| 14 |
+
.masthead { background: var(--navy); color: #fff; border-bottom: 4px solid #68bbef; }
|
| 15 |
+
.header-inner, main { width: min(1100px, calc(100% - 3rem)); margin-inline: auto; }
|
| 16 |
+
.header-inner { padding: 2rem 0 2.7rem; }
|
| 17 |
+
.course { margin: 0; font-size: .83rem; letter-spacing: .07em; color: #a6d9fa; font-weight: 650; }
|
| 18 |
+
h1 { margin: .2rem 0 .45rem; font-size: clamp(2.65rem, 6vw, 4.4rem); line-height: 1.07; letter-spacing: -.045em; }
|
| 19 |
+
h1 span { color: #91d0fa; }
|
| 20 |
+
.subtitle { color: #e6f2ff; max-width: 52ch; line-height: 1.5; margin: 0; }
|
| 21 |
+
main { padding-block: 1.45rem 2.3rem; }
|
| 22 |
+
.lab { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 1.25rem; align-items: start; }
|
| 23 |
+
.panel, .sql { background: #fff; border: 1px solid var(--border); border-radius: 14px; }
|
| 24 |
+
.panel { padding: 1.3rem; }
|
| 25 |
+
h2 { font-size: 1.26rem; margin: 0 0 .35rem; }
|
| 26 |
+
.intro, .empty, .note { color: var(--muted); line-height: 1.5; }
|
| 27 |
+
.intro { margin: 0 0 1rem; font-size: .92rem; }
|
| 28 |
+
.examples { display: flex; flex-wrap: wrap; gap: .45rem; margin-bottom: .9rem; }
|
| 29 |
+
button, input { font: inherit; }
|
| 30 |
+
button { cursor: pointer; }
|
| 31 |
+
button:disabled { opacity: .6; cursor: wait; }
|
| 32 |
+
button:focus-visible, input:focus-visible, summary:focus-visible { outline: 3px solid #2095d5; outline-offset: 2px; }
|
| 33 |
+
.example {
|
| 34 |
+
background: #f0f7fe; color: #114972; border: 1px solid #c4d9eb;
|
| 35 |
+
border-radius: 20px; padding: .43rem .72rem; font-size: .82rem; font-weight: 620;
|
| 36 |
}
|
| 37 |
+
.example:hover:enabled { background: #dbeefa; }
|
| 38 |
+
.conversation {
|
| 39 |
+
display: flex; flex-direction: column; gap: .6rem; min-height: 8.6rem; max-height: 20rem;
|
| 40 |
+
overflow: auto; padding: .85rem; background: #f7fafd; border: 1px solid var(--border);
|
| 41 |
+
border-radius: 10px; overflow-wrap: anywhere;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
+
.empty { margin: .15rem 0; font-size: .9rem; }
|
| 44 |
+
.turn { display: flow-root; }
|
| 45 |
+
.message { max-width: 95%; border-radius: 9px; padding: .64rem .75rem; font-size: .91rem; line-height: 1.5; }
|
| 46 |
+
.message + .message { margin-top: .53rem; }
|
| 47 |
+
.message p { margin: .2rem 0 0; }
|
| 48 |
+
.message strong { display: block; font-size: .81rem; color: #154c75; }
|
| 49 |
+
.user-message { margin-left: auto; width: fit-content; background: #dceefd; }
|
| 50 |
+
.answer { background: #fff; border: 1px solid #d8e4ef; }
|
| 51 |
+
.answer p.source-id { margin-top: .55rem; font-size: .79rem; color: var(--muted); }
|
| 52 |
+
form { margin-top: .85rem; }
|
| 53 |
+
label { display: block; margin-bottom: .35rem; font-size: .85rem; font-weight: 650; }
|
| 54 |
+
.question-input { display: flex; gap: .5rem; }
|
| 55 |
+
input { min-width: 0; flex: 1; padding: .66rem .7rem; border: 1px solid #a9bdcf; border-radius: 8px; }
|
| 56 |
+
#send { background: var(--blue); border: 1px solid var(--blue); color: #fff; padding: .55rem 1rem; border-radius: 8px; font-weight: 700; }
|
| 57 |
+
#send:hover:enabled { background: #064476; }
|
| 58 |
+
.status { color: #164973; font-size: .84rem; font-weight: 650; margin: .65rem 0 .25rem; }
|
| 59 |
+
.status.error { color: #982f12; }
|
| 60 |
+
.note { font-size: .8rem; margin: 0; }
|
| 61 |
+
.ranking { list-style: none; padding: 0; margin: 0; }
|
| 62 |
+
.ranking li + li { margin-top: .65rem; }
|
| 63 |
+
.ranking details { border: 1px solid var(--border); border-radius: 9px; padding: .68rem .78rem; background: #f7fafd; }
|
| 64 |
+
.ranking li:first-child details { border-color: #84b8df; background: #edf6fe; }
|
| 65 |
+
.ranking summary { cursor: pointer; }
|
| 66 |
+
.source-metadata { display: flex; gap: .65rem; justify-content: space-between; margin: .36rem 0 0 1rem; font-size: .78rem; color: var(--muted); }
|
| 67 |
+
.distance { font-variant-numeric: tabular-nums; font-weight: 720; color: #0d4e82; }
|
| 68 |
+
.source-body { margin: .63rem 0 .1rem; font-size: .85rem; line-height: 1.53; }
|
| 69 |
+
.sql { margin-top: 1.25rem; padding: 1rem 1.2rem; }
|
| 70 |
+
.sql summary { font-weight: 680; cursor: pointer; }
|
| 71 |
+
.sql p { color: var(--muted); line-height: 1.53; max-width: 86ch; }
|
| 72 |
+
.sql pre { background: var(--navy); color: #e8f1fa; padding: .85rem; border-radius: 8px; overflow: auto; font-size: .84rem; }
|
| 73 |
|
| 74 |
+
@media (max-width: 720px) {
|
| 75 |
+
.header-inner, main { width: calc(100% - 1.7rem); }
|
| 76 |
+
.header-inner { padding: 1.35rem 0 1.65rem; }
|
| 77 |
+
main { padding-block: 1rem 1.7rem; }
|
| 78 |
+
.lab { grid-template-columns: minmax(0, 1fr); gap: .9rem; }
|
| 79 |
+
.panel { padding: .95rem; }
|
| 80 |
+
.sql { margin-top: .9rem; }
|
| 81 |
}
|