File size: 9,379 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0

"""Build and inspect the ImageJ FAISS knowledge base.

The index is rebuilt as one coherent artifact. Incremental updates are intentionally
not supported because FAISS deletion/replacement must keep the docstore, index, and
manifest in lockstep.
"""

import argparse
import hashlib
import json
import sys
from datetime import datetime
from pathlib import Path
from typing import Any, Sequence

from langchain_community.document_loaders import PyMuPDFLoader, TextLoader, UnstructuredMarkdownLoader
from langchain_community.vectorstores import FAISS
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter

PROJECT_ROOT = Path(__file__).parent.parent.resolve()
sys.path.insert(0, str(PROJECT_ROOT))

from copilotj.core.config import get_home  # noqa: E402
from copilotj.core.embedding import get_embeddings  # noqa: E402
from copilotj.core.kb import _file_hash, ensure_faiss_index, export_jsonl, save_rebuild_hash  # noqa: E402

HOME = get_home()
DEFAULT_DATA_DIR = HOME / "data"
DEFAULT_INDEX_DIR = HOME / "assets" / "knowledge_base"
DEFAULT_MANIFEST_DIR = DEFAULT_INDEX_DIR
INDEX_NAME = "knowledge_base"
MANIFEST_FILE = "rag_manifest.json"
SUPPORTED_EXTENSIONS = (".md", ".markdown", ".pdf", ".txt")
CHUNK_SIZE = 1000
CHUNK_OVERLAP = 200
HASH_READ_SIZE = 65536


def get_file_hash(filepath: Path) -> str:
    hasher = hashlib.md5()
    with filepath.open("rb") as handle:
        for chunk in iter(lambda: handle.read(HASH_READ_SIZE), b""):
            hasher.update(chunk)
    return hasher.hexdigest()


def to_relative_path(filepath: Path) -> str:
    try:
        return str(filepath.resolve().relative_to(HOME))
    except ValueError:
        return str(filepath.resolve())


def get_supported_files(data_dir: Path) -> list[Path]:
    files = [
        path
        for path in data_dir.rglob("*")
        if path.is_file() and path.suffix.lower() in SUPPORTED_EXTENSIONS and not _is_hidden(path)
    ]
    return sorted(files)


def _is_hidden(path: Path) -> bool:
    return any(part.startswith(".") for part in path.parts)


def load_documents_from_file(filepath: Path) -> list[Document]:
    loader = _new_loader(filepath)
    documents = loader.load()
    for document in documents:
        document.metadata["source"] = to_relative_path(filepath)
        document.metadata["filename"] = filepath.name
        document.metadata["extension"] = filepath.suffix.lower()
    return documents


def _new_loader(filepath: Path):
    suffix = filepath.suffix.lower()
    if suffix == ".pdf":
        return PyMuPDFLoader(str(filepath))
    if suffix == ".txt":
        return TextLoader(str(filepath), encoding="utf-8")
    if suffix in {".md", ".markdown"}:
        return UnstructuredMarkdownLoader(str(filepath))
    raise ValueError(f"Unsupported file type: {filepath}")


def split_documents(documents: list[Document]) -> list[Document]:
    splitter = RecursiveCharacterTextSplitter(
        chunk_size=CHUNK_SIZE,
        chunk_overlap=CHUNK_OVERLAP,
        separators=["\n\n", "\n", ". ", " ", ""],
    )
    return splitter.split_documents(documents)


def build_rag(data_dir: Path = DEFAULT_DATA_DIR, index_dir: Path = DEFAULT_INDEX_DIR) -> None:
    files = get_supported_files(data_dir)
    if not files:
        raise ValueError(f"No supported files found in {data_dir}")

    documents = load_documents(files)
    chunks = split_documents(documents)
    if not chunks:
        raise ValueError(f"No text chunks created from {data_dir}")

    index_dir.mkdir(parents=True, exist_ok=True)
    vector_store = FAISS.from_documents(chunks, get_embeddings())
    vector_store.save_local(str(index_dir), index_name=INDEX_NAME)
    jsonl_path = index_dir / f"{INDEX_NAME}.jsonl.gz"
    export_jsonl(chunks, jsonl_path)
    save_rebuild_hash(index_dir, _file_hash(jsonl_path))
    save_manifest(DEFAULT_MANIFEST_DIR, build_manifest(files, len(chunks)))


def load_documents(files: list[Path]) -> list[Document]:
    documents = []
    for filepath in files:
        documents.extend(load_documents_from_file(filepath))
    return documents


def build_manifest(files: list[Path], total_chunks: int) -> dict[str, Any]:
    now = datetime.now().isoformat()
    return {
        "version": "1.0",
        "created": now,
        "last_updated": now,
        "files": {to_relative_path(filepath): get_file_metadata(filepath) for filepath in files},
        "stats": {"total_files": len(files), "total_chunks": total_chunks},
    }


