Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

7.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { SegmentedControl } from "@/components/segmented-control";
import { InfoHint } from "@/components/ui/info-hint";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { Slider } from "@/components/ui/slider";
import { Switch } from "@/components/ui/switch";
import {
  type RagMode,
  useChatRuntimeStore,
} from "@/features/chat/stores/chat-runtime-store";
import { ChevronDownStandardIcon } from "@/lib/chevron-icons";
import { cn } from "@/lib/utils";

const MODE_LABEL: Record<RagMode, string> = {
  hybrid: "Hybrid",
  dense: "Semantic only",
  lexical: "BM25 only",
};

function SliderRow({
  label,
  value,
  min,
  max,
  step,
  onChange,
  disabled = false,
  format = (v: number) => String(v),
}: {
  label: string;
  value: number;
  min: number;
  max: number;
  step: number;
  onChange: (v: number) => void;
  disabled?: boolean;
  format?: (v: number) => string;
}) {
  return (
    <div
      className={cn(
        "flex flex-col gap-2",
        disabled && "pointer-events-none opacity-50",
      )}
    >
      <div className="flex min-h-8 items-center justify-between">
        <span className="text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
          {label}
        </span>
        <span className="text-ui-13 tabular-nums text-muted-foreground">
          {format(value)}
        </span>
      </div>
      <Slider
        value={[value]}
        min={min}
        max={max}
        step={step}
        disabled={disabled}
        onValueChange={([v]) => onChange(v)}
        aria-label={label}
      />
    </div>
  );
}

export function RetrievalSettingsSection() {
  const ragMode = useChatRuntimeStore((s) => s.ragMode);
  const setRagMode = useChatRuntimeStore((s) => s.setRagMode);
  const ragTopK = useChatRuntimeStore((s) => s.ragTopK);
  const setRagTopK = useChatRuntimeStore((s) => s.setRagTopK);
  const ragAutoInject = useChatRuntimeStore((s) => s.ragAutoInject);
  const setRagAutoInject = useChatRuntimeStore((s) => s.setRagAutoInject);
  const ragAutoInjectMinScore = useChatRuntimeStore(
    (s) => s.ragAutoInjectMinScore,
  );
  const setRagAutoInjectMinScore = useChatRuntimeStore(
    (s) => s.setRagAutoInjectMinScore,
  );
  const ragOcrScanned = useChatRuntimeStore((s) => s.ragOcrScanned);
  const setRagOcrScanned = useChatRuntimeStore((s) => s.setRagOcrScanned);
  const ragCaptionFigures = useChatRuntimeStore((s) => s.ragCaptionFigures);
  const setRagCaptionFigures = useChatRuntimeStore(
    (s) => s.setRagCaptionFigures,
  );

  return (
    <div className="flex flex-col gap-5">
      <div className="flex flex-col gap-2">
        <span className="text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
          Search mode
        </span>
        <Select
          value={ragMode}
          onValueChange={(value) => setRagMode(value as RagMode)}
        >
          <SelectTrigger
            icon={ChevronDownStandardIcon}
            iconClassName="size-3.5"
            className="panel-select-trigger w-full"
            aria-label="Search mode"
          >
            <SelectValue />
          </SelectTrigger>
          <SelectContent>
            <SelectItem value="hybrid">{MODE_LABEL.hybrid}</SelectItem>
            <SelectItem value="dense">{MODE_LABEL.dense}</SelectItem>
            <SelectItem value="lexical">{MODE_LABEL.lexical}</SelectItem>
          </SelectContent>
        </Select>
      </div>

      <div className="flex flex-col gap-2">
        <div className="flex min-h-8 items-center justify-between">
          <span className="text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
            Passages (top K)
          </span>
          <span className="text-ui-13 tabular-nums text-muted-foreground">
            {ragTopK}
          </span>
        </div>
        <Slider
          value={[ragTopK]}
          min={1}
          max={20}
          step={1}
          onValueChange={([value]) => setRagTopK(value)}
          aria-label="Number of passages to retrieve"
        />
      </div>

      <div className="flex flex-col gap-2">
        <div className="flex flex-col">
          <span className="flex items-center gap-1.5 text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
            Auto-retrieve documents
            <InfoHint>
              Auto turns retrieval on for smaller models (9B and below), which
              tend to answer from memory instead of searching, and leaves it to
              larger ones. On and Off force it either way.
            </InfoHint>
          </span>
          <span className="text-ui-12 leading-[1.3] text-muted-foreground">
            Search attached documents before answering.
          </span>
        </div>
        <SegmentedControl
          value={ragAutoInject}
          options={[
            { value: "auto", label: "Auto" },
            { value: "on", label: "On" },
            { value: "off", label: "Off" },
          ]}
          onValueChange={setRagAutoInject}
          ariaLabel="Auto-retrieve documents"
          size="compact"
        />
        <SliderRow
          label="Auto-retrieve threshold"
          value={ragAutoInjectMinScore}
          min={0}
          max={1}
          step={0.05}
          disabled={ragAutoInject === "off"}
          onChange={setRagAutoInjectMinScore}
          format={(v) => v.toFixed(2)}
        />
      </div>

      <div className="flex items-start justify-between gap-3">
        <div className="flex flex-col">
          <span className="flex items-center gap-1.5 text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
            OCR scanned pages
            <InfoHint>
              Read scanned or image-only PDF pages at upload time using the
              loaded vision model or local Tesseract OCR. Local OCR requires
              installed language data. Selectable headers alone may not cover a
              scanned page's body.
            </InfoHint>
          </span>
          <span className="text-ui-12 leading-[1.3] text-muted-foreground">
            Transcribe image-only PDF pages when attaching.
          </span>
        </div>
        <Switch
          checked={ragOcrScanned}
          onCheckedChange={setRagOcrScanned}
          aria-label="OCR scanned pages"
          className="mt-[calc(var(--text-ui-13)*0.2615)]"
        />
      </div>

      <div className="flex items-start justify-between gap-3">
        <div className="flex flex-col">
          <span className="flex items-center gap-1.5 text-ui-13 font-medium leading-[1.25] tracking-nav text-nav-fg">
            Describe figures &amp; charts
            <InfoHint>
              Caption PDF figures, charts, tables and diagrams at upload with
              the loaded model's vision, so their content becomes searchable.
              Needs a vision model; adds vision calls for detected figures.
            </InfoHint>
          </span>
          <span className="text-ui-12 leading-[1.3] text-muted-foreground">
            Optional vision pass; can take several minutes.
          </span>
        </div>
        <Switch
          checked={ragCaptionFigures}
          onCheckedChange={setRagCaptionFigures}
          aria-label="Describe figures and charts"
          className="mt-[calc(var(--text-ui-13)*0.2615)]"
        />
      </div>
    </div>
  );
}