Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-yardsticks/blob/main/yardsticks/mojollama-eval-runners/eval/bbh.py # aien-yardsticks aien-dev/aien-yardsticks Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-yardsticks / yardsticks / mojollama-eval-runners / eval / bbh.py` 128 lines · 4557 bytes Raw Blame History 1 """BBH — Big-Bench Hard benchmark. 2 3 27 challenging reasoning tasks (subset of BIG-Bench). 4 Few-shot CoT evaluation. 5 """ 6 7 import json 8 import re 9 import time 10 from typing import Optional 11 12 from .base import BaseBenchmark, LlamaCppEvaluator, normalize_answer 13 from .dataset import get_dataset_stream, DATASET_SOURCES 14 15 16 # Example for each BBH task 17 TASK_EXAMPLES = { 18 "boolean_expressions": { 19 "ex": "Question: (not True or False) and (True or False)\nAnswer: Let's think step by step. (not True) is False. (False or False) is False. (True or False) is True. False and True is False. So the answer is False.\nTherefore, the answer is False.", 20 }, 21 "causal_judgment": { 22 "ex": "Question: ...\nAnswer: Let's think step by step. ... Therefore, the answer is No.", 23 }, 24 } 25 26 27 def _build_bbh_prompt(task_name: str, question: str, cot: bool = True) -> str: 28 """Build BBH prompt with optional chain-of-thought.""" 29 examples = TASK_EXAMPLES.get(task_name, {}) 30 31 if cot and examples: 32 prompt = f"{examples['ex']}\n\nQuestion: {question}\nAnswer: Let's think step by step." 33 else: 34 prompt = f"Question: {question}\nAnswer:" 35 return prompt 36 37 38 def _extract_bbh_answer(text: str) -> str: 39 """Extract final answer from BBH output.""" 40 text = text.strip() 41 # Look for "the answer is X" patterns 42 patterns = [ 43 r'(?:answer is|so the answer is|therefore,?\s*(?:the\s+)?answer\s+is)\s*[:.]?\s*["\']?(\w+)["\']?', 44 r'(?:^|\n)\s*answer\s*:\s*["\']?(\w+)["\']?', 45 r'(\w+)\s*$', 46 ] 47 for pat in patterns: 48 m = re.search(pat, text, re.IGNORECASE) 49 if m: 50 return m.group(1).strip().strip('.,;:!?') 51 # Last word as fallback 52 words = text.split() 53 return words[-1].strip('.,;:!?') if words else "" 54 55 56 class BBHBenchmark(BaseBenchmark): 57 """BBH — Big-Bench Hard, 27 challenging reasoning tasks.""" 58 59 @property 60 def name(self) -> str: 61 return "BBH" 62 63 @property 64 def tasks(self) -> list: 65 return list(DATASET_SOURCES.get("bbh", {}).get("url_templates", {}).keys()) 66 67 def run(self, backend_url: str, model_name: str, 68 max_samples: int = 0, tasks: Optional[list] = None, 69 use_cot: bool = True, 70 **kwargs) -> dict: 71 evaluator = LlamaCppEvaluator(backend_url, temperature=0.0, max_tokens=256) 72 t0 = time.time() 73 74 all_tasks = tasks or self.tasks 75 per_category = {} 76 total_correct = 0 77 total_all = 0 78 79 for task_name in all_tasks: 80 records = get_dataset_stream("bbh", task_name) 81 if not records: 82 print(f" ⚠ No data for {task_name}, skipping") 83 per_category[task_name] = {"correct": 0, "total": 0, "accuracy": 0} 84 continue 85 86 if max_samples > 0: 87 records = records[:max_samples] 88 89 task_correct = 0 90 task_total = 0 91 92 for record in records: 93 question = record.get("input", record.get("question", "")) 94 target = record.get("target", record.get("answer", "")) 95 96 prompt = _build_bbh_prompt(task_name, question, use_cot) 97 98 try: 99 max_tok = 128 if not use_cot else 256 100 generated = evaluator.complete(prompt, max_tokens=max_tok) 101 predicted = _extract_bbh_answer(generated) 102 if normalize_answer(predicted) == normalize_answer(target): 103 task_correct += 1 104 except Exception: 105 pass 106 107 task_total += 1 108 109 acc = task_correct / task_total if task_total > 0 else 0 110 per_category[task_name] = {"correct": task_correct, "total": task_total, "accuracy": acc} 111 total_correct += task_correct 112 total_all += task_total 113 print(f" {task_name:40s} {acc*100:6.2f}% ({task_correct}/{task_total})") 114 115 elapsed = time.time() - t0 116 stats = evaluator.get_stats() 117 overall_acc = total_correct / total_all if total_all > 0 else 0 118 119 return { 120 "name": "BBH", 121 "accuracy": overall_acc, 122 "correct": total_correct, 123 "total": total_all, 124 "per_category": per_category, 125 "metrics": {"elapsed": elapsed, "api_calls": stats["api_calls"], "api_errors": stats["api_errors"], 126 "tasks_evaluated": len([t for t in per_category if per_category[t]["total"] > 0])}, 127 "samples": total_all 128 }