Spaces:
Running
Running
Delete benchmarks/longmemeval/dataset.py
Browse files
benchmarks/longmemeval/dataset.py
DELETED
|
@@ -1,199 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import json
|
| 4 |
-
import re
|
| 5 |
-
from collections import Counter
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
from typing import Any
|
| 8 |
-
from urllib.request import urlretrieve
|
| 9 |
-
|
| 10 |
-
from longmemeval.config import (
|
| 11 |
-
DEFAULT_VARIANT,
|
| 12 |
-
VARIANT_FILES,
|
| 13 |
-
Settings,
|
| 14 |
-
variant_path,
|
| 15 |
-
variant_url,
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def brain_id_for(question_id: str) -> str:
|
| 20 |
-
slug = re.sub(r"[^a-zA-Z0-9]", "", f"lme{question_id}")
|
| 21 |
-
if not slug.isalnum():
|
| 22 |
-
raise ValueError(f"Could not derive alphanumeric brain_id from {question_id!r}")
|
| 23 |
-
return slug.lower()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def is_abstention(question_id: str) -> bool:
|
| 27 |
-
return str(question_id).endswith("_abs")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def download_dataset(
|
| 31 |
-
dest: Path | None = None,
|
| 32 |
-
url: str | None = None,
|
| 33 |
-
*,
|
| 34 |
-
variant: str = DEFAULT_VARIANT,
|
| 35 |
-
) -> Path:
|
| 36 |
-
dest = dest or variant_path(variant)
|
| 37 |
-
url = url or variant_url(variant)
|
| 38 |
-
dest.parent.mkdir(parents=True, exist_ok=True)
|
| 39 |
-
if dest.exists() and dest.stat().st_size > 0:
|
| 40 |
-
return dest
|
| 41 |
-
urlretrieve(url, dest)
|
| 42 |
-
return dest
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def load_dataset(path: Path | None = None, *, variant: str = DEFAULT_VARIANT) -> list[dict[str, Any]]:
|
| 46 |
-
path = path or variant_path(variant)
|
| 47 |
-
if not path.exists():
|
| 48 |
-
raise FileNotFoundError(
|
| 49 |
-
f"Dataset not found at {path}. Run: python -m longmemeval download --variant {variant}"
|
| 50 |
-
)
|
| 51 |
-
with path.open(encoding="utf-8") as f:
|
| 52 |
-
data = json.load(f)
|
| 53 |
-
if not isinstance(data, list):
|
| 54 |
-
raise ValueError(f"Expected {path.name} to be a JSON array")
|
| 55 |
-
return data
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def get_question(dataset: list[dict[str, Any]], question_id: str) -> dict[str, Any]:
|
| 59 |
-
for entry in dataset:
|
| 60 |
-
if entry.get("question_id") == question_id:
|
| 61 |
-
return entry
|
| 62 |
-
raise KeyError(f"question_id {question_id!r} not found")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def format_turn(turn: dict[str, Any]) -> str:
|
| 66 |
-
role = str(turn.get("role") or "unknown").strip()
|
| 67 |
-
content = str(turn.get("content") or "").strip()
|
| 68 |
-
return f"{role}: {content}"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
def format_session(
|
| 72 |
-
session_id: str,
|
| 73 |
-
session_date: str,
|
| 74 |
-
turns: list[Any],
|
| 75 |
-
) -> str:
|
| 76 |
-
lines = [
|
| 77 |
-
f"Session time: {session_date or 'unknown time'}.",
|
| 78 |
-
f"Session id: {session_id}.",
|
| 79 |
-
"",
|
| 80 |
-
]
|
| 81 |
-
for turn in turns or []:
|
| 82 |
-
if not isinstance(turn, dict):
|
| 83 |
-
continue
|
| 84 |
-
clean = {
|
| 85 |
-
"role": turn.get("role"),
|
| 86 |
-
"content": turn.get("content"),
|
| 87 |
-
}
|
| 88 |
-
lines.append(format_turn(clean))
|
| 89 |
-
return "\n".join(lines)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
def iter_ingest_units(
|
| 93 |
-
entry: dict[str, Any],
|
| 94 |
-
*,
|
| 95 |
-
limit_sessions: int | None = None,
|
| 96 |
-
) -> list[dict[str, Any]]:
|
| 97 |
-
session_ids = list(entry.get("haystack_session_ids") or [])
|
| 98 |
-
dates = list(entry.get("haystack_dates") or [])
|
| 99 |
-
sessions = list(entry.get("haystack_sessions") or [])
|
| 100 |
-
n = min(len(session_ids), len(dates), len(sessions))
|
| 101 |
-
if limit_sessions is not None:
|
| 102 |
-
n = min(n, limit_sessions)
|
| 103 |
-
|
| 104 |
-
units: list[dict[str, Any]] = []
|
| 105 |
-
for i in range(n):
|
| 106 |
-
sid = str(session_ids[i])
|
| 107 |
-
units.append(
|
| 108 |
-
{
|
| 109 |
-
"unit_id": sid,
|
| 110 |
-
"session_id": sid,
|
| 111 |
-
"text": format_session(sid, str(dates[i]), sessions[i]),
|
| 112 |
-
"source_timestamp": str(dates[i]) if dates[i] else None,
|
| 113 |
-
}
|
| 114 |
-
)
|
| 115 |
-
return units
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
def dataset_stats(dataset: list[dict[str, Any]]) -> dict[str, Any]:
|
| 119 |
-
type_counts = Counter(str(e.get("question_type") or "unknown") for e in dataset)
|
| 120 |
-
abstention = sum(1 for e in dataset if is_abstention(str(e.get("question_id") or "")))
|
| 121 |
-
sessions = [
|
| 122 |
-
len(e.get("haystack_session_ids") or [])
|
| 123 |
-
for e in dataset
|
| 124 |
-
]
|
| 125 |
-
turns = []
|
| 126 |
-
for e in dataset:
|
| 127 |
-
for sess in e.get("haystack_sessions") or []:
|
| 128 |
-
if isinstance(sess, list):
|
| 129 |
-
turns.append(len(sess))
|
| 130 |
-
return {
|
| 131 |
-
"n_questions": len(dataset),
|
| 132 |
-
"n_abstention": abstention,
|
| 133 |
-
"by_type": dict(sorted(type_counts.items())),
|
| 134 |
-
"sessions_per_question": {
|
| 135 |
-
"min": min(sessions) if sessions else 0,
|
| 136 |
-
"max": max(sessions) if sessions else 0,
|
| 137 |
-
"mean": (sum(sessions) / len(sessions)) if sessions else 0.0,
|
| 138 |
-
},
|
| 139 |
-
"turns_per_session": {
|
| 140 |
-
"min": min(turns) if turns else 0,
|
| 141 |
-
"max": max(turns) if turns else 0,
|
| 142 |
-
"mean": (sum(turns) / len(turns)) if turns else 0.0,
|
| 143 |
-
},
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
def resolve_questions(
|
| 148 |
-
settings: Settings,
|
| 149 |
-
*,
|
| 150 |
-
question_ids: list[str] | None = None,
|
| 151 |
-
limit: int | None = None,
|
| 152 |
-
question_types: set[str] | None = None,
|
| 153 |
-
) -> list[dict[str, Any]]:
|
| 154 |
-
dataset = load_dataset(settings.dataset_path, variant=settings.variant)
|
| 155 |
-
if question_ids:
|
| 156 |
-
wanted = set(question_ids)
|
| 157 |
-
dataset = [e for e in dataset if e.get("question_id") in wanted]
|
| 158 |
-
missing = wanted - {e.get("question_id") for e in dataset}
|
| 159 |
-
if missing:
|
| 160 |
-
raise KeyError(f"question_id(s) not found: {sorted(missing)}")
|
| 161 |
-
if question_types:
|
| 162 |
-
dataset = [
|
| 163 |
-
e for e in dataset if str(e.get("question_type") or "") in question_types
|
| 164 |
-
]
|
| 165 |
-
if limit is not None:
|
| 166 |
-
dataset = dataset[: max(0, limit)]
|
| 167 |
-
return dataset
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
def resolve_variant_settings(
|
| 171 |
-
settings: Settings, variant: str | None
|
| 172 |
-
) -> Settings:
|
| 173 |
-
if not variant or variant == settings.variant:
|
| 174 |
-
return settings
|
| 175 |
-
key = variant.strip().lower()
|
| 176 |
-
if key not in VARIANT_FILES:
|
| 177 |
-
raise SystemExit(f"Unknown variant {variant!r}; choose from {sorted(VARIANT_FILES)}")
|
| 178 |
-
return Settings(
|
| 179 |
-
brainapi_url=settings.brainapi_url,
|
| 180 |
-
brainpat_token=settings.brainpat_token,
|
| 181 |
-
openai_api_key=settings.openai_api_key,
|
| 182 |
-
llm_base_url=settings.llm_base_url,
|
| 183 |
-
answer_model=settings.answer_model,
|
| 184 |
-
judge_model=settings.judge_model,
|
| 185 |
-
judge_api_key=settings.judge_api_key,
|
| 186 |
-
judge_base_url=settings.judge_base_url,
|
| 187 |
-
judge_azure_endpoint=settings.judge_azure_endpoint,
|
| 188 |
-
judge_azure_api_version=settings.judge_azure_api_version,
|
| 189 |
-
dataset_path=variant_path(key),
|
| 190 |
-
dataset_url=variant_url(key),
|
| 191 |
-
variant=key,
|
| 192 |
-
runs_dir=settings.runs_dir,
|
| 193 |
-
bench_profile=settings.bench_profile,
|
| 194 |
-
sc_samples=settings.sc_samples,
|
| 195 |
-
sc_temperature=settings.sc_temperature,
|
| 196 |
-
gap_fill=settings.gap_fill,
|
| 197 |
-
answer_azure_endpoint=settings.answer_azure_endpoint,
|
| 198 |
-
answer_azure_api_version=settings.answer_azure_api_version,
|
| 199 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|