Download tests/eval_02_entities.py from recommender-system/llmproccrs: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/recommender-system/llmproccrs/resolve/main/tests/eval_02_entities.py
- Command line
-
hf download hf://recommender-system/llmproccrs/tests/eval_02_entities.py
-
curl -L -o eval_02_entities.py https://huggingface.co/recommender-system/llmproccrs/resolve/main/tests/eval_02_entities.py
10.7 kB
| import sys | |
| import os | |
| import json | |
| import csv | |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | |
| from modules.user_modeling.extractor import EntityExtractor | |
| def normalize_val(v): | |
| if isinstance(v, str): return v.lower().strip() | |
| return v | |
| def compare_json(predicted, expected): | |
| errors = [] | |
| match_count = 0 | |
| total_keys = 0 | |
| for key, exp_val in expected.items(): | |
| total_keys += 1 | |
| pred_val = predicted.get(key) | |
| if exp_val is None: | |
| if pred_val is not None: errors.append(f"{key}: Exp None vs Got {pred_val}") | |
| else: match_count += 1 | |
| continue | |
| # Handling khusus untuk Price (Sudah benar di kode awal) | |
| if key == "price" and isinstance(exp_val, dict): | |
| if not isinstance(pred_val, dict): | |
| errors.append(f"Price Type Mismatch") | |
| continue | |
| p_op = normalize_val(pred_val.get("operator")) | |
| e_op = normalize_val(exp_val.get("operator")) | |
| p_val = int(pred_val.get("value", 0)) if pred_val.get("value") else 0 | |
| e_val = int(exp_val.get("value", 0)) if exp_val.get("value") else 0 | |
| if p_op == e_op and abs(p_val - e_val) < 1000: match_count += 1 | |
| else: errors.append(f"Price mismatch") | |
| continue | |
| # --- PERBAIKAN DI SINI --- | |
| # Jika nilai adalah integer (seperti pada 'quantity'), bandingkan langsung secara numerik | |
| if isinstance(exp_val, int): | |
| try: | |
| if int(pred_val) == exp_val: match_count += 1 | |
| else: errors.append(f"{key} mismatch (int)") | |
| except (ValueError, TypeError): | |
| errors.append(f"{key} type error") | |
| continue | |
| # Normalisasi ke string untuk perbandingan teks | |
| p_str = str(normalize_val(pred_val)) if pred_val is not None else "" | |
| e_str = str(normalize_val(exp_val)) | |
| if e_str == p_str or e_str in p_str or p_str in e_str: | |
| match_count += 1 | |
| else: | |
| errors.append(f"{key} mismatch") | |
| score = match_count / total_keys if total_keys > 0 else 1.0 | |
| return score, errors | |
| def run_eval_ner(): | |
| print("\n" + "="*70) | |
| print("📊 EVALUASI SKENARIO 2: NER (With CSV Report)") | |
| print("="*70) | |
| # Init Output | |
| report_dir = os.path.join(os.path.dirname(__file__), "reports") | |
| os.makedirs(report_dir, exist_ok=True) | |
| # csv_file = os.path.join(report_dir, "report_02_ner_mistral_single_hop.csv") | |
| csv_file = os.path.join(report_dir, "report_02_ner_gemma3_single_hop.csv") | |
| # csv_file = os.path.join(report_dir, "report_02_ner_qwen3_single_hop.csv") | |
| extractor = EntityExtractor() | |
| # Data A | |
| one_hop_cases = [ | |
| { | |
| "input": "Carikan laptop Lenovo buat staf administrasi, RAM 8GB cukup, budget maksimal 7 juta, lokasi Solo.", | |
| "expect": { | |
| "product": "laptop", | |
| "brand": "lenovo", | |
| "specs": "ram 8gb", | |
| "price": { "value": 7000000, "operator": "<=" }, | |
| "location": "solo" | |
| } | |
| }, | |
| { | |
| "input": "Butuh pengadaan kursi tunggu besi untuk puskesmas, area Banyumas.", | |
| "expect": { | |
| "product": "kursi tunggu", | |
| "material": "besi", | |
| "use_case": "puskesmas", | |
| "location": "banyumas" | |
| } | |
| }, | |
| { | |
| "input": "Cari printer laser hitam putih buat kantor kelurahan, yang hemat toner.", | |
| "expect": { | |
| "product": "printer", | |
| "type": "laser", | |
| "color": "monokrom", | |
| "use_case": "kantor kelurahan" | |
| } | |
| }, | |
| { | |
| "input": "Mau pengadaan proyektor minimal 4000 lumens buat ruang rapat di Medan.", | |
| "expect": { | |
| "product": "proyektor", | |
| "specs": "4000 lumens", | |
| "use_case": "ruang rapat", | |
| "location": "medan" | |
| } | |
| }, | |
| { | |
| "input": "Tolong carikan jasa konsultan IT untuk audit sistem informasi.", | |
| "expect": { | |
| "service": "jasa konsultan it", | |
| "topic": "audit sistem informasi" | |
| } | |
| }, | |
| { | |
| "input": "Butuh komputer desktop i5 untuk lab SMK, kirim ke Garut.", | |
| "expect": { | |
| "product": "komputer desktop", | |
| "specs": "intel i5", | |
| "use_case": "lab smk", | |
| "location": "garut" | |
| } | |
| }, | |
| { | |
| "input": "Cari pengadaan genset 5000 watt untuk kantor kecamatan di Sumbawa.", | |
| "expect": { | |
| "product": "genset", | |
| "specs": "5000 watt", | |
| "use_case": "kantor kecamatan", | |
| "location": "sumbawa" | |
| } | |
| }, | |
| { | |
| "input": "Ada jasa pelatihan Microsoft Excel tingkat lanjut untuk pegawai?", | |
| "expect": { | |
| "service": "jasa pelatihan", | |
| "topic": "microsoft excel", | |
| "level": "lanjutan", | |
| "target_user": "pegawai" | |
| } | |
| }, | |
| { | |
| "input": "Pengadaan lemari arsip besi 4 pintu buat kantor dinas.", | |
| "expect": { | |
| "product": "lemari arsip", | |
| "material": "besi", | |
| "specs": "4 pintu", | |
| "use_case": "kantor dinas" | |
| } | |
| }, | |
| { | |
| "input": "Cari CCTV indoor 4 channel, lokasi pemasangan di Cilegon.", | |
| "expect": { | |
| "product": "cctv", | |
| "type": "indoor", | |
| "specs": "4 channel", | |
| "location": "cilegon" | |
| } | |
| }, | |
| { | |
| "input": "Butuh jasa pemeliharaan jaringan komputer untuk kantor pajak.", | |
| "expect": { | |
| "service": "jasa pemeliharaan", | |
| "object": "jaringan komputer", | |
| "organization_type": "kantor pajak" | |
| } | |
| }, | |
| { | |
| "input": "Carikan ATK spidol dan papan tulis untuk sekolah dasar.", | |
| "expect": { | |
| "category": "atk", | |
| "product": "spidol atau papan tulis", | |
| "use_case": "sekolah dasar" | |
| } | |
| }, | |
| { | |
| "input": "Mau beli mesin fotokopi second yang masih layak, area Bekasi.", | |
| "expect": { | |
| "product": "mesin fotokopi", | |
| "condition": "second", | |
| "location": "bekasi" | |
| } | |
| }, | |
| { | |
| "input": "Cari laptop HP layar 14 inci buat operator data.", | |
| "expect": { | |
| "product": "laptop", | |
| "brand": "hp", | |
| "specs": "layar 14 inci", | |
| "user_role": "operator data" | |
| } | |
| }, | |
| { | |
| "input": "Pengadaan software antivirus lisensi 1 tahun untuk 50 komputer.", | |
| "expect": { | |
| "product": "software antivirus", | |
| "license_duration": "1 tahun", | |
| "quantity": 50 | |
| } | |
| }, | |
| { | |
| "input": "Butuh jasa pembuatan aplikasi absensi pegawai berbasis web.", | |
| "expect": { | |
| "service": "jasa pembuatan aplikasi", | |
| "application_type": "absensi pegawai", | |
| "platform": "web" | |
| } | |
| }, | |
| { | |
| "input": "Carikan meja rapat panjang minimal 3 meter untuk ruang sidang.", | |
| "expect": { | |
| "product": "meja rapat", | |
| "specs": "panjang 3 meter", | |
| "use_case": "ruang sidang" | |
| } | |
| }, | |
| { | |
| "input": "Cari AC standing 5 PK untuk aula serbaguna di Purwokerto.", | |
| "expect": { | |
| "product": "ac", | |
| "type": "standing", | |
| "specs": "5 pk", | |
| "location": "purwokerto" | |
| } | |
| }, | |
| { | |
| "input": "Pengadaan banner dan backdrop acara sosialisasi.", | |
| "expect": { | |
| "product": "banner atau backdrop", | |
| "use_case": "acara sosialisasi" | |
| } | |
| }, | |
| { | |
| "input": "Butuh jasa katering untuk kegiatan pelatihan 2 hari.", | |
| "expect": { | |
| "service": "jasa katering", | |
| "event": "pelatihan", | |
| "duration": "2 hari" | |
| } | |
| }, | |
| { | |
| "input": "Cari laptop Dell SSD minimal 512GB, lokasi Padang.", | |
| "expect": { | |
| "product": "laptop", | |
| "brand": "dell", | |
| "specs": "ssd 512gb", | |
| "location": "padang" | |
| } | |
| }, | |
| { | |
| "input": "Pengadaan helm proyek SNI untuk kegiatan konstruksi.", | |
| "expect": { | |
| "product": "helm proyek", | |
| "standard": "sni", | |
| "use_case": "konstruksi" | |
| } | |
| }, | |
| { | |
| "input": "Butuh jasa sewa mobil dinas untuk kunjungan lapangan.", | |
| "expect": { | |
| "service": "jasa sewa mobil", | |
| "vehicle_type": "mobil dinas", | |
| "use_case": "kunjungan lapangan" | |
| } | |
| }, | |
| { | |
| "input": "Cari switch jaringan 24 port untuk server room.", | |
| "expect": { | |
| "product": "switch jaringan", | |
| "specs": "24 port", | |
| "use_case": "server room" | |
| } | |
| }, | |
| { | |
| "input": "Mau pengadaan tenda dan kursi untuk acara desa.", | |
| "expect": { | |
| "product": "tenda atau kursi", | |
| "use_case": "acara desa" | |
| } | |
| }, | |
| { | |
| "input": "Butuh jasa cleaning service untuk gedung 3 lantai.", | |
| "expect": { | |
| "service": "jasa cleaning service", | |
| "object": "gedung", | |
| "specs": "3 lantai" | |
| } | |
| }, | |
| { | |
| "input": "Carikan harddisk eksternal 1TB buat backup data.", | |
| "expect": { | |
| "product": "harddisk eksternal", | |
| "specs": "1tb", | |
| "use_case": "backup data" | |
| } | |
| }, | |
| { | |
| "input": "Pengadaan papan nama kantor berbahan akrilik.", | |
| "expect": { | |
| "product": "papan nama kantor", | |
| "material": "akrilik" | |
| } | |
| }, | |
| { | |
| "input": "Cari jasa dokumentasi foto dan video untuk kegiatan dinas.", | |
| "expect": { | |
| "service": "jasa dokumentasi", | |
| "media": "foto atau video", | |
| "organization_type": "dinas" | |
| } | |
| } | |
| ] | |
| with open(csv_file, mode='w', newline='', encoding='utf-8') as f: | |
| writer = csv.writer(f) | |
| writer.writerow(["Scenario", "Input User", "Expected JSON", "Predicted JSON", "Score", "Errors"]) | |
| # Run A | |
| print("[A] Running One-Hop...") | |
| for case in one_hop_cases: | |
| state = extractor.run({"user_text": case["input"], "collected_constraints": {}}) | |
| res = state["collected_constraints"] | |
| score, errs = compare_json(res, case["expect"]) | |
| writer.writerow(["One-Hop", case["input"], json.dumps(case["expect"]), json.dumps(res), f"{score:.2f}", str(errs)]) | |
| print(f" Score: {score:.2f} | {case['input'][:40]}...") | |
| print("-" * 75) | |
| print(f"✅ Report saved to: {csv_file}") | |
| print("="*70) | |
| if __name__ == "__main__": | |
| run_eval_ner() |