| |
| |
| |
| |
| |
| """Fable izolasyon deneyi: expand ↔ retrieval ayrıştır. 40 DEV × 3 kol × n=8, aynı-seed. |
| Kollar: NORAG / RAG-no-expand (RAG_DISABLE_EXPAND=1) / RAG-with-expand (mevcut).""" |
| import sys, os, json, re, argparse |
| os.environ["CHAT_MODEL"] = "qwen3:8b" |
| os.environ["RAG_EXPAND_WB"] = "0" |
| os.environ["RAG_SORT_DISTANCE"] = "0" |
| sys.path.insert(0, "/path/to/kadim") |
| import test_regression as T |
| import kadim_core |
| from config.model_config import MODEL_CONFIG |
| from config.settings import Settings |
|
|
| HELD = {"ACL-02","ACL-06","NRL-02","DAH-01","DAH-04","DAH-05","DAH-06","JNK-02", |
| "URO-01","URO-02","KBB-01","KRD-04","KRD-05","DRM-01","ENF-02","NEF-02", |
| "PED-02","PSK-02","GAS-02","GNL-01"} |
| DEV = [c for c in T.TEST_CASES if c["id"] not in HELD] |
| OPT = MODEL_CONFIG.get_options("clinical"); OPT["temperature"]=0.1 |
|
|
| def primary_section(ans): |
| m = re.search(r"Olas[ıi]\s*Tan[ıi]lar", ans, re.I) |
| return ans[m.end():m.end()+300] if m else ans[:280] |
| def grp_hit(text, group): |
| low=text.lower(); return any(t.lower() in low for t in group) |
|
|
| def run_rag(q): |
| kadim_core._rag_cache._cache.clear() |
| return kadim_core.rag_query(query=q, model_url=Settings.CHAT_URL, options=OPT, |
| k=Settings.DEFAULT_TOP_K, api_key=Settings.OPENWEBUI_API_KEY, mode="auto") |
| def run_norag(q): |
| mode=kadim_core.PROMPTS.detect_mode(q) |
| specs=kadim_core.route_multi_specialty(q, max_specs=2) |
| base=kadim_core.PROMPTS.get_system_prompt(mode, specialties=specs) |
| a=kadim_core.ollama_chat(base+"\n\n(Not: Context bulunamadı)", q, "qwen3:8b", OPT, Settings.CHAT_URL, Settings.OPENWEBUI_API_KEY) |
| return kadim_core.clean_response(a) |
|
|
| def main(): |
| ap=argparse.ArgumentParser(); ap.add_argument("--runs",type=int,default=8); args=ap.parse_args() |
| N=args.runs; res={} |
| for ci,c in enumerate(DEV,1): |
| cid,q,must=c["id"],c["query"],c.get("must_contain",[]) |
| g0=must[0] if must else None |
| row={"NORAG":0,"NOEXP":0,"EXP":0} |
| for r in range(N): |
| OPT["seed"]=42+ci*100+r |
| |
| a=run_norag(q) |
| if g0 and grp_hit(primary_section(a),g0): row["NORAG"]+=1 |
| |
| os.environ["RAG_DISABLE_EXPAND"]="1" |
| a=run_rag(q); a=a if isinstance(a,str) else a.get("answer","") |
| if g0 and grp_hit(primary_section(a),g0): row["NOEXP"]+=1 |
| |
| os.environ["RAG_DISABLE_EXPAND"]="0" |
| a=run_rag(q); a=a if isinstance(a,str) else a.get("answer","") |
| if g0 and grp_hit(primary_section(a),g0): row["EXP"]+=1 |
| res[cid]=row |
| json.dump(res, open("/path/to/kadim/logs/rag_isolation_3col.json","w"), ensure_ascii=False, indent=1) |
| print(f"[{ci}/{len(DEV)}] {cid:8} NORAG={row['NORAG']} NOEXP={row['NOEXP']} EXP={row['EXP']} (n={N})") |
| |
| tot=len(res)*N |
| sN=sum(r["NORAG"] for r in res.values()); sX=sum(r["NOEXP"] for r in res.values()); sE=sum(r["EXP"] for r in res.values()) |
| print(f"\n=== İZOLASYON ÖZET ({len(res)} vaka × n={N} = {tot}) ===") |
| print(f"NORAG : {sN}/{tot} ({sN/tot:.0%})") |
| print(f"RAG no-expand : {sX}/{tot} ({sX/tot:.0%})") |
| print(f"RAG with-expand : {sE}/{tot} ({sE/tot:.0%})") |
| print(f"\n--- expand'in ETKİSİ (EXP - NOEXP, vaka bazı |Δ|>=2) ---") |
| for cid,r in res.items(): |
| d=r["EXP"]-r["NOEXP"] |
| if abs(d)>=2: print(f" {cid:8} EXP={r['EXP']} NOEXP={r['NOEXP']} (Δ{d:+d}) {'expand ZARAR' if d<0 else 'expand YARDIM'}") |
| print(f"\n--- retrieval'in ETKİSİ (NOEXP - NORAG, vaka bazı |Δ|>=2) ---") |
| for cid,r in res.items(): |
| d=r["NOEXP"]-r["NORAG"] |
| if abs(d)>=2: print(f" {cid:8} NOEXP={r['NOEXP']} NORAG={r['NORAG']} (Δ{d:+d}) {'retrieval ZARAR' if d<0 else 'retrieval YARDIM'}") |
|
|
| if __name__=="__main__": main() |
|
|