Download ui/web/src/components/chat/RagControls.svelte from localailb/assistant: direct link, hf CLI and curl.
- Browser
- Download file 6.93 kB
-
https://huggingface.co/spaces/localailb/assistant/resolve/main/ui/web/src/components/chat/RagControls.svelte
- Command line
-
hf download hf://spaces/localailb/assistant/ui/web/src/components/chat/RagControls.svelte
-
curl -L -o RagControls.svelte https://huggingface.co/spaces/localailb/assistant/resolve/main/ui/web/src/components/chat/RagControls.svelte
6.93 kB
| <script> | |
| import { onMount } from "svelte"; | |
| import { t, app, setActiveTab } from "../../lib/stores.svelte.js"; | |
| import { kbDocs, kbThemes } from "../../lib/api.js"; | |
| import "./Controls.css"; | |
| import MiniSelect from "./MiniSelect.svelte"; | |
| /** RAG-only retrieval-topic filters (multi-select): `themes` / | |
| * `subthemes` are arrays of selected names (empty = filter on all), and | |
| * `files` is the array of selected KB source filenames to restrict | |
| * retrieval to. All three are sent in the WS payload and become Chroma | |
| * `$in` filters on the metadata. */ | |
| let { themes = $bindable([]), subthemes = $bindable([]), files = $bindable([]) } = $props(); | |
| /** All KB theme names (the picker list). */ | |
| let themeOptions = $state([]); | |
| /** Subthemes under the currently selected themes β the union of each | |
| * selected theme's subthemes, or ALL subthemes when no theme is picked. */ | |
| let subthemeOptions = $state([]); | |
| /** All KB source filenames (the file picker list). */ | |
| let fileOptions = $state([]); | |
| /** Existing KB doc count + total chunks β the "files Β· chunks" notice. */ | |
| let kbStats = $state({ files: 0, chunks: 0 }); | |
| /** Load the theme list (always full) and the subtheme list scoped to the | |
| * selected themes (union when several are picked). */ | |
| async function loadThemes(selected = []) { | |
| try { | |
| const res = await kbThemes(""); | |
| if (res?.ok) { | |
| themeOptions = res.themes || []; | |
| const sel = (selected || []).filter(Boolean); | |
| if (sel.length === 0) { | |
| subthemeOptions = res.subthemes || []; | |
| } else { | |
| const results = await Promise.all(sel.map((th) => kbThemes(th))); | |
| const merged = new Set(); | |
| for (const r of results) { | |
| if (r?.ok) for (const s of r.subthemes || []) merged.add(s); | |
| } | |
| subthemeOptions = [...merged].sort(); | |
| } | |
| } | |
| } catch { | |
| // Degrade gracefully | |
| } | |
| } | |
| /** Whether a doc-table row [source, type, pages, chunks, theme, subtheme] | |
| * belongs to the active retrieval filters. A file can be multi-tagged, so | |
| * the theme/subtheme columns are comma-joined β a row matches when it | |
| * carries ANY selected theme and ANY selected subtheme (an empty | |
| * selection on an axis means "no constraint"). */ | |
| function rowMatchesFilters(row, selThemes, selSubthemes) { | |
| const rowThemes = String(row?.[4] ?? "") | |
| .split(",").map((s) => s.trim()).filter(Boolean); | |
| const rowSubthemes = String(row?.[5] ?? "") | |
| .split(",").map((s) => s.trim()).filter(Boolean); | |
| if (selThemes.length && !selThemes.some((th) => rowThemes.includes(th))) return false; | |
| if (selSubthemes.length && !selSubthemes.some((st) => rowSubthemes.includes(st))) return false; | |
| return true; | |
| } | |
| /** Fetch the KB doc table and aggregate the source filenames for the file | |
| * picker β SCOPED to the selected theme/subtheme filters (the selector | |
| * never shows files outside the current filter scope), plus the KB-wide | |
| * file count + total chunks. get_doc_table rows are [source, type, pages, | |
| * chunks, theme, subtheme], so files = row count, chunks = sum of the | |
| * row[3] column. The current themes/subthemes are passed as explicit | |
| * arguments (defaults) so the calling $effect tracks BOTH as reactive | |
| * dependencies β a read inside the async body alone wouldn't be seen. */ | |
| async function loadFiles(selThemes = themes, selSubthemes = subthemes) { | |
| try { | |
| const res = await kbDocs(false); | |
| const rows = res?.rows || []; | |
| const scoped = rows.filter((row) => rowMatchesFilters(row, selThemes, selSubthemes)); | |
| let chunks = 0; | |
| const srcs = new Set(); | |
| for (const row of scoped) { | |
| const c = parseInt(row?.[3], 10); | |
| if (Number.isFinite(c)) chunks += c; | |
| const src = String(row?.[0] ?? "").trim(); | |
| if (src) srcs.add(src); | |
| } | |
| fileOptions = [...srcs].sort(); | |
| kbStats = { files: rows.length, chunks }; | |
| // Drop file selections the narrower scope hides β keeping them would | |
| // AND a contradictory file filter into retrieval (files + themes are | |
| // combined server-side) and silently return nothing. | |
| const valid = new Set(fileOptions); | |
| const pruned = files.filter((f) => valid.has(f)); | |
| if (pruned.length !== files.length) files = pruned; | |
| } catch { | |
| fileOptions = []; | |
| kbStats = { files: 0, chunks: 0 }; | |
| } | |
| } | |
| // Re-scope the subtheme list whenever the selected themes change, and | |
| // keep the file list / stats fresh. | |
| $effect(() => { | |
| loadThemes(themes); | |
| loadFiles(); | |
| }); | |
| onMount(() => { | |
| if (app.meta?.themes) themeOptions = app.meta.themes; | |
| if (app.meta?.subthemes) subthemeOptions = app.meta.subthemes; | |
| loadThemes(themes); | |
| loadFiles(); | |
| }); | |
| function removeFile(name) { | |
| files = files.filter((f) => f !== name); | |
| } | |
| </script> | |
| <div class="kb-notice"> | |
| <span> | |
| {t("web_kb_status").replace("{files}", kbStats.files).replace("{chunks}", kbStats.chunks)} | |
| </span> | |
| <button class="kb-link" onclick={() => setActiveTab("kb")} data-tooltip={t("web_tt_kb_open")}> | |
| {t("web_kb_open")} β | |
| </button> | |
| </div> | |
| <div class="theme-row"> | |
| <label class="mini-field" data-tooltip={t("web_tt_theme_filter")}> | |
| <span>{t("web_theme")}</span> | |
| <MiniSelect | |
| multiple | |
| bind:value={themes} | |
| options={themeOptions} | |
| emptyLabel={t("web_filter_all")} | |
| noneLabel={t("web_filter_none")} | |
| tip={t("web_tt_theme_filter")} | |
| onopen={() => loadThemes(themes)} | |
| /> | |
| </label> | |
| <label class="mini-field" data-tooltip={t("web_tt_subtheme_filter")}> | |
| <span>{t("web_subtheme")}</span> | |
| <MiniSelect | |
| multiple | |
| bind:value={subthemes} | |
| options={subthemeOptions} | |
| emptyLabel={t("web_filter_all")} | |
| noneLabel={t("web_filter_none")} | |
| tip={t("web_tt_subtheme_filter")} | |
| onopen={() => loadThemes(themes)} | |
| /> | |
| </label> | |
| <label class="mini-field" data-tooltip={t("web_tt_filter_files")}> | |
| <span>{t("web_filter_files")}</span> | |
| <MiniSelect | |
| multiple | |
| bind:value={files} | |
| options={fileOptions} | |
| emptyLabel={t("web_filter_all")} | |
| noneLabel={t("web_filter_none")} | |
| tip={t("web_tt_filter_files")} | |
| onopen={() => loadFiles()} | |
| /> | |
| </label> | |
| </div> | |
| {#if files.length > 0} | |
| <div class="rag-files"> | |
| {#each files as f (f)} | |
| <span class="chip file-chip" data-tooltip={f}> | |
| <span class="file-chip-name">{f}</span> | |
| <button | |
| type="button" | |
| class="chip-x" | |
| aria-label={t("web_tt_remove_file")} | |
| data-tooltip={t("web_tt_remove_file")} | |
| onclick={() => removeFile(f)} | |
| >β</button> | |
| </span> | |
| {/each} | |
| </div> | |
| {/if} | |