| 23 | pass |
| 24 | |
| 25 | def evaluate( |
| 26 | self, |
| 27 | data_items: List[Dict], |
| 28 | model, |
| 29 | max_out_len: int = 512, |
| 30 | batch_size: Optional[int] = None, |
| 31 | save_path: str = "./eval_results", |
| 32 | ) -> Dict: |
| 33 | if not data_items: |
| 34 | print("❌ No data items provided") |
| 35 | return {"error": "No data items provided"} |
| 36 | |
| 37 | print(f"🔄 Starting evaluation on {len(data_items)} items...") |
| 38 | print(f"📝 Model: {type(model).__name__}") |
| 39 | |
| 40 | # 1. Build prompts |
| 41 | print("📝 Building prompts...") |
| 42 | prompts = [self._build_prompt(item) for item in data_items] |
| 43 | |
| 44 | # 2. Run model inference |
| 45 | print("🚀 Running model inference...") |
| 46 | responses = [] |
| 47 | try: |
| 48 | # 统一使用sequential generation with progress bar |
| 49 | for i, prompt in enumerate( |
| 50 | tqdm(prompts, desc="Generating responses", unit="item") |
| 51 | ): |
| 52 | try: |
| 53 | response = model.generate(prompt, max_out_len) |
| 54 | responses.append(response) |
| 55 | except Exception as e: |
| 56 | print(f"\n⚠️ Error on item {i + 1}: {e}") |
| 57 | responses.append(f"Error: {str(e)}") |
| 58 | |
| 59 | except Exception as e: |
| 60 | return {"error": f"Model generation failed: {e}"} |
| 61 | |
| 62 | # 3. Extract predictions and add to data |
| 63 | print("🔍 Extracting predictions...") |
| 64 | processed_items = [] |
| 65 | for item, response in tqdm( |
| 66 | zip(data_items, responses), |
| 67 | desc="Processing responses", |
| 68 | total=len(data_items), |
| 69 | unit="item", |
| 70 | ): |
| 71 | item_copy = item.copy() |
| 72 | prediction = self._extract_prediction(response, item) |
| 73 | item_copy[self.prediction_key] = prediction |
| 74 | item_copy["model_response"] = response |
| 75 | |
| 76 | answer = item.get("answer", "") |
| 77 | is_correct = self._calculate_accuracy(answer, prediction, item) |
| 78 | item_copy["pass"] = is_correct |
| 79 | |
| 80 | processed_items.append(item_copy) |
| 81 | |
| 82 | # 4. Save results |