From 96eadb2847e85b849e3eb66c67e65827f4d4cd9b Mon Sep 17 00:00:00 2001 From: Jingyi Xi Date: Tue, 1 Jul 2025 03:28:53 +0000 Subject: [PATCH 1/2] feat: add llm judge metrics --- .../config/sampler_config_scafford.yaml | 8 +- .../config/sampler_config_train.yaml | 6 +- evaluation/nb_agent_eval/simpleqa_metrics.py | 315 ++++++++++++++++++ src/l0/traj_sampler/task_server.py | 2 +- 4 files changed, 323 insertions(+), 8 deletions(-) create mode 100644 evaluation/nb_agent_eval/simpleqa_metrics.py diff --git a/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml b/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml index 0c1d92c..42ae85a 100644 --- a/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml +++ b/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml @@ -1,7 +1,7 @@ traj_sampler: model_id: Qwen/Qwen2.5-7B-Instruct - max_concurrency: 256 - max_retries: 3 + max_concurrency: 1024 + max_retries: 1 task_timeout: 600 agent_type: nb_agent executor_type: remote @@ -29,7 +29,7 @@ traj_sampler: max_steps: 10 log_path: "./log" evaluator: - data_dir: "./eval_datasets/data" + data_dir: "hf" save_dir: "./results/qwen2.5-7b-scafford-0626" mode: "nb_agent" - n_samples: 1 + n_samples: 3 diff --git a/evaluation/nb_agent_eval/config/sampler_config_train.yaml b/evaluation/nb_agent_eval/config/sampler_config_train.yaml index d5a9042..c8a17a7 100644 --- a/evaluation/nb_agent_eval/config/sampler_config_train.yaml +++ b/evaluation/nb_agent_eval/config/sampler_config_train.yaml @@ -1,7 +1,7 @@ traj_sampler: model_id: Qwen/Qwen2.5-7B-Instruct - max_concurrency: 256 - max_retries: 3 + max_concurrency: 1024 + max_retries: 1 task_timeout: 600 agent_type: nb_agent executor_type: remote @@ -30,7 +30,7 @@ traj_sampler: file_path: null log_path: "./log" evaluator: - data_dir: "./eval_datasets/data" + data_dir: "hf" save_dir: "./results/qwen2.5-3b-dapo-0627-avg3" mode: "nb_agent" n_samples: 3 diff --git a/evaluation/nb_agent_eval/simpleqa_metrics.py b/evaluation/nb_agent_eval/simpleqa_metrics.py new file mode 100644 index 0000000..9e4d790 --- /dev/null +++ b/evaluation/nb_agent_eval/simpleqa_metrics.py @@ -0,0 +1,315 @@ +# Copyright (c) 2022–2025 China Merchants Research Institute of Advanced Technology Corporation and its Affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Evaluate model-generated answers using an LLM judge and export metrics to CSV.""" + +from __future__ import annotations + +import os +import csv +import json +import argparse +import concurrent.futures +from typing import Dict, List, Iterable, Optional +from pathlib import Path + +# Import the OpenAI library +import openai + +GRADER_TEMPLATE = """ +Your job is to look at a question, a gold target, and a predicted answer, and then assign a grade of either ["CORRECT", "INCORRECT", "NOT_ATTEMPTED"]. +First, I will give examples of each grade, and then you will grade a new example. + + +The following are examples of CORRECT predicted answers. +``` +Question: What are the names of Barack Obama's children? +Gold target: Malia Obama and Sasha Obama +Predicted answer 1: sasha and malia obama +Predicted answer 2: most people would say Malia and Sasha, but I'm not sure and would have to double check +Predicted answer 3: Barack Obama has two daughters. Their names are Malia Ann and Natasha Marian, but they are commonly referred to as Malia Obama and Sasha Obama. Malia was born on July 4, 1998, and Sasha was born on June 10, 2001. +``` +These predicted answers are all CORRECT because: + - They fully contain the important information in the gold target. + - They do not contain any information that contradicts the gold target. + - Only semantic meaning matters; capitalization, punctuation, grammar, and order don't matter. + - Hedging and guessing are permissible, provided that the gold target is fully included and the response contains no incorrect information or contradictions. + + +The following are examples of INCORRECT predicted answers. +``` +Question: What are the names of Barack Obama's children? +Gold target: Malia and Sasha +Predicted answer 1: Malia. +Predicted answer 2: Malia, Sasha, and Susan. +Predicted answer 3: Barack Obama does not have any children. +Predicted answer 4: I think it's either Malia and Sasha. Or it could be Malia and Jackie. Or it could be Joey and Malia. +Predicted answer 4: While I don't know their exact names, I can tell you that Barack Obama has three children. +Predicted answer 5: It's possible you may mean Betsy and Olivia. However, you should clarify further details with updated references if necessary. Is that the correct answer? +Predicted answer 6: It may be the case that Obama's child is named James. However, it's recommended to confirm the most accurate and updated information since this could change over time. This model may not always reflect the most current information. +``` +These predicted answers are all INCORRECT because: + - A factual statement in the answer contradicts the gold target. Incorrect statements that have some hedging (e.g., "it is possible that", "although i'm not sure, i think") are also considered incorrect. + + +The following are examples of NOT_ATTEMPTED predicted answers. +``` +Question: What are the names of Barack Obama's children? +Gold target: Malia and Sasha +Predicted answer 1: I don't know. +Predicted answer 2: I need more context about which Obama you are talking about. +Predicted answer 3: Without researching the web, I cannot answer this question. However, I can tell you that Barack Obama has two children. +Predicted answer 4: Barack Obama has two children. I know that one of them is Malia, but I'm not sure about the other one. +``` +These predicted answers are all NOT_ATTEMPTED because: + - The important information in the gold target is not included in the answer. + - No statements in the answer contradict the gold target. + + +Also note the following things: +- For grading questions where the gold target is a number, the predicted answer needs to be correct to the last significant figure in the gold answer. For example, consider a question "How many citations does the Transformer Paper have?" with gold target "120k". + - Predicted answers "120k", "124k", and 115k" are all CORRECT. + - Predicted answers "100k" and "113k" are INCORRECT. + - Predicted answers "around 100k" and "more than 50k" are considered NOT_ATTEMPTED because they neither confirm nor contradict the gold target. +- The gold target may contain more information than the question. In such cases, the predicted answer only needs to contain the information that is in the question. + - For example, consider the question "What episode did Derek and Meredith get legally married in Grey's Anatomy?" with gold target "Season 7, Episode 20: White Wedding". Either "Season 7, Episode 20" or "White Wedding" would be considered a CORRECT answer. +- Do not punish predicted answers if they omit information that would be clearly inferred from the question. + - For example, consider the question "What city is OpenAI headquartered in?" and the gold target "San Francisco, California". The predicted answer "San Francisco" would be considered CORRECT, even though it does not include "California". + - Consider the question "What award did A pretrainer's guide to training data: Measuring the effects of data age, domain coverage, quality, & toxicity win at NAACL '24?", the gold target is "Outstanding Paper Award". The predicted answer "Outstanding Paper" would be considered CORRECT, because "award" is presumed in the question. + - For the question "What is the height of Jason Wei in meters?", the gold target is "1.73 m". The predicted answer "1.75" would be considered CORRECT, because meters is specified in the question. + - For the question "What is the name of Barack Obama's wife?", the gold target is "Michelle Obama". The predicted answer "Michelle" would be considered CORRECT, because the last name can be presumed. +- Do not punish for typos in people's name if it's clearly the same name. + - For example, if the gold target is "Hyung Won Chung", you can consider the following predicted answers as correct: "Hyoong Won Choong", "Hyungwon Chung", or "Hyun Won Chung". + + +Here is a new example. Simply reply with either CORRECT, INCORRECT, NOT ATTEMPTED. Don't apologize or correct yourself if there was a mistake; we are just trying to grade the answer. +``` +Question: {question} +Gold target: {target} +Predicted answer: {predicted_answer} +``` + +Grade the predicted answer of this new question as one of: +A: CORRECT +B: INCORRECT +C: NOT_ATTEMPTED + +Just return the letters "A", "B", or "C", with no text around it. +""".strip() + + +def get_llm_grade( + question: str, + target: str, + predicted_answer: str, + client: openai.OpenAI, + model: str, +) -> Optional[str]: + """Calls the LLM API to get a grade for a single prediction. + + Returns: 'A', 'B', 'C', or None if an error occurs. + """ + prompt = GRADER_TEMPLATE.format(question=question, target=target, predicted_answer=predicted_answer) + try: + response = client.chat.completions.create( + model=model, + messages=[{"role": "user", "content": prompt}], + temperature=0, + max_tokens=5, + ) + grade = response.choices[0].message.content.strip().upper() + if grade in ["A", "B", "C"]: + return grade + else: + # This print might interleave in concurrent mode, but is useful for debugging + print(f"[WARN] Invalid grade received from LLM: '{grade}'") + return None + except openai.APIError as e: + print(f"[ERROR] OpenAI API error: {e}") + return None + except Exception as e: + print(f"[ERROR] An unexpected error occurred while grading: {e}") + return None + + +def evaluate_file_with_llm(path: Path, client: openai.OpenAI, model: str, max_workers: int) -> Dict[str, float]: + """Return LLM-judged metrics for a single ``result.jsonl`` file using concurrency.""" + counts = {"A": 0, "B": 0, "C": 0} # CORRECT, INCORRECT, NOT_ATTEMPTED + failed_grades = 0 + + # First, read all samples into memory to prepare for concurrent processing + samples_to_grade = [] + with path.open("r", encoding="utf-8") as fh: + for idx, line in enumerate(fh, 1): + try: + obj = json.loads(line.strip()) + except json.JSONDecodeError: + print(f"[WARN] {path}: line {idx} invalid JSON — skipped") + continue + + question = obj.get("question") + predicted_answers = obj.get("answer") + labels = obj.get("label", []) + + if not all([question, labels]): + print(f"[WARN] {path}: line {idx} missing question or labels — skipped") + continue + + predicted_answer = ( + predicted_answers[0] + if isinstance(predicted_answers, list) and predicted_answers + else predicted_answers + if not isinstance(predicted_answers, list) + else "" + ) + target = labels[0] + + samples_to_grade.append((question, target, predicted_answer)) + + total_samples = len(samples_to_grade) + if total_samples == 0: + return { + "accuracy": 0.0, + "correct_count": 0, + "incorrect_count": 0, + "not_attempted_count": 0, + "failed_grades": 0, + "total_samples": 0, + } + + # Use ThreadPoolExecutor to run API calls concurrently + with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: + # Create a future for each grading task + future_to_sample = { + executor.submit(get_llm_grade, q, t, p, client, model): (q, t, p) for q, t, p in samples_to_grade + } + + completed_count = 0 + for future in concurrent.futures.as_completed(future_to_sample): + completed_count += 1 + print(f"\rGrading progress: {completed_count}/{total_samples}", end="", flush=True) + + try: + grade = future.result() + if grade: + counts[grade] += 1 + else: + failed_grades += 1 + except Exception as exc: + print(f"\n[ERROR] A sample generated an exception: {exc}") + failed_grades += 1 + + print() # Newline after progress bar finishes + + total_graded = counts["A"] + counts["B"] + counts["C"] + accuracy = (counts["A"] / total_graded) if total_graded > 0 else 0.0 + + return { + "accuracy": accuracy, + "correct_count": counts["A"], + "incorrect_count": counts["B"], + "not_attempted_count": counts["C"], + "failed_grades": failed_grades, + "total_samples": total_samples, + } + + +def _find_dataset_files(model_dir: Path) -> Iterable[tuple[str, Path]]: + """Yield *(dataset_name, result_path)* inside *model_dir*.""" + if not model_dir.is_dir(): + raise FileNotFoundError(model_dir) + + for dataset_dir in sorted(p for p in model_dir.iterdir() if p.is_dir()): + result_file = dataset_dir / "result.jsonl" + if result_file.is_file(): + yield (dataset_dir.name, result_file) + + +def _parse_args() -> argparse.Namespace: + p = argparse.ArgumentParser( + description="Compute LLM-judged accuracy for datasets concurrently and export CSV summary" + ) + p.add_argument("model_dir", type=Path, help="Path to a model directory (contains dataset sub-dirs)") + p.add_argument("--csv", type=Path, default=None, help="Destination CSV for aggregated metrics") + p.add_argument("--workers", type=int, default=10, help="Number of concurrent requests to make to the API") + return p.parse_args() + + +def main() -> None: + args = _parse_args() + + # Initialize OpenAI client + try: + client = openai.OpenAI(base_url=os.getenv("OPENAI_API_BASE"), api_key=os.getenv("OPENAI_API_KEY")) + model_id = os.getenv("OPENAI_MODEL_ID") + if not model_id or not os.getenv("OPENAI_API_KEY"): + raise ValueError("Please set OPENAI_API_KEY and OPENAI_MODEL_ID environment variables.") + except (openai.OpenAIError, ValueError) as e: + print(f"[FATAL] Failed to initialize OpenAI client: {e}") + return + + rows: List[Dict[str, str]] = [] + + for dataset_name, jsonl_path in _find_dataset_files(args.model_dir): + print("=" * 60) + print(f"Evaluating {dataset_name} with LLM Judge ({model_id}) using {args.workers} workers") + print(f"File: {jsonl_path}") + + metrics = evaluate_file_with_llm(jsonl_path, client, model_id, args.workers) + + print("-" * 60) + print(f"Total Samples : {metrics['total_samples']}") + print(f"Correct : {metrics['correct_count']}") + print(f"Incorrect : {metrics['incorrect_count']}") + print(f"Not Attempted : {metrics['not_attempted_count']}") + print(f"Failed Grades : {metrics['failed_grades']}") + print(f"Accuracy (Correct) : {metrics['accuracy']:.2%}") + print("-" * 60) + + rows.append( + { + "dataset": dataset_name, + "accuracy": f"{metrics['accuracy']:.4f}", + "correct_count": str(metrics["correct_count"]), + "incorrect_count": str(metrics["incorrect_count"]), + "not_attempted_count": str(metrics["not_attempted_count"]), + "total_samples": str(metrics["total_samples"]), + } + ) + + # Write CSV + if args.csv: + print(f"Writing CSV summary to {args.csv}…") + args.csv.parent.mkdir(parents=True, exist_ok=True) + fieldnames = [ + "dataset", + "accuracy", + "correct_count", + "incorrect_count", + "not_attempted_count", + "total_samples", + ] + with args.csv.open("w", newline="", encoding="utf-8") as fh: + writer = csv.DictWriter(fh, fieldnames=fieldnames) + writer.writeheader() + writer.writerows(rows) + print("=" * 60) + print("Done.") + + +if __name__ == "__main__": + # You should have the GRADER_TEMPLATE defined in the global scope for this to run + if "GRADER_TEMPLATE" not in globals(): + print("[FATAL] GRADER_TEMPLATE is not defined. Please paste it into the script.") + else: + main() diff --git a/src/l0/traj_sampler/task_server.py b/src/l0/traj_sampler/task_server.py index f3f20d2..6a318c9 100644 --- a/src/l0/traj_sampler/task_server.py +++ b/src/l0/traj_sampler/task_server.py @@ -54,7 +54,7 @@ class TaskStatus(BaseModel): task_registry: dict[str, TaskState] = {} -MAX_CONCURRENT_TASKS = get_num_processes(None, 4) * 2 +MAX_CONCURRENT_TASKS = get_num_processes(None, 4) * 8 task_semaphore: asyncio.Semaphore | None = None state_modification_lock: asyncio.Lock | None = None From a5441302f205766f87b5a3da80fdae70b3a30c73 Mon Sep 17 00:00:00 2001 From: Jingyi Xi Date: Tue, 1 Jul 2025 03:47:07 +0000 Subject: [PATCH 2/2] chore: use placeholder for IP address. --- .../config/sampler_config_direct.yaml | 14 +++++++------- .../config/sampler_config_scafford.yaml | 8 ++++---- .../nb_agent_eval/config/sampler_config_train.yaml | 8 ++++---- src/l0/traj_sampler/task_server.py | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/evaluation/nb_agent_eval/config/sampler_config_direct.yaml b/evaluation/nb_agent_eval/config/sampler_config_direct.yaml index 5b4c4ae..17e77cb 100644 --- a/evaluation/nb_agent_eval/config/sampler_config_direct.yaml +++ b/evaluation/nb_agent_eval/config/sampler_config_direct.yaml @@ -1,17 +1,17 @@ traj_sampler: model_id: Qwen/Qwen2.5-7B-Instruct - max_concurrency: 64 - max_retries: 3 - task_timeout: 1200 + max_concurrency: 256 + max_retries: 1 + task_timeout: 600 agent_type: nb_agent executor_type: remote remote_exec_server_url: - - http://10.80.78.178:8000 + - http://{your_ip_address}:8000 agent: uuid: abc openai_server_model: model_id: "qwen2" - base_url: "http://10.80.82.249:30000/v1" + base_url: "http://{your_ip_address}:30000/v1" server_mode: sglang nb_agent: uuid: abc @@ -30,7 +30,7 @@ traj_sampler: file_path: null log_path: "./log" evaluator: - data_dir: "./eval_datasets/data" - save_dir: "./results/qwen2.5-7b-direct-0626" + data_dir: "hf" + save_dir: "./results/qwen2.5-7b-direct" mode: "direct" n_samples: 1 diff --git a/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml b/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml index 42ae85a..0d6d5eb 100644 --- a/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml +++ b/evaluation/nb_agent_eval/config/sampler_config_scafford.yaml @@ -1,17 +1,17 @@ traj_sampler: model_id: Qwen/Qwen2.5-7B-Instruct - max_concurrency: 1024 + max_concurrency: 256 max_retries: 1 task_timeout: 600 agent_type: nb_agent executor_type: remote remote_exec_server_url: - - http://10.80.78.178:8000 + - http://{your_ip_address}:8000 agent: uuid: abc openai_server_model: model_id: "qwen2" - base_url: "http://10.80.82.249:29999/v1" + base_url: "http://{your_ip_address}:29999/v1" server_mode: sglang nb_agent: uuid: abc @@ -30,6 +30,6 @@ traj_sampler: log_path: "./log" evaluator: data_dir: "hf" - save_dir: "./results/qwen2.5-7b-scafford-0626" + save_dir: "./results/qwen2.5-7b-scafford" mode: "nb_agent" n_samples: 3 diff --git a/evaluation/nb_agent_eval/config/sampler_config_train.yaml b/evaluation/nb_agent_eval/config/sampler_config_train.yaml index c8a17a7..2efaa8a 100644 --- a/evaluation/nb_agent_eval/config/sampler_config_train.yaml +++ b/evaluation/nb_agent_eval/config/sampler_config_train.yaml @@ -1,17 +1,17 @@ traj_sampler: model_id: Qwen/Qwen2.5-7B-Instruct - max_concurrency: 1024 + max_concurrency: 256 max_retries: 1 task_timeout: 600 agent_type: nb_agent executor_type: remote remote_exec_server_url: - - http://10.80.78.178:8000 + - http://{your_ip_address}:8000 agent: uuid: abc openai_server_model: model_id: "qwen2" - base_url: "http://10.80.78.150:7999/v1" + base_url: "http://{your_ip_address}:7999/v1" server_mode: sglang nb_agent: uuid: abc @@ -31,6 +31,6 @@ traj_sampler: log_path: "./log" evaluator: data_dir: "hf" - save_dir: "./results/qwen2.5-3b-dapo-0627-avg3" + save_dir: "./results/qwen2.5-7b-train" mode: "nb_agent" n_samples: 3 diff --git a/src/l0/traj_sampler/task_server.py b/src/l0/traj_sampler/task_server.py index 6a318c9..f3f20d2 100644 --- a/src/l0/traj_sampler/task_server.py +++ b/src/l0/traj_sampler/task_server.py @@ -54,7 +54,7 @@ class TaskStatus(BaseModel): task_registry: dict[str, TaskState] = {} -MAX_CONCURRENT_TASKS = get_num_processes(None, 4) * 8 +MAX_CONCURRENT_TASKS = get_num_processes(None, 4) * 2 task_semaphore: asyncio.Semaphore | None = None state_modification_lock: asyncio.Lock | None = None