-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_update.py
More file actions
178 lines (153 loc) · 6.04 KB
/
Copy pathdata_update.py
File metadata and controls
178 lines (153 loc) · 6.04 KB
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
from __future__ import annotations
import argparse
import shutil
from pathlib import Path
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Clean raw semiconductor documents, chunk them, and sync embeddings into ChromaDB.",
)
parser.add_argument(
"--rebuild",
action="store_true",
help="Clear processed outputs and rebuild the full vector index from data/raw.",
)
return parser
def main() -> int:
parser = build_parser()
args = parser.parse_args()
from rag_support import (
MANIFEST_PATH,
PROCESSED_DIR,
RAW_DIR,
build_chunk_records,
collection_document_count,
delete_chunk_ids,
doc_id_for_path,
embed_texts,
ensure_data_directories,
get_pipeline_fingerprint,
get_chroma_client,
get_or_create_collection,
load_embedding_model,
load_environment,
list_raw_files,
prepare_document,
processed_path_for_doc_id,
read_manifest,
reset_collection,
sha256_file,
sha256_text,
source_type_for_path,
upsert_chunk_records,
utc_now_iso,
write_manifest,
)
load_environment()
ensure_data_directories()
raw_files = list_raw_files(RAW_DIR)
if not raw_files:
print("No supported files found under data/raw. Add .md, .txt, or .pdf files first.")
return 0
manifest = read_manifest()
documents: dict[str, dict] = manifest.get("documents", {})
pipeline_fingerprint = get_pipeline_fingerprint()
client = get_chroma_client()
if args.rebuild:
if PROCESSED_DIR.exists():
for child in PROCESSED_DIR.iterdir():
if child.is_file():
child.unlink()
elif child.is_dir():
shutil.rmtree(child)
reset_collection(client)
manifest = {"version": 2, "pipeline_fingerprint": pipeline_fingerprint, "documents": {}}
documents = manifest["documents"]
collection = get_or_create_collection(client)
embedding_model = load_embedding_model()
current_doc_ids: set[str] = set()
added_count = 0
updated_count = 0
unchanged_count = 0
for raw_path in raw_files:
doc_id = doc_id_for_path(raw_path)
current_doc_ids.add(doc_id)
raw_hash = sha256_file(raw_path)
existing = documents.get(doc_id)
processed_path = processed_path_for_doc_id(doc_id, PROCESSED_DIR)
unchanged = (
existing
and existing.get("raw_hash") == raw_hash
and processed_path.exists()
and existing.get("processed_path") == processed_path.as_posix()
and existing.get("pipeline_fingerprint") == pipeline_fingerprint
)
if unchanged and not args.rebuild:
unchanged_count += 1
continue
source_type = source_type_for_path(raw_path)
prepared_document = prepare_document(raw_path, source_type)
if not prepared_document.content_text.strip():
print(f"Skipping empty document: {raw_path}")
continue
processed_path.write_text(prepared_document.processed_text, encoding="utf-8")
chunk_records = build_chunk_records(
prepared_document.content_text,
raw_path,
source_type,
prepared_document.metadata,
)
embeddings = embed_texts(embedding_model, [record.text for record in chunk_records])
if existing:
delete_chunk_ids(collection, existing.get("chunk_ids", []))
upsert_chunk_records(collection, chunk_records, embeddings)
documents[doc_id] = {
"doc_id": doc_id,
"raw_path": raw_path.as_posix(),
"raw_hash": raw_hash,
"processed_path": processed_path.as_posix(),
"processed_hash": sha256_text(prepared_document.processed_text),
"updated_at": utc_now_iso(),
"pipeline_fingerprint": pipeline_fingerprint,
"source_type": source_type,
"source_name": prepared_document.metadata.get("source_name", ""),
"source_date": prepared_document.metadata.get("source_date", ""),
"source_kind": prepared_document.metadata.get("source_kind", ""),
"source_url": prepared_document.metadata.get("source_url", ""),
"license_note": prepared_document.metadata.get("license_note", ""),
"compliance_note": prepared_document.metadata.get("compliance_note", ""),
"primary_references": prepared_document.metadata.get("primary_references", ""),
"chunk_ids": [record.chunk_id for record in chunk_records],
"chunk_count": len(chunk_records),
"title": prepared_document.metadata.get("title", raw_path.stem),
}
if existing:
updated_count += 1
print(f"Updated: {raw_path.name} ({len(chunk_records)} chunks)")
else:
added_count += 1
print(f"Added: {raw_path.name} ({len(chunk_records)} chunks)")
removed_count = 0
for doc_id in sorted(set(documents) - current_doc_ids):
existing = documents.pop(doc_id)
delete_chunk_ids(collection, existing.get("chunk_ids", []))
processed_path = Path(existing.get("processed_path", ""))
if processed_path.exists():
processed_path.unlink()
removed_count += 1
print(f"Removed: {doc_id}")
manifest["version"] = 2
manifest["pipeline_fingerprint"] = pipeline_fingerprint
manifest["documents"] = documents
write_manifest(manifest)
print("")
print("Sync complete")
print(f"- Raw files scanned: {len(raw_files)}")
print(f"- Added: {added_count}")
print(f"- Updated: {updated_count}")
print(f"- Unchanged: {unchanged_count}")
print(f"- Removed: {removed_count}")
print(f"- Indexed chunks in collection: {collection_document_count(collection)}")
print(f"- Manifest: {MANIFEST_PATH}")
return 0
if __name__ == "__main__":
raise SystemExit(main())