def get_file_metadata(filepath: Path) -> dict[str, Any]:
    stat = filepath.stat()
    return {
        "path": to_relative_path(filepath),
        "hash": get_file_hash(filepath),
        "size": stat.st_size,
        "mtime": stat.st_mtime,
        "mtime_str": datetime.fromtimestamp(stat.st_mtime).isoformat(),
    }


def save_manifest(manifest_dir: Path, manifest: dict[str, Any]) -> None:
    manifest_dir.mkdir(parents=True, exist_ok=True)
    manifest_path = manifest_dir / MANIFEST_FILE
    with manifest_path.open("w", encoding="utf-8") as handle:
        json.dump(manifest, handle, indent=2)


def load_manifest(manifest_dir: Path) -> dict[str, Any]:
    manifest_path = manifest_dir / MANIFEST_FILE
    with manifest_path.open("r", encoding="utf-8") as handle:
        return json.load(handle)


def show_status(data_dir: Path = DEFAULT_DATA_DIR, index_dir: Path = DEFAULT_INDEX_DIR) -> None:
    paths = index_paths(index_dir)
    print("RAG System Status")
    print("=================")
    print(f"Index Directory: {index_dir}")
    print(f"  FAISS index: {_format_exists(paths['faiss'])}")
    print(f"  Document store: {_format_exists(paths['pkl'])}")
    print(f"  JSONL export: {_format_exists(paths['jsonl'])}")
    print(f"  Manifest: {_format_exists(paths['manifest'])}")

    if paths["manifest"].exists():
        manifest = load_manifest(DEFAULT_MANIFEST_DIR)
        print_manifest(manifest)
        print_change_summary(data_dir, manifest)


def index_paths(index_dir: Path) -> dict[str, Path]:
    return {
        "faiss": index_dir / f"{INDEX_NAME}.faiss",
        "pkl": index_dir / f"{INDEX_NAME}.pkl",
        "jsonl": index_dir / f"{INDEX_NAME}.jsonl.gz",
        "manifest": DEFAULT_MANIFEST_DIR / MANIFEST_FILE,
    }


def _format_exists(path: Path) -> str:
    return "exists" if path.exists() else "missing"


def print_manifest(manifest: dict[str, Any]) -> None:
    stats = manifest.get("stats", {})
    print("Manifest Info:")
    print(f"  Version: {manifest.get('version', 'unknown')}")
    print(f"  Created: {manifest.get('created', 'unknown')}")
    print(f"  Last Updated: {manifest.get('last_updated', 'unknown')}")
    print(f"  Total Files: {stats.get('total_files', 'unknown')}")
    print(f"  Total Chunks: {stats.get('total_chunks', 'unknown')}")


def print_change_summary(data_dir: Path, manifest: dict[str, Any]) -> None:
    changes = detect_changes(data_dir, manifest)
    print(f"Data Directory: {data_dir}")
    print(f"  Supported files found: {len(get_supported_files(data_dir))}")
    print(f"  New: {len(changes['new'])}")
    print(f"  Modified: {len(changes['modified'])}")
    print(f"  Deleted: {len(changes['deleted'])}")


def detect_changes(data_dir: Path, manifest: dict[str, Any]) -> dict[str, list[str]]:
    current_files = get_supported_files(data_dir)
    current_paths = {to_relative_path(path): path for path in current_files}
    manifest_files = manifest.get("files", {})

    return {
        "new": sorted(set(current_paths) - set(manifest_files)),
        "modified": sorted(_modified_paths(current_paths, manifest_files)),
        "deleted": sorted(set(manifest_files) - set(current_paths)),
    }


def _modified_paths(current_paths: dict[str, Path], manifest_files: dict[str, Any]) -> list[str]:
    modified = []
    for relative_path, filepath in current_paths.items():
        if relative_path not in manifest_files:
            continue
        if get_file_hash(filepath) != manifest_files[relative_path].get("hash"):
            modified.append(relative_path)
    return modified


def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--build", action="store_true", help="Rebuild the RAG index from the data directory")
    parser.add_argument("--rebuild", action="store_true", help="Rebuild FAISS index from JSONL export")
    parser.add_argument("--status", action="store_true", help="Show index and data directory status")
    parser.add_argument("--data-dir", type=Path, default=DEFAULT_DATA_DIR)
    parser.add_argument("--index-dir", type=Path, default=DEFAULT_INDEX_DIR)
    args = parser.parse_args(argv)

    if not args.build and not args.status and not args.rebuild:
        args.status = True
    return args


def main(argv: Sequence[str] | None = None) -> int:
    args = parse_args(argv)

    if args.build:
        build_rag(data_dir=args.data_dir, index_dir=args.index_dir)
        return 0

    if args.rebuild:
        ok = ensure_faiss_index(args.index_dir)
        return 0 if ok else 1

    show_status(data_dir=args.data_dir, index_dir=args.index_dir)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())