|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import click |
| 7 | + |
| 8 | + |
| 9 | +def _ensure_eval_deps() -> None: |
| 10 | + try: |
| 11 | + import langchain_anthropic # noqa: F401 |
| 12 | + import langgraph.graph # noqa: F401 |
| 13 | + except ImportError as e: |
| 14 | + raise click.ClickException( |
| 15 | + "Eval commands need optional dependencies. Install with:\n" |
| 16 | + " pip install 'tiledb-vector-search[eval]'\n" |
| 17 | + "(Use [cli] as well to build indexes from documents.)" |
| 18 | + ) from e |
| 19 | + |
| 20 | + |
| 21 | +@click.group(name="eval", invoke_without_command=True) |
| 22 | +@click.pass_context |
| 23 | +def eval_cli(ctx: click.Context) -> None: |
| 24 | + """LangGraph + Anthropic evals and index comparison.""" |
| 25 | + _ensure_eval_deps() |
| 26 | + if ctx.invoked_subcommand is None: |
| 27 | + click.echo(ctx.get_help(), color=ctx.color) |
| 28 | + |
| 29 | + |
| 30 | +@eval_cli.command("agent") |
| 31 | +@click.argument( |
| 32 | + "dataset", |
| 33 | + type=click.Path(path_type=Path, exists=True, dir_okay=False), |
| 34 | +) |
| 35 | +@click.option( |
| 36 | + "--index-uri", |
| 37 | + default=None, |
| 38 | + help="TileDB vector index URI. With the default flags, runs both with and without retrieval.", |
| 39 | +) |
| 40 | +@click.option( |
| 41 | + "--no-index-only", |
| 42 | + is_flag=True, |
| 43 | + help="Only run the baseline (no retrieval). Does not require --index-uri.", |
| 44 | +) |
| 45 | +@click.option( |
| 46 | + "--skip-without-index", |
| 47 | + is_flag=True, |
| 48 | + help="When using --index-uri, do not run the no-retrieval pass.", |
| 49 | +) |
| 50 | +@click.option( |
| 51 | + "--skip-with-index", |
| 52 | + is_flag=True, |
| 53 | + help="When using --index-uri, do not run the retrieval pass.", |
| 54 | +) |
| 55 | +@click.option("--top-k", default=10, show_default=True, type=int) |
| 56 | +@click.option( |
| 57 | + "--model", |
| 58 | + default="claude-haiku-4-5-20251001", |
| 59 | + show_default=True, |
| 60 | + help="Anthropic model id (langchain-anthropic).", |
| 61 | +) |
| 62 | +@click.option( |
| 63 | + "--llm-judge", |
| 64 | + is_flag=True, |
| 65 | + help="Also call the model to score each answer vs gold (0–1).", |
| 66 | +) |
| 67 | +@click.option( |
| 68 | + "--out", |
| 69 | + type=click.Path(path_type=Path, dir_okay=False), |
| 70 | + default=None, |
| 71 | + help="Write JSON report to this path.", |
| 72 | +) |
| 73 | +def eval_agent( |
| 74 | + dataset: Path, |
| 75 | + index_uri: str | None, |
| 76 | + no_index_only: bool, |
| 77 | + skip_without_index: bool, |
| 78 | + skip_with_index: bool, |
| 79 | + top_k: int, |
| 80 | + model: str, |
| 81 | + llm_judge: bool, |
| 82 | + out: Path | None, |
| 83 | +) -> None: |
| 84 | + """Run eval examples: compare answers with vs without vector retrieval.""" |
| 85 | + from tiledb.vector_search.evals.runner import run_agent_eval_report |
| 86 | + |
| 87 | + if no_index_only: |
| 88 | + run_with = False |
| 89 | + run_without = True |
| 90 | + if index_uri and (skip_with_index or skip_without_index): |
| 91 | + raise click.UsageError( |
| 92 | + "--no-index-only cannot be combined with --index-uri skip flags meaningfully." |
| 93 | + ) |
| 94 | + else: |
| 95 | + if not index_uri: |
| 96 | + raise click.UsageError( |
| 97 | + "Provide --index-uri, or use --no-index-only for baseline-only." |
| 98 | + ) |
| 99 | + run_with = not skip_with_index |
| 100 | + run_without = not skip_without_index |
| 101 | + if not run_with and not run_without: |
| 102 | + raise click.UsageError( |
| 103 | + "Choose at least one of retrieval or baseline (check skip flags)." |
| 104 | + ) |
| 105 | + |
| 106 | + report = run_agent_eval_report( |
| 107 | + dataset_path=dataset, |
| 108 | + index_uri=index_uri, |
| 109 | + top_k=top_k, |
| 110 | + anthropic_model=model, |
| 111 | + llm_judge=llm_judge, |
| 112 | + run_without_index=run_without, |
| 113 | + run_with_index=run_with, |
| 114 | + show_progress=True, |
| 115 | + ) |
| 116 | + text = json.dumps(report, indent=2) |
| 117 | + click.echo(text) |
| 118 | + if out is not None: |
| 119 | + out.write_text(text, encoding="utf-8") |
| 120 | + click.echo(f"Wrote {out}", err=True) |
| 121 | + |
| 122 | + |
| 123 | +@eval_cli.command("compare-indexes") |
| 124 | +@click.argument( |
| 125 | + "dataset", |
| 126 | + type=click.Path(path_type=Path, exists=True, dir_okay=False), |
| 127 | +) |
| 128 | +@click.option("--index-a", required=True, help="First index URI.") |
| 129 | +@click.option("--index-b", required=True, help="Second index URI.") |
| 130 | +@click.option("--top-k", default=10, show_default=True, type=int) |
| 131 | +@click.option( |
| 132 | + "--answer-metrics", |
| 133 | + is_flag=True, |
| 134 | + help="Run the full agent per index and score answers vs gold (calls the API).", |
| 135 | +) |
| 136 | +@click.option( |
| 137 | + "--model", |
| 138 | + default="claude-haiku-4-5-20251001", |
| 139 | + show_default=True, |
| 140 | +) |
| 141 | +@click.option("--llm-judge", is_flag=True) |
| 142 | +@click.option( |
| 143 | + "--out", |
| 144 | + type=click.Path(path_type=Path, dir_okay=False), |
| 145 | + default=None, |
| 146 | +) |
| 147 | +def eval_compare_indexes( |
| 148 | + dataset: Path, |
| 149 | + index_a: str, |
| 150 | + index_b: str, |
| 151 | + top_k: int, |
| 152 | + answer_metrics: bool, |
| 153 | + model: str, |
| 154 | + llm_judge: bool, |
| 155 | + out: Path | None, |
| 156 | +) -> None: |
| 157 | + """Compare two indexes: retrieval recall@k (labeled rows) and optional answer metrics.""" |
| 158 | + from tiledb.vector_search.evals.runner import run_compare_indexes_report |
| 159 | + |
| 160 | + report = run_compare_indexes_report( |
| 161 | + dataset_path=dataset, |
| 162 | + index_uri_a=index_a, |
| 163 | + index_uri_b=index_b, |
| 164 | + top_k=top_k, |
| 165 | + anthropic_model=model, |
| 166 | + answer_metrics=answer_metrics, |
| 167 | + llm_judge=llm_judge, |
| 168 | + show_progress=True, |
| 169 | + ) |
| 170 | + r = report.get("retrieval") or {} |
| 171 | + if r.get("per_example") == []: |
| 172 | + click.secho( |
| 173 | + "No rows with relevant_file_paths; retrieval section is empty. " |
| 174 | + "Add labels or use eval agent for answer-only runs.", |
| 175 | + fg="yellow", |
| 176 | + err=True, |
| 177 | + ) |
| 178 | + text = json.dumps(report, indent=2) |
| 179 | + click.echo(text) |
| 180 | + if out is not None: |
| 181 | + out.write_text(text, encoding="utf-8") |
| 182 | + click.echo(f"Wrote {out}", err=True) |
0 commit comments