-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·256 lines (191 loc) · 4.73 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·256 lines (191 loc) · 4.73 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
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
#!/usr/bin/env bash
set -euo pipefail
########################################
# Defaults
########################################
DEPTH=5
WORKERS=4
BASE_DIR="llm-docs"
########################################
# Logging
########################################
timestamp() {
date "+%Y-%m-%d %H:%M:%S"
}
log() {
echo "[$(timestamp)] INFO $1"
}
success() {
echo "[$(timestamp)] OK $1"
}
error() {
echo "[$(timestamp)] ERROR $1"
exit 1
}
########################################
# CLI
########################################
usage() {
echo "Usage:"
echo " ./run.sh --name rabbitmq --url https://rabbitmq.com/docs"
exit 1
}
PROJECT=""
URL=""
while [[ $# -gt 0 ]]; do
case $1 in
--name)
PROJECT="$2"
shift 2
;;
--url)
URL="$2"
shift 2
;;
--depth)
DEPTH="$2"
shift 2
;;
--workers)
WORKERS="$2"
shift 2
;;
*)
usage
;;
esac
done
[[ -z "$PROJECT" || -z "$URL" ]] && usage
########################################
# Paths
########################################
ROOT="$BASE_DIR/$PROJECT"
SITE="$ROOT/site"
EXTRACT="$ROOT/extracted"
DOCS="$ROOT/docs"
LLM_FILE="$DOCS/$PROJECT-llm.txt"
RAG_FILE="$DOCS/$PROJECT-rag.jsonl"
########################################
# Dependency checks
########################################
log "Checking dependencies..."
command -v httrack >/dev/null || error "httrack not installed"
command -v python3 >/dev/null || error "python3 not installed"
python3 - <<EOF || error "Install python deps: pip install trafilatura tqdm"
import trafilatura
import tqdm
EOF
success "Dependencies satisfied"
########################################
# Workspace
########################################
log "Preparing workspace..."
mkdir -p "$SITE" "$EXTRACT" "$DOCS"
success "Workspace ready"
########################################
# Step 1 — Crawl docs
########################################
log "Mirroring documentation..."
httrack "$URL" \
-O "$SITE" \
"+${URL}/*" \
"-*/blog/*" \
"-*/news/*" \
"-*/archive/*" \
"-*.jpg" "-*.png" "-*.gif" "-*.pdf" \
--depth="$DEPTH" \
--quiet
success "Website mirrored"
########################################
# Step 2 — Extract clean text
########################################
log "Extracting documentation content..."
python3 <<PYTHON
import os
import json
import hashlib
from pathlib import Path
import trafilatura
from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor
SITE = "$SITE"
EXTRACT = "$EXTRACT"
WORKERS = $WORKERS
Path(EXTRACT).mkdir(exist_ok=True)
html_files = []
for root, _, files in os.walk(SITE):
for f in files:
if f.endswith(".html"):
html_files.append(os.path.join(root, f))
seen_hashes = set()
def process_file(path):
with open(path, "r", encoding="utf-8", errors="ignore") as f:
html = f.read()
result = trafilatura.extract(
html,
include_tables=True,
include_comments=False,
with_metadata=True
)
if not result:
return None
text = result
h = hashlib.md5(text.encode()).hexdigest()
if h in seen_hashes:
return None
seen_hashes.add(h)
title = trafilatura.extract_metadata(html)
title = title.title if title and title.title else Path(path).stem
return {
"title": title,
"content": text
}
docs = []
with ProcessPoolExecutor(max_workers=WORKERS) as ex:
for r in tqdm(ex.map(process_file, html_files), total=len(html_files)):
if r:
docs.append(r)
out = Path(EXTRACT) / "docs.json"
with open(out, "w") as f:
json.dump(docs, f)
print("Extracted", len(docs), "documents")
PYTHON
success "Extraction completed"
########################################
# Step 3 — Build LLM file
########################################
log "Generating LLM documentation..."
python3 <<PYTHON
import json
from pathlib import Path
DATA="$EXTRACT/docs.json"
LLM="$LLM_FILE"
RAG="$RAG_FILE"
URL="$URL"
PROJECT="$PROJECT"
docs=json.load(open(DATA))
docs=sorted(docs, key=lambda d: d["title"])
with open(LLM,"w") as out:
out.write(f"# {PROJECT} Documentation (LLM Optimized)\\n\\n")
out.write(f"Source: {URL}\\n\\n")
out.write("Each section represents one documentation page.\\n")
out.write("---\\n")
for d in docs:
out.write(f"\\n## {d['title']}\\n\\n")
out.write(d["content"])
out.write("\\n\\n---\\n")
with open(RAG,"w") as out:
for d in docs:
out.write(json.dumps(d)+"\\n")
print("LLM + RAG files created")
PYTHON
success "Documentation generated"
########################################
# Done
########################################
echo
echo "Output files:"
echo " $LLM_FILE"
echo " $RAG_FILE"
echo
success "Process completed"