File size: 12,335 Bytes
b225a21 |
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 |
import glob
import json
import os
from typing import Dict, List, Optional, Union
import pandas as pd
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from pydantic import BaseModel, Field
# from agbenchmark.reports.processing.report_types import Report, SuiteTest
class Metrics(BaseModel):
difficulty: str
success: bool
success_percent: float = Field(alias="success_%")
run_time: Optional[str] = None
fail_reason: Optional[str] = None
attempted: Optional[bool] = None
class MetricsOverall(BaseModel):
run_time: str
highest_difficulty: str
percentage: Optional[float] = None
class Test(BaseModel):
data_path: str
is_regression: bool
answer: str
description: str
metrics: Metrics
category: List[str]
task: Optional[str] = None
reached_cutoff: Optional[bool] = None
class SuiteTest(BaseModel):
data_path: str
metrics: MetricsOverall
tests: Dict[str, Test]
category: Optional[List[str]] = None
task: Optional[str] = None
reached_cutoff: Optional[bool] = None
class Report(BaseModel):
command: str
completion_time: str
benchmark_start_time: str
metrics: MetricsOverall
tests: Dict[str, Union[Test, SuiteTest]]
config: Dict[str, str | dict[str, str]]
def get_reports():
# Initialize an empty list to store the report data
report_data = []
# Get the current working directory
current_dir = os.getcwd()
# Check if the current directory ends with 'reports'
if current_dir.endswith("reports"):
reports_dir = "/"
else:
reports_dir = "reports"
# Iterate over all agent directories in the reports directory
for agent_name in os.listdir(reports_dir):
if agent_name is None:
continue
agent_dir = os.path.join(reports_dir, agent_name)
# Check if the item is a directory (an agent directory)
if os.path.isdir(agent_dir):
# Construct the path to the report.json file
# Get all directories and files, but note that this will also include any file, not just directories.
run_dirs = glob.glob(os.path.join(agent_dir, "*"))
# Get all json files starting with 'file'
# old_report_files = glob.glob(os.path.join(agent_dir, "file*.json"))
# For each run directory, add the report.json to the end
# Only include the path if it's actually a directory
report_files = [
os.path.join(run_dir, "report.json")
for run_dir in run_dirs
if os.path.isdir(run_dir)
]
# old_report_files already contains the full paths, so no need to join again
# report_files = report_files + old_report_files
for report_file in report_files:
# Check if the report.json file exists
if os.path.isfile(report_file):
# Open the report.json file
with open(report_file, "r") as f:
# Load the JSON data from the file
json_data = json.load(f)
print(f"Processing {report_file}")
report = Report.model_validate(json_data)
for test_name, test_data in report.tests.items():
test_json = {
"agent": agent_name.lower(),
"benchmark_start_time": report.benchmark_start_time,
}
if isinstance(test_data, SuiteTest):
if (
test_data.category
): # this means it's a same task test
test_json["challenge"] = test_name
test_json["attempted"] = test_data.tests[
list(test_data.tests.keys())[0]
].metrics.attempted
test_json["categories"] = ", ".join(
test_data.category
)
test_json["task"] = test_data.task
test_json["success"] = test_data.metrics.percentage
test_json[
"difficulty"
] = test_data.metrics.highest_difficulty
test_json[
"success_%"
] = test_data.metrics.percentage
test_json["run_time"] = test_data.metrics.run_time
test_json["is_regression"] = test_data.tests[
list(test_data.tests.keys())[0]
].is_regression
else: # separate tasks in 1 suite
for (
suite_test_name,
suite_data,
) in test_data.tests.items():
test_json["challenge"] = suite_test_name
test_json[
"attempted"
] = suite_data.metrics.attempted
test_json["categories"] = ", ".join(
suite_data.category
)
test_json["task"] = suite_data.task
test_json["success"] = (
100.0 if suite_data.metrics.success else 0
)
test_json[
"difficulty"
] = suite_data.metrics.difficulty
test_json[
"success_%"
] = suite_data.metrics.success_percentage
test_json[
"run_time"
] = suite_data.metrics.run_time
test_json[
"is_regression"
] = suite_data.is_regression
else:
test_json["challenge"] = test_name
test_json["attempted"] = test_data.metrics.attempted
test_json["categories"] = ", ".join(test_data.category)
test_json["task"] = test_data.task
test_json["success"] = (
100.0 if test_data.metrics.success else 0
)
test_json["difficulty"] = test_data.metrics.difficulty
test_json[
"success_%"
] = test_data.metrics.success_percentage
test_json["run_time"] = test_data.metrics.run_time
test_json["is_regression"] = test_data.is_regression
report_data.append(test_json)
return pd.DataFrame(report_data)
def get_helicone_data():
helicone_api_key = os.getenv("HELICONE_API_KEY")
url = "https://www.helicone.ai/api/graphql"
# Replace <KEY> with your personal access key
transport = AIOHTTPTransport(
url=url, headers={"authorization": f"Bearer {helicone_api_key}"}
)
client = Client(transport=transport, fetch_schema_from_transport=True)
SIZE = 250
i = 0
data = []
print("Fetching data from Helicone")
while True:
query = gql(
"""
query ExampleQuery($limit: Int, $offset: Int){
heliconeRequest(
limit: $limit
offset: $offset
) {
costUSD
prompt
properties{
name
value
}
requestBody
response
createdAt
}
}
"""
)
print(f"Fetching {i * SIZE} to {(i + 1) * SIZE} records")
try:
result = client.execute(
query, variable_values={"limit": SIZE, "offset": i * SIZE}
)
except Exception as e:
print(f"Error occurred: {e}")
result = None
i += 1
if result:
for item in result["heliconeRequest"]:
properties = {
prop["name"]: prop["value"] for prop in item["properties"]
}
data.append(
{
"createdAt": item["createdAt"],
"agent": properties.get("agent"),
"costUSD": item["costUSD"],
"job_id": properties.get("job_id"),
"challenge": properties.get("challenge"),
"benchmark_start_time": properties.get("benchmark_start_time"),
"prompt": item["prompt"],
"response": item["response"],
"model": item["requestBody"].get("model"),
"request": item["requestBody"].get("messages"),
}
)
if not result or (len(result["heliconeRequest"]) == 0):
print("No more results")
break
df = pd.DataFrame(data)
# Drop rows where agent is None
df = df.dropna(subset=["agent"])
# Convert the remaining agent names to lowercase
df["agent"] = df["agent"].str.lower()
return df
if os.path.exists("raw_reports.pkl") and os.path.exists("raw_helicone.pkl"):
reports_df = pd.read_pickle("raw_reports.pkl")
helicone_df = pd.read_pickle("raw_helicone.pkl")
else:
reports_df = get_reports()
reports_df.to_pickle("raw_reports.pkl")
helicone_df = get_helicone_data()
helicone_df.to_pickle("raw_helicone.pkl")
def try_formats(date_str):
formats = ["%Y-%m-%d-%H:%M", "%Y-%m-%dT%H:%M:%S%z"]
for fmt in formats:
try:
return pd.to_datetime(date_str, format=fmt)
except ValueError:
pass
return None
helicone_df["benchmark_start_time"] = pd.to_datetime(
helicone_df["benchmark_start_time"].apply(try_formats), utc=True
)
helicone_df = helicone_df.dropna(subset=["benchmark_start_time"])
helicone_df["createdAt"] = pd.to_datetime(
helicone_df["createdAt"], unit="ms", origin="unix"
)
reports_df["benchmark_start_time"] = pd.to_datetime(
reports_df["benchmark_start_time"].apply(try_formats), utc=True
)
reports_df = reports_df.dropna(subset=["benchmark_start_time"])
assert pd.api.types.is_datetime64_any_dtype(
helicone_df["benchmark_start_time"]
), "benchmark_start_time in helicone_df is not datetime"
assert pd.api.types.is_datetime64_any_dtype(
reports_df["benchmark_start_time"]
), "benchmark_start_time in reports_df is not datetime"
reports_df["report_time"] = reports_df["benchmark_start_time"]
# df = pd.merge_asof(
# helicone_df.sort_values("benchmark_start_time"),
# reports_df.sort_values("benchmark_start_time"),
# left_on="benchmark_start_time",
# right_on="benchmark_start_time",
# by=["agent", "challenge"],
# direction="backward",
# )
df = pd.merge(
helicone_df,
reports_df,
on=["benchmark_start_time", "agent", "challenge"],
how="inner",
)
df.to_pickle("df.pkl")
print(df.info())
print("Data saved to df.pkl")
print("To load the data use: df = pd.read_pickle('df.pkl')")
|