mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 15:59:59 +08:00
1184 lines
46 KiB
Python
1184 lines
46 KiB
Python
#!/usr/bin/env python
|
|
"""Incrementally add documents to an existing knowledge base."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
import shutil
|
|
import sys
|
|
from typing import TYPE_CHECKING, Any, List, Optional
|
|
|
|
from deeptutor.knowledge.staging import content_hash, stage_source
|
|
from deeptutor.services.config import resolve_llm_runtime_config
|
|
from deeptutor.services.file_io import atomic_write_json, atomic_write_text
|
|
from deeptutor.services.parsing.cache import cache_root_for, reclaim_sources
|
|
from deeptutor.services.rag.factory import (
|
|
DEFAULT_PROVIDER,
|
|
LIGHTRAG_PROVIDER,
|
|
has_ready_provider_index,
|
|
normalize_provider_name,
|
|
)
|
|
from deeptutor.services.rag.index_probe import provider_failure_summary
|
|
from deeptutor.services.rag.index_versioning import list_kb_versions
|
|
from deeptutor.services.rag.provider_binding import resolve_bound_provider
|
|
from deeptutor.services.rag.service import RAGService
|
|
from deeptutor.services.rag.visual_assets import VisualAssetStore
|
|
from deeptutor.services.setup.data_volume import (
|
|
DataVolumePermissionError,
|
|
ensure_data_volume_writable,
|
|
format_data_volume_permission_error,
|
|
)
|
|
from deeptutor.utils.file_types import FileTypeRouter
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DEFAULT_BASE_DIR = "./data/knowledge_bases"
|
|
|
|
if TYPE_CHECKING:
|
|
from deeptutor.knowledge.manager import KnowledgeBaseManager
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DocumentIndexFailure:
|
|
"""One file that could not be added to the provider index."""
|
|
|
|
file_path: Path
|
|
error: str
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DocumentIndexResult:
|
|
"""Structured incremental-index result.
|
|
|
|
A task can no longer infer success from "no exception": every staged file is
|
|
explicitly accounted for as either processed or failed.
|
|
"""
|
|
|
|
processed_files: list[Path]
|
|
failures: list[DocumentIndexFailure]
|
|
|
|
@property
|
|
def processed_count(self) -> int:
|
|
return len(self.processed_files)
|
|
|
|
@property
|
|
def failed_count(self) -> int:
|
|
return len(self.failures)
|
|
|
|
@property
|
|
def has_failures(self) -> bool:
|
|
return bool(self.failures)
|
|
|
|
def failure_summary(self, *, limit: int = 3) -> str:
|
|
if not self.failures:
|
|
return ""
|
|
shown = [f"{failure.file_path.name}: {failure.error}" for failure in self.failures[:limit]]
|
|
remaining = len(self.failures) - len(shown)
|
|
if remaining > 0:
|
|
shown.append(f"... and {remaining} more file(s)")
|
|
return "; ".join(shown)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RawDocumentRemoval:
|
|
"""Outcome of removing one raw document from a knowledge base."""
|
|
|
|
rel_path: str
|
|
was_indexed: bool
|
|
|
|
|
|
def _read_metadata(metadata_file: Path) -> dict:
|
|
"""Load a KB's metadata.json, returning {} when absent or unreadable."""
|
|
if not metadata_file.exists():
|
|
return {}
|
|
try:
|
|
with open(metadata_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
except Exception:
|
|
return {}
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
def _write_metadata(metadata_file: Path, metadata: dict) -> None:
|
|
"""Persist a KB's metadata.json atomically (pretty-printed, non-ASCII preserved)."""
|
|
atomic_write_json(metadata_file, metadata)
|
|
|
|
|
|
def _raw_hash_key(file_path: Path, raw_dir: Path) -> str:
|
|
"""Stable ``file_hashes`` key for a staged file.
|
|
|
|
The key is the POSIX path relative to ``raw/`` (so folder uploads keep
|
|
distinct entries), falling back to the basename for anything that resolves
|
|
outside ``raw/``. Indexing and removal MUST share this rule, otherwise a
|
|
removed file's hash record would leak and silently skip a later re-add.
|
|
"""
|
|
try:
|
|
return file_path.resolve().relative_to(raw_dir.resolve()).as_posix()
|
|
except ValueError:
|
|
return file_path.name
|
|
|
|
|
|
def hashes_for_indexed_files(file_paths: list[str], raw_dir: Path) -> dict[str, str]:
|
|
"""Hash source files already confirmed in an index, keyed by their raw path."""
|
|
return {
|
|
_raw_hash_key(Path(file_path), raw_dir): content_hash(Path(file_path))
|
|
for file_path in file_paths
|
|
}
|
|
|
|
|
|
def remove_raw_document(kb_dir: Path, file_path: Path) -> RawDocumentRemoval:
|
|
"""Delete one staged raw file and drop its indexed-hash record.
|
|
|
|
Also reclaims the document's derived parse-cache entries, hashing the bytes
|
|
while they still exist. That is best-effort and never fails the delete; the
|
|
cache is shared across KBs, so a byte-identical copy in another KB loses its
|
|
entry too and re-parses on next use (see
|
|
:func:`deeptutor.services.parsing.cache.reclaim_sources`).
|
|
|
|
Deliberately decoupled from :class:`DocumentAdder` (whose constructor
|
|
requires a ready provider index): a KB stuck in an *error* state often has
|
|
no index at all, yet its raw/ files must still be removable so the user can
|
|
drop an unparseable document instead of deleting and rebuilding the whole
|
|
KB. Vectors are not touched here — the providers index whole KBs, so any
|
|
vectors from an already-indexed file are cleared by a subsequent re-index,
|
|
which the returned ``was_indexed`` flag lets the caller surface.
|
|
|
|
Source figures are dropped here too, because the vector index does not own
|
|
them: this is the single deletion layer shared by the HTTP route and the
|
|
GitHub/web source syncs, so cleaning up anywhere else leaves a path that
|
|
leaks. The manifest is keyed by ``_raw_hash_key`` — the same rule indexing
|
|
used to record ``source_path`` — so removal cannot miss assets that were
|
|
published under a nested raw/ path.
|
|
|
|
``file_path`` must already be sandbox-resolved under the KB's raw/ dir.
|
|
"""
|
|
raw_dir = kb_dir / "raw"
|
|
hash_key = _raw_hash_key(file_path, raw_dir)
|
|
|
|
target = file_path.resolve()
|
|
if target.exists():
|
|
# Reclaim the derived parse cache while the bytes are still readable:
|
|
# the cache key is the source content hash. The cache root is derived
|
|
# from this KB's directory (a sibling of knowledge_bases/), not from the
|
|
# request scope, which is the account scope in library view. Reclaiming
|
|
# is best-effort (see ``reclaim_sources``); a cache failure must not
|
|
# block the delete.
|
|
try:
|
|
reclaim_sources([target], cache_root=cache_root_for(Path(kb_dir).parent))
|
|
except Exception as exc: # noqa: BLE001 - cache cleanup is never fatal
|
|
logger.warning("Could not reclaim the parse cache for %s: %s", target, exc)
|
|
target.unlink()
|
|
|
|
# Best-effort: the manifest may be corrupt or read-only, and reclaiming its
|
|
# bytes must never turn a user's delete into an error. ``remove_source`` is
|
|
# idempotent and does not create visual_assets/ for KBs that have none.
|
|
try:
|
|
VisualAssetStore(kb_dir).remove_source(hash_key)
|
|
except Exception as exc: # noqa: BLE001 - asset cleanup is never fatal
|
|
logger.warning("Could not remove visual assets for %s: %s", hash_key, exc)
|
|
|
|
metadata_file = kb_dir / "metadata.json"
|
|
if not metadata_file.exists():
|
|
return
|
|
metadata = json.loads(metadata_file.read_text(encoding="utf-8"))
|
|
hashes = metadata.get("file_hashes")
|
|
was_indexed = isinstance(hashes, dict) and hash_key in hashes
|
|
if was_indexed:
|
|
del hashes[hash_key]
|
|
_write_metadata(metadata_file, metadata)
|
|
|
|
return RawDocumentRemoval(rel_path=hash_key, was_indexed=was_indexed)
|
|
|
|
|
|
def _normalize_rel_path(value: str) -> str:
|
|
return value.replace("\\", "/").strip("/")
|
|
|
|
|
|
def _remap_rel_path(value: Any, old_rel: str, new_rel: str) -> str | None:
|
|
if not isinstance(value, str):
|
|
return None
|
|
normalized = _normalize_rel_path(value)
|
|
if normalized == old_rel:
|
|
return new_rel
|
|
if normalized.startswith(old_rel + "/"):
|
|
return new_rel + normalized[len(old_rel) :]
|
|
return None
|
|
|
|
|
|
def _remap_file_path(value: Any, raw_dir: Path, old_rel: str, new_rel: str) -> str | None:
|
|
if not isinstance(value, str) or not value.strip():
|
|
return None
|
|
direct = _remap_rel_path(value, old_rel, new_rel)
|
|
if direct is not None:
|
|
return direct
|
|
raw_resolved = raw_dir.resolve()
|
|
candidate = Path(value)
|
|
rel: Path | None = None
|
|
for root in (raw_dir, raw_resolved):
|
|
try:
|
|
rel = candidate.relative_to(root)
|
|
break
|
|
except ValueError:
|
|
continue
|
|
if rel is None:
|
|
try:
|
|
rel = candidate.resolve(strict=False).relative_to(raw_resolved)
|
|
except (OSError, ValueError):
|
|
return None
|
|
remapped = _remap_rel_path(rel.as_posix(), old_rel, new_rel)
|
|
return str(raw_resolved / remapped) if remapped is not None else None
|
|
|
|
|
|
def _remap_metadata_dict(meta: dict[str, Any], raw_dir: Path, old_rel: str, new_rel: str) -> bool:
|
|
changed = False
|
|
remapped_key = _remap_rel_path(meta.get("document_key"), old_rel, new_rel)
|
|
if remapped_key is not None and meta.get("document_key") != remapped_key:
|
|
meta["document_key"] = remapped_key
|
|
changed = True
|
|
|
|
remapped_file_path = _remap_file_path(meta.get("file_path"), raw_dir, old_rel, new_rel)
|
|
if remapped_file_path is None and remapped_key is not None:
|
|
existing_path = meta.get("file_path")
|
|
if isinstance(existing_path, str) and existing_path.strip():
|
|
remapped_file_path = str(raw_dir.resolve() / remapped_key)
|
|
if remapped_file_path is not None and meta.get("file_path") != remapped_file_path:
|
|
meta["file_path"] = remapped_file_path
|
|
changed = True
|
|
return changed
|
|
|
|
|
|
def _remap_node_dict(node_data: dict[str, Any], raw_dir: Path, old_rel: str, new_rel: str) -> bool:
|
|
changed = False
|
|
meta = node_data.get("metadata")
|
|
if isinstance(meta, dict) and _remap_metadata_dict(meta, raw_dir, old_rel, new_rel):
|
|
changed = True
|
|
|
|
relationships = node_data.get("relationships")
|
|
if not isinstance(relationships, dict):
|
|
return changed
|
|
for related in relationships.values():
|
|
items = related if isinstance(related, list) else [related]
|
|
for item in items:
|
|
if isinstance(item, dict):
|
|
rel_meta = item.get("metadata")
|
|
if isinstance(rel_meta, dict) and _remap_metadata_dict(
|
|
rel_meta, raw_dir, old_rel, new_rel
|
|
):
|
|
changed = True
|
|
return changed
|
|
|
|
|
|
def _relocate_file_hashes(kb_dir: Path, old_rel: str, new_rel: str) -> None:
|
|
metadata_file = kb_dir / "metadata.json"
|
|
metadata = _read_metadata(metadata_file)
|
|
hashes = metadata.get("file_hashes")
|
|
if not isinstance(hashes, dict):
|
|
return
|
|
updated: dict[str, Any] = {}
|
|
changed = False
|
|
for key, digest in hashes.items():
|
|
remapped = _remap_rel_path(key, old_rel, new_rel)
|
|
if remapped is not None and remapped != key:
|
|
updated[remapped] = digest
|
|
changed = True
|
|
else:
|
|
updated[key] = digest
|
|
if changed:
|
|
metadata["file_hashes"] = updated
|
|
_write_metadata(metadata_file, metadata)
|
|
|
|
|
|
def _relocate_source_pages(storage_dir: Path, old_rel: str, new_rel: str) -> None:
|
|
catalog_path = storage_dir / "source_pages.json"
|
|
if not catalog_path.is_file():
|
|
return
|
|
payload = json.loads(catalog_path.read_text(encoding="utf-8"))
|
|
pages = payload.get("pages") if isinstance(payload, dict) else None
|
|
if not isinstance(pages, list):
|
|
return
|
|
changed = False
|
|
for page in pages:
|
|
if not isinstance(page, dict):
|
|
continue
|
|
remapped = _remap_rel_path(page.get("document_key"), old_rel, new_rel)
|
|
if remapped is not None and page.get("document_key") != remapped:
|
|
page["document_key"] = remapped
|
|
changed = True
|
|
if changed:
|
|
atomic_write_json(catalog_path, payload)
|
|
|
|
|
|
def _relocate_docstore(storage_dir: Path, raw_dir: Path, old_rel: str, new_rel: str) -> bool:
|
|
docstore_path = storage_dir / "docstore.json"
|
|
if not docstore_path.is_file():
|
|
return False
|
|
payload = json.loads(docstore_path.read_text(encoding="utf-8"))
|
|
if not isinstance(payload, dict):
|
|
return False
|
|
|
|
changed = False
|
|
ref_docs = payload.get("docstore/ref_doc_info")
|
|
if isinstance(ref_docs, dict):
|
|
for info in ref_docs.values():
|
|
if isinstance(info, dict) and isinstance(info.get("metadata"), dict):
|
|
if _remap_metadata_dict(info["metadata"], raw_dir, old_rel, new_rel):
|
|
changed = True
|
|
|
|
nodes = payload.get("docstore/data")
|
|
if isinstance(nodes, dict):
|
|
for wrapper in nodes.values():
|
|
if isinstance(wrapper, dict) and isinstance(wrapper.get("__data__"), dict):
|
|
if _remap_node_dict(wrapper["__data__"], raw_dir, old_rel, new_rel):
|
|
changed = True
|
|
|
|
if changed:
|
|
atomic_write_json(docstore_path, payload)
|
|
return changed
|
|
|
|
|
|
def _corpus_line_offsets(raw_bytes: bytes) -> list[int]:
|
|
offsets: list[int] = []
|
|
pos = 0
|
|
for line_bytes in raw_bytes.splitlines(keepends=True):
|
|
offsets.append(pos)
|
|
pos += len(line_bytes)
|
|
return offsets
|
|
|
|
|
|
def _write_bm25_snapshot(storage_dir: Path, persist_dir: Path) -> None:
|
|
docstore_path = storage_dir / "docstore.json"
|
|
snapshot_path = persist_dir / "snapshot.json"
|
|
if not docstore_path.is_file() or not snapshot_path.is_file():
|
|
return
|
|
with docstore_path.open("rb") as docstore:
|
|
docstore_digest = hashlib.file_digest(docstore, "sha256").hexdigest()
|
|
snapshot = {
|
|
"docstore_sha256": docstore_digest,
|
|
"files": {
|
|
path.name: path.stat().st_size
|
|
for path in persist_dir.iterdir()
|
|
if path.name != "snapshot.json" and path.is_file()
|
|
},
|
|
}
|
|
atomic_write_text(snapshot_path, json.dumps(snapshot))
|
|
|
|
|
|
def _relocate_bm25_corpus(persist_dir: Path, raw_dir: Path, old_rel: str, new_rel: str) -> bool:
|
|
corpus_path = persist_dir / "corpus.jsonl"
|
|
if not corpus_path.is_file():
|
|
return False
|
|
raw_lines = corpus_path.read_text(encoding="utf-8").splitlines()
|
|
updated_lines: list[str] = []
|
|
changed = False
|
|
for line in raw_lines:
|
|
if not line.strip():
|
|
continue
|
|
row = json.loads(line)
|
|
if not isinstance(row, dict):
|
|
raise ValueError("BM25 corpus row must be a JSON object")
|
|
row_changed = _remap_metadata_dict(row, raw_dir, old_rel, new_rel)
|
|
node_content = row.get("_node_content")
|
|
if isinstance(node_content, str):
|
|
inner = json.loads(node_content)
|
|
if isinstance(inner, dict) and _remap_node_dict(inner, raw_dir, old_rel, new_rel):
|
|
row["_node_content"] = json.dumps(inner, ensure_ascii=False)
|
|
row_changed = True
|
|
if row_changed:
|
|
changed = True
|
|
updated_lines.append(json.dumps(row, ensure_ascii=False))
|
|
|
|
if not changed:
|
|
return False
|
|
new_corpus = ("\n".join(updated_lines) + "\n") if updated_lines else ""
|
|
atomic_write_text(corpus_path, new_corpus)
|
|
mmindex_path = persist_dir / "corpus.mmindex.json"
|
|
if mmindex_path.is_file():
|
|
offsets = _corpus_line_offsets(new_corpus.encode("utf-8"))
|
|
atomic_write_text(mmindex_path, json.dumps(offsets))
|
|
return True
|
|
|
|
|
|
def _relocate_bm25_sidecar(
|
|
storage_dir: Path,
|
|
persist_dir: Path,
|
|
raw_dir: Path,
|
|
old_rel: str,
|
|
new_rel: str,
|
|
*,
|
|
docstore_changed: bool,
|
|
) -> None:
|
|
try:
|
|
bm25_changed = _relocate_bm25_corpus(persist_dir, raw_dir, old_rel, new_rel)
|
|
if docstore_changed or bm25_changed:
|
|
_write_bm25_snapshot(storage_dir, persist_dir)
|
|
except Exception as exc: # noqa: BLE001 - unreadable sidecar can be rebuilt on demand
|
|
if docstore_changed:
|
|
logger.warning(
|
|
"Could not update BM25 sidecar %s after move; dropping stale sidecar: %s",
|
|
persist_dir,
|
|
exc,
|
|
)
|
|
shutil.rmtree(persist_dir, ignore_errors=True)
|
|
|
|
|
|
def _kb_storage_dirs(kb_dir: Path) -> list[Path]:
|
|
seen: set[Path] = set()
|
|
dirs: list[Path] = []
|
|
for entry in list_kb_versions(kb_dir):
|
|
storage_path = entry.get("storage_path")
|
|
if not storage_path:
|
|
continue
|
|
candidate = Path(str(storage_path)).resolve()
|
|
if candidate not in seen and candidate.is_dir():
|
|
seen.add(candidate)
|
|
dirs.append(candidate)
|
|
return dirs
|
|
|
|
|
|
def relocate_raw_document_metadata(kb_dir: Path, old_rel_path: str, new_rel_path: str) -> None:
|
|
"""Keep KB hash records, page catalogs, and docstores aligned after a move.
|
|
|
|
Handles both single-file moves and whole-folder moves (prefix match with a
|
|
path boundary) without triggering re-embedding or rebuilding BM25 in-process.
|
|
"""
|
|
old_rel = _normalize_rel_path(old_rel_path)
|
|
new_rel = _normalize_rel_path(new_rel_path)
|
|
if not old_rel or not new_rel or old_rel == new_rel:
|
|
return
|
|
|
|
raw_dir = kb_dir / "raw"
|
|
|
|
try:
|
|
VisualAssetStore(kb_dir).move_source(old_rel, new_rel)
|
|
except Exception as exc: # noqa: BLE001 - asset metadata is best-effort
|
|
logger.warning("Could not relocate visual assets %s -> %s: %s", old_rel, new_rel, exc)
|
|
|
|
_relocate_file_hashes(kb_dir, old_rel, new_rel)
|
|
|
|
docstore_updated = False
|
|
for storage_dir in _kb_storage_dirs(kb_dir):
|
|
_relocate_source_pages(storage_dir, old_rel, new_rel)
|
|
storage_docstore_changed = _relocate_docstore(storage_dir, raw_dir, old_rel, new_rel)
|
|
if storage_docstore_changed:
|
|
docstore_updated = True
|
|
persist_dir = storage_dir / "bm25_retriever"
|
|
if persist_dir.is_dir():
|
|
_relocate_bm25_sidecar(
|
|
storage_dir,
|
|
persist_dir,
|
|
raw_dir,
|
|
old_rel,
|
|
new_rel,
|
|
docstore_changed=storage_docstore_changed,
|
|
)
|
|
|
|
if docstore_updated:
|
|
storage_module = sys.modules.get("deeptutor.services.rag.pipelines.llamaindex.storage")
|
|
clear_cache = getattr(storage_module, "clear_index_cache", None)
|
|
if callable(clear_cache):
|
|
clear_cache()
|
|
|
|
|
|
class DocumentAdder:
|
|
"""Stage and index new files through a KB's bound RAG provider."""
|
|
|
|
def __init__(
|
|
self,
|
|
kb_name: str,
|
|
base_dir: str = DEFAULT_BASE_DIR,
|
|
api_key: str | None = None,
|
|
base_url: str | None = None,
|
|
progress_tracker=None,
|
|
rag_provider: str | None = None,
|
|
accepted_indexing_snapshot=None,
|
|
):
|
|
self.kb_name = kb_name
|
|
self.base_dir = Path(base_dir)
|
|
self.kb_dir = self.base_dir / kb_name
|
|
|
|
if not self.kb_dir.exists():
|
|
raise ValueError(f"Knowledge base does not exist: {kb_name}")
|
|
|
|
self.raw_dir = self.kb_dir / "raw"
|
|
self.llamaindex_storage_dir = self.kb_dir / "llamaindex_storage"
|
|
self.legacy_rag_storage_dir = self.kb_dir / "rag_storage"
|
|
self.metadata_file = self.kb_dir / "metadata.json"
|
|
|
|
# Fail on UID-mismatched / unwritable volumes before the "not initialized"
|
|
# check, which otherwise hides Unraid bind-mount permission errors.
|
|
ensure_data_volume_writable(self.kb_dir)
|
|
|
|
# Incremental adds must use the engine DeepTutor has bound to this KB. An
|
|
# explicit rag_provider (from the API, already matched against the KB)
|
|
# wins; otherwise use the shared binding resolver.
|
|
if rag_provider:
|
|
self.rag_provider = normalize_provider_name(rag_provider)
|
|
else:
|
|
self.rag_provider = self._provider_from_binding()
|
|
|
|
has_provider_index = has_ready_provider_index(self.kb_dir, self.rag_provider)
|
|
|
|
if (
|
|
self.rag_provider == DEFAULT_PROVIDER
|
|
and not has_provider_index
|
|
and self.legacy_rag_storage_dir.exists()
|
|
):
|
|
raise ValueError(
|
|
f"Knowledge base '{kb_name}' uses legacy index format and requires reindex before incremental add"
|
|
)
|
|
|
|
# Both pipelines create their first index on add; existing broken versions
|
|
# still require reindex instead of being silently replaced (#1458).
|
|
versions = list_kb_versions(self.kb_dir)
|
|
allows_bootstrap = (
|
|
self.rag_provider
|
|
in {
|
|
DEFAULT_PROVIDER,
|
|
LIGHTRAG_PROVIDER,
|
|
}
|
|
and not versions
|
|
)
|
|
if not has_provider_index and not allows_bootstrap:
|
|
if versions:
|
|
summary = provider_failure_summary(self.kb_dir, self.rag_provider)
|
|
raise ValueError(
|
|
f"Knowledge base has no ready {self.rag_provider} index; reindex required: "
|
|
f"{summary or 'stored index version is incomplete'}"
|
|
)
|
|
raise ValueError(f"Knowledge base not initialized ({self.rag_provider}): {kb_name}")
|
|
|
|
self.accepted_indexing_snapshot = accepted_indexing_snapshot
|
|
if self.rag_provider == LIGHTRAG_PROVIDER and self.accepted_indexing_snapshot is None:
|
|
from deeptutor.services.rag.pipelines.lightrag.indexing_policy import (
|
|
bind_target,
|
|
resolve_write_snapshot,
|
|
)
|
|
|
|
self.accepted_indexing_snapshot = bind_target(
|
|
resolve_write_snapshot(
|
|
self.kb_dir, base_dir=str(self.base_dir), kb_name=self.kb_name
|
|
),
|
|
self.kb_dir,
|
|
)
|
|
|
|
self.api_key = api_key
|
|
self.base_url = base_url
|
|
self.progress_tracker = progress_tracker
|
|
|
|
self.raw_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def _provider_from_binding(self) -> str:
|
|
return resolve_bound_provider(self.base_dir, self.kb_name)
|
|
|
|
def _get_file_hash(self, file_path: Path) -> str:
|
|
return content_hash(file_path)
|
|
|
|
def get_ingested_hashes(self) -> dict[str, str]:
|
|
if self.metadata_file.exists():
|
|
try:
|
|
with open(self.metadata_file, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return data.get("file_hashes", {})
|
|
except Exception:
|
|
return {}
|
|
return {}
|
|
|
|
def add_documents(
|
|
self,
|
|
source_files: List[str],
|
|
allow_duplicates: bool = False,
|
|
source_root: str | Path | None = None,
|
|
) -> List[Path]:
|
|
"""Validate and stage files into raw/ before indexing.
|
|
|
|
``source_root``, when given, is the root a caller is syncing from
|
|
(e.g. a linked folder). Any source file found under it is staged at
|
|
the same path *relative to that root*, instead of being flattened to
|
|
its bare filename: without this, every externally-sourced subfolder
|
|
collapses into raw/'s top level and same-named files in different
|
|
subfolders collide.
|
|
"""
|
|
logger.info(f"Validating documents for '{self.kb_name}'...")
|
|
|
|
ingested_hashes = self.get_ingested_hashes()
|
|
files_to_process: list[Path] = []
|
|
resolved_source_root = Path(source_root).resolve() if source_root else None
|
|
|
|
for source in source_files:
|
|
source_path = Path(source)
|
|
if not source_path.exists() or not source_path.is_file():
|
|
logger.warning(f"Missing file: {source}")
|
|
continue
|
|
|
|
current_hash = self._get_file_hash(source_path)
|
|
if current_hash in ingested_hashes.values() and not allow_duplicates:
|
|
logger.info(f"Skipped (content already indexed): {source_path.name}")
|
|
continue
|
|
|
|
resolved_source = source_path.resolve()
|
|
|
|
# Files already saved under raw/ (e.g. by the upload route, possibly
|
|
# inside a folder) are indexed in place — never flattened to the
|
|
# basename — so the uploaded folder structure is preserved verbatim.
|
|
if resolved_source.is_relative_to(self.raw_dir.resolve()):
|
|
files_to_process.append(source_path)
|
|
continue
|
|
|
|
if resolved_source_root and resolved_source.is_relative_to(resolved_source_root):
|
|
dest_path = self.raw_dir / resolved_source.relative_to(resolved_source_root)
|
|
else:
|
|
dest_path = self.raw_dir / source_path.name
|
|
|
|
staged_path, copied = stage_source(source_path, dest_path, source_hash=current_hash)
|
|
if copied:
|
|
logger.info(f"Staged to raw: {staged_path.relative_to(self.raw_dir).as_posix()}")
|
|
else:
|
|
logger.info(f"Recovering staged file: {source_path.name}")
|
|
files_to_process.append(staged_path)
|
|
|
|
return files_to_process
|
|
|
|
async def process_new_documents(self, new_files: List[Path]) -> DocumentIndexResult:
|
|
"""Index staged files via the KB's bound provider."""
|
|
if not new_files:
|
|
return DocumentIndexResult(processed_files=[], failures=[])
|
|
|
|
if self.rag_provider == LIGHTRAG_PROVIDER:
|
|
return await self._process_lightrag_batch(new_files)
|
|
# LlamaIndex publishes a full snapshot on every add_documents call.
|
|
# One call for the whole staged list keeps folder sync at one version
|
|
# instead of one version per file (#2173). A single file stays on the
|
|
# per-file path, and a batch that throws falls back there so one bad
|
|
# file does not discard the others.
|
|
if self.rag_provider == DEFAULT_PROVIDER and len(new_files) > 1:
|
|
return await self._process_llamaindex_batch(new_files)
|
|
return await self._process_provider_files_one_by_one(new_files)
|
|
|
|
async def _process_llamaindex_batch(self, new_files: List[Path]) -> DocumentIndexResult:
|
|
"""Index every staged file in one LlamaIndex publish."""
|
|
requested = {str(path.resolve()): path for path in new_files}
|
|
confirmed: list[Path] = []
|
|
seen: set[str] = set()
|
|
callback_fired = False
|
|
|
|
def on_indexed(paths: list[str]) -> None:
|
|
nonlocal callback_fired
|
|
callback_fired = True
|
|
for raw in paths:
|
|
try:
|
|
key = str(Path(raw).resolve())
|
|
except OSError:
|
|
continue
|
|
path = requested.get(key)
|
|
if path is None or key in seen:
|
|
continue
|
|
seen.add(key)
|
|
confirmed.append(path)
|
|
|
|
rag_service = RAGService(kb_base_dir=str(self.base_dir), provider=self.rag_provider)
|
|
total_files = len(new_files)
|
|
|
|
def on_progress(current: int, total: int) -> None:
|
|
if self.progress_tracker is None:
|
|
return
|
|
from deeptutor.knowledge.progress_tracker import ProgressStage
|
|
|
|
self.progress_tracker.update(
|
|
ProgressStage.PROCESSING_DOCUMENTS,
|
|
message_key="Embedding batches: {{current}}/{{total}}",
|
|
message_params={"current": current, "total": total},
|
|
current=current,
|
|
total=total,
|
|
)
|
|
|
|
if self.progress_tracker is not None:
|
|
from deeptutor.knowledge.progress_tracker import ProgressStage
|
|
|
|
self.progress_tracker.update(
|
|
ProgressStage.PROCESSING_FILE,
|
|
message_key="Queued {{count}} files for indexing.",
|
|
message_params={"count": total_files},
|
|
current=0,
|
|
total=total_files,
|
|
)
|
|
try:
|
|
success = await rag_service.add_documents(
|
|
self.kb_name,
|
|
[str(path) for path in new_files],
|
|
indexed_file_callback=on_indexed,
|
|
progress_callback=on_progress,
|
|
task_id=self.progress_tracker.task_id if self.progress_tracker is not None else "",
|
|
)
|
|
except PermissionError as exc:
|
|
logger.exception("Permission denied while indexing a LlamaIndex batch: %s", exc)
|
|
raise DataVolumePermissionError(
|
|
format_data_volume_permission_error(self.kb_dir, cause=exc)
|
|
) from exc
|
|
except Exception as exc:
|
|
logger.warning(
|
|
"LlamaIndex batch of %d file(s) failed (%s); retrying one file at a time",
|
|
total_files,
|
|
exc,
|
|
)
|
|
return await self._process_provider_files_one_by_one(new_files)
|
|
|
|
if not success:
|
|
return DocumentIndexResult(
|
|
processed_files=[],
|
|
failures=[
|
|
DocumentIndexFailure(path, "Provider returned failure without details.")
|
|
for path in new_files
|
|
],
|
|
)
|
|
# Pipelines that do not report per-file receipts still indexed the
|
|
# batch. A receipt that omits a file means that file was skipped.
|
|
processed = list(new_files) if not callback_fired else confirmed
|
|
failures = [
|
|
DocumentIndexFailure(path, "Provider skipped this file.")
|
|
for path in new_files
|
|
if path not in processed
|
|
]
|
|
recorded: list[Path] = []
|
|
for path in processed:
|
|
try:
|
|
await asyncio.to_thread(self._record_successful_hash, path)
|
|
except Exception as exc:
|
|
logger.exception("Failed %s: %s", path.name, exc)
|
|
failures.append(DocumentIndexFailure(path, str(exc)))
|
|
continue
|
|
recorded.append(path)
|
|
logger.info("Processed: %s", path.name)
|
|
if self.progress_tracker is not None:
|
|
from deeptutor.knowledge.progress_tracker import ProgressStage
|
|
|
|
self.progress_tracker.update(
|
|
ProgressStage.PROCESSING_FILE,
|
|
message_key="Successfully processed {{count}} files!",
|
|
message_params={"count": len(recorded)},
|
|
current=len(recorded),
|
|
total=total_files,
|
|
)
|
|
return DocumentIndexResult(processed_files=recorded, failures=failures)
|
|
|
|
async def _process_provider_files_one_by_one(
|
|
self, new_files: List[Path]
|
|
) -> DocumentIndexResult:
|
|
"""Index each staged file with its own provider call.
|
|
|
|
PageIndex and GraphRAG stay on this path. LlamaIndex uses it for a
|
|
single file, and as the fallback when a multi-file publish throws.
|
|
"""
|
|
rag_service = RAGService(kb_base_dir=str(self.base_dir), provider=self.rag_provider)
|
|
processed_files: list[Path] = []
|
|
failures: list[DocumentIndexFailure] = []
|
|
total_files = len(new_files)
|
|
|
|
for doc_file in new_files:
|
|
try:
|
|
if self.progress_tracker is not None:
|
|
from deeptutor.knowledge.progress_tracker import ProgressStage
|
|
|
|
self.progress_tracker.update(
|
|
ProgressStage.PROCESSING_FILE,
|
|
message_key="Indexing {{name}}",
|
|
message_params={"name": doc_file.name},
|
|
current=len(processed_files),
|
|
total=total_files,
|
|
)
|
|
|
|
success = await rag_service.add_documents(self.kb_name, [str(doc_file)])
|
|
if success:
|
|
processed_files.append(doc_file)
|
|
# Re-hashes the whole file, so it stays off the event loop:
|
|
# this runs inside a FastAPI BackgroundTasks entry, which is
|
|
# awaited on the loop and would otherwise stall unrelated
|
|
# requests once per indexed file (#777).
|
|
try:
|
|
await asyncio.to_thread(self._record_successful_hash, doc_file)
|
|
except Exception:
|
|
if self.rag_provider != LIGHTRAG_PROVIDER:
|
|
raise
|
|
logger.warning(
|
|
"LightRAG published '%s' before its duplicate-detection hash "
|
|
"was recorded",
|
|
doc_file.name,
|
|
exc_info=True,
|
|
)
|
|
logger.info(f"Processed: {doc_file.name}")
|
|
if self.progress_tracker is not None:
|
|
self.progress_tracker.update(
|
|
ProgressStage.PROCESSING_FILE,
|
|
message_key="Indexed {{name}}",
|
|
message_params={"name": doc_file.name},
|
|
current=len(processed_files),
|
|
total=total_files,
|
|
)
|
|
else:
|
|
error = "Provider returned failure without details."
|
|
failures.append(DocumentIndexFailure(doc_file, error))
|
|
logger.error(f"Failed to index: {doc_file.name}")
|
|
except PermissionError as e:
|
|
logger.exception("Permission denied while indexing %s: %s", doc_file.name, e)
|
|
raise DataVolumePermissionError(
|
|
format_data_volume_permission_error(self.kb_dir, cause=e)
|
|
) from e
|
|
except Exception as e:
|
|
logger.exception(f"Failed {doc_file.name}: {e}")
|
|
failures.append(DocumentIndexFailure(doc_file, str(e)))
|
|
|
|
return DocumentIndexResult(processed_files=processed_files, failures=failures)
|
|
|
|
async def _process_lightrag_batch(self, files: List[Path]) -> DocumentIndexResult:
|
|
"""Hold native write ownership across the entire accepted batch."""
|
|
from deeptutor.services.rag.pipelines.lightrag.pipeline import LightRagBatchError
|
|
|
|
def prepare_publication(root: Path) -> None:
|
|
from deeptutor.knowledge.progress_tracker import ProgressStage, ProgressTracker
|
|
|
|
tracker = self.progress_tracker or ProgressTracker(self.kb_name, self.base_dir)
|
|
tracker.update(
|
|
ProgressStage.COMPLETED,
|
|
message="Document indexing complete",
|
|
current=len(files),
|
|
total=len(files),
|
|
indexed_count=len(files),
|
|
index_changed=True,
|
|
index_action="upload",
|
|
publication_version=root.name,
|
|
)
|
|
tracker.verify_terminal(
|
|
current=len(files),
|
|
total=len(files),
|
|
indexed_count=len(files),
|
|
index_action="upload",
|
|
publication_version=root.name,
|
|
)
|
|
|
|
service = RAGService(kb_base_dir=str(self.base_dir), provider=LIGHTRAG_PROVIDER)
|
|
try:
|
|
success = await service.add_documents(
|
|
self.kb_name,
|
|
[str(path) for path in files],
|
|
accepted_indexing_snapshot=self.accepted_indexing_snapshot,
|
|
before_publish=prepare_publication,
|
|
)
|
|
if not success:
|
|
raise RuntimeError("LightRAG returned failure without details.")
|
|
processed = files
|
|
failures = []
|
|
except LightRagBatchError as exc:
|
|
processed = [path for path in files if path.name in exc.outcome.processed]
|
|
failures = [
|
|
DocumentIndexFailure(path, str(exc)) for path in files if path not in processed
|
|
]
|
|
except Exception as exc:
|
|
return DocumentIndexResult([], [DocumentIndexFailure(path, str(exc)) for path in files])
|
|
for path in processed:
|
|
try:
|
|
await asyncio.to_thread(self._record_successful_hash, path)
|
|
except Exception:
|
|
logger.warning(
|
|
"Indexed document hash could not be recorded: %s", path.name, exc_info=True
|
|
)
|
|
return DocumentIndexResult(processed, failures)
|
|
|
|
def _record_successful_hash(self, file_path: Path) -> None:
|
|
file_hash = self._get_file_hash(file_path)
|
|
metadata = _read_metadata(self.metadata_file)
|
|
hash_key = _raw_hash_key(file_path, self.raw_dir)
|
|
metadata.setdefault("file_hashes", {})[hash_key] = file_hash
|
|
_write_metadata(self.metadata_file, metadata)
|
|
|
|
def update_metadata(self, added_count: int) -> None:
|
|
"""Update metadata after incremental add."""
|
|
metadata: dict = {}
|
|
if self.metadata_file.exists():
|
|
try:
|
|
with open(self.metadata_file, "r", encoding="utf-8") as f:
|
|
metadata = json.load(f)
|
|
except Exception:
|
|
metadata = {}
|
|
|
|
metadata["rag_provider"] = self.rag_provider
|
|
metadata["needs_reindex"] = False
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
metadata["last_updated"] = timestamp
|
|
if added_count > 0:
|
|
metadata["last_indexed_at"] = timestamp
|
|
metadata["last_indexed_count"] = added_count
|
|
metadata["last_indexed_action"] = "upload"
|
|
|
|
history = metadata.get("update_history", [])
|
|
history.append(
|
|
{
|
|
"timestamp": metadata["last_updated"],
|
|
"action": "incremental_add",
|
|
"count": added_count,
|
|
"provider": self.rag_provider,
|
|
}
|
|
)
|
|
metadata["update_history"] = history
|
|
|
|
_write_metadata(self.metadata_file, metadata)
|
|
|
|
|
|
async def _bootstrap_index_from_files(
|
|
kb_name: str,
|
|
source_files: list[str],
|
|
base_dir: str,
|
|
manager: "KnowledgeBaseManager",
|
|
) -> int:
|
|
"""Create a fresh index for an empty KB from the given source files.
|
|
|
|
Called when :class:`DocumentAdder` rejects an add because the KB has no
|
|
existing index (it was created empty, e.g. via the no-files fast path or
|
|
a web/GitHub source sync before any documents were indexed). Uses
|
|
:meth:`RAGService.initialize` to build the index in one batch, then
|
|
records file hashes so subsequent incremental adds can detect duplicates.
|
|
"""
|
|
rag_service = RAGService(kb_base_dir=base_dir)
|
|
kb_dir = Path(base_dir) / kb_name
|
|
raw_dir = kb_dir / "raw"
|
|
metadata_file = kb_dir / "metadata.json"
|
|
|
|
requested = {str(Path(path).resolve()): str(Path(path)) for path in source_files}
|
|
confirmed: set[str] = set()
|
|
|
|
def on_indexed_file(paths: list[str]) -> None:
|
|
for path in paths:
|
|
canonical = str(Path(path).resolve())
|
|
if canonical in requested:
|
|
confirmed.add(canonical)
|
|
|
|
success = await rag_service.initialize(
|
|
kb_name=kb_name,
|
|
file_paths=source_files,
|
|
indexed_file_callback=on_indexed_file,
|
|
)
|
|
if not success:
|
|
raise RuntimeError(
|
|
f"Failed to initialize index for KB '{kb_name}' from {len(source_files)} file(s)"
|
|
)
|
|
|
|
indexed_files = [requested[path] for path in sorted(confirmed)]
|
|
indexed = len(indexed_files)
|
|
provider = rag_service._resolve_provider(kb_name)
|
|
try:
|
|
# Record hashes so future syncs detect unchanged files.
|
|
metadata = _read_metadata(metadata_file)
|
|
metadata.setdefault("file_hashes", {}).update(
|
|
hashes_for_indexed_files(indexed_files, raw_dir)
|
|
)
|
|
metadata["rag_provider"] = provider
|
|
metadata["needs_reindex"] = False
|
|
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
metadata["last_updated"] = ts
|
|
metadata["last_indexed_at"] = ts
|
|
metadata["last_indexed_count"] = indexed
|
|
metadata["last_indexed_action"] = "create"
|
|
_write_metadata(metadata_file, metadata)
|
|
|
|
manager.update_kb_status(
|
|
name=kb_name,
|
|
status="ready",
|
|
progress={
|
|
"stage": "completed",
|
|
"message": f"Initialized index with {indexed} file(s).",
|
|
"percent": 100,
|
|
"current": indexed,
|
|
"total": max(indexed, 1),
|
|
"file_name": "",
|
|
"error": None,
|
|
"timestamp": datetime.now().isoformat(),
|
|
"indexed_count": indexed,
|
|
"index_changed": True,
|
|
"index_action": "create",
|
|
},
|
|
)
|
|
except Exception:
|
|
if provider != LIGHTRAG_PROVIDER:
|
|
raise
|
|
logger.warning(
|
|
"LightRAG bootstrap for '%s' published before bookkeeping failed",
|
|
kb_name,
|
|
exc_info=True,
|
|
)
|
|
logger.info("Bootstrapped index for empty KB '%s' with %d file(s)", kb_name, indexed)
|
|
return indexed
|
|
|
|
|
|
async def add_documents(
|
|
kb_name: str,
|
|
source_files: list[str],
|
|
base_dir: str = DEFAULT_BASE_DIR,
|
|
api_key: Optional[str] = None,
|
|
base_url: Optional[str] = None,
|
|
allow_duplicates: bool = False,
|
|
accepted_indexing_snapshot=None,
|
|
) -> int:
|
|
"""Convenience function used by CLI wrappers."""
|
|
from deeptutor.knowledge.manager import KnowledgeBaseManager
|
|
|
|
manager = KnowledgeBaseManager(base_dir=base_dir)
|
|
published_count = 0
|
|
try:
|
|
manager.update_kb_status(
|
|
name=kb_name,
|
|
status="processing",
|
|
progress={
|
|
"stage": "processing_documents",
|
|
"message": "Processing uploaded documents...",
|
|
"percent": 0,
|
|
"current": 0,
|
|
"total": max(len(source_files), 1),
|
|
"file_name": "",
|
|
"error": None,
|
|
"timestamp": datetime.now().isoformat(),
|
|
},
|
|
)
|
|
|
|
try:
|
|
adder = DocumentAdder(
|
|
kb_name=kb_name,
|
|
base_dir=base_dir,
|
|
api_key=api_key,
|
|
base_url=base_url,
|
|
accepted_indexing_snapshot=accepted_indexing_snapshot,
|
|
)
|
|
except ValueError as exc:
|
|
if "not initialized" not in str(exc).lower() or not source_files:
|
|
raise
|
|
# Empty KB (created without initial documents): bootstrap the
|
|
# index from these files instead of erroring.
|
|
return await _bootstrap_index_from_files(
|
|
kb_name=kb_name,
|
|
source_files=source_files,
|
|
base_dir=base_dir,
|
|
manager=manager,
|
|
)
|
|
|
|
new_files = adder.add_documents(source_files, allow_duplicates=allow_duplicates)
|
|
if not new_files:
|
|
manager.update_kb_status(
|
|
name=kb_name,
|
|
status="ready",
|
|
progress={
|
|
"stage": "completed",
|
|
"message": "No new unique documents to process.",
|
|
"percent": 100,
|
|
"current": 1,
|
|
"total": 1,
|
|
"file_name": "",
|
|
"error": None,
|
|
"timestamp": datetime.now().isoformat(),
|
|
},
|
|
)
|
|
return 0
|
|
result = await adder.process_new_documents(new_files)
|
|
if result.has_failures:
|
|
raise RuntimeError(
|
|
f"Failed to index {result.failed_count}/{len(new_files)} file(s): "
|
|
f"{result.failure_summary()}"
|
|
)
|
|
if adder.rag_provider == LIGHTRAG_PROVIDER:
|
|
published_count = result.processed_count
|
|
adder.update_metadata(result.processed_count)
|
|
|
|
manager.update_kb_status(
|
|
name=kb_name,
|
|
status="ready",
|
|
progress={
|
|
"stage": "completed",
|
|
"message": f"Successfully processed {result.processed_count} files!",
|
|
"percent": 100,
|
|
"current": result.processed_count,
|
|
"total": max(len(new_files), 1),
|
|
"file_name": "",
|
|
"error": None,
|
|
"timestamp": datetime.now().isoformat(),
|
|
"indexed_count": result.processed_count,
|
|
"index_changed": result.processed_count > 0,
|
|
"index_action": "upload",
|
|
},
|
|
)
|
|
return result.processed_count
|
|
except Exception as exc:
|
|
if published_count:
|
|
logger.warning(
|
|
"LightRAG append for '%s' published before bookkeeping failed",
|
|
kb_name,
|
|
exc_info=True,
|
|
)
|
|
return published_count
|
|
manager.update_kb_status(
|
|
name=kb_name,
|
|
status="error",
|
|
progress={
|
|
"stage": "error",
|
|
"message": "Document upload failed",
|
|
"percent": 0,
|
|
"current": 0,
|
|
"total": max(len(source_files), 1),
|
|
"file_name": "",
|
|
"error": str(exc),
|
|
"timestamp": datetime.now().isoformat(),
|
|
},
|
|
)
|
|
raise
|
|
|
|
|
|
async def main() -> None:
|
|
try:
|
|
llm_config = resolve_llm_runtime_config()
|
|
default_api_key = llm_config.api_key
|
|
default_base_url = llm_config.effective_url
|
|
except Exception:
|
|
default_api_key = ""
|
|
default_base_url = ""
|
|
|
|
parser = argparse.ArgumentParser(description="Incrementally add documents to a KB")
|
|
parser.add_argument("kb_name", help="KB Name")
|
|
parser.add_argument("--docs", nargs="+", help="Files")
|
|
parser.add_argument("--docs-dir", help="Directory")
|
|
parser.add_argument("--base-dir", default=DEFAULT_BASE_DIR)
|
|
parser.add_argument("--api-key", default=default_api_key)
|
|
parser.add_argument("--base-url", default=default_base_url)
|
|
parser.add_argument("--allow-duplicates", action="store_true")
|
|
|
|
args = parser.parse_args()
|
|
doc_files: list[str] = []
|
|
if args.docs:
|
|
doc_files.extend(args.docs)
|
|
if args.docs_dir:
|
|
p = Path(args.docs_dir)
|
|
doc_files.extend(str(f) for f in FileTypeRouter.collect_supported_files(p))
|
|
|
|
if not doc_files:
|
|
logger.error("No documents provided.")
|
|
return
|
|
|
|
processed_count = await add_documents(
|
|
kb_name=args.kb_name,
|
|
source_files=doc_files,
|
|
base_dir=args.base_dir,
|
|
api_key=args.api_key,
|
|
base_url=args.base_url,
|
|
allow_duplicates=args.allow_duplicates,
|
|
)
|
|
|
|
if processed_count:
|
|
logger.info(f"Done! Successfully added {processed_count} documents.")
|
|
else:
|
|
logger.info("No new unique documents to add.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|