Spaces:
Running
Running
File size: 15,771 Bytes
7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 30b1aef 7ac3a44 | 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | import React from 'react';
import { useMemo, useState } from "react";
import { Badge, Button } from "../common/index";
const EMBEDDING_MODELS = {
nvidia: [
{ value: "nvidia/nv-embed-v1", label: "nvidia/nv-embed-v1 (Default)" },
{
value: "nvidia/llama-3.2-nemoretriever-300m-embed-v1",
label: "nvidia/llama-3.2-nemoretriever-300m-embed-v1",
},
],
huggingface: [
{
value: "sentence-transformers/all-MiniLM-L6-v2",
label: "sentence-transformers/all-MiniLM-L6-v2",
},
{ value: "BAAI/bge-base-en-v1.5", label: "BAAI/bge-base-en-v1.5" },
{ value: "intfloat/e5-base-v2", label: "intfloat/e5-base-v2" },
{ value: "thenlper/gte-base", label: "thenlper/gte-base" },
{
value: "sentence-transformers/multi-qa-mpnet-base-dot-v1",
label: "sentence-transformers/multi-qa-mpnet-base-dot-v1",
},
],
};
const CHUNK_STRATEGIES = [
{ value: "fixed_size", label: "Fixed Size" },
{ value: "recursive", label: "Recursive" },
{ value: "semantic", label: "Semantic" },
{ value: "chapter_based", label: "Chapter-Based" },
{ value: "regex", label: "Regex" },
{ value: "sentence_window", label: "Sentence Window" },
];
const DEFAULT_CHUNK_PARAMS = {
fixed_size: { chunk_size: 512, overlap: 50 },
recursive: {
chunk_size: 512,
overlap: 50,
min_chunk_size: 100,
separators: ["\n\n", "\n", "(?<=[.!?])\\s+", " "],
apply_overlap_recursively: true,
},
semantic: {
max_chunk_size: 512,
min_chunk_size: 100,
similarity_threshold: 0.7,
hard_split_threshold: 0.4,
overlap_sentences: 1,
},
chapter_based: { max_chunk_size: 1024, overlap_lines: 1 },
regex: { pattern: "\\n\\n+", min_chunk_size: 100 },
sentence_window: { window_size: 3, max_chunk_size: 150 },
};
export default function ConfigFormModal({ onSave, onCancel, existingNames = [], isDisabled }) {
const [name, setName] = useState("");
const [chunkStrategy, setChunkStrategy] = useState("fixed_size");
const [chunkParams, setChunkParams] = useState({ ...DEFAULT_CHUNK_PARAMS.fixed_size });
const [embeddingProvider, setEmbeddingProvider] = useState("nvidia");
const [embeddingModel, setEmbeddingModel] = useState(EMBEDDING_MODELS.nvidia[0].value);
const [topK, setTopK] = useState(5);
const [threshold, setThreshold] = useState(0.5);
const [error, setError] = useState("");
const normalizedName = useMemo(() => name.trim(), [name]);
const handleSubmit = () => {
const trimmed = normalizedName;
if (!trimmed) {
setError("Config name is required.");
return;
}
if (existingNames.some((item) => item.toLowerCase() === trimmed.toLowerCase())) {
setError("A config with this name already exists.");
return;
}
onSave({
name: trimmed,
chunk_strategy: chunkStrategy,
chunk_params: chunkParams,
embedding_provider: embeddingProvider,
embedding_model: embeddingModel,
top_k: Number(topK),
threshold: Number(threshold),
isPreset: false,
indexingStatus: "indexing",
});
};
return (
<div className="fixed inset-0 z-40 flex items-center justify-center bg-black/40 px-4">
<div className="w-full max-w-lg rounded-2xl border border-gray-200 bg-white p-5 shadow-2xl">
<div className="flex items-start justify-between gap-3">
<div>
<h3 className="text-lg font-semibold text-gray-900">Create Config</h3>
<p className="text-sm text-gray-500">Configure chunking, embeddings, and retrieval values.</p>
</div>
<Badge color="blue">Advanced</Badge>
</div>
<div className="mt-5 space-y-4">
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">Config Name</span>
<input
type="text"
value={name}
onChange={(e) => {
setName(e.target.value);
setError("");
}}
disabled={isDisabled}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
placeholder="e.g. High Precision"
/>
</label>
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">Chunking Strategy</span>
<select
value={chunkStrategy}
onChange={(e) => {
const nextStrategy = e.target.value;
setChunkStrategy(nextStrategy);
setChunkParams({ ...(DEFAULT_CHUNK_PARAMS[nextStrategy] || {}) });
}}
disabled={isDisabled}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
>
{CHUNK_STRATEGIES.map((strategy) => (
<option key={strategy.value} value={strategy.value}>{strategy.label}</option>
))}
</select>
</label>
{(chunkStrategy === "fixed_size" || chunkStrategy === "recursive") && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<NumericParam
label="Chunk Size"
value={chunkParams.chunk_size ?? 512}
onChange={(value) => setChunkParams((prev) => ({ ...prev, chunk_size: value }))}
min={128}
max={2048}
/>
<NumericParam
label="Overlap"
value={chunkParams.overlap ?? 50}
onChange={(value) => setChunkParams((prev) => ({ ...prev, overlap: value }))}
min={0}
max={200}
/>
</div>
)}
{chunkStrategy === "recursive" && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<NumericParam
label="Min Chunk Size"
value={chunkParams.min_chunk_size ?? 100}
onChange={(value) => setChunkParams((prev) => ({ ...prev, min_chunk_size: value }))}
min={10}
max={512}
/>
<label className="flex items-center gap-2 rounded-xl border border-gray-300 px-3 py-2 text-sm text-gray-700">
<input
type="checkbox"
checked={chunkParams.apply_overlap_recursively ?? true}
onChange={(e) =>
setChunkParams((prev) => ({ ...prev, apply_overlap_recursively: e.target.checked }))
}
className="h-4 w-4 rounded border-gray-300 text-blue-600"
/>
Apply overlap recursively
</label>
<label className="block space-y-1 sm:col-span-2">
<span className="text-sm font-medium text-gray-700">Separators (comma-separated)</span>
<input
type="text"
value={Array.isArray(chunkParams.separators) ? chunkParams.separators.join(",") : ""}
onChange={(e) => {
const separators = e.target.value
.split(",")
.map((part) => part.trim())
.filter(Boolean);
setChunkParams((prev) => ({ ...prev, separators }));
}}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm font-mono focus:border-blue-500 focus:outline-none"
/>
</label>
</div>
)}
{chunkStrategy === "semantic" && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<NumericParam
label="Max Chunk Size"
value={chunkParams.max_chunk_size ?? 512}
onChange={(value) => setChunkParams((prev) => ({ ...prev, max_chunk_size: value }))}
min={128}
max={4096}
/>
<NumericParam
label="Min Chunk Size"
value={chunkParams.min_chunk_size ?? 100}
onChange={(value) => setChunkParams((prev) => ({ ...prev, min_chunk_size: value }))}
min={10}
max={512}
/>
<FloatParam
label="Similarity Threshold"
value={chunkParams.similarity_threshold ?? 0.7}
onChange={(value) => setChunkParams((prev) => ({ ...prev, similarity_threshold: value }))}
min={0}
max={1}
step={0.01}
/>
<FloatParam
label="Hard Split Threshold"
value={chunkParams.hard_split_threshold ?? 0.4}
onChange={(value) => setChunkParams((prev) => ({ ...prev, hard_split_threshold: value }))}
min={0}
max={1}
step={0.01}
/>
<NumericParam
label="Overlap Sentences"
value={chunkParams.overlap_sentences ?? 1}
onChange={(value) => setChunkParams((prev) => ({ ...prev, overlap_sentences: value }))}
min={0}
max={5}
/>
</div>
)}
{chunkStrategy === "chapter_based" && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<NumericParam
label="Max Chunk Size"
value={chunkParams.max_chunk_size ?? 1024}
onChange={(value) => setChunkParams((prev) => ({ ...prev, max_chunk_size: value }))}
min={256}
max={4096}
/>
<NumericParam
label="Overlap Lines"
value={chunkParams.overlap_lines ?? 1}
onChange={(value) => setChunkParams((prev) => ({ ...prev, overlap_lines: value }))}
min={0}
max={10}
/>
</div>
)}
{chunkStrategy === "regex" && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<label className="block space-y-1 sm:col-span-2">
<span className="text-sm font-medium text-gray-700">Pattern</span>
<input
type="text"
value={chunkParams.pattern ?? "\\n\\n+"}
onChange={(e) => setChunkParams((prev) => ({ ...prev, pattern: e.target.value }))}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm font-mono focus:border-blue-500 focus:outline-none"
/>
</label>
<NumericParam
label="Min Chunk Size"
value={chunkParams.min_chunk_size ?? 100}
onChange={(value) => setChunkParams((prev) => ({ ...prev, min_chunk_size: value }))}
min={1}
max={1024}
/>
</div>
)}
{chunkStrategy === "sentence_window" && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<NumericParam
label="Window Size"
value={chunkParams.window_size ?? 3}
onChange={(value) => setChunkParams((prev) => ({ ...prev, window_size: value }))}
min={1}
max={7}
/>
<NumericParam
label="Max Chunk Size"
value={chunkParams.max_chunk_size ?? 150}
onChange={(value) => setChunkParams((prev) => ({ ...prev, max_chunk_size: value }))}
min={50}
max={150}
/>
</div>
)}
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">Embedding Provider</span>
<select
value={embeddingProvider}
onChange={(e) => {
const provider = e.target.value;
setEmbeddingProvider(provider);
setEmbeddingModel(EMBEDDING_MODELS[provider][0].value);
}}
disabled={isDisabled}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
>
<option value="nvidia">NVIDIA</option>
<option value="huggingface">Hugging Face</option>
</select>
</label>
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">Embedding Model</span>
<select
value={embeddingModel}
onChange={(e) => setEmbeddingModel(e.target.value)}
disabled={isDisabled}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
>
{EMBEDDING_MODELS[embeddingProvider].map((model) => (
<option key={model.value} value={model.value}>{model.label}</option>
))}
</select>
</label>
<label className="block space-y-2">
<div className="flex items-center justify-between text-sm font-medium text-gray-700">
<span>top_k</span>
<span className="font-mono text-blue-600">{topK}</span>
</div>
<input
type="range"
min={1}
max={10}
step={1}
value={topK}
onChange={(e) => setTopK(Number(e.target.value))}
disabled={isDisabled}
className="w-full accent-blue-600"
/>
</label>
<label className="block space-y-2">
<div className="flex items-center justify-between text-sm font-medium text-gray-700">
<span>Similarity Threshold</span>
<span className="font-mono text-blue-600">{Number(threshold).toFixed(2)}</span>
</div>
<input
type="range"
min={0}
max={1}
step={0.05}
value={threshold}
onChange={(e) => setThreshold(Number(e.target.value))}
disabled={isDisabled}
className="w-full accent-blue-600"
/>
</label>
{error && <p className="text-sm text-red-600">{error}</p>}
</div>
<div className="mt-6 flex justify-end gap-2">
<Button variant="secondary" onClick={onCancel} disabled={isDisabled}>Cancel</Button>
<Button onClick={handleSubmit} disabled={isDisabled}>Save Config</Button>
</div>
</div>
</div>
);
}
function NumericParam({ label, value, onChange, min, max }) {
return (
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">{label}</span>
<input
type="number"
min={min}
max={max}
step={1}
value={value}
onChange={(e) => onChange(Number(e.target.value) || 0)}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
/>
</label>
);
}
function FloatParam({ label, value, onChange, min, max, step }) {
return (
<label className="block space-y-1">
<span className="text-sm font-medium text-gray-700">{label}</span>
<input
type="number"
min={min}
max={max}
step={step}
value={value}
onChange={(e) => onChange(Number(e.target.value) || 0)}
className="w-full rounded-xl border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
/>
</label>
);
}
|