Repository navigation
Expand file tree
/
Copy pathrag_app.py
More file actions
204 lines (173 loc) · 7.21 KB
/
Copy pathrag_app.py
File metadata and controls
204 lines (173 loc) · 7.21 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
#!/usr/bin/env python3
"""A complete RAG pipeline over real documentation — zero dependencies.
Retrieval-augmented generation against XERJ's own manual: strips the HTML
docs that ship in this repo (landing/docs/*.html), chunks them with
overlap, indexes every chunk into a `semantic_text` field (XERJ embeds at
ingest — no API keys needed), and answers questions with hybrid retrieval
(BM25 + semantic, RRF-fused) plus citations.
Works fully offline. If you export an OpenAI-compatible chat endpoint the
final answer is generated by an LLM grounded in the retrieved chunks;
otherwise the recipe prints the retrieved passages (extractive mode).
Run it:
xerj --insecure --data-dir ./data & # start XERJ
python3 recipes/rag_app.py # index + eval + one answer
python3 recipes/rag_app.py "your question here"
Optional env:
XERJ_URL (default http://localhost:9200)
LLM_ENDPOINT OpenAI-compatible /v1/chat/completions URL
LLM_API_KEY bearer token for LLM_ENDPOINT
LLM_MODEL (default gpt-4o-mini)
"""
import html.parser
import json
import os
import pathlib
import re
import sys
import urllib.error
import urllib.request
XERJ = os.environ.get("XERJ_URL", "http://localhost:9200")
DOCS = pathlib.Path(__file__).resolve().parent.parent / "landing" / "docs"
INDEX = "xerj-manual"
CHUNK_CHARS, OVERLAP = 900, 150
def call(method: str, path: str, body=None):
data = None
headers = {}
if body is not None:
data = body.encode() if isinstance(body, str) else json.dumps(body).encode()
headers["Content-Type"] = (
"application/x-ndjson" if isinstance(body, str) else "application/json"
)
req = urllib.request.Request(f"{XERJ}{path}", data=data, headers=headers, method=method)
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read())
# ── 1. Turn the shipped HTML docs into plain text ───────────────────────────
class TextExtractor(html.parser.HTMLParser):
"""Keep visible text from <main>, drop scripts/styles/nav."""
SKIP = {"script", "style", "nav", "footer", "header"}
def __init__(self):
super().__init__()
self.parts, self._skip_depth, self.title = [], 0, ""
self._in_title = False
def handle_starttag(self, tag, attrs):
if tag in self.SKIP:
self._skip_depth += 1
if tag == "title":
self._in_title = True
def handle_endtag(self, tag):
if tag in self.SKIP and self._skip_depth:
self._skip_depth -= 1
if tag == "title":
self._in_title = False
def handle_data(self, data):
if self._in_title:
self.title += data
elif not self._skip_depth:
self.parts.append(data)
def page_text(path: pathlib.Path):
p = TextExtractor()
p.feed(path.read_text(errors="ignore"))
text = re.sub(r"\s+", " ", " ".join(p.parts)).strip()
title = p.title.split("·")[-1].strip() or path.stem
return title, text
def chunk(text: str):
"""Overlapping chunks, preferring sentence boundaries — the same idea
XERJ's internal ingest chunker uses."""
out, start = [], 0
while start < len(text):
end = min(start + CHUNK_CHARS, len(text))
if end < len(text):
cut = text.rfind(". ", start, end)
if cut > start + CHUNK_CHARS // 2:
end = cut + 1
out.append(text[start:end].strip())
if end >= len(text):
break
start = end - OVERLAP
return out
def build_index():
try:
call("DELETE", f"/{INDEX}")
except urllib.error.HTTPError:
pass
call("PUT", f"/{INDEX}", {
"mappings": {
"properties": {
"page": {"type": "keyword"},
"title": {"type": "keyword"},
"text": {"type": "semantic_text", "dimensions": 384},
}
}
})
lines, n_pages = [], 0
for f in sorted(DOCS.glob("*.html")):
title, text = page_text(f)
if len(text) < 200:
continue
n_pages += 1
for i, c in enumerate(chunk(text)):
lines.append(json.dumps({"index": {"_index": INDEX, "_id": f"{f.stem}-{i}"}}))
lines.append(json.dumps({"page": f.stem, "title": title, "text": c}))
call("POST", "/_bulk?refresh=true", "\n".join(lines) + "\n")
print(f"indexed {len(lines) // 2} chunks from {n_pages} real docs pages\n")
# ── 2. Retrieval: hybrid = BM25 + semantic, fused with RRF ──────────────────
def retrieve(question: str, k: int = 4):
hits = call("POST", f"/{INDEX}/_search", {
"size": k,
"query": {"hybrid": {"queries": [
{"query": {"match": {"text": question}}, "weight": 1.0},
{"query": {"semantic": {"field": "text", "query": question, "k": 20}}, "weight": 1.0},
]}},
})["hits"]["hits"]
return [(h["_source"]["page"], h["_source"]["title"], h["_source"]["text"]) for h in hits]
# ── 3. Generation: LLM if configured, extractive otherwise ─────────────────
def answer(question: str):
ctx = retrieve(question)
print(f"Q: {question}\n")
endpoint = os.environ.get("LLM_ENDPOINT")
if endpoint:
prompt = "Answer strictly from the context.\n\n" + "\n---\n".join(t for _, _, t in ctx)
req = urllib.request.Request(
endpoint,
5F83
data=json.dumps({
"model": os.environ.get("LLM_MODEL", "gpt-4o-mini"),
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": question},
],
}).encode(),
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('LLM_API_KEY', '')}",
},
)
with urllib.request.urlopen(req) as resp:
print(json.loads(resp.read())["choices"][0]["message"]["content"], "\n")
else:
print("(no LLM_ENDPOINT set — extractive mode)\n")
for page, title, text in ctx[:2]:
print(f" {text[:300]}…\n")
print("sources:", ", ".join(dict.fromkeys(f"docs/{p}.html" for p, _, _ in ctx)), "\n")
# ── 4. A tiny honest retrieval eval on real questions ──────────────────────
EVAL = [
("how do I run xerj as a systemd service?", "install"),
("which quantization modes exist for vectors?", "vectors"),
("how do I migrate an existing Elasticsearch index?", "migration-from-es"),
("what does the bool query's filter clause do?", "queries"),
("how do I take a backup to S3?", "backup-restore"),
]
def run_eval():
ok = 0
for q, expect in EVAL:
pages = [p for p, _, _ in retrieve(q, k=3)]
hit = expect in pages
ok += hit
print(f" {'✓' if hit else '✗'} {q} → {pages}")
print(f"\nretrieval hit@3: {ok}/{len(EVAL)}\n")
if __name__ == "__main__":
build_index()
if len(sys.argv) > 1:
answer(" ".join(sys.argv[1:]))
else:
run_eval()
answer("how do I keep memory usage down on a vector-heavy index?")