Spaces:
Sleeping
Sleeping
File size: 9,690 Bytes
4d068e8 93acdf7 4d068e8 93acdf7 27bcc7f 93acdf7 4d068e8 93acdf7 176a09c 4419533 176a09c 93acdf7 176a09c 4d068e8 176a09c 4d068e8 fd8c579 27bcc7f f7f504f 176a09c fd8c579 176a09c 3ee432b fd8c579 176a09c fd8c579 27bcc7f de72d5d fd8c579 27bcc7f fd8c579 de72d5d fd8c579 de72d5d fd8c579 27bcc7f fd8c579 27bcc7f fd8c579 93acdf7 fd8c579 176a09c 27bcc7f 176a09c de72d5d fd8c579 de72d5d 27bcc7f 176a09c fd8c579 27bcc7f 3ee432b fd8c579 3ee432b 93acdf7 fd8c579 93acdf7 de72d5d fd8c579 3ee432b 27bcc7f 3ee432b fd8c579 3ee432b de72d5d 93acdf7 de72d5d fd8c579 93acdf7 fd8c579 de72d5d fd8c579 93acdf7 fd8c579 93acdf7 fd8c579 de72d5d fd8c579 de72d5d fd8c579 de72d5d fd8c579 de72d5d fd8c579 de72d5d fd8c579 de72d5d fd8c579 27bcc7f 93acdf7 27bcc7f fd8c579 4419533 fd8c579 27bcc7f fd8c579 27bcc7f de72d5d fd8c579 de72d5d fd8c579 93acdf7 fd8c579 27bcc7f fd8c579 de72d5d 27bcc7f de72d5d fd8c579 de72d5d fd8c579 de72d5d 27bcc7f fd8c579 27bcc7f fd8c579 de72d5d 27bcc7f fd8c579 de72d5d fd8c579 27bcc7f |
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
import os
import re
import time
import requests
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import gradio as gr
from bs4 import BeautifulSoup
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from rank_bm25 import BM25Okapi
from sentence_transformers import CrossEncoder
PERSIST_DIR = "k8s_chroma_db"
URLS = {
"pods": "https://kubernetes.io/docs/concepts/workloads/pods/",
"deployments": "https://kubernetes.io/docs/concepts/workloads/controllers/deployment/",
"services": "https://kubernetes.io/docs/concepts/services-networking/service/",
"namespaces": "https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/",
"nodes": "https://kubernetes.io/docs/concepts/architecture/nodes/",
"statefulsets": "https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/",
"rbac": "https://kubernetes.io/docs/reference/access-authn-authz/rbac/",
"persistent-volumes": "https://kubernetes.io/docs/concepts/storage/persistent-volumes/",
"ingress": "https://kubernetes.io/docs/concepts/services-networking/ingress/",
"autoscaling": "https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/",
}
# ----------------- SCRAPING + KB ----------------- #
def scrape_page(name, url):
try:
r = requests.get(url, timeout=20)
r.raise_for_status()
soup = BeautifulSoup(r.text, "html.parser")
content = soup.find("div", class_="td-content")
if not content:
return None
text = content.get_text(separator="\n").strip()
return Document(page_content=text, metadata={"doc_id": name, "url": url})
except Exception as e:
print(f"[ERROR] scraping {url}: {e}")
return None
def build_or_load_kb():
embedding_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
# If DB exists, load it
if os.path.isdir(PERSIST_DIR):
print("[INFO] Loading existing Chroma DB")
vectordb = Chroma(
embedding_function=embedding_model,
persist_directory=PERSIST_DIR,
)
raw = vectordb._collection.get(include=["documents", "metadatas"])
chunks = [
Document(page_content=doc, metadata=meta)
for doc, meta in zip(raw["documents"], raw["metadatas"])
]
return vectordb, chunks
# Else: scrape + build
print("[INFO] No DB found, scraping docs...")
docs = []
for name, url in URLS.items():
d = scrape_page(name, url)
if d:
docs.append(d)
print(f"[INFO] Scraped {len(docs)} docs")
splitter = RecursiveCharacterTextSplitter(chunk_size=900, chunk_overlap=200)
chunks = splitter.split_documents(docs)
vectordb = Chroma.from_documents(
chunks, embedding_model, persist_directory=PERSIST_DIR
)
return vectordb, chunks
vectordb, chunks = build_or_load_kb()
# ----------------- HYBRID SEARCH ----------------- #
bm25_corpus = [doc.page_content.split() for doc in chunks]
bm25 = BM25Okapi(bm25_corpus)
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-12-v2")
retriever = vectordb.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"k": 8, "score_threshold": 0.35},
)
def hybrid_search(query, top_k=5):
vector_results = retriever.invoke(query)
tokenized_query = query.lower().split()
bm25_scores = bm25.get_scores(tokenized_query)
bm25_ranked = sorted(zip(bm25_scores, chunks), key=lambda x: x[0], reverse=True)
bm25_results = [d for _, d in bm25_ranked[:top_k]]
combined = vector_results + bm25_results
seen = set()
unique = []
for d in combined:
key = (d.metadata.get("doc_id"), d.page_content[:80])
if key not in seen:
seen.add(key)
unique.append(d)
if not unique:
return []
pairs = [(query, doc.page_content) for doc in unique]
scores = reranker.predict(pairs)
ranked = sorted(zip(scores, unique), key=lambda x: x[0], reverse=True)[:top_k]
for s, doc in ranked:
doc.metadata["rerank_score"] = float(s)
return [doc for _, doc in ranked]
# ----------------- LLM CALL ----------------- #
def call_llm(prompt: str) -> str:
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
return "⚠️ Missing OPENROUTER_API_KEY in Space secrets.\nGroundedness: 0%"
try:
r = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"HTTP-Referer": "https://huggingface.co/",
"X-Title": "Kubernetes RAG Assistant",
},
json={
"model": "meta-llama/llama-3.1-8b-instruct",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 400,
"temperature": 0.0,
},
timeout=60,
)
r.raise_for_status()
data = r.json()
return data["choices"][0]["message"]["content"]
except Exception as e:
print("[ERROR] LLM:", e)
return f"⚠️ LLM error: {e}\nGroundedness: 0%"
# ----------------- CONTEXT + METRICS ----------------- #
def build_context(query: str):
docs = hybrid_search(query)
if not docs:
return "", [], []
context, sources, scores = "", [], []
for i, d in enumerate(docs, start=1):
label = f"[{i}]"
context += f"{label} {d.page_content[:900]}\nSource: {d.metadata['url']}\n\n"
sources.append(f"{label} → {d.metadata['url']}")
scores.append(d.metadata["rerank_score"])
return context, sources, scores
def classify_query(q: str) -> str:
q = q.lower()
if "how" in q:
return "how-to"
if "error" in q or "fail" in q:
return "debug"
return "general"
def init_metrics():
return {"q": [], "lat": [], "tok": [], "g": [], "r": [], "c": [], "t": []}
# global analytics, no gr.State
METRICS = init_metrics()
# ----------------- CHAT HANDLER ----------------- #
def answer_question(query, history):
global METRICS
if METRICS is None:
METRICS = init_metrics()
start = time.time()
ctx, sources, scores = build_context(query)
if not ctx:
reply = "Not in docs or insufficient context.\nGroundedness: 0%"
history.append((query, reply))
return history, ""
prompt = f"""
Use ONLY the context below to answer.
Every sentence MUST end with a citation like [1].
Question: {query}
Context:
{ctx}
At the end add a line: Groundedness: XX%
"""
answer = call_llm(prompt)
latency = time.time() - start
# robust groundedness parsing
grounded = 0
m = re.search(r"Groundedness:\s*(\d+)%", answer)
if m:
try:
grounded = int(m.group(1))
except ValueError:
grounded = 0
cites = len(set(re.findall(r"\[(\d+)\]", answer)))
avg_score = sum(scores) / len(scores) if scores else 0.0
tokens = len(answer.split()) + len(prompt.split())
alert = ""
if grounded < 70 or cites == 0:
alert = "⚠️ Low support from docs; please verify in official Kubernetes docs.\n\n"
final = alert + answer + "\n\n---\nSources:\n" + "\n".join(sources)
history.append((query, final))
METRICS["q"].append(query)
METRICS["lat"].append(latency)
METRICS["tok"].append(tokens)
METRICS["g"].append(grounded)
METRICS["r"].append(avg_score)
METRICS["c"].append(cites)
METRICS["t"].append(classify_query(query))
return history, ""
# ----------------- ANALYTICS HELPERS ----------------- #
def render_metrics():
if len(METRICS["q"]) == 0:
return [], 0.0, 0.0, 0.0
rows = []
for i, q in enumerate(METRICS["q"]):
rows.append([
i + 1,
q,
round(METRICS["lat"][i], 3),
METRICS["tok"][i],
METRICS["g"][i],
round(METRICS["r"][i], 3),
METRICS["c"][i],
METRICS["t"][i],
])
avg_ground = sum(METRICS["g"]) / len(METRICS["g"])
avg_lat = sum(METRICS["lat"]) / len(METRICS["lat"])
avg_tok = sum(METRICS["tok"]) / len(METRICS["tok"])
return rows, avg_ground, avg_lat, avg_tok
# ----------------- GRADIO UI ----------------- #
with gr.Blocks(title="Kubernetes RAG Assistant") as app:
gr.Markdown("# ☸ Kubernetes RAG Assistant")
with gr.Tab("Chat"):
chat = gr.Chatbot(height=450)
inp = gr.Textbox(label="Ask anything about Kubernetes")
clear_btn = gr.Button("Reset Conversation")
inp.submit(answer_question, [inp, chat], [chat, inp])
clear_btn.click(lambda: ([], ""), None, [chat, inp])
with gr.Tab("Analytics"):
gr.Markdown("### 📊 Query Analytics (this session)")
table = gr.DataFrame(
headers=[
"ID",
"Query",
"Latency (s)",
"Tokens",
"Groundedness (%)",
"Avg Rerank Score",
"Citations",
"Type",
],
interactive=False,
)
avgG = gr.Number(label="Avg Groundedness (%)")
avgL = gr.Number(label="Avg Latency (s)")
avgT = gr.Number(label="Avg Tokens")
refresh = gr.Button("Update Dashboard")
refresh.click(render_metrics, None, [table, avgG, avgL, avgT])
app.launch()
|