|
| 1 | +import asyncio |
| 2 | +import typer |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from tqdm.asyncio import tqdm |
| 6 | +from typing import List |
| 7 | + |
| 8 | +from generate_benchmark import generate_benchmark_dataset |
| 9 | +from depthapi_client import DepthAPIClient |
| 10 | +from langchain_baseline import LangChainBaseline |
| 11 | +from run_judge import CustomLLMJudge |
| 12 | +from run_deepeval import evaluate_deepeval |
| 13 | +from run_ragas import evaluate_ragas |
| 14 | +from analyze_results import analyze_and_report |
| 15 | + |
| 16 | +app = typer.Typer() |
| 17 | + |
| 18 | +SEMAPHORE = asyncio.Semaphore(1) |
| 19 | + |
| 20 | +async def evaluate_single(item, system_name, client, judge, evals_to_run): |
| 21 | + """Run evaluation for a single item.""" |
| 22 | + async with SEMAPHORE: |
| 23 | + await asyncio.sleep(1.0) |
| 24 | + query = item["query"] |
| 25 | + prompt_spec = item.get("prompt_spec") |
| 26 | + |
| 27 | + if system_name == "depthapi": |
| 28 | + res = await client.query(query, prompt_spec) |
| 29 | + else: |
| 30 | + res = await client.query(query) |
| 31 | + |
| 32 | + answer = res.get("answer", "") |
| 33 | + context = res.get("context", res.get("sources", [])) # Handle different keys |
| 34 | + |
| 35 | + result_entry = { |
| 36 | + "id": item["id"], |
| 37 | + "system": system_name, |
| 38 | + "query": query, |
| 39 | + "prompt_spec": prompt_spec, |
| 40 | + "metadata": item.get("metadata", {}), |
| 41 | + "answer": answer |
| 42 | + } |
| 43 | + |
| 44 | + if "judge" in evals_to_run: |
| 45 | + judge_res = await judge.evaluate(query, answer, context, prompt_spec) |
| 46 | + result_entry["judge"] = judge_res |
| 47 | + |
| 48 | + if "deepeval" in evals_to_run: |
| 49 | + deepeval_res = evaluate_deepeval(query, answer, context) |
| 50 | + result_entry["deepeval"] = deepeval_res |
| 51 | + |
| 52 | + if "ragas" in evals_to_run: |
| 53 | + ragas_res = evaluate_ragas(query, answer, context) |
| 54 | + result_entry["ragas"] = ragas_res |
| 55 | + |
| 56 | + return result_entry |
| 57 | + |
| 58 | +async def run_benchmark(size: int, evals: List[str], compare_baseline: bool): |
| 59 | + """Run the complete benchmark.""" |
| 60 | + print(f"Generating dataset of size {size}...") |
| 61 | + dataset = generate_benchmark_dataset(size) |
| 62 | + |
| 63 | + # Save dataset |
| 64 | + Path("evaluation").mkdir(exist_ok=True) |
| 65 | + with open("evaluation/benchmark_dataset.json", "w") as f: |
| 66 | + json.dump(dataset, f, indent=2) |
| 67 | + |
| 68 | + judge = CustomLLMJudge() if "judge" in evals else None |
| 69 | + |
| 70 | + results = [] |
| 71 | + |
| 72 | + async with DepthAPIClient() as depth_client: |
| 73 | + print("Evaluating DepthAPI...") |
| 74 | + tasks = [evaluate_single(item, "depthapi", depth_client, judge, evals) for item in dataset] |
| 75 | + depth_results = await tqdm.gather(*tasks) |
| 76 | + results.extend(depth_results) |
| 77 | + |
| 78 | + if compare_baseline: |
| 79 | + print("Evaluating LangChain Baseline...") |
| 80 | + # Create some dummy docs for baseline initialization |
| 81 | + dummy_docs = [{"content": f"Dummy context for {item['query']}", "metadata": {}} for item in dataset] |
| 82 | + baseline_client = LangChainBaseline(dummy_docs) |
| 83 | + tasks = [evaluate_single(item, "langchain_baseline", baseline_client, judge, evals) for item in dataset] |
| 84 | + baseline_results = await tqdm.gather(*tasks) |
| 85 | + results.extend(baseline_results) |
| 86 | + |
| 87 | + print("Analyzing results...") |
| 88 | + analyze_and_report(results, "evaluation/results/reports") |
| 89 | + |
| 90 | + # Save raw to evaluation/results/raw |
| 91 | + Path("evaluation/results/raw").mkdir(parents=True, exist_ok=True) |
| 92 | + with open("evaluation/results/raw/all_results.json", "w") as f: |
| 93 | + json.dump(results, f, indent=2) |
| 94 | + |
| 95 | + print("Benchmark complete!") |
| 96 | + |
| 97 | +@app.command() |
| 98 | +def main( |
| 99 | + size: int = typer.Option(120, help="Number of test cases to generate"), |
| 100 | + evals: List[str] = typer.Option(["deepeval", "ragas", "judge"], help="Evaluations to run"), |
| 101 | + compare_baseline: bool = typer.Option(True, "--compare-baseline/--no-baseline", help="Run against LangChain baseline") |
| 102 | +): |
| 103 | + """Run the DepthAPI evaluation benchmark.""" |
| 104 | + asyncio.run(run_benchmark(size, evals, compare_baseline)) |
| 105 | + |
| 106 | +def fix_sys_argv(): |
| 107 | + """Workaround for Typer to support `--evals a b c` syntax.""" |
| 108 | + import sys |
| 109 | + new_argv = [] |
| 110 | + i = 0 |
| 111 | + while i < len(sys.argv): |
| 112 | + if sys.argv[i] == "--evals": |
| 113 | + new_argv.append("--evals") |
| 114 | + i += 1 |
| 115 | + if i < len(sys.argv) and not sys.argv[i].startswith("-"): |
| 116 | + new_argv.append(sys.argv[i]) |
| 117 | + i += 1 |
| 118 | + while i < len(sys.argv) and not sys.argv[i].startswith("-"): |
| 119 | + new_argv.append("--evals") |
| 120 | + new_argv.append(sys.argv[i]) |
| 121 | + i += 1 |
| 122 | + continue |
| 123 | + new_argv.append(sys.argv[i]) |
| 124 | + i += 1 |
| 125 | + sys.argv = new_argv |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + fix_sys_argv() |
| 129 | + app() |
0 commit comments