Spaces:
Running
Running
File size: 11,881 Bytes
54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 4dd34e4 c43d43b 4dd34e4 c43d43b 4dd34e4 54ce8e5 4dd34e4 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 c43d43b 54ce8e5 | 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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | """
Knowledge Universe β Block 3 Gap Analysis (Cycle 2)
=====================================================
Run: python scripts/gap_analysis.py
Cycle 2 auto-grader fix:
The Cycle 1 auto-grader gave HuggingFace RLHF datasets a grade of 0
because "Anthropic/hh-rlhf" doesn't contain all the words
"reward model training" in the dataset name.
Fix: HuggingFace datasets are graded by whether their name/title
contains ANY of the significant query words β not all of them.
"hh-rlhf" contains "rlhf" β grade 2 for RLHF query.
"Anthropic/hh-rlhf" from Anthropic for RLHF query β grade 2.
Also: "Generalisation of RLHF under Reward Shift" arXiv paper
contains "RLHF" and "Reward" β grade 3, not 1.
"""
import os
import httpx
import time
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("API_KEY")
BASE = "http://localhost:8000"
TEST_QUERIES = [
(
"mixture of experts architecture", 4,
"Expect: arxiv papers on MoE (sparse, gating, routing). "
"Bug if: OpenLibrary returns building-architecture books."
),
(
"Claude 3.5 sonnet", 2,
"Fast-moving topic. Expect: recent blog posts, GitHub repos, YouTube. "
"Bug if: anything labeled 'decayed' ranks in top 3."
),
(
"LangChain streaming callbacks", 3,
"Practical coding. Expect: GitHub + StackOverflow specifically about "
"streaming callbacks. Bug if: generic LangChain results with no "
"streaming or callback mention."
),
(
"RLHF reward model training", 5,
"Research-heavy. Expect: arxiv papers, difficulty 4-5 results. "
"Bug if: beginner YouTube tutorials rank above research papers."
),
(
"what is machine learning", 1,
"Beginner. Expect: Wikipedia, YouTube explainers, difficulty 1-2. "
"Bug if: arxiv papers with difficulty 4+ dominate top 3."
),
]
GRADING = """
GRADE each result 0-3:
3 = Exactly what an expert would recommend
2 = Relevant, not the best
1 = Tangentially related
0 = Irrelevant (e.g. restaurant recommendation paper for ML query)
"""
def auto_grade(source: dict, topic: str, requested_difficulty: int) -> int:
"""
Improved auto-grader:
- Checks title AND summary for topic word matches
- HuggingFace datasets: any 1 topic word match = grade 2
- arXiv: match in title = grade 3, match in summary = grade 2
- Difficulty mismatch > 2 = grade 0 regardless
"""
title = (source.get("title") or "").lower()
summary = (source.get("summary") or "").lower()
platform = source.get("source_platform", "")
diff = source.get("difficulty", 3)
topic_lower = topic.lower()
topic_words = [
w for w in topic_lower.split()
if len(w) > 3 and w not in {
"what", "does", "how", "the", "and", "for", "with", "from"
}
]
# Hard penalty for severe difficulty mismatch
diff_gap = abs(int(diff) - requested_difficulty)
if diff_gap > 2:
return 0
# Check matches in title and summary
title_matches = sum(1 for w in topic_words if w in title)
summary_matches = sum(1 for w in topic_words if w in summary)
total_matches = title_matches + summary_matches
# Platform-specific grading
if platform == "arxiv":
if title_matches >= 2:
return 3 # Multiple topic words in arXiv title β perfect
elif title_matches >= 1 or summary_matches >= 2:
return 2 # Partial title or strong summary match
elif summary_matches >= 1:
return 1
return 0
if platform == "wikipedia":
if title_matches >= 1:
return 3 # Wikipedia article directly about the topic
elif summary_matches >= 1:
return 2
return 1 # Wikipedia is always somewhat relevant
if platform == "stackoverflow":
if title_matches >= 2:
return 3
elif title_matches >= 1:
return 2
elif summary_matches >= 1:
return 1
return 0
if platform == "github":
if title_matches >= 2:
return 3
elif title_matches >= 1 or summary_matches >= 2:
return 2
elif summary_matches >= 1:
return 1
return 0
if platform == "youtube":
if title_matches >= 2:
return 3
elif title_matches >= 1:
return 2
elif summary_matches >= 1:
return 1
return 0
if platform == "huggingface":
# HuggingFace datasets: any 1 word match in name = relevant
# "Anthropic/hh-rlhf" β contains "rlhf" β grade 2 for RLHF query
if title_matches >= 1 or summary_matches >= 1:
return 2
return 0
if platform == "kaggle":
if title_matches >= 2:
return 3
elif title_matches >= 1:
return 2
elif summary_matches >= 1:
return 1
return 0
if platform == "mit_ocw":
# MIT OCW courses: if topic is in their description, it's grade 2
if total_matches >= 1:
return 2
return 1 # MIT OCW is always somewhat relevant for ML queries
# Default: any match = 1, good match = 2
if title_matches >= 1:
return 2
elif summary_matches >= 1:
return 1
return 0
def run_query(topic, difficulty):
resp = httpx.post(
f"{BASE}/v1/discover",
headers={"X-API-Key": API_KEY},
json={
"topic": topic,
"difficulty": difficulty,
"formats": ["pdf", "github", "jupyter", "video", "stackoverflow", "html"],
"max_results": 10,
},
timeout=90,
)
return resp.json()
def flag_issues(sources, topic, difficulty):
flags = []
platforms = [s.get("source_platform", "") for s in sources]
# 1. OpenLibrary mismatch for ML queries
ol = [s for s in sources if s.get("source_platform") == "openlibrary"]
if ol:
flags.append(f"β OpenLibrary: {[s.get('title', '')[:45] for s in ol]}")
# 2. Stale content on fast-moving topics
fast_keywords = ["claude", "sonnet", "gpt-4", "gemini", "llama 3", "mistral"]
if any(kw in topic.lower() for kw in fast_keywords):
stale = [
s for s in sources
if s.get("decay_report", {}).get("label") in ("stale", "decayed")
]
if stale:
flags.append(
f"β {len(stale)}/10 stale/decayed on fast topic: "
f"{[s.get('title', '')[:35] for s in stale[:2]]}"
)
# 3. Difficulty ceiling violations (should be 0 after fix)
wrong = [
s for s in sources
if abs(s.get("difficulty", 3) - difficulty) > 2
]
if wrong:
wrong_desc = [
str(s.get("source_platform", "")) + " d=" + str(s.get("difficulty"))
for s in wrong[:3]
]
flags.append(f"β Difficulty ceiling violated: {wrong_desc}")
# 4. Platform dominance
for p in set(platforms):
if not p: continue
count = platforms.count(p)
if count > 4:
flags.append(f"β {p} dominates: {count}/10 results")
# 5. Result count too low
if len(sources) < 7:
flags.append(
f"β LOW RESULTS: only {len(sources)}/10 returned. "
f"Platforms: {list(set(platforms))}"
)
# 6. Compound query specific check
if "streaming" in topic.lower() and "callback" in topic.lower():
streaming_hits = [
s for s in sources
if "streaming" in (s.get("title") or "").lower()
or "callback" in (s.get("title") or "").lower()
]
if len(streaming_hits) < 2:
flags.append(
f"β COMPOUND QUERY: only {len(streaming_hits)} results "
f"mention 'streaming' or 'callback' in title"
)
return flags
def main():
print("KU API GAP ANALYSIS β BLOCK 3 CYCLE 2")
print("=" * 62)
print(GRADING)
print("TIP: Auto-grades shown. Override manually if wrong.")
print(" Any query with avg < 2.0 is a bug to fix.\n")
summary = []
total_auto_grades = []
for topic, difficulty, note in TEST_QUERIES:
print(f"\n{'β'*62}")
print(f"QUERY: '{topic}'")
print(f"DIFFICULTY: {difficulty}")
print(f"EXPECTED: {note}")
print(f"{'β'*62}")
start = time.time()
try:
data = run_query(topic, difficulty)
ms = round((time.time() - start) * 1000)
sources = data.get("sources", [])
cache = data.get("cache_hit", False)
print(f"Returned {len(sources)} results in {ms}ms cache={cache}")
print()
query_grades = []
for i, s in enumerate(sources[:5], 1):
d = s.get("decay_report") or {}
diff_delta = abs(s.get("difficulty", 3) - difficulty)
diff_icon = "β" if diff_delta <= 1 else f"β dΒ±{diff_delta}"
auto_g = auto_grade(s, topic, difficulty)
query_grades.append(auto_g)
total_auto_grades.append(auto_g)
grade_icon = "β
" if auto_g >= 2 else ("π‘" if auto_g == 1 else "π΄")
print(
f" [{i}] {diff_icon:<8} "
f"platform={s.get('source_platform', 'unknown'):<16} "
f"quality={s.get('quality_score', 0):<5} "
f"decay={d.get('decay_score', '?')} "
f"({d.get('label', '?')}, {d.get('age_days', '?')}d)"
)
print(f" {s.get('title', '')[:62]}")
print(f" AUTO-GRADE: {grade_icon} {auto_g}/3 | MANUAL GRADE: [ /3]")
print()
avg = sum(query_grades) / len(query_grades) if query_grades else 0
verdict = "β
PASS" if avg >= 2.0 else "β FAIL"
print(f" Auto avg: {avg:.1f}/3 β {verdict}")
flags = flag_issues(sources, topic, difficulty)
if flags:
print("\n AUTO-FLAGS:")
for f in flags:
print(f" {f}")
summary.append({
"query": topic,
"difficulty": difficulty,
"count": len(sources),
"ms": ms,
"avg_grade": avg,
"verdict": verdict,
"flags": flags,
})
except httpx.ConnectError:
print(" β Cannot connect. Run: uvicorn src.api.main:app --reload --port 8000")
except Exception as e:
print(f" β Error: {e}")
# Summary table
print(f"\n{'='*62}")
print("BLOCK 3 CYCLE 2 SUMMARY")
print(f"{'='*62}")
print(f"{'Query':<40} {'Results':>8} {'ms':>6} {'Avg':>5} {'Verdict':>8}")
print("-" * 62)
for s in summary:
print(
f"{s['query'][:40]:<40} "
f"{s['count']:>8} "
f"{s['ms']:>6} "
f"{s['avg_grade']:>5.1f} "
f"{s['verdict']:>8}"
)
passed = sum(1 for s in summary if "PASS" in s.get("verdict", ""))
total = len(summary)
overall_avg = (
sum(total_auto_grades) / len(total_auto_grades)
if total_auto_grades else 0
)
print(f"\n{'='*62}")
print(
f"OVERALL: {passed}/{total} queries pass | "
f"Avg grade: {overall_avg:.2f}/3"
)
if passed == total:
print("π Block 3 complete. Record the demo video. Post to Reddit.")
else:
print(f"β {total - passed} queries still failing. Check AUTO-FLAGS above.")
print(f"{'='*62}")
if __name__ == "__main__":
main() |