Download testing/agent_evaluation/loop.py from yuhanqin/ut-evaluation: direct link, hf CLI and curl.
- Browser
- Download file 7.58 kB
-
https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/loop.py
- Command line
-
hf download hf://datasets/yuhanqin/ut-evaluation/testing/agent_evaluation/loop.py
-
curl -L -o loop.py https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/loop.py
7.58 kB
| """Assemble, run, and consume one Skill-based evaluation experiment.""" | |
| from __future__ import annotations | |
| import json | |
| import hashlib | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any | |
| from .artifacts import ExperimentArtifactWriter | |
| from .config import SkillEvaluationConfig | |
| from .instrumentation import build_instrumentation | |
| from .evaluators import ( | |
| DefaultLLMBackendResolver, | |
| EvaluationServices, | |
| build_evaluators, | |
| ) | |
| from .evolution import CandidateSkill, normalize_feedback, propose_candidate | |
| from .datasets.models import DatasetDefinition | |
| from .models import ( | |
| BackendRunResult, | |
| FeedbackRecord, | |
| ScoreRecord, | |
| SkillBundle, | |
| TaskArtifact, | |
| TestPointSet, | |
| deep_thaw, | |
| score_statistics, | |
| ) | |
| from .ports import ( | |
| AgentAdapter, | |
| DatasetLoader, | |
| ExperimentBackend, | |
| LLMCompletionBackend, | |
| TraceStore, | |
| ) | |
| from .testpoints import compile_testpoint_set | |
| class EvaluationResult: | |
| experiment_id: str | |
| skill: SkillBundle | |
| evaluation_profile: SkillEvaluationConfig | |
| dataset: DatasetDefinition | |
| testpoint_set: TestPointSet | |
| runs: list[TaskArtifact] | |
| scores: list[ScoreRecord] | |
| feedback: list[FeedbackRecord] | |
| candidate: CandidateSkill | None | |
| backend_name: str | |
| backend_run_count: int | |
| backend_evaluation_count: int | |
| backend_failed_evaluation_count: int | |
| published_result_count: int | |
| backend_metadata: dict[str, Any] | |
| def pass_rate(self) -> float: | |
| return score_statistics(self.scores)["pass_rate"] | |
| def summary(self, *, artifacts: dict[str, Any] | None = None) -> dict[str, Any]: | |
| return { | |
| "schema_version": "experiment-artifacts/v2", | |
| "experiment_id": self.experiment_id, | |
| "inputs": { | |
| "skill": self.skill.ref_dict(), | |
| "evaluation_profile": self.evaluation_profile.reference_dict(), | |
| "dataset": self.dataset.manifest.to_dict(), | |
| }, | |
| "evaluation": { | |
| "case_count": len(self.runs), | |
| "testpoint_count": len(self.testpoint_set.test_points), | |
| "testpoint_set_hash": self.testpoint_set.set_hash, | |
| **score_statistics(self.scores), | |
| "feedback_count": len(self.feedback), | |
| }, | |
| "backend": { | |
| "name": self.backend_name, | |
| "experiment_id": self.experiment_id, | |
| "run_count": self.backend_run_count, | |
| "evaluation_count": self.backend_evaluation_count, | |
| "failed_evaluation_count": self.backend_failed_evaluation_count, | |
| "published_result_count": self.published_result_count, | |
| "metadata": deep_thaw(self.backend_metadata), | |
| }, | |
| "artifacts": artifacts or {}, | |
| } | |
| class EvaluationLoop: | |
| def __init__( | |
| self, | |
| adapter: AgentAdapter, | |
| *, | |
| experiment_backend: ExperimentBackend, | |
| dataset_loader: DatasetLoader, | |
| trace_store: TraceStore | None = None, | |
| llm_backend: LLMCompletionBackend | None = None, | |
| improvement_backend: LLMCompletionBackend | None = None, | |
| ): | |
| self.adapter = adapter | |
| self.experiment_backend = experiment_backend | |
| self.trace_store = trace_store | |
| self.llm_backend = llm_backend | |
| self.improvement_backend = improvement_backend | |
| self.dataset_loader = dataset_loader | |
| def run(self, skill_path: str | Path, dataset_path: str | Path, artifact_dir: str | Path, *, | |
| skill_version: str = "working", evolve: bool = True, | |
| case_ids: list[str] | None = None, limit: int | None = None, | |
| evaluation_profile: str | Path | None = None) -> EvaluationResult: | |
| skill = SkillBundle.load(skill_path, version=skill_version) | |
| profile = SkillEvaluationConfig.resolve(skill.root, evaluation_profile) | |
| dataset = self.dataset_loader.load(dataset_path) | |
| if case_ids or limit is not None: | |
| selected = dataset.cases | |
| if case_ids: | |
| requested = set(case_ids) | |
| selected = [case for case in selected if case.case_id in requested] | |
| missing = requested - {case.case_id for case in selected} | |
| if missing: | |
| raise ValueError(f"unknown dataset case ids: {sorted(missing)}") | |
| if limit is not None: | |
| if limit < 1: | |
| raise ValueError("case limit must be at least 1") | |
| selected = selected[:limit] | |
| if not selected: | |
| raise ValueError("dataset selection is empty") | |
| from dataclasses import replace | |
| selected_bytes = "".join( | |
| json.dumps(case.to_dict(), ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n" | |
| for case in selected | |
| ).encode("utf-8") | |
| dataset = DatasetDefinition(replace(dataset.manifest, case_count=len(selected), metadata={ | |
| **dataset.manifest.metadata, "selected_case_ids": [c.case_id for c in selected], | |
| "case_count": len(selected), "case_ids": [c.case_id for c in selected], | |
| "cases_sha256": hashlib.sha256(selected_bytes).hexdigest()}), selected) | |
| testpoint_set = compile_testpoint_set(skill) | |
| resolver = ( | |
| DefaultLLMBackendResolver(default_backend=self.llm_backend) | |
| if self.llm_backend is not None | |
| else None | |
| ) | |
| instrumentation = build_instrumentation( | |
| profile.instrumentation.protocol, | |
| profile.instrumentation.event_carriers, | |
| ) | |
| evaluators = build_evaluators( | |
| skill=skill, profile=profile, | |
| testpoint_set=testpoint_set, | |
| runtime=EvaluationServices( | |
| trace_store=self.trace_store, | |
| llm_backend_resolver=resolver, | |
| instrumentation=instrumentation, | |
| ), | |
| ) | |
| metadata = {"testpoint_set_hash": testpoint_set.set_hash, | |
| "testpoint_ids": [point.test_point_id for point in testpoint_set.test_points], | |
| "compiler_version": testpoint_set.compiler_version, | |
| "evaluator_ids": [evaluator.evaluator_id for evaluator in evaluators]} | |
| try: | |
| self.adapter.start() | |
| collected = self.experiment_backend.run( | |
| dataset, skill, profile, self.adapter, evaluators, | |
| testpoint_metadata=metadata, | |
| ) | |
| finally: | |
| self.adapter.stop() | |
| if not isinstance(collected, BackendRunResult): | |
| raise TypeError("experiment backend run() must return BackendRunResult") | |
| runs = list(collected.runs) | |
| scores = list(collected.scores) | |
| feedback = normalize_feedback(scores) | |
| candidate = propose_candidate(skill, feedback, self.improvement_backend) if evolve else None | |
| result = EvaluationResult( | |
| collected.experiment_id, | |
| skill, | |
| profile, | |
| dataset, | |
| testpoint_set, | |
| runs, | |
| scores, | |
| feedback, | |
| candidate, | |
| collected.backend_name, | |
| collected.run_count, | |
| collected.evaluation_count, | |
| collected.failed_evaluation_count, | |
| collected.published_result_count, | |
| deep_thaw(collected.metadata), | |
| ) | |
| ExperimentArtifactWriter.write(result, artifact_dir) | |
| return result | |