Spaces:
Sleeping
Sleeping
File size: 12,887 Bytes
09da05c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | """Run persona authenticity evaluation pipelines."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
from typing import Any, Dict, List, Sequence
from adsp.core.orchestrator import Orchestrator
from adsp.core.rag.fact_data_index import build_fact_data_index_from_markdown
from adsp.core.rag.persona_index import PersonaRAGIndex
from adsp.core.types import ChatRequest
from adsp.data_pipeline.schema import load_persona_profile
from adsp.monitoring.evaluation_pipeline import AuthenticityEvaluator
REPO_ROOT = Path(__file__).resolve().parents[2]
def _default_questions_path() -> Path:
ground_truth_dir = REPO_ROOT / "data/evaluation/authenticity/ground_truth"
if ground_truth_dir.exists() and any(ground_truth_dir.glob("*.json")):
return ground_truth_dir
return REPO_ROOT / "data/evaluation/authenticity/test_questions.json"
def _default_evaluations_path() -> Path:
evaluation_dir = REPO_ROOT / "data/evaluation/authenticity/system_output"
if evaluation_dir.exists() and any(evaluation_dir.glob("*.json")):
return evaluation_dir
return REPO_ROOT / "data/evaluation/authenticity/system_output/generated_responses.json"
def _default_persona_dir() -> Path:
return REPO_ROOT / "data/processed/personas/individual"
def _default_fact_data_dir() -> Path:
return REPO_ROOT / "data/processed/fact_data/pages"
def _default_generated_output() -> Path:
return REPO_ROOT / "data/evaluation/authenticity/system_output/generated_responses.json"
def _default_evaluation_output() -> Path:
return REPO_ROOT / "data/evaluation/authenticity/evaluation_results.json"
def _parse_persona_ids(raw: str) -> List[str]:
values = [item.strip() for item in raw.split(",") if item.strip()]
if not values:
raise ValueError("persona ids cannot be empty")
return values
def _load_questions_file(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def _normalize_question_entries(payload: Any) -> List[Dict[str, Any]]:
if isinstance(payload, dict) and isinstance(payload.get("test_questions"), list):
payload = payload["test_questions"]
if isinstance(payload, dict) and not isinstance(payload.get("test_questions"), list):
entries: List[Dict[str, Any]] = []
for category, items in payload.items():
if not isinstance(items, list):
continue
for item in items:
if isinstance(item, str):
entries.append({"query": item, "category": category})
elif isinstance(item, dict):
query = item.get("query") or item.get("question")
if query:
entries.append(
{
"query": query,
"category": item.get("category", category),
"persona_id": item.get("persona_id"),
}
)
return entries
if isinstance(payload, list):
entries: List[Dict[str, Any]] = []
for item in payload:
if isinstance(item, str):
entries.append({"query": item})
elif isinstance(item, dict):
query = item.get("query") or item.get("question")
if not query:
continue
entries.append(
{
"query": query,
"category": item.get("category"),
"persona_id": item.get("persona_id"),
}
)
return entries
raise ValueError("Unsupported test questions format.")
def _load_questions_payload(path: Path) -> List[Dict[str, Any]]:
if path.is_dir():
json_files = sorted(path.glob("*.json"))
if not json_files:
raise FileNotFoundError(f"No question JSON files found in: {path}")
entries: List[Dict[str, Any]] = []
for file_path in json_files:
payload = _load_questions_file(file_path)
entries.extend(_normalize_question_entries(payload))
return entries
payload = _load_questions_file(path)
return _normalize_question_entries(payload)
def _resolve_persona_ids(
entries: Sequence[Dict[str, Any]],
persona_dir: Path,
persona_ids: Sequence[str] | None,
) -> List[str]:
if persona_ids:
return list(persona_ids)
from_entries = sorted(
{
entry.get("persona_id")
for entry in entries
if isinstance(entry.get("persona_id"), str) and entry.get("persona_id")
}
)
if from_entries:
return list(from_entries)
persona_files = sorted(persona_dir.glob("*.json"))
if not persona_files:
raise FileNotFoundError(f"No persona profiles found in {persona_dir}")
return [path.stem for path in persona_files]
def _expand_questions(
entries: Sequence[Dict[str, Any]], persona_ids: Sequence[str]
) -> List[Dict[str, Any]]:
expanded = []
for entry in entries:
query = entry.get("query")
if not query:
continue
category = entry.get("category")
persona_id = entry.get("persona_id")
if persona_id:
expanded.append({"persona_id": persona_id, "query": query, "category": category})
else:
for pid in persona_ids:
expanded.append({"persona_id": pid, "query": query, "category": category})
return expanded
def _load_persona_profiles(persona_dir: Path, persona_ids: Sequence[str]) -> List[Any]:
profiles = []
for persona_id in persona_ids:
persona_path = persona_dir / f"{persona_id}.json"
if not persona_path.exists():
raise FileNotFoundError(f"Persona profile not found: {persona_path}")
profiles.append(load_persona_profile(persona_path))
return profiles
def _build_orchestrator(
profiles: Sequence[Any],
*,
fact_data_dir: Path | None,
) -> Orchestrator:
orchestrator = Orchestrator()
orchestrator.prompt_builder.registry.upsert_many(
[(profile.persona_id, profile) for profile in profiles if profile.persona_id]
)
persona_index = PersonaRAGIndex()
persona_index.index_personas(profiles)
orchestrator.retriever.persona_index = persona_index
if fact_data_dir and fact_data_dir.exists():
fact_index = build_fact_data_index_from_markdown(fact_data_dir)
if fact_index is not None:
orchestrator.fact_data_index = fact_index
return orchestrator
def _add_generate_subparser(subparsers: argparse._SubParsersAction) -> None:
parser = subparsers.add_parser("generate", help="Generate persona responses for evaluation.")
parser.add_argument(
"--questions-file",
type=Path,
default=_default_questions_path(),
help="JSON file or directory containing test questions.",
)
parser.add_argument(
"--persona-dir",
type=Path,
default=_default_persona_dir(),
help="Directory containing persona profiles.",
)
parser.add_argument(
"--persona-ids",
type=_parse_persona_ids,
default=None,
help="Comma-separated list of persona ids to evaluate.",
)
parser.add_argument(
"--fact-data-dir",
type=Path,
default=_default_fact_data_dir(),
help="Directory containing fact data markdown pages (optional).",
)
parser.add_argument(
"--top-k",
type=int,
default=20,
help="Top-k documents to retrieve for each response.",
)
parser.add_argument(
"--output",
type=Path,
default=_default_generated_output(),
help="Where to write the generated responses JSON.",
)
def _add_evaluate_subparser(subparsers: argparse._SubParsersAction) -> None:
parser = subparsers.add_parser("evaluate", help="Compute authenticity metrics.")
parser.add_argument(
"--evaluations-file",
type=Path,
default=_default_evaluations_path(),
help="JSON file or directory containing persona responses with ratings.",
)
parser.add_argument(
"--output",
type=Path,
default=_default_evaluation_output(),
help="Where to write the evaluation results JSON.",
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Persona authenticity evaluation pipelines.")
subparsers = parser.add_subparsers(dest="command", required=True)
_add_generate_subparser(subparsers)
_add_evaluate_subparser(subparsers)
return parser.parse_args()
def _build_questions_payload(path: Path) -> List[Dict[str, Any]]:
if path.exists():
return _load_questions_payload(path)
fallback = _default_evaluations_path()
if path == _default_questions_path() and fallback.exists():
return _load_questions_payload(fallback)
raise FileNotFoundError(f"Questions path not found: {path}")
def _generate_template_ratings() -> Dict[str, Any]:
return {
"authenticity": None,
"style_alignment": None,
"factual_grounding": None,
}
def _run_generate(args: argparse.Namespace) -> int:
if not args.persona_dir.exists():
raise FileNotFoundError(f"Persona dir not found: {args.persona_dir}")
question_entries = _build_questions_payload(args.questions_file)
persona_ids = _resolve_persona_ids(question_entries, args.persona_dir, args.persona_ids)
expanded_questions = _expand_questions(question_entries, persona_ids)
profiles = _load_persona_profiles(args.persona_dir, persona_ids)
orchestrator = _build_orchestrator(
profiles,
fact_data_dir=args.fact_data_dir if args.fact_data_dir.exists() else None,
)
generated: List[Dict[str, Any]] = []
for entry in expanded_questions:
persona_id = entry["persona_id"]
query = entry["query"]
response = orchestrator.handle(
ChatRequest(
persona_id=persona_id,
query=query,
session_id=f"auth_eval_{persona_id}",
top_k=args.top_k,
)
)
generated.append(
{
"persona_id": persona_id,
"query": query,
"category": entry.get("category"),
"response": response.answer,
"citations": [c.model_dump() for c in response.citations],
"context": response.context,
"ratings": _generate_template_ratings(),
}
)
payload = {"test_questions": generated}
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", encoding="utf-8") as handle:
json.dump(payload, handle, indent=2)
print(f"Generated responses saved to {args.output}")
return 0
def _run_evaluate(args: argparse.Namespace) -> int:
if not args.evaluations_file.exists():
raise FileNotFoundError(f"Evaluations path not found: {args.evaluations_file}")
if args.evaluations_file.is_dir() and not any(args.evaluations_file.glob("*.json")):
raise FileNotFoundError(f"No evaluation JSON files found in: {args.evaluations_file}")
evaluator = AuthenticityEvaluator(evaluations_file=args.evaluations_file)
results = evaluator.run_evaluation()
print("Authenticity Evaluation")
print(f"Expert Authenticity Score: {results['expert_authenticity_score']:.2f}")
print(f"Style Alignment Score: {results['style_alignment_score']:.2f}")
print(f"Factual Grounding Score: {results['factual_grounding_score']:.2f}")
print(f"Total Evaluations: {results['total_evaluations']}")
persona_scores = results.get("persona_scores", {})
if persona_scores:
print("Per-Persona Averages")
for persona_id in sorted(persona_scores):
scores = persona_scores[persona_id]
print(
" "
f"{persona_id}: authenticity={scores['authenticity']:.2f}, "
f"style_alignment={scores['style_alignment']:.2f}, "
f"factual_grounding={scores['factual_grounding']:.2f}, "
f"total={scores['total_evaluations']}"
)
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", encoding="utf-8") as handle:
json.dump(results, handle, indent=2)
print(f"Results saved to {args.output}")
return 0
def main() -> int:
args = parse_args()
if args.command == "generate":
return _run_generate(args)
if args.command == "evaluate":
return _run_evaluate(args)
raise ValueError(f"Unknown command: {args.command}")
if __name__ == "__main__":
raise SystemExit(main())
|