-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathload_and_query_sample.ts
More file actions
71 lines (57 loc) · 1.82 KB
/
load_and_query_sample.ts
File metadata and controls
71 lines (57 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Moss Browser SDK - Load & Query Sample
*
* Loads an existing index into browser memory and runs semantic searches.
*
* Requires a Moss project with at least one published index.
*/
import { MossClient } from "@moss-dev/moss-web";
const $ = (id: string) => document.getElementById(id) as HTMLInputElement;
const logEl = $("log");
const log = (msg: string) => {
logEl.textContent += msg + "\n";
};
const clear = () => {
logEl.textContent = "";
};
$("run").addEventListener("click", run);
async function run() {
clear();
const projectId = $("projectId").value.trim();
const projectKey = $("projectKey").value.trim();
const indexName = $("indexName").value.trim();
if (!projectId || !projectKey || !indexName) {
log("Please fill in all fields.");
return;
}
let client: MossClient | null = null;
try {
log("Initializing MossClient...");
client = new MossClient(projectId, projectKey);
log(`Loading index "${indexName}"...`);
await client.loadIndex(indexName);
log("Index loaded into browser memory.\n");
const queries = [
"how to return damaged item",
"return shipping label process",
"refund processing time and policy",
];
for (let i = 0; i < queries.length; i++) {
const q = queries[i];
log(`Query ${i + 1}: "${q}"`);
const results = await client.query(indexName, q, { topK: 3 });
log(` ${results.docs.length} results in ${results.timeTakenMs ?? "?"}ms`);
for (const doc of results.docs) {
const preview =
doc.text.length > 100 ? doc.text.slice(0, 100) + "..." : doc.text;
log(` ${doc.score.toFixed(3)} [${doc.id}] ${preview}`);
}
log("");
}
log("Done.");
} catch (err) {
log(`Error: ${err instanceof Error ? err.message : err}`);
} finally {
client?.dispose();
}
}