|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Manual evaluation script for the /summary endpoint. |
| 3 | +
|
| 4 | +Fetches each example asset from DocDB v2, compacts it, and asks Bedrock |
| 5 | +for a summary. Prints a human-readable report so you can evaluate quality. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + .venv/bin/python scripts/test_summary_endpoint.py |
| 9 | +
|
| 10 | +Optional env vars: |
| 11 | + CHAT_MODEL_ID Override the default Bedrock model |
| 12 | + BEDROCK_ROLE_ARN Assume a role for Bedrock (if needed) |
| 13 | + AWS_REGION Bedrock region (default us-west-2) |
| 14 | +""" |
| 15 | + |
| 16 | +import asyncio |
| 17 | +import json |
| 18 | +import sys |
| 19 | +import textwrap |
| 20 | + |
| 21 | +from biodata_query.query import retrieve_records |
| 22 | + |
| 23 | +from aind_metadata_viz.chat.summary import ( |
| 24 | + DEFAULT_MAX_RECORD_BYTES, |
| 25 | + compact_record, |
| 26 | + summarize_record, |
| 27 | +) |
| 28 | + |
| 29 | +ASSETS = [ |
| 30 | + "440_SmartSPIM1_20250116", |
| 31 | + "422_MESO2_20260122", |
| 32 | + "860900_2026-05-28_17-35-36_processed_2026-05-29_11-19-54", |
| 33 | + "behavior_850743_2026-06-11_09-45-06", |
| 34 | + "exaSPIM_681465_2025-01-22_13-45-35_flatfield-correction_2025-05-15_17-49-43", |
| 35 | +] |
| 36 | + |
| 37 | +SEP = "=" * 80 |
| 38 | + |
| 39 | + |
| 40 | +async def main(): |
| 41 | + results = [] |
| 42 | + for name in ASSETS: |
| 43 | + print(f"\n{SEP}") |
| 44 | + print(f"Asset: {name}") |
| 45 | + print(SEP) |
| 46 | + |
| 47 | + print(" Fetching from DocDB v2 ...", end="", flush=True) |
| 48 | + record = await asyncio.get_event_loop().run_in_executor( |
| 49 | + None, |
| 50 | + lambda n=name: ( |
| 51 | + retrieve_records({"name": n}, limit=1, force_backend="docdb").records |
| 52 | + or [] |
| 53 | + ), |
| 54 | + ) |
| 55 | + if not record: |
| 56 | + print(" NOT FOUND (skipping)") |
| 57 | + results.append({"name": name, "status": "not_found"}) |
| 58 | + continue |
| 59 | + record = record[0] |
| 60 | + original_bytes = len(json.dumps(record, default=str)) |
| 61 | + compacted = compact_record(record, max_bytes=DEFAULT_MAX_RECORD_BYTES) |
| 62 | + compacted_bytes = len(json.dumps(compacted, default=str)) |
| 63 | + print( |
| 64 | + f" ok ({original_bytes:,} bytes -> compacted to {compacted_bytes:,} bytes," |
| 65 | + f" {100 * compacted_bytes / original_bytes:.0f}%)" |
| 66 | + ) |
| 67 | + |
| 68 | + print(" Calling Bedrock ...", end="", flush=True) |
| 69 | + try: |
| 70 | + result = await summarize_record(record, max_bytes=DEFAULT_MAX_RECORD_BYTES) |
| 71 | + except Exception as exc: |
| 72 | + print(f" FAILED: {exc}") |
| 73 | + results.append({"name": name, "status": "error", "error": str(exc)}) |
| 74 | + continue |
| 75 | + print(" done") |
| 76 | + |
| 77 | + print() |
| 78 | + print(textwrap.fill(result.summary, width=78, initial_indent=" ", subsequent_indent=" ")) |
| 79 | + results.append( |
| 80 | + { |
| 81 | + "name": name, |
| 82 | + "status": "ok", |
| 83 | + "original_bytes": result.original_bytes, |
| 84 | + "compacted_bytes": result.compacted_bytes, |
| 85 | + "summary": result.summary, |
| 86 | + } |
| 87 | + ) |
| 88 | + |
| 89 | + print(f"\n{SEP}") |
| 90 | + ok = sum(1 for r in results if r["status"] == "ok") |
| 91 | + not_found = sum(1 for r in results if r["status"] == "not_found") |
| 92 | + errors = sum(1 for r in results if r["status"] == "error") |
| 93 | + print(f"Summary: {ok} summarized, {not_found} not found in v2, {errors} errors") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + asyncio.run(main()) |
0 commit comments