Skip to content

Commit cdf777d

Browse files
committed
Allow users to change ef-search
Signed-off-by: Jay Wang <jay@zijie.wang>
1 parent 6760029 commit cdf777d

File tree

8 files changed

+130
-23
lines changed

8 files changed

+130
-23
lines changed

examples/rag-playground/public/data/ml-arxiv-papers-1000-index.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/rag-playground/public/data/ml-arxiv-papers-1000.ndjson

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/rag-playground/public/data/ml-arxiv-papers-1000.ndjson.gzip

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/rag-playground/src/components/app/app.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ export class MememoRagPlayground extends LitElement {
5959
let playground = html``;
6060

6161
switch (this.curDataset) {
62+
case Dataset.arXiv1k: {
63+
playground = html`<mememo-playground
64+
curDataset=${this.curDataset}
65+
></mememo-playground>`;
66+
break;
67+
}
68+
6269
case Dataset.arXiv10k: {
6370
playground = html`<mememo-playground
6471
curDataset=${this.curDataset}
@@ -142,6 +149,12 @@ export class MememoRagPlayground extends LitElement {
142149
<div class="app-tabs">
143150
<div class="tab">
144151
ML arXiv Abstracts
152+
<button
153+
?selected=${this.curDataset === Dataset.arXiv1k}
154+
@click=${() => this.tabButtonClicked(Dataset.arXiv1k)}
155+
>
156+
1k
157+
</button>
145158
<button
146159
?selected=${this.curDataset === Dataset.arXiv10k}
147160
@click=${() => this.tabButtonClicked(Dataset.arXiv10k)}

examples/rag-playground/src/components/playground/playground.css

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
text-align: center;
409409
position: relative;
410410
top: -10px;
411+
gap: 10px;
411412
}
412413

413414
.search-top-info {
@@ -448,3 +449,67 @@
448449
input {
449450
all: unset;
450451
}
452+
453+
.svg-icon {
454+
display: flex;
455+
justify-content: center;
456+
align-items: center;
457+
width: 1em;
458+
height: 1em;
459+
460+
color: currentColor;
461+
transition: transform 80ms linear;
462+
transform-origin: center;
463+
464+
& svg {
465+
fill: currentColor;
466+
width: 100%;
467+
height: 100%;
468+
}
469+
}
470+
471+
.button-group {
472+
display: flex;
473+
flex-flow: row;
474+
align-items: center;
475+
gap: 8px;
476+
}
477+
478+
button {
479+
all: unset;
480+
481+
display: flex;
482+
line-height: 1;
483+
padding: 4px 6px;
484+
border-radius: 5px;
485+
white-space: nowrap;
486+
487+
cursor: pointer;
488+
user-select: none;
489+
-webkit-user-select: none;
490+
491+
background-color: color-mix(in lab, var(--gray-200), white 20%);
492+
color: var(--gray-800);
493+
display: flex;
494+
flex-flow: row;
495+
align-items: center;
496+
font-size: var(--header-secondary-size);
497+
height: 1em;
498+
499+
&:hover {
500+
background-color: color-mix(in lab, var(--gray-300), white 30%);
501+
}
502+
503+
&:active {
504+
background-color: color-mix(in lab, var(--gray-300), white 20%);
505+
}
506+
507+
.svg-icon {
508+
position: relative;
509+
top: 1px;
510+
margin-right: 3px;
511+
color: var(--gray-700);
512+
width: 12px;
513+
height: 12px;
514+
}
515+
}

examples/rag-playground/src/components/playground/playground.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import '../text-viewer/text-viewer';
3131
import componentCSS from './playground.css?inline';
3232
import TextGenLocalWorkerInline from '../../llms/web-llm?worker&inline';
3333
import EmbeddingWorkerInline from '../../workers/embedding?worker';
34-
import logoIcon from '../../images/icon-logo.svg?raw';
34+
import gearIcon from '../../images/icon-gear.svg?raw';
3535

36-
const STORE_ENDPOINT = 'https://pub-4eccf317e01e4aa3a5caa9991c8b1e2a.r2.dev/';
36+
const STORE_ENDPOINT = import.meta.env.DEV
37+
? '/data/'
38+
: 'https://pub-4eccf317e01e4aa3a5caa9991c8b1e2a.r2.dev/';
3739

3840
interface DatasetInfo {
3941
dataURL: string;
@@ -43,6 +45,7 @@ interface DatasetInfo {
4345
}
4446

4547
export enum Dataset {
48+
arXiv1k = 'arxiv-1k',
4649
arXiv10k = 'arxiv-10k',
4750
arXiv120k = 'arxiv-120k',
4851
DiffusionDB10k = 'diffusiondb-10k',
@@ -62,6 +65,13 @@ enum Arrow {
6265
const promptTemplate = promptTemplates as Record<Dataset, string>;
6366

6467
const datasets: Record<Dataset, DatasetInfo> = {
68+
[Dataset.arXiv1k]: {
69+
indexURL: STORE_ENDPOINT + 'ml-arxiv-papers-index-1k.json.gzip',
70+
dataURL: STORE_ENDPOINT + 'ml-arxiv-papers-1k.ndjson.gzip',
71+
datasetName: 'ml-arxiv-papers-1k',
72+
datasetNameDisplay: 'ML arXiv Abstracts (1k)'
73+
},
74+
6575
[Dataset.arXiv10k]: {
6676
indexURL: STORE_ENDPOINT + 'ml-arxiv-papers-index-10k.json.gzip',
6777
dataURL: STORE_ENDPOINT + 'ml-arxiv-papers-10k.ndjson.gzip',
@@ -147,7 +157,7 @@ export class MememoPlayground extends LitElement {
147157
topK = 10;
148158

149159
@state()
150-
maxDistance = 0.25;
160+
efSearch = 100;
151161

152162
@query('mememo-text-viewer')
153163
textViewerComponent: MememoTextViewer | undefined | null;
@@ -280,7 +290,12 @@ export class MememoPlayground extends LitElement {
280290
// Loading the arrow
281291
this.arrowElements[Arrow.Search]?.classList.add('running');
282292

283-
this.textViewerComponent.semanticSearch(embedding, this.topK, 0.5);
293+
this.textViewerComponent.semanticSearch(
294+
embedding,
295+
this.topK,
296+
0.5,
297+
this.efSearch
298+
);
284299
}
285300

286301
//==========================================================================||
@@ -346,17 +361,17 @@ export class MememoPlayground extends LitElement {
346361
* @param e Input event
347362
* @param parameter Type of the parameter
348363
*/
349-
parameterInputChanged(e: InputEvent, parameter: 'distance' | 'top-k') {
364+
parameterInputChanged(e: InputEvent, parameter: 'efSearch' | 'top-k') {
350365
const element = e.currentTarget as HTMLInputElement;
351366

352-
if (parameter === 'distance') {
353-
const value = parseFloat(element.value);
354-
if (value > 0 && value < 1) {
355-
this.maxDistance = value;
367+
if (parameter === 'efSearch') {
368+
const value = parseInt(element.value);
369+
if (value > 0) {
370+
this.efSearch = value;
356371
} else {
357-
this.maxDistance = 0.25;
372+
this.efSearch = 100;
358373
}
359-
element.value = String(this.maxDistance);
374+
element.value = String(this.efSearch);
360375
}
361376

362377
if (parameter === 'top-k') {
@@ -574,13 +589,13 @@ export class MememoPlayground extends LitElement {
574589
<div class="search-box">
575590
<div class="search-top-info">
576591
<span class="row"
577-
>distance &lt;
592+
>ef-search =
578593
<input
579-
id="input-distance"
594+
id="input-efSearch"
580595
type="text"
581-
value="0.25"
596+
value="100"
582597
@change=${(e: InputEvent) =>
583-
this.parameterInputChanged(e, 'distance')}
598+
this.parameterInputChanged(e, 'efSearch')}
584599
/></span>
585600
<span class="row"
586601
>top-k =
@@ -623,6 +638,12 @@ export class MememoPlayground extends LitElement {
623638
<div class="container container-model">
624639
<div class="model-box">
625640
<div class="header">GPT 3.5</div>
641+
<div class="button-group">
642+
<button>
643+
<span class="svg-icon">${unsafeHTML(gearIcon)}</span>
644+
change
645+
</button>
646+
</div>
626647
</div>
627648
</div>
628649

examples/rag-playground/src/components/text-viewer/text-viewer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,20 @@ export class MememoTextViewer extends LitElement {
147147
* @param topK Top k relevant documents to retrieve
148148
* @param maxDistance Distance threshold for relevance
149149
*/
150-
semanticSearch(embedding: number[], topK: number, maxDistance: number) {
150+
semanticSearch(
151+
embedding: number[],
152+
topK: number,
153+
maxDistance: number,
154+
efSearch: number
155+
) {
151156
const message: MememoWorkerMessage = {
152157
command: 'startSemanticSearch',
153158
payload: {
154159
embedding,
155160
requestID: this.semanticSearchRequestID,
156161
topK,
157-
maxDistance
162+
maxDistance,
163+
efSearch
158164
}
159165
};
160166
this.mememoWorker.postMessage(message);

examples/rag-playground/src/workers/mememo-worker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type MememoWorkerMessage =
7474
requestID: number;
7575
topK: number;
7676
maxDistance: number;
77+
efSearch: number;
7778
};
7879
}
7980
| {
@@ -83,6 +84,7 @@ export type MememoWorkerMessage =
8384
requestID: number;
8485
topK: number;
8586
maxDistance: number;
87+
efSearch: number;
8688
documents: string[];
8789
documentDistances: number[];
8890
};
@@ -165,8 +167,9 @@ self.onmessage = (e: MessageEvent<MememoWorkerMessage>) => {
165167
}
166168

167169
case 'startSemanticSearch': {
168-
const { requestID, embedding, topK, maxDistance } = e.data.payload;
169-
semanticSearch(embedding, topK, maxDistance, requestID).then(
170+
const { requestID, embedding, topK, efSearch, maxDistance } =
171+
e.data.payload;
172+
semanticSearch(embedding, topK, maxDistance, efSearch, requestID).then(
170173
() => {},
171174
() => {}
172175
);
@@ -381,6 +384,7 @@ const semanticSearch = async (
381384
embedding: number[],
382385
topK: number,
383386
maxDistance: number,
387+
efSearch: number,
384388
requestID: number
385389
) => {
386390
if (documentDBPromise === null) {
@@ -389,7 +393,7 @@ const semanticSearch = async (
389393
const documentDB = await documentDBPromise;
390394
await finishedLoading;
391395

392-
const { keys, distances } = await hnswIndex.query(embedding, topK);
396+
const { keys, distances } = await hnswIndex.query(embedding, topK, efSearch);
393397

394398
// Batched query documents from indexedDB
395399
const results = await documentDB.bulkGet(keys);
@@ -414,6 +418,7 @@ const semanticSearch = async (
414418
topK,
415419
requestID,
416420
maxDistance,
421+
efSearch,
417422
documents,
418423
documentDistances
419424
}

0 commit comments

Comments
 (0)