Spaces:
Running
Running
Deploy a24aa92f
Browse files- eval_data/evaluate.py +88 -59
eval_data/evaluate.py
CHANGED
|
@@ -51,6 +51,7 @@ from langgraph.types import Command
|
|
| 51 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 52 |
|
| 53 |
from graph import build_graph
|
|
|
|
| 54 |
from state import ChangedFile, Finding
|
| 55 |
|
| 56 |
logger = logging.getLogger(__name__)
|
|
@@ -177,12 +178,14 @@ def _fuzzy_match(
|
|
| 177 |
|
| 178 |
def _run_graph_for_pr(
|
| 179 |
entry: dict, graph,
|
| 180 |
-
) -> tuple[list[Finding], Optional[dict], list]:
|
| 181 |
"""Run the full OpenCodeReview graph on a single PR.
|
| 182 |
|
| 183 |
-
Returns ``(final_findings, verdict_dict_or_None, context_chunks)``.
|
| 184 |
"""
|
| 185 |
thread_id = str(uuid.uuid4())
|
|
|
|
|
|
|
| 186 |
config = {"configurable": {"thread_id": f"eval-{thread_id}"}}
|
| 187 |
|
| 188 |
# Build initial state from pre-fetched data to avoid API calls
|
|
@@ -196,44 +199,63 @@ def _run_graph_for_pr(
|
|
| 196 |
"changed_files": changed_files,
|
| 197 |
}
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
list(graph.stream(initial_state, config))
|
| 202 |
-
except Exception as exc:
|
| 203 |
-
logger.warning(" Graph stream error: %s", exc)
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
return (
|
| 225 |
-
values.get("final_findings", []),
|
| 226 |
-
values.get("verdict"),
|
| 227 |
-
context_chunks,
|
|
|
|
| 228 |
)
|
| 229 |
|
| 230 |
-
final_state = graph.get_state(config)
|
| 231 |
-
return (
|
| 232 |
-
final_state.values.get("final_findings", []),
|
| 233 |
-
final_state.values.get("verdict"),
|
| 234 |
-
final_state.values.get("context_chunks", []),
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
|
| 238 |
# --- Metrics -----------------------------------------------------------------
|
| 239 |
|
|
@@ -436,7 +458,7 @@ def main() -> None:
|
|
| 436 |
)
|
| 437 |
|
| 438 |
try:
|
| 439 |
-
findings, verdict, context_chunks = _run_graph_for_pr(entry, graph)
|
| 440 |
except Exception as exc:
|
| 441 |
print(f"ERROR: {exc}")
|
| 442 |
continue
|
|
@@ -450,6 +472,7 @@ def main() -> None:
|
|
| 450 |
"repo": entry["repo"],
|
| 451 |
"pr_number": entry["pr_number"],
|
| 452 |
"title": entry.get("title", ""),
|
|
|
|
| 453 |
})
|
| 454 |
|
| 455 |
# ββ Compute RAGAS retrieval & generation metrics (optional) ββ
|
|
@@ -546,15 +569,13 @@ def _print_verbose(entry: dict, findings: list[Finding], metrics: dict, use_keyw
|
|
| 546 |
# βββ Quality-over-time: log to observability βββββββββββββββββββββββββββββββ
|
| 547 |
|
| 548 |
|
| 549 |
-
def _log_to_observability(results: list[dict]
|
| 550 |
"""Log evaluation results to the active observability backend(s).
|
| 551 |
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
β LangChain auto-traces the graph runs).
|
| 557 |
-
For Langfuse: creates a score for each metric via the Score API.
|
| 558 |
|
| 559 |
This function is called only when ``--log-to-observability`` is passed.
|
| 560 |
"""
|
|
@@ -610,38 +631,46 @@ def _log_to_observability(results: list[dict], dataset_name: str = "opencoderevi
|
|
| 610 |
lf = Langfuse()
|
| 611 |
run_name = f"eval-{datetime.now():%Y%m%d-%H%M%S}"
|
| 612 |
|
| 613 |
-
# Log per-PR scores
|
| 614 |
for r in results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
lf.create_score(
|
| 616 |
-
name=
|
| 617 |
value=r["f1"],
|
| 618 |
-
|
| 619 |
-
|
|
|
|
| 620 |
)
|
| 621 |
-
#
|
| 622 |
-
# faithfulness, answer_relevancy, mmr)
|
| 623 |
for ragas_key in ("context_precision", "context_recall",
|
| 624 |
"faithfulness", "answer_relevancy", "mmr"):
|
| 625 |
if ragas_key in r:
|
| 626 |
lf.create_score(
|
| 627 |
-
name=
|
| 628 |
value=r[ragas_key],
|
| 629 |
-
|
|
|
|
|
|
|
| 630 |
)
|
| 631 |
|
| 632 |
-
# Log aggregate scores
|
| 633 |
-
lf.create_score(name=
|
| 634 |
-
lf.create_score(name=
|
| 635 |
-
lf.create_score(name=
|
| 636 |
-
lf.create_score(name=
|
| 637 |
|
| 638 |
-
# Log aggregate RAGAS scores
|
| 639 |
if has_ragas:
|
| 640 |
for metric, avg in ragas_avgs.items():
|
| 641 |
-
lf.create_score(
|
| 642 |
-
name=f"{dataset_name}/{metric}",
|
| 643 |
-
value=avg,
|
| 644 |
-
)
|
| 645 |
|
| 646 |
# Flush and shutdown to ensure scores are sent before process exits
|
| 647 |
lf.flush()
|
|
|
|
| 51 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 52 |
|
| 53 |
from graph import build_graph
|
| 54 |
+
from observability import langfuse_trace, _resolve_langfuse_trace_id
|
| 55 |
from state import ChangedFile, Finding
|
| 56 |
|
| 57 |
logger = logging.getLogger(__name__)
|
|
|
|
| 178 |
|
| 179 |
def _run_graph_for_pr(
|
| 180 |
entry: dict, graph,
|
| 181 |
+
) -> tuple[list[Finding], Optional[dict], list, Optional[str]]:
|
| 182 |
"""Run the full OpenCodeReview graph on a single PR.
|
| 183 |
|
| 184 |
+
Returns ``(final_findings, verdict_dict_or_None, context_chunks, trace_id_or_None)``.
|
| 185 |
"""
|
| 186 |
thread_id = str(uuid.uuid4())
|
| 187 |
+
repo = entry.get("repo", "?")
|
| 188 |
+
pr_number = entry.get("pr_number", 0)
|
| 189 |
config = {"configurable": {"thread_id": f"eval-{thread_id}"}}
|
| 190 |
|
| 191 |
# Build initial state from pre-fetched data to avoid API calls
|
|
|
|
| 199 |
"changed_files": changed_files,
|
| 200 |
}
|
| 201 |
|
| 202 |
+
trace_id: Optional[str] = None
|
| 203 |
+
context_chunks: list = []
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
+
with langfuse_trace(
|
| 206 |
+
trace_name=f"opencodereview/eval/{repo}#{pr_number}",
|
| 207 |
+
tags=["eval", repo],
|
| 208 |
+
session_id=thread_id,
|
| 209 |
+
) as handler:
|
| 210 |
+
callbacks = []
|
| 211 |
+
if handler:
|
| 212 |
+
callbacks.append(handler)
|
| 213 |
+
config["callbacks"] = callbacks
|
| 214 |
|
| 215 |
+
# Run -- the graph will pause at human_approval (interrupt)
|
| 216 |
+
try:
|
| 217 |
+
list(graph.stream(initial_state, config))
|
| 218 |
+
except Exception as exc:
|
| 219 |
+
logger.warning(" Graph stream error: %s", exc)
|
| 220 |
|
| 221 |
+
state = graph.get_state(config)
|
| 222 |
+
tasks = state.tasks
|
| 223 |
+
values = state.values
|
| 224 |
+
|
| 225 |
+
context_chunks = values.get("context_chunks", [])
|
| 226 |
+
|
| 227 |
+
if not tasks:
|
| 228 |
+
# Graph completed without interrupt -- no findings to review
|
| 229 |
+
trace_id = _resolve_langfuse_trace_id(handler)
|
| 230 |
+
return (
|
| 231 |
+
values.get("final_findings", []),
|
| 232 |
+
values.get("verdict"),
|
| 233 |
+
context_chunks,
|
| 234 |
+
trace_id,
|
| 235 |
+
)
|
| 236 |
|
| 237 |
+
# Auto-approve to continue through the executor
|
| 238 |
+
try:
|
| 239 |
+
graph.invoke(Command(resume={"action": "approve"}), config)
|
| 240 |
+
except Exception as exc:
|
| 241 |
+
logger.warning(" Resume error: %s", exc)
|
| 242 |
+
trace_id = _resolve_langfuse_trace_id(handler)
|
| 243 |
+
return (
|
| 244 |
+
values.get("final_findings", []),
|
| 245 |
+
values.get("verdict"),
|
| 246 |
+
context_chunks,
|
| 247 |
+
trace_id,
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
final_state = graph.get_state(config)
|
| 251 |
+
trace_id = _resolve_langfuse_trace_id(handler)
|
| 252 |
return (
|
| 253 |
+
final_state.values.get("final_findings", []),
|
| 254 |
+
final_state.values.get("verdict"),
|
| 255 |
+
final_state.values.get("context_chunks", []),
|
| 256 |
+
trace_id,
|
| 257 |
)
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
# --- Metrics -----------------------------------------------------------------
|
| 261 |
|
|
|
|
| 458 |
)
|
| 459 |
|
| 460 |
try:
|
| 461 |
+
findings, verdict, context_chunks, trace_id = _run_graph_for_pr(entry, graph)
|
| 462 |
except Exception as exc:
|
| 463 |
print(f"ERROR: {exc}")
|
| 464 |
continue
|
|
|
|
| 472 |
"repo": entry["repo"],
|
| 473 |
"pr_number": entry["pr_number"],
|
| 474 |
"title": entry.get("title", ""),
|
| 475 |
+
"trace_id": trace_id,
|
| 476 |
})
|
| 477 |
|
| 478 |
# ββ Compute RAGAS retrieval & generation metrics (optional) ββ
|
|
|
|
| 569 |
# βββ Quality-over-time: log to observability βββββββββββββββββββββββββββββββ
|
| 570 |
|
| 571 |
|
| 572 |
+
def _log_to_observability(results: list[dict]) -> None:
|
| 573 |
"""Log evaluation results to the active observability backend(s).
|
| 574 |
|
| 575 |
+
Per-PR scores (f1, context_precision, etc.) are linked to each PR's
|
| 576 |
+
pipeline trace via ``trace_id``, so they appear directly on the trace
|
| 577 |
+
in the Langfuse UI. Aggregate scores (micro_f1, pr_count, etc.) are
|
| 578 |
+
logged without a trace since they summarise multiple runs.
|
|
|
|
|
|
|
| 579 |
|
| 580 |
This function is called only when ``--log-to-observability`` is passed.
|
| 581 |
"""
|
|
|
|
| 631 |
lf = Langfuse()
|
| 632 |
run_name = f"eval-{datetime.now():%Y%m%d-%H%M%S}"
|
| 633 |
|
| 634 |
+
# Log per-PR scores (linked to each PR's pipeline trace)
|
| 635 |
for r in results:
|
| 636 |
+
pr_trace_id = r.get("trace_id")
|
| 637 |
+
pr_comment = f"{r.get('repo', '?')}#{r.get('pr_number', '?')}"
|
| 638 |
+
pr_metadata = {
|
| 639 |
+
"pr_id": r.get("id"),
|
| 640 |
+
"repo": r.get("repo"),
|
| 641 |
+
"pr_number": r.get("pr_number"),
|
| 642 |
+
}
|
| 643 |
+
|
| 644 |
+
# Standard eval metrics
|
| 645 |
lf.create_score(
|
| 646 |
+
name="f1",
|
| 647 |
value=r["f1"],
|
| 648 |
+
trace_id=pr_trace_id,
|
| 649 |
+
comment=pr_comment + f" β P={r['precision']:.3f} R={r['recall']:.3f}",
|
| 650 |
+
metadata=pr_metadata,
|
| 651 |
)
|
| 652 |
+
# Per-PR RAGAS scores
|
|
|
|
| 653 |
for ragas_key in ("context_precision", "context_recall",
|
| 654 |
"faithfulness", "answer_relevancy", "mmr"):
|
| 655 |
if ragas_key in r:
|
| 656 |
lf.create_score(
|
| 657 |
+
name=ragas_key,
|
| 658 |
value=r[ragas_key],
|
| 659 |
+
trace_id=pr_trace_id,
|
| 660 |
+
comment=pr_comment,
|
| 661 |
+
metadata=pr_metadata,
|
| 662 |
)
|
| 663 |
|
| 664 |
+
# Log aggregate scores (no trace_id β spans multiple runs)
|
| 665 |
+
lf.create_score(name="micro_f1", value=micro_f)
|
| 666 |
+
lf.create_score(name="micro_precision", value=micro_p)
|
| 667 |
+
lf.create_score(name="micro_recall", value=micro_r)
|
| 668 |
+
lf.create_score(name="pr_count", value=len(results))
|
| 669 |
|
| 670 |
+
# Log aggregate RAGAS scores
|
| 671 |
if has_ragas:
|
| 672 |
for metric, avg in ragas_avgs.items():
|
| 673 |
+
lf.create_score(name=metric, value=avg)
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
# Flush and shutdown to ensure scores are sent before process exits
|
| 676 |
lf.flush()
|