File size: 94,597 Bytes
a3116de 4a16168 a3116de 4a16168 a3116de 4a16168 e4b0c31 4a16168 a3116de 266ceb7 a3116de fbd2ae8 a3116de 82a3de0 a3116de 82a3de0 e4b0c31 82a3de0 a3116de fbd2ae8 a3116de fbd2ae8 a3116de fbd2ae8 a3116de 266ceb7 a3116de fbd2ae8 a3116de 82a3de0 a3116de 82a3de0 e4b0c31 82a3de0 a3116de 84a5f9c fbd2ae8 a3116de fbd2ae8 a3116de 266ceb7 a3116de fbd2ae8 a3116de 82a3de0 a3116de e4b0c31 82a3de0 a3116de b714b1e a3116de b714b1e a3116de 84a5f9c fbd2ae8 3001796 a3116de b714b1e a3116de b714b1e 3001796 b714b1e 3001796 b714b1e a3116de 3001796 a3116de 3001796 a3116de 60c4817 a3116de 60c4817 a3116de 3001796 a3116de 3001796 a3116de 3001796 b714b1e a3116de b714b1e 3001796 a3116de 3001796 a3116de b714b1e 3001796 a3116de 3001796 b714b1e a3116de 3001796 a3116de b714b1e a3116de 3001796 a3116de 3001796 a3116de 266ceb7 a3116de fbd2ae8 a3116de e4b0c31 a3116de 84a5f9c fbd2ae8 928e67a a3116de eb3c2b5 fbd2ae8 eb3c2b5 e4b0c31 eb3c2b5 fbd2ae8 eb3c2b5 fbd2ae8 eb3c2b5 02e6639 eb3c2b5 02e6639 eb3c2b5 02e6639 eb3c2b5 02e6639 eb3c2b5 4b91aa4 eb3c2b5 4b91aa4 eb3c2b5 ee84e6a 266ceb7 a3116de fbd2ae8 a3116de ee84e6a a3116de 5cc4a9e a3116de ee84e6a a3116de ee84e6a a3116de ee84e6a a3116de ee84e6a a3116de fbd2ae8 a3116de 82a3de0 a3116de 82a3de0 a3116de fbd2ae8 a3116de 82a3de0 a3116de fbd2ae8 a3116de 0c0a9f1 a3116de 0c0a9f1 a3116de fbd2ae8 a3116de 82a3de0 a3116de 82a3de0 a3116de fbd2ae8 a3116de 82a3de0 a3116de fbd2ae8 a3116de 0c0a9f1 a3116de 0c0a9f1 a3116de 0c0a9f1 a3116de 82a3de0 a3116de 82a3de0 a3116de 82a3de0 a3116de 0c0a9f1 a3116de 0c0a9f1 a3116de 4a16168 e4b0c31 4a16168 16e44ce e8281aa 4a16168 e8281aa 4a16168 e8281aa 4a16168 16e44ce 4a16168 16e44ce 4a16168 e8281aa 4a16168 16e44ce 4a16168 16e44ce 4a16168 16e44ce 4a16168 e8281aa 16e44ce e8281aa 4a16168 e8281aa 4a16168 e8281aa 4a16168 e8281aa 4a16168 807fe76 68c1485 807fe76 68c1485 807fe76 68c1485 807fe76 e4b0c31 807fe76 |
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 |
"""
MCP Tool Implementations for TraceMind MCP Server
This module implements 13 MCP components (7 Tools + 3 Resources + 3 Prompts) for
AI-powered agent evaluation analysis.
With Gradio's native MCP support (mcp_server=True), these are automatically
exposed based on decorators (@gr.mcp.tool, @gr.mcp.resource, @gr.mcp.prompt),
docstrings, and type hints.
🛠️ Tools (7 AI-Powered):
📊 analyze_leaderboard - Get AI insights from evaluation leaderboard data
🐛 debug_trace - Debug agent execution traces with AI assistance
💰 estimate_cost - Predict evaluation costs with AI recommendations
⚖️ compare_runs - Compare two evaluation runs with AI analysis
📦 get_dataset - Load SMOLTRACE datasets as JSON for flexible analysis
🧪 generate_synthetic_dataset - Create domain-specific test datasets
📤 push_dataset_to_hub - Upload datasets to HuggingFace Hub
📦 Resources (3 Data Access):
leaderboard://{repo} - Raw leaderboard data in JSON format
trace://{trace_id}/{repo} - Raw OpenTelemetry trace data
cost://model/{model_name} - Model pricing and hardware cost data
📝 Prompts (3 Templates):
analysis_prompt - Standardized templates for analysis requests
debug_prompt - Standardized templates for debugging scenarios
optimization_prompt - Standardized templates for optimization goals
All AI analysis powered by Google Gemini 2.5 Flash.
Track 1: Building MCP Servers - Enterprise Category
"""
import os
import json
from typing import Optional
from datasets import load_dataset
import pandas as pd
from datetime import datetime, timedelta
import gradio as gr
from gemini_client import GeminiClient
@gr.mcp.tool()
async def analyze_leaderboard(
leaderboard_repo: str = "kshitijthakkar/smoltrace-leaderboard",
metric_focus: str = "overall",
time_range: str = "last_week",
top_n: int = 5
) -> str:
"""
Answer questions about the leaderboard with AI-powered analysis and insights.
USE THIS TOOL when you need to:
- Answer questions like "Which model is leading?", "What's the best model for cost?"
- Get intelligent insights about top performers and trends
- Compare models and understand trade-offs
- Get recommendations based on leaderboard data
DO NOT use the leaderboard:// resource for questions - use this tool instead!
The resource only returns raw JSON data without any analysis.
This tool uses Google Gemini 2.5 Flash to provide intelligent analysis of
agent evaluation results, including top performers, trends, cost/performance
trade-offs, and actionable recommendations.
**Security**: Requires GEMINI_API_KEY environment variable.
**Note**: All SMOLTRACE datasets are public - no HF token required.
Args:
leaderboard_repo (str): HuggingFace dataset repository containing leaderboard data. Default: "kshitijthakkar/smoltrace-leaderboard"
metric_focus (str): Primary metric to focus analysis on. Options: "overall", "accuracy", "cost", "latency", "co2". Default: "overall"
time_range (str): Time range for analysis. Options: "last_week", "last_month", "all_time". Default: "last_week"
top_n (int): Number of top models to highlight in analysis. Must be between 3 and 10. Default: 5
Returns:
str: Markdown-formatted analysis with top performers, insights, trade-offs, and recommendations
"""
try:
# Initialize Gemini client from environment variable only
gemini_client = GeminiClient()
# Load leaderboard data from HuggingFace (public dataset)
print(f"Loading leaderboard from {leaderboard_repo}...")
ds = load_dataset(leaderboard_repo, split="train")
df = pd.DataFrame(ds)
# Filter by time range
if time_range != "all_time":
df['timestamp'] = pd.to_datetime(df['timestamp'])
now = datetime.now()
if time_range == "last_week":
cutoff = now - timedelta(days=7)
elif time_range == "last_month":
cutoff = now - timedelta(days=30)
df = df[df['timestamp'] >= cutoff]
# Sort by metric
metric_column_map = {
"overall": "success_rate",
"accuracy": "success_rate",
"cost": "total_cost_usd",
"latency": "avg_duration_ms",
"co2": "co2_emissions_g"
}
sort_column = metric_column_map.get(metric_focus, "success_rate")
ascending = metric_focus in ["cost", "latency", "co2"] # Lower is better for these
df_sorted = df.sort_values(sort_column, ascending=ascending)
# Get top N
top_models = df_sorted.head(top_n)
# Prepare data summary for Gemini
analysis_data = {
"total_evaluations": len(df),
"time_range": time_range,
"metric_focus": metric_focus,
"top_models": top_models[[
"model", "agent_type", "provider",
"success_rate", "total_cost_usd", "avg_duration_ms",
"co2_emissions_g", "submitted_by"
]].to_dict('records'),
"summary_stats": {
"avg_success_rate": float(df['success_rate'].mean()),
"avg_cost": float(df['total_cost_usd'].mean()),
"avg_duration_ms": float(df['avg_duration_ms'].mean()),
"total_co2_g": float(df['co2_emissions_g'].sum()),
"models_tested": df['model'].nunique(),
"unique_submitters": df['submitted_by'].nunique()
}
}
# Get AI analysis from Gemini
result = await gemini_client.analyze_with_context(
data=analysis_data,
analysis_type="leaderboard",
specific_question=f"Focus on {metric_focus} performance. What are the key insights?"
)
return result
except Exception as e:
return f"❌ **Error analyzing leaderboard**: {str(e)}\n\nPlease check:\n- Repository name is correct\n- You have access to the dataset\n- HF_TOKEN is set correctly"
@gr.mcp.tool()
async def debug_trace(
trace_id: str,
traces_repo: str,
question: str = "Analyze this trace and explain what happened"
) -> str:
"""
Answer questions about agent traces with AI-powered debugging and analysis.
USE THIS TOOL when you need to:
- Answer questions like "Why did this fail?", "What took the most time?", "Why was X called?"
- Debug agent execution traces and understand what happened
- Identify bottlenecks and performance issues
- Get explanations about agent behavior
DO NOT use the trace:// resource for questions - use this tool instead!
The resource only returns raw OTEL JSON data without any analysis.
This tool uses Google Gemini 2.5 Flash to analyze OpenTelemetry trace data and
provide intelligent debugging insights, step-by-step breakdowns, and answers
to specific questions about execution flow.
Args:
trace_id (str): Unique identifier for the trace to analyze (e.g., "trace_abc123")
traces_repo (str): HuggingFace dataset repository containing trace data (e.g., "username/agent-traces-model-timestamp")
question (str): Specific question about the trace. Default: "Analyze this trace and explain what happened"
Returns:
str: Markdown-formatted debug analysis with step-by-step breakdown, timing information, and answer to the question
"""
try:
# Initialize Gemini client with provided key or from environment
gemini_client = GeminiClient()
# Load traces dataset (public dataset)
print(f"Loading traces from {traces_repo}...")
ds = load_dataset(traces_repo, split="train")
df = pd.DataFrame(ds)
# Find the specific trace
trace_data = df[df['trace_id'] == trace_id]
if len(trace_data) == 0:
return f"❌ **Trace not found**: No trace with ID `{trace_id}` in repository `{traces_repo}`"
trace_row = trace_data.iloc[0]
# Parse spans (OpenTelemetry format)
spans = trace_row['spans']
if isinstance(spans, str):
import json
spans = json.loads(spans)
# Helper function to handle different OTEL timestamp field formats
def get_timestamp(span, field):
"""Get timestamp handling multiple OTEL formats"""
# Try different field name variations
for key in [field, f"{field}UnixNano", f"{field}_unix_nano", "timeUnixNano"]:
if key in span:
return span[key]
return 0
# Build trace analysis data
start_time = get_timestamp(spans[0], 'startTime')
end_time = get_timestamp(spans[-1], 'endTime')
trace_analysis = {
"trace_id": trace_id,
"run_id": trace_row.get('run_id', 'unknown'),
"total_duration_ms": (end_time - start_time) / 1_000_000 if end_time > start_time else 0,
"num_spans": len(spans),
"spans": []
}
# Process each span
for span in spans:
span_start = get_timestamp(span, 'startTime')
span_end = get_timestamp(span, 'endTime')
span_info = {
"name": span.get('name', 'Unknown'),
"kind": span.get('kind', 'INTERNAL'),
"duration_ms": (span_end - span_start) / 1_000_000 if span_end > span_start else 0,
"attributes": span.get('attributes', {}),
"status": span.get('status', {}).get('code', 'UNKNOWN')
}
trace_analysis["spans"].append(span_info)
# Get AI analysis from Gemini
result = await gemini_client.analyze_with_context(
data=trace_analysis,
analysis_type="trace",
specific_question=question
)
return result
except Exception as e:
return f"❌ **Error debugging trace**: {str(e)}\n\nPlease check:\n- Trace ID is correct\n- Repository name is correct\n- You have access to the dataset"
@gr.mcp.tool()
async def estimate_cost(
model: str,
agent_type: str,
num_tests: int = 100,
hardware: str = "auto"
) -> str:
"""
Answer questions about evaluation costs with AI-powered estimates and recommendations.
USE THIS TOOL when you need to:
- Answer questions like "How much will this cost?", "What's the cheapest option?"
- Get cost predictions for running evaluations
- Compare costs between different models or hardware
- Get optimization recommendations to reduce costs
DO NOT use the cost:// resource for estimates - use this tool instead!
The resource only returns raw pricing tables without calculations.
This tool uses Google Gemini 2.5 Flash to calculate LLM API costs, HuggingFace
Jobs compute costs, CO2 emissions, and provide intelligent cost breakdowns with
optimization recommendations.
Args:
model (str): Model identifier in litellm format (e.g., "openai/gpt-4", "meta-llama/Llama-3.1-8B")
agent_type (str): Type of agent capabilities to test. Options: "tool", "code", "both"
num_tests (int): Number of test cases to run. Must be between 10 and 1000. Default: 100
hardware (str): Hardware type for compute. Supports Modal (gpu_t4, gpu_a10, gpu_h200, etc.) and HuggingFace Jobs (cpu-basic, t4-small, a10g-small, a100-large, etc.). Default: "auto"
Returns:
str: Markdown-formatted cost estimate with breakdown of LLM costs, compute costs, duration, CO2 emissions, and optimization tips
"""
try:
# Initialize Gemini client with provided key or from environment
gemini_client = GeminiClient()
# Fetch LLM pricing from genai_otel project
import requests
pricing_url = "https://raw.githubusercontent.com/Mandark-droid/genai_otel_instrument/refs/heads/main/genai_otel/llm_pricing.json"
try:
response = requests.get(pricing_url, timeout=5)
response.raise_for_status()
llm_pricing_db = response.json()
print(f"[INFO] Loaded {len(llm_pricing_db)} models from pricing database")
except Exception as e:
print(f"[WARNING] Failed to load pricing database: {e}, using fallback")
llm_pricing_db = {}
# Determine if API or local model
is_api_model = any(provider in model.lower() for provider in ["openai", "anthropic", "google", "cohere"])
# Auto-select hardware
if hardware == "auto":
hardware = "cpu-basic" if is_api_model else "a10g-small"
# Compute costs (per second) - Modal + HuggingFace Jobs
compute_costs = {
# Modal GPU Tasks (per second)
"gpu_b200": 0.001736, # Nvidia B200
"gpu_h200": 0.001261, # Nvidia H200
"gpu_h100": 0.001097, # Nvidia H100
"gpu_a100_80gb": 0.000694, # Nvidia A100, 80 GB
"gpu_a100": 0.000583, # Nvidia A100, 40 GB
"gpu_l40s": 0.000542, # Nvidia L40S
"gpu_a10": 0.000306, # Nvidia A10
"gpu_l4": 0.000222, # Nvidia L4
"gpu_t4": 0.000164, # Nvidia T4
# Modal CPU (per core)
"cpu": 0.0000131, # Physical core (2 vCPU equivalent)
# HuggingFace Jobs (estimated per second based on typical hourly rates)
# Note: HF Jobs pricing varies, these are estimates
"cpu-basic": 0.0000167, # ~$0.06/hour
"cpu-upgrade": 0.0000278, # ~$0.10/hour
"t4-small": 0.000167, # ~$0.60/hour
"t4-medium": 0.000278, # ~$1.00/hour
"l4x1": 0.000250, # ~$0.90/hour
"l4x4": 0.001000, # ~$3.60/hour
"a10g-small": 0.000333, # ~$1.20/hour
"a10g-large": 0.000556, # ~$2.00/hour
"a10g-largex2": 0.001111, # ~$4.00/hour
"a10g-largex4": 0.002222, # ~$8.00/hour
"a100-large": 0.001389, # ~$5.00/hour
# TPU (estimated)
"v5e-1x1": 0.000417, # ~$1.50/hour
"v5e-2x2": 0.001667, # ~$6.00/hour
"v5e-2x4": 0.003333 # ~$12.00/hour
}
# Get model costs from pricing database
model_cost = None
# Try exact match first
if model in llm_pricing_db:
model_cost = llm_pricing_db[model]
else:
# Try without provider prefix (e.g., "gpt-4" instead of "openai/gpt-4")
model_name = model.split('/')[-1]
for key in llm_pricing_db:
if model_name in key or key in model_name:
model_cost = llm_pricing_db[key]
print(f"[INFO] Found pricing for {model} via fuzzy match: {key}")
break
# Fallback to default if not found
if model_cost is None:
print(f"[WARNING] Model {model} not in pricing database, using default")
if is_api_model:
model_cost = {"input_cost_per_token": 0.000001, "output_cost_per_token": 0.000002}
else:
model_cost = {"input_cost_per_token": 0, "output_cost_per_token": 0} # Local model
# Estimate token usage per test (based on real data from kshitijthakkar/smoltrace-results-20251117_104845)
# These are averages from actual agent evaluation runs
# Input/output split estimated at 60/40 based on typical agent patterns
# (agents have large context with system prompts, tool outputs, etc.)
token_estimates = {
"tool": {
"input": 7577, # 60% of 12,629 avg total tokens
"output": 5052 # 40% of 12,629 avg total tokens
},
"code": {
"input": 10321, # 60% of 17,202 avg total tokens
"output": 6881 # 40% of 17,202 avg total tokens
},
"both": {
"input": 8900, # Average of tool+code inputs
"output": 5933 # Average of tool+code outputs
}
}
tokens_per_test = token_estimates[agent_type]
# Calculate LLM costs (pricing is per token, not per 1K tokens)
llm_cost_per_test = (
tokens_per_test["input"] * model_cost.get("input_cost_per_token", 0) +
tokens_per_test["output"] * model_cost.get("output_cost_per_token", 0)
)
total_llm_cost = llm_cost_per_test * num_tests
# Estimate duration (seconds per test)
if is_api_model:
duration_per_test = 3.0 # API models are fast
else:
duration_per_test = 8.0 # Local models slower but depends on GPU
total_duration_seconds = duration_per_test * num_tests
# Calculate compute costs (per second)
compute_rate_per_sec = compute_costs.get(hardware, compute_costs.get("cpu-basic", 0.0000167))
# For CPU-based hardware, estimate core usage (assume 2 cores for agent workload)
# For GPU/TPU, direct cost
if hardware in ["cpu", "cpu-basic", "cpu-upgrade"]:
num_cores = 2 # Estimate 2 cores for typical agent workload
total_compute_cost = total_duration_seconds * compute_rate_per_sec * num_cores
else:
total_compute_cost = total_duration_seconds * compute_rate_per_sec
# Estimate CO2 (rough estimates in kg per hour)
co2_per_hour = {
# Modal
"cpu": 0.05,
"gpu_t4": 0.10,
"gpu_l4": 0.12,
"gpu_a10": 0.15,
"gpu_l40s": 0.20,
"gpu_a100": 0.25,
"gpu_a100_80gb": 0.28,
"gpu_h100": 0.30,
"gpu_h200": 0.32,
"gpu_b200": 0.35,
# HuggingFace Jobs
"cpu-basic": 0.03,
"cpu-upgrade": 0.04,
"t4-small": 0.08,
"t4-medium": 0.10,
"l4x1": 0.12,
"l4x4": 0.48,
"a10g-small": 0.13,
"a10g-large": 0.15,
"a10g-largex2": 0.30,
"a10g-largex4": 0.60,
"a100-large": 0.25,
"v5e-1x1": 0.18,
"v5e-2x2": 0.72,
"v5e-2x4": 1.44
}
total_co2_kg = (total_duration_seconds / 3600) * co2_per_hour.get(hardware, 0.05)
# Prepare estimate data
estimate_data = {
"model": model,
"agent_type": agent_type,
"num_tests": num_tests,
"hardware": hardware,
"is_api_model": is_api_model,
"pricing_source": "genai_otel pricing database + Modal/HF Jobs compute costs",
"estimates": {
"llm_cost_usd": round(total_llm_cost, 6),
"llm_cost_per_test": round(llm_cost_per_test, 6),
"compute_cost_usd": round(total_compute_cost, 6),
"total_cost_usd": round(total_llm_cost + total_compute_cost, 6),
"duration_seconds": round(total_duration_seconds, 2),
"duration_minutes": round(total_duration_seconds / 60, 2),
"duration_per_test_seconds": round(duration_per_test, 2),
"co2_emissions_kg": round(total_co2_kg, 4),
"tokens_per_test": tokens_per_test,
"compute_rate_per_second": compute_rate_per_sec
},
"model_pricing": {
"input_cost_per_token": model_cost.get("input_cost_per_token", 0),
"output_cost_per_token": model_cost.get("output_cost_per_token", 0),
"found_in_database": model in llm_pricing_db
}
}
# Get AI analysis from Gemini
result = await gemini_client.analyze_with_context(
data=estimate_data,
analysis_type="cost_estimate",
specific_question="Provide cost breakdown and optimization recommendations"
)
return result
except Exception as e:
return f"❌ **Error estimating cost**: {str(e)}"
@gr.mcp.tool()
async def compare_runs(
run_id_1: str,
run_id_2: str,
leaderboard_repo: str = "kshitijthakkar/smoltrace-leaderboard",
comparison_focus: str = "comprehensive"
) -> str:
"""
Compare two evaluation runs and generate AI-powered comparative analysis.
This tool fetches data for two evaluation runs from the leaderboard and uses
Google Gemini 2.5 Flash to provide intelligent comparison across multiple dimensions:
success rate, cost efficiency, speed, environmental impact, and use case recommendations.
Args:
run_id_1 (str): First run ID to compare
run_id_2 (str): Second run ID to compare
leaderboard_repo (str): HuggingFace dataset repository containing leaderboard data. Default: "kshitijthakkar/smoltrace-leaderboard"
comparison_focus (str): Focus area for comparison. Options: "comprehensive", "cost", "performance", "eco_friendly". Default: "comprehensive"
Returns:
str: Markdown-formatted comparative analysis with winner for each category, trade-offs, and use case recommendations
"""
try:
# Initialize Gemini client with provided key or from environment
gemini_client = GeminiClient()
# Load leaderboard data
dataset = load_dataset(leaderboard_repo, split="train")
df = pd.DataFrame(dataset)
# Find the two runs
run1 = df[df['run_id'] == run_id_1]
run2 = df[df['run_id'] == run_id_2]
if run1.empty:
return f"❌ **Error**: Run ID '{run_id_1}' not found in leaderboard"
if run2.empty:
return f"❌ **Error**: Run ID '{run_id_2}' not found in leaderboard"
run1_data = run1.iloc[0].to_dict()
run2_data = run2.iloc[0].to_dict()
# Build comparison context for Gemini
comparison_data = {
"run_1": {
"run_id": run1_data.get('run_id'),
"model": run1_data.get('model'),
"agent_type": run1_data.get('agent_type'),
"success_rate": run1_data.get('success_rate'),
"total_tests": run1_data.get('total_tests'),
"successful_tests": run1_data.get('successful_tests'),
"avg_duration_ms": run1_data.get('avg_duration_ms'),
"total_cost_usd": run1_data.get('total_cost_usd'),
"avg_cost_per_test_usd": run1_data.get('avg_cost_per_test_usd'),
"co2_emissions_g": run1_data.get('co2_emissions_g'),
"gpu_utilization_avg": run1_data.get('gpu_utilization_avg'),
"total_tokens": run1_data.get('total_tokens'),
"provider": run1_data.get('provider'),
"job_type": run1_data.get('job_type'),
"timestamp": run1_data.get('timestamp')
},
"run_2": {
"run_id": run2_data.get('run_id'),
"model": run2_data.get('model'),
"agent_type": run2_data.get('agent_type'),
"success_rate": run2_data.get('success_rate'),
"total_tests": run2_data.get('total_tests'),
"successful_tests": run2_data.get('successful_tests'),
"avg_duration_ms": run2_data.get('avg_duration_ms'),
"total_cost_usd": run2_data.get('total_cost_usd'),
"avg_cost_per_test_usd": run2_data.get('avg_cost_per_test_usd'),
"co2_emissions_g": run2_data.get('co2_emissions_g'),
"gpu_utilization_avg": run2_data.get('gpu_utilization_avg'),
"total_tokens": run2_data.get('total_tokens'),
"provider": run2_data.get('provider'),
"job_type": run2_data.get('job_type'),
"timestamp": run2_data.get('timestamp')
},
"comparison_focus": comparison_focus
}
# Create comparison prompt based on focus
if comparison_focus == "comprehensive":
prompt = f"""
You are analyzing a comparison between two agent evaluation runs. Provide a comprehensive analysis covering all aspects.
**Run 1 ({comparison_data['run_1']['model']}):**
{json.dumps(comparison_data['run_1'], indent=2)}
**Run 2 ({comparison_data['run_2']['model']}):**
{json.dumps(comparison_data['run_2'], indent=2)}
Please provide a detailed comparison in the following format:
## 📊 Head-to-Head Comparison
### 🎯 Accuracy Winner
[Which run has better success rate and by how much? Explain significance]
### ⚡ Speed Winner
[Which run is faster and by how much? Include average duration comparison]
### 💰 Cost Winner
[Which run is more cost-effective? Compare total cost AND cost per test]
### 🌱 Eco-Friendly Winner
[Which run has lower CO2 emissions? Calculate the difference]
### 🔧 GPU Efficiency Winner (if applicable)
[For GPU jobs, which has better utilization? Explain implications]
## 📈 Performance Summary
### Run 1 Strengths
- [List 3-4 key strengths]
### Run 2 Strengths
- [List 3-4 key strengths]
## 💡 Use Case Recommendations
### When to Choose Run 1 ({comparison_data['run_1']['model']})
[Specific scenarios where Run 1 is the better choice]
### When to Choose Run 2 ({comparison_data['run_2']['model']})
[Specific scenarios where Run 2 is the better choice]
## ⚖️ Overall Recommendation
[Based on the analysis, provide a balanced recommendation considering different priorities]
Be specific with numbers and percentages. Make the comparison actionable and insightful.
"""
elif comparison_focus == "cost":
prompt = f"""
Compare these two evaluation runs focusing specifically on cost efficiency:
**Run 1:** {json.dumps(comparison_data['run_1'], indent=2)}
**Run 2:** {json.dumps(comparison_data['run_2'], indent=2)}
Provide detailed cost analysis:
1. Which run has lower total cost and by what percentage?
2. Cost per test comparison - which is more efficient?
3. Calculate cost per successful test (accounting for failures)
4. Token usage efficiency - cost per 1000 tokens
5. ROI analysis - is higher cost justified by better accuracy?
6. Scaling implications - at 1000 tests, what would each cost?
Provide actionable cost optimization recommendations.
"""
elif comparison_focus == "performance":
prompt = f"""
Compare these two evaluation runs focusing on performance (speed + accuracy):
**Run 1:** {json.dumps(comparison_data['run_1'], indent=2)}
**Run 2:** {json.dumps(comparison_data['run_2'], indent=2)}
Analyze:
1. Success rate difference - statistical significance?
2. Speed comparison - average duration per test
3. Which delivers faster results without sacrificing accuracy?
4. Throughput analysis - tests per minute
5. Quality vs Speed trade-off assessment
6. GPU utilization efficiency (if applicable)
Recommend which run offers best performance for production workloads.
"""
elif comparison_focus == "eco_friendly":
prompt = f"""
Compare these two evaluation runs focusing on environmental impact:
**Run 1:** {json.dumps(comparison_data['run_1'], indent=2)}
**Run 2:** {json.dumps(comparison_data['run_2'], indent=2)}
Analyze:
1. CO2 emissions comparison - which is greener?
2. Emissions per test and per successful test
3. GPU vs API model environmental trade-offs
4. Energy efficiency based on duration and GPU utilization
5. Emissions reduction if scaled to 10,000 tests
6. Carbon offset cost comparison
Provide eco-conscious recommendations for sustainable AI deployment.
"""
# Get AI analysis from Gemini
analysis = await gemini_client.analyze_with_context(
comparison_data,
analysis_type="comparison",
specific_question=prompt
)
return analysis
except Exception as e:
return f"❌ **Error comparing runs**: {str(e)}"
@gr.mcp.tool()
async def analyze_results(
results_repo: str,
analysis_focus: str = "comprehensive",
max_rows: int = 100
) -> str:
"""
Analyze detailed test results and provide optimization recommendations.
USE THIS TOOL when you need to:
- Understand why tests are failing and get recommendations
- Identify performance bottlenecks in specific test cases
- Find cost optimization opportunities
- Get insights about tool usage patterns
- Analyze which types of tasks work well vs poorly
This tool analyzes individual test case results (not aggregate leaderboard data)
and uses Google Gemini 2.5 Flash to provide actionable optimization recommendations.
Args:
results_repo (str): HuggingFace dataset repository containing results (e.g., "username/smoltrace-results-gpt4-20251114")
analysis_focus (str): Focus area. Options: "failures", "performance", "cost", "comprehensive". Default: "comprehensive"
max_rows (int): Maximum test cases to analyze. Default: 100. Range: 10-500
Returns:
str: Markdown-formatted analysis with failure patterns, performance insights, cost analysis, and optimization recommendations
"""
try:
# Initialize Gemini client
gemini_client = GeminiClient()
# Load results dataset
print(f"Loading results from {results_repo}...")
ds = load_dataset(results_repo, split="train")
df = pd.DataFrame(ds)
if df.empty:
return "❌ **Error**: Results dataset is empty"
# Limit rows
max_rows = max(10, min(500, max_rows))
df_sample = df.head(max_rows)
# Calculate statistics
total_tests = len(df_sample)
successful = df_sample[df_sample['success'] == True]
failed = df_sample[df_sample['success'] == False]
success_rate = (len(successful) / total_tests * 100) if total_tests > 0 else 0
# Analyze by category/difficulty
category_stats = {}
if 'category' in df_sample.columns:
cat_agg = df_sample.groupby('category').agg({
'success': ['count', 'sum', 'mean'],
'execution_time_ms': 'mean',
'cost_usd': 'sum'
})
# Flatten multi-index columns
cat_agg.columns = ['_'.join(col).strip() for col in cat_agg.columns.values]
category_stats = cat_agg.to_dict('index')
difficulty_stats = {}
if 'difficulty' in df_sample.columns:
diff_agg = df_sample.groupby('difficulty').agg({
'success': ['count', 'sum', 'mean'],
'execution_time_ms': 'mean'
})
# Flatten multi-index columns
diff_agg.columns = ['_'.join(col).strip() for col in diff_agg.columns.values]
difficulty_stats = diff_agg.to_dict('index')
# Find slowest tests
slowest_tests = df_sample.nlargest(5, 'execution_time_ms')[
['task_id', 'prompt', 'execution_time_ms', 'success', 'cost_usd']
].to_dict('records')
# Find most expensive tests
if 'cost_usd' in df_sample.columns:
most_expensive = df_sample.nlargest(5, 'cost_usd')[
['task_id', 'prompt', 'cost_usd', 'total_tokens', 'success']
].to_dict('records')
else:
most_expensive = []
# Analyze failures
failure_analysis = []
if len(failed) > 0:
# Define which columns to include in failure sample
failure_columns = ['task_id', 'prompt']
# Add optional columns if they exist
optional_columns = ['error', 'error_type', 'tool_called', 'expected_tool']
for col in optional_columns:
if col in failed.columns:
failure_columns.append(col)
# Get sample of failures with only existing columns
failure_sample = failed.head(10)[failure_columns].to_dict('records')
# Count error types if column exists
if 'error_type' in failed.columns:
error_type_counts = failed['error_type'].value_counts().to_dict()
else:
error_type_counts = {}
failure_analysis = {
"total_failures": len(failed),
"failure_rate": (len(failed) / total_tests * 100),
"error_type_counts": error_type_counts,
"sample_failures": failure_sample
}
# Prepare data for Gemini analysis
analysis_data = {
"results_repo": results_repo,
"total_tests_analyzed": total_tests,
"overall_stats": {
"success_rate": round(success_rate, 2),
"successful_tests": len(successful),
"failed_tests": len(failed),
"avg_execution_time_ms": float(df_sample['execution_time_ms'].mean()),
"total_cost_usd": float(df_sample['cost_usd'].sum()) if 'cost_usd' in df_sample.columns else 0,
"avg_tokens_per_test": float(df_sample['total_tokens'].mean()) if 'total_tokens' in df_sample.columns else 0
},
"category_performance": category_stats,
"difficulty_performance": difficulty_stats,
"slowest_tests": slowest_tests,
"most_expensive_tests": most_expensive,
"failure_analysis": failure_analysis,
"analysis_focus": analysis_focus
}
# Create focus-specific prompt
focus_prompts = {
"failures": "Focus specifically on failure patterns. Analyze why tests are failing, identify common error types, and provide actionable recommendations to improve success rate.",
"performance": "Focus on performance optimization. Analyze execution times, identify bottlenecks, and recommend ways to speed up test execution.",
"cost": "Focus on cost optimization. Analyze token usage and costs, identify expensive tests, and recommend ways to reduce evaluation costs.",
"comprehensive": "Provide comprehensive analysis covering failures, performance, cost, and overall optimization opportunities."
}
specific_question = focus_prompts.get(analysis_focus, focus_prompts["comprehensive"])
# Get AI analysis
result = await gemini_client.analyze_with_context(
data=analysis_data,
analysis_type="results",
specific_question=specific_question
)
return result
except Exception as e:
return f"❌ **Error analyzing results**: {str(e)}\n\nPlease check:\n- Repository name is correct (should be smoltrace-results-*)\n- You have access to the dataset\n- HF_TOKEN is set correctly"
@gr.mcp.tool()
async def get_top_performers(
leaderboard_repo: str = "kshitijthakkar/smoltrace-leaderboard",
metric: str = "success_rate",
top_n: int = 5
) -> str:
"""
Get top performing models from leaderboard - optimized for quick queries.
**USE THIS TOOL** instead of get_dataset() when you need to answer questions like:
- "Which model is leading?"
- "Show me the top 5 models"
- "What's the best model for cost?"
This tool returns ONLY the essential data for top performers, avoiding the
full 51-run dataset that causes token bloat. Returns properly formatted JSON
that's ready to use without parsing.
Args:
leaderboard_repo (str): HuggingFace dataset repository. Default: "kshitijthakkar/smoltrace-leaderboard"
metric (str): Metric to rank by. Options: "success_rate", "total_cost_usd", "avg_duration_ms", "co2_emissions_g". Default: "success_rate"
top_n (int): Number of top models to return. Range: 1-20. Default: 5
Returns:
str: JSON object with top performers - ready to use, no parsing needed
"""
try:
# Load leaderboard dataset
ds = load_dataset(leaderboard_repo, split="train")
df = pd.DataFrame(ds)
if df.empty:
return json.dumps({
"error": "Leaderboard dataset is empty",
"top_performers": []
}, indent=2)
# Validate metric
valid_metrics = ["success_rate", "total_cost_usd", "avg_duration_ms", "co2_emissions_g"]
if metric not in valid_metrics:
return json.dumps({
"error": f"Invalid metric '{metric}'. Valid options: {valid_metrics}",
"top_performers": []
}, indent=2)
# Limit top_n
top_n = max(1, min(20, top_n))
# Sort by metric (ascending for cost/latency/co2, descending for success_rate)
ascending = metric in ["total_cost_usd", "avg_duration_ms", "co2_emissions_g"]
df_sorted = df.sort_values(metric, ascending=ascending)
# Get top N
top_models = df_sorted.head(top_n)
# Select only essential columns to minimize tokens
essential_columns = [
"run_id", "model", "agent_type", "provider",
"success_rate", "total_cost_usd", "avg_duration_ms",
"co2_emissions_g", "total_tests", "timestamp"
]
# Filter to only columns that exist
available_columns = [col for col in essential_columns if col in top_models.columns]
top_models_filtered = top_models[available_columns]
# CRITICAL FIX: Handle NaN/None properly
top_models_filtered = top_models_filtered.where(pd.notnull(top_models_filtered), None)
# Convert to dict
top_performers_data = top_models_filtered.to_dict(orient="records")
result = {
"metric_ranked_by": metric,
"ranking_order": "ascending (lower is better)" if ascending else "descending (higher is better)",
"total_runs_in_leaderboard": len(df),
"top_n": top_n,
"top_performers": top_performers_data
}
return json.dumps(result, indent=2)
except Exception as e:
return json.dumps({
"error": f"Failed to get top performers: {str(e)}",
"top_performers": []
}, indent=2)
@gr.mcp.tool()
async def get_leaderboard_summary(
leaderboard_repo: str = "kshitijthakkar/smoltrace-leaderboard"
) -> str:
"""
Get high-level leaderboard summary statistics - optimized for overview queries.
**USE THIS TOOL** instead of get_dataset() when you need to answer questions like:
- "How many runs are in the leaderboard?"
- "What's the average success rate?"
- "Give me an overview of the leaderboard"
This tool returns ONLY summary statistics (no individual runs), avoiding the
full dataset that causes token bloat. Returns properly formatted JSON that's
ready to use without parsing.
Args:
leaderboard_repo (str): HuggingFace dataset repository. Default: "kshitijthakkar/smoltrace-leaderboard"
Returns:
str: JSON object with summary statistics - ready to use, no parsing needed
"""
try:
# Load leaderboard dataset
ds = load_dataset(leaderboard_repo, split="train")
df = pd.DataFrame(ds)
if df.empty:
return json.dumps({
"error": "Leaderboard dataset is empty",
"summary": {}
}, indent=2)
# Calculate summary statistics
summary = {
"total_runs": len(df),
"unique_models": int(df['model'].nunique()) if 'model' in df.columns else 0,
"unique_submitters": int(df['submitted_by'].nunique()) if 'submitted_by' in df.columns else 0,
"overall_stats": {
"avg_success_rate": float(df['success_rate'].mean()) if 'success_rate' in df.columns else None,
"best_success_rate": float(df['success_rate'].max()) if 'success_rate' in df.columns else None,
"worst_success_rate": float(df['success_rate'].min()) if 'success_rate' in df.columns else None,
"avg_cost_per_run_usd": float(df['total_cost_usd'].mean()) if 'total_cost_usd' in df.columns else None,
"avg_duration_ms": float(df['avg_duration_ms'].mean()) if 'avg_duration_ms' in df.columns else None,
"total_co2_emissions_g": float(df['co2_emissions_g'].sum()) if 'co2_emissions_g' in df.columns else None
},
"breakdown_by_agent_type": {},
"breakdown_by_provider": {},
"top_3_models_by_success_rate": []
}
# Breakdown by agent type
if 'agent_type' in df.columns and 'success_rate' in df.columns:
agent_stats = df.groupby('agent_type').agg({
'success_rate': 'mean',
'run_id': 'count'
}).to_dict()
summary["breakdown_by_agent_type"] = {
agent_type: {
"count": int(agent_stats['run_id'][agent_type]),
"avg_success_rate": float(agent_stats['success_rate'][agent_type])
}
for agent_type in agent_stats['run_id'].keys()
}
# Breakdown by provider
if 'provider' in df.columns and 'success_rate' in df.columns:
provider_stats = df.groupby('provider').agg({
'success_rate': 'mean',
'run_id': 'count'
}).to_dict()
summary["breakdown_by_provider"] = {
provider: {
"count": int(provider_stats['run_id'][provider]),
"avg_success_rate": float(provider_stats['success_rate'][provider])
}
for provider in provider_stats['run_id'].keys()
}
# Top 3 models by success rate
if 'success_rate' in df.columns and 'model' in df.columns:
top_3 = df.nlargest(3, 'success_rate')[['model', 'success_rate', 'total_cost_usd', 'avg_duration_ms']]
top_3 = top_3.where(pd.notnull(top_3), None)
summary["top_3_models_by_success_rate"] = top_3.to_dict(orient="records")
result = {
"leaderboard_repo": leaderboard_repo,
"summary": summary
}
return json.dumps(result, indent=2)
except Exception as e:
return json.dumps({
"error": f"Failed to get leaderboard summary: {str(e)}",
"summary": {}
}, indent=2)
@gr.mcp.tool()
async def get_dataset(
dataset_repo: str,
max_rows: int = 50
) -> str:
"""
Load SMOLTRACE datasets from HuggingFace and return as JSON.
This tool loads datasets with the "smoltrace-" prefix and returns the raw data
as JSON. Use this to access:
- Leaderboard data (kshitijthakkar/smoltrace-leaderboard)
- Results datasets (e.g., username/smoltrace-results-*)
- Traces datasets (e.g., username/smoltrace-traces-*)
- Metrics datasets (e.g., username/smoltrace-metrics-*)
- Any other smoltrace-prefixed evaluation dataset
If you don't know which dataset to load, first load the leaderboard to see
the dataset references in the results_dataset, traces_dataset, metrics_dataset,
and dataset_used fields.
Args:
dataset_repo (str): HuggingFace dataset repository path with "smoltrace-" prefix (e.g., "kshitijthakkar/smoltrace-leaderboard")
max_rows (int): Maximum number of rows to return. Default: 50. Range: 1-200
Returns:
str: JSON object with dataset data and metadata
"""
try:
# Validate dataset has smoltrace- prefix
if "smoltrace-" not in dataset_repo:
return json.dumps({
"dataset_repo": dataset_repo,
"error": "Only datasets with 'smoltrace-' prefix are allowed. Please use smoltrace-leaderboard or other smoltrace-* datasets.",
"data": []
}, indent=2)
# Load dataset from HuggingFace
dataset = load_dataset(dataset_repo, split="train")
df = pd.DataFrame(dataset)
if df.empty:
return json.dumps({
"dataset_repo": dataset_repo,
"error": "Dataset is empty",
"total_rows": 0,
"data": []
}, indent=2)
# Get total row count before limiting
total_rows = len(df)
# Limit rows to avoid overwhelming the context
max_rows = max(1, min(200, max_rows))
# Sort by timestamp if available (newest first)
if "timestamp" in df.columns:
df = df.sort_values("timestamp", ascending=False)
df_limited = df.head(max_rows)
# CRITICAL FIX: Replace NaN/None values with proper None before conversion
# This ensures json.dumps() handles them correctly as null instead of "None" string
df_limited = df_limited.where(pd.notnull(df_limited), None)
# Convert to list of dictionaries
data = df_limited.to_dict(orient="records")
# Build response with metadata
result = {
"dataset_repo": dataset_repo,
"total_rows": total_rows,
"rows_returned": len(data),
"columns": list(df.columns),
"data": data
}
# CRITICAL FIX: Remove default=str to ensure proper JSON serialization
# Using default=str was converting None to string "None" causing agent parsing issues
return json.dumps(result, indent=2)
except Exception as e:
return json.dumps({
"dataset_repo": dataset_repo,
"error": f"Failed to load dataset: {str(e)}",
"data": []
}, indent=2)
# ============================================================================
# MCP RESOURCES - Expose data for retrieval by MCP clients
# ============================================================================
@gr.mcp.resource("leaderboard://{repo}")
def get_leaderboard_data(repo: str = "kshitijthakkar/smoltrace-leaderboard") -> str:
"""
[RAW DATA ONLY] Get raw leaderboard data in JSON format - NO analysis or insights.
⚠️ DO NOT USE THIS for questions like "Which model is leading?" or "What's the best model?"
Instead, use the analyze_leaderboard TOOL which provides AI-powered insights.
This resource is ONLY for:
- Getting raw JSON data when you need to process it yourself
- Low-level data access for custom analysis
- Direct dataset retrieval without AI interpretation
For questions, insights, recommendations, or analysis → use analyze_leaderboard tool instead!
**Note**: All SMOLTRACE datasets are public - no authentication required.
Args:
repo (str): HuggingFace dataset repository name. Default: "kshitijthakkar/smoltrace-leaderboard"
Returns:
str: Raw JSON string containing all evaluation runs without any analysis
"""
try:
ds = load_dataset(repo, split="train")
df = pd.DataFrame(ds)
# Convert to JSON with proper formatting
data = df.to_dict('records')
return json.dumps({
"total_runs": len(data),
"repository": repo,
"data": data
}, indent=2, default=str)
except Exception as e:
return json.dumps({
"error": str(e),
"repository": repo
}, indent=2, default=str)
@gr.mcp.resource("trace://{trace_id}/{repo}")
def get_trace_data(trace_id: str, repo: str) -> str:
"""
[RAW DATA ONLY] Get raw OpenTelemetry trace data in JSON format - NO analysis.
⚠️ DO NOT USE THIS for questions like "Why did this fail?" or "What took the most time?"
Instead, use the debug_trace TOOL which provides AI-powered debugging and insights.
This resource is ONLY for:
- Getting raw OTEL span data when you need to process it yourself
- Low-level trace access for custom analysis
- Direct dataset retrieval without AI interpretation
For debugging, questions, or analysis → use debug_trace tool instead!
**Note**: All SMOLTRACE datasets are public - no authentication required.
Args:
trace_id (str): Unique identifier for the trace (e.g., "trace_abc123")
repo (str): HuggingFace dataset repository containing traces (e.g., "username/agent-traces-model")
Returns:
str: Raw JSON string containing OpenTelemetry spans without any analysis
"""
try:
ds = load_dataset(repo, split="train")
df = pd.DataFrame(ds)
# Find specific trace
trace_data = df[df['trace_id'] == trace_id]
if len(trace_data) == 0:
return json.dumps({
"error": f"Trace {trace_id} not found",
"trace_id": trace_id,
"repository": repo
}, indent=2, default=str)
trace_row = trace_data.iloc[0]
# Parse spans if they're stored as string
spans = trace_row['spans']
if isinstance(spans, str):
spans = json.loads(spans)
return json.dumps({
"trace_id": trace_id,
"repository": repo,
"run_id": trace_row.get('run_id', 'unknown'),
"spans": spans
}, indent=2, default=str)
except Exception as e:
return json.dumps({
"error": str(e),
"trace_id": trace_id,
"repository": repo
}, indent=2, default=str)
@gr.mcp.resource("cost://model/{model_name}")
def get_cost_data(model_name: str) -> str:
"""
[RAW DATA ONLY] Get raw pricing data for a model in JSON format - NO estimates or analysis.
⚠️ DO NOT USE THIS for questions like "How much will this cost?" or "What's the best value?"
Instead, use the estimate_cost TOOL which provides AI-powered cost estimates and recommendations.
This resource is ONLY for:
- Getting raw pricing tables when you need to process them yourself
- Looking up base rates for models and hardware
- Direct price data retrieval without calculations
For cost estimates, predictions, or recommendations → use estimate_cost tool instead!
Args:
model_name (str): Model identifier (e.g., "openai/gpt-4", "meta-llama/Llama-3.1-8B")
Returns:
str: Raw JSON string with pricing rates without any cost estimation
"""
# Cost database
llm_costs = {
"openai/gpt-4": {
"input_per_1k_tokens": 0.03,
"output_per_1k_tokens": 0.06,
"type": "api",
"provider": "openai"
},
"openai/gpt-3.5-turbo": {
"input_per_1k_tokens": 0.0015,
"output_per_1k_tokens": 0.002,
"type": "api",
"provider": "openai"
},
"anthropic/claude-3-opus": {
"input_per_1k_tokens": 0.015,
"output_per_1k_tokens": 0.075,
"type": "api",
"provider": "anthropic"
},
"anthropic/claude-3-sonnet": {
"input_per_1k_tokens": 0.003,
"output_per_1k_tokens": 0.015,
"type": "api",
"provider": "anthropic"
},
"meta-llama/Llama-3.1-8B": {
"input_per_1k_tokens": 0,
"output_per_1k_tokens": 0,
"type": "local",
"provider": "meta",
"requires_gpu": True,
"recommended_hardware": "gpu_a10"
}
}
hardware_costs = {
"cpu": {"hourly_rate_usd": 0.60, "type": "cpu"},
"gpu_a10": {"hourly_rate_usd": 1.10, "type": "gpu", "model": "A10"},
"gpu_h200": {"hourly_rate_usd": 4.50, "type": "gpu", "model": "H200"}
}
model_cost = llm_costs.get(model_name)
if model_cost:
return json.dumps({
"model": model_name,
"cost_data": model_cost,
"hardware_options": hardware_costs,
"currency": "USD"
}, indent=2, default=str)
else:
return json.dumps({
"model": model_name,
"error": "Model not found in cost database",
"available_models": list(llm_costs.keys()),
"hardware_options": hardware_costs
}, indent=2, default=str)
# ============================================================================
# MCP PROMPTS - Reusable prompt templates for common workflows
# ============================================================================
@gr.mcp.prompt()
def analysis_prompt(
analysis_type: str = "leaderboard",
focus_area: str = "overall",
detail_level: str = "detailed"
) -> str:
"""
Generate a prompt template for analyzing agent evaluation data.
This prompt helps standardize analysis requests across different
evaluation data types and focus areas.
Args:
analysis_type (str): Type of analysis. Options: "leaderboard", "trace", "cost". Default: "leaderboard"
focus_area (str): What to focus on. Options: "overall", "performance", "cost", "efficiency". Default: "overall"
detail_level (str): Level of detail. Options: "summary", "detailed", "comprehensive". Default: "detailed"
Returns:
str: Formatted prompt template for analysis
"""
templates = {
"leaderboard": {
"overall": "Analyze the agent evaluation leaderboard data comprehensively. Identify top performers across all metrics (accuracy, cost, latency, CO2), explain trade-offs between different approaches, and provide actionable recommendations for model selection.",
"performance": "Focus on performance metrics in the leaderboard. Compare success rates and accuracy across different models and agent types. Identify which configurations achieve the highest success rates and explain why.",
"cost": "Analyze cost efficiency in the leaderboard. Compare costs across different models and identify the best cost-performance ratios. Recommend the most cost-effective configurations for different use cases.",
"efficiency": "Evaluate efficiency metrics including latency, GPU utilization, and CO2 emissions. Identify the most efficient models and explain how to optimize for speed while maintaining quality."
},
"trace": {
"overall": "Analyze this agent execution trace comprehensively. Explain the sequence of operations, identify any bottlenecks or inefficiencies, and suggest optimizations.",
"performance": "Focus on performance aspects of this trace. Identify which steps took the most time, explain why, and suggest ways to improve execution speed.",
"cost": "Analyze the cost implications of this trace execution. Break down token usage and API calls, calculate costs, and suggest ways to reduce expenses.",
"efficiency": "Evaluate the efficiency of this trace. Identify redundant operations, suggest ways to optimize the execution flow, and recommend best practices."
},
"cost": {
"overall": "Analyze the cost estimation comprehensively. Break down LLM API costs, infrastructure costs, and provide optimization recommendations.",
"performance": "Focus on the cost-performance trade-off. Compare different hardware options and explain which provides the best value.",
"cost": "Deep dive into cost breakdown. Explain each cost component in detail and provide specific recommendations for cost reduction.",
"efficiency": "Analyze cost efficiency. Compare different model configurations and recommend the most cost-effective approach for the given use case."
}
}
detail_prefixes = {
"summary": "Provide a brief, high-level summary. ",
"detailed": "Provide a detailed analysis with specific insights. ",
"comprehensive": "Provide a comprehensive, in-depth analysis with detailed recommendations. "
}
prefix = detail_prefixes.get(detail_level, detail_prefixes["detailed"])
template = templates.get(analysis_type, {}).get(focus_area, templates["leaderboard"]["overall"])
return f"{prefix}{template}"
@gr.mcp.prompt()
def debug_prompt(
debug_type: str = "error",
context: str = "agent_execution"
) -> str:
"""
Generate a prompt template for debugging agent traces.
This prompt helps standardize debugging requests for different
types of issues and contexts.
Args:
debug_type (str): Type of debugging. Options: "error", "performance", "behavior", "optimization". Default: "error"
context (str): Execution context. Options: "agent_execution", "tool_calling", "llm_reasoning". Default: "agent_execution"
Returns:
str: Formatted prompt template for debugging
"""
templates = {
"error": {
"agent_execution": "Debug this agent execution trace to identify why it failed. Analyze each step in the execution flow, identify where the error occurred, explain the root cause, and suggest how to fix it.",
"tool_calling": "Debug this tool calling sequence. Identify which tool call failed or produced unexpected results, explain why it happened, and suggest corrections.",
"llm_reasoning": "Debug the LLM reasoning in this trace. Analyze the prompts and responses, identify where the reasoning went wrong, and suggest improvements to the prompts or approach."
},
"performance": {
"agent_execution": "Analyze this trace for performance issues. Identify bottlenecks, measure time spent in each component, and recommend optimizations to improve execution speed.",
"tool_calling": "Analyze tool calling performance. Identify which tools are slow, explain why, and suggest ways to optimize tool execution or caching.",
"llm_reasoning": "Analyze LLM reasoning efficiency. Identify unnecessary calls, redundant reasoning steps, and suggest ways to streamline the reasoning process."
},
"behavior": {
"agent_execution": "Analyze the agent's behavior in this trace. Explain why the agent made certain decisions, whether the behavior is expected, and suggest improvements if needed.",
"tool_calling": "Analyze tool selection behavior. Explain why certain tools were called, whether the choices were optimal, and suggest alternative approaches if applicable.",
"llm_reasoning": "Analyze the LLM's reasoning patterns. Explain the logic flow, identify any unexpected reasoning, and suggest how to guide the model toward better decisions."
},
"optimization": {
"agent_execution": "Analyze this trace for optimization opportunities. Identify redundant operations, suggest caching strategies, and recommend ways to reduce costs and improve efficiency.",
"tool_calling": "Optimize tool usage in this trace. Suggest ways to reduce tool calls, batch operations, or use more efficient alternatives.",
"llm_reasoning": "Optimize LLM usage. Suggest ways to reduce token usage, improve prompt efficiency, and achieve the same results with lower costs."
}
}
template = templates.get(debug_type, {}).get(context, templates["error"]["agent_execution"])
return template
@gr.mcp.prompt()
def optimization_prompt(
optimization_goal: str = "cost",
constraints: str = "maintain_quality"
) -> str:
"""
Generate a prompt template for optimization recommendations.
This prompt helps standardize optimization requests for different
goals and constraints.
Args:
optimization_goal (str): What to optimize. Options: "cost", "speed", "quality", "efficiency". Default: "cost"
constraints (str): Constraints to consider. Options: "maintain_quality", "maintain_speed", "no_constraints". Default: "maintain_quality"
Returns:
str: Formatted prompt template for optimization
"""
templates = {
"cost": {
"maintain_quality": "Analyze this evaluation setup and recommend cost optimizations while maintaining quality. Consider cheaper models, optimized prompts, caching strategies, and hardware selection. Quantify potential savings.",
"maintain_speed": "Recommend cost optimizations while maintaining execution speed. Consider model alternatives, batch processing, and infrastructure choices that reduce costs without adding latency.",
"no_constraints": "Recommend aggressive cost optimizations. Identify all opportunities to reduce expenses, even if it means trade-offs in quality or speed. Prioritize maximum cost reduction."
},
"speed": {
"maintain_quality": "Recommend speed optimizations while maintaining quality. Consider parallel execution, caching, faster models with similar accuracy, and infrastructure upgrades. Quantify potential speedups.",
"maintain_cost": "Recommend speed optimizations within the current cost budget. Suggest configuration changes, caching strategies, and optimizations that don't increase expenses.",
"no_constraints": "Recommend aggressive speed optimizations. Identify all opportunities to reduce latency, even if it increases costs. Prioritize maximum performance."
},
"quality": {
"maintain_cost": "Recommend quality improvements within the current cost budget. Suggest better prompts, model configurations, and strategies that improve accuracy without increasing expenses.",
"maintain_speed": "Recommend quality improvements while maintaining execution speed. Suggest prompt improvements, reasoning enhancements, and configurations that improve accuracy without adding latency.",
"no_constraints": "Recommend quality improvements without budget constraints. Suggest the best models, optimal configurations, and strategies to maximize accuracy and success rates."
},
"efficiency": {
"maintain_quality": "Recommend overall efficiency improvements. Optimize for the best cost-speed-quality balance. Identify waste, suggest streamlined processes, and provide holistic optimization strategies.",
"maintain_cost": "Recommend efficiency improvements within budget. Focus on reducing waste, optimizing resource usage, and getting better results with the same cost.",
"maintain_speed": "Recommend efficiency improvements maintaining speed. Reduce unnecessary operations, optimize resource usage, and improve output quality without adding latency."
}
}
# Handle constraint variations
if constraints == "maintain_quality" and optimization_goal == "speed":
constraints = "maintain_quality" # Use existing template
elif constraints == "maintain_speed" and optimization_goal == "cost":
constraints = "maintain_speed" # Use existing template
template = templates.get(optimization_goal, {}).get(constraints, templates["cost"]["maintain_quality"])
return template
# ========================================
# NEW TOOLS: Synthetic Dataset Generation
# ========================================
@gr.mcp.tool()
async def generate_synthetic_dataset(
domain: str,
tool_names: str,
num_tasks: int = 10,
difficulty_distribution: str = "balanced",
agent_type: str = "both"
) -> str:
"""
Generate domain-specific synthetic test datasets for SMOLTRACE evaluations using AI.
This tool uses Google Gemini 2.5 Flash to create realistic, domain-specific evaluation
tasks that follow the SMOLTRACE task dataset format. Perfect for creating custom
benchmarks when standard datasets don't fit your use case.
**🚀 Batched Generation for Scale**:
- Requests >20 tasks are automatically split into parallel batches
- Utilizes Gemini's large context window efficiently
- Supports up to 100 tasks with 120s timeout per batch
- Example: 100 tasks = 5 parallel batches (20 tasks each)
**Enterprise Use Case**: Quickly create evaluation datasets for:
- Custom tools and APIs your agents use
- Industry-specific domains (finance, healthcare, legal, manufacturing, etc.)
- Internal workflows and business processes
- Specialized agent capabilities
**Security**: Requires GEMINI_API_KEY environment variable.
Args:
domain (str): The domain for synthetic tasks (e.g., "finance", "healthcare", "travel", "ecommerce", "customer_support")
tool_names (str): Comma-separated list of tool names to include (e.g., "get_weather,search_web,calculator")
num_tasks (int): Number of synthetic tasks to generate. Must be between 5 and 100. Default: 10
- 5-20 tasks: Single batch (fast, ~30-60s)
- 21-100 tasks: Multiple parallel batches (slower, ~60-120s per batch)
difficulty_distribution (str): How to distribute task difficulty. Options: "balanced" (40% easy, 40% medium, 20% hard), "easy_only", "medium_only", "hard_only", "progressive" (50% easy, 30% medium, 20% hard). Default: "balanced"
agent_type (str): Target agent type for tasks. Options: "tool" (ToolCallingAgent), "code" (CodeAgent), "both" (50/50 mix). Default: "both"
Returns:
str: JSON-formatted response with dataset_info (including batch statistics), tasks array (SMOLTRACE format), and usage_instructions
"""
try:
# Initialize Gemini client
gemini_client = GeminiClient()
# Validate inputs
if num_tasks < 5 or num_tasks > 100:
return json.dumps({
"error": "num_tasks must be between 5 and 100",
"num_tasks_provided": num_tasks
}, indent=2)
# Parse tool names
tools = [tool.strip() for tool in tool_names.split(",") if tool.strip()]
if len(tools) == 0:
return json.dumps({
"error": "At least one tool name must be provided",
"tool_names_provided": tool_names
}, indent=2)
# Calculate distributions
difficulty_counts = _calculate_difficulty_distribution(num_tasks, difficulty_distribution)
agent_type_counts = _calculate_agent_type_distribution(num_tasks, agent_type)
# Create generation prompt
generation_prompt = f"""You are an expert at creating synthetic evaluation datasets for AI agents.
Generate {num_tasks} synthetic test tasks for the **{domain}** domain following the SMOLTRACE task format.
**Available Tools**: {", ".join(tools)}
**Difficulty Distribution**:
- Easy ({difficulty_counts['easy']} tasks): Single tool call, straightforward input, clear expected output
- Medium ({difficulty_counts['medium']} tasks): Multiple tool calls OR complex input parsing OR conditional logic
- Hard ({difficulty_counts['hard']} tasks): Multiple tools, complex reasoning, edge cases, error handling
**Agent Type Distribution**:
- Tool Agent ({agent_type_counts['tool']} tasks): Uses ToolCallingAgent - declarative tool calling
- Code Agent ({agent_type_counts['code']} tasks): Uses CodeAgent - writes Python code with tools
**SMOLTRACE Task Format** (required structure):
```json
{{
"id": "string - unique identifier like '{domain.lower()}_{{tool}}_{{number}}'",
"prompt": "string - clear, specific task description",
"expected_tool": "string - the tool name that should be used",
"expected_tool_calls": "integer - how many times the tool should be called (optional, default 1)",
"difficulty": "string - 'easy', 'medium', or 'hard'",
"agent_type": "string - 'tool' or 'code'",
"expected_keywords": "array of strings - keywords expected in response (optional)"
}}
```
**Generation Guidelines**:
1. **Domain Specificity**: Make tasks realistic and specific to the {domain} domain
2. **Tool Usage**: Ensure each task requires using one of: {", ".join(tools)}
3. **Prompt Quality**: Write clear, unambiguous prompts that an agent can execute
4. **Expected Keywords**: Include 2-4 expected keywords for validation (optional but recommended)
5. **Variety**: Vary the tasks to cover different aspects of the domain
**IMPORTANT**: Return ONLY a valid JSON array of tasks. No explanatory text, no markdown formatting, no code blocks. Just the raw JSON array starting with [ and ending with ].
Generate exactly {num_tasks} tasks:"""
print(f"[GENERATE_SYNTHETIC_DATASET] Generating {num_tasks} tasks for domain '{domain}'...")
print(f"[GENERATE_SYNTHETIC_DATASET] Tools: {', '.join(tools)}")
# Import required modules
import asyncio
import google.generativeai as genai
# Determine batching strategy
# Gemini can handle ~20 tasks per call with 8192 token output limit
TASKS_PER_BATCH = 20
num_batches = (num_tasks + TASKS_PER_BATCH - 1) // TASKS_PER_BATCH # Ceiling division
if num_batches > 1:
print(f"[GENERATE_SYNTHETIC_DATASET] Large request detected. Splitting into {num_batches} parallel batches...")
# Create batch generation tasks
async def generate_batch(batch_num: int, batch_size: int, batch_difficulty: dict, batch_agent_type: dict):
"""Generate a single batch of tasks"""
batch_prompt = f"""You are an expert at creating synthetic evaluation datasets for AI agents.
Generate {batch_size} synthetic test tasks for the **{domain}** domain following the SMOLTRACE task format.
**Available Tools**: {", ".join(tools)}
**Difficulty Distribution for this batch**:
- Easy ({batch_difficulty['easy']} tasks): Single tool call, straightforward input, clear expected output
- Medium ({batch_difficulty['medium']} tasks): Multiple tool calls OR complex input parsing OR conditional logic
- Hard ({batch_difficulty['hard']} tasks): Multiple tools, complex reasoning, edge cases, error handling
**Agent Type Distribution for this batch**:
- Tool Agent ({batch_agent_type['tool']} tasks): Uses ToolCallingAgent - declarative tool calling
- Code Agent ({batch_agent_type['code']} tasks): Uses CodeAgent - writes Python code with tools
**SMOLTRACE Task Format** (required structure):
```json
{{
"id": "string - unique identifier like '{domain.lower()}_{{tool}}_batch{batch_num}_{{number}}'",
"prompt": "string - clear, specific task description",
"expected_tool": "string - the tool name that should be used",
"expected_tool_calls": "integer - how many times the tool should be called (optional, default 1)",
"difficulty": "string - 'easy', 'medium', or 'hard'",
"agent_type": "string - 'tool' or 'code'",
"expected_keywords": "array of strings - keywords expected in response (optional)"
}}
```
**Generation Guidelines**:
1. **Domain Specificity**: Make tasks realistic and specific to the {domain} domain
2. **Tool Usage**: Ensure each task requires using one of: {", ".join(tools)}
3. **Prompt Quality**: Write clear, unambiguous prompts that an agent can execute
4. **Expected Keywords**: Include 2-4 expected keywords for validation (optional but recommended)
5. **Variety**: Vary the tasks to cover different aspects of the domain
6. **Unique IDs**: Include 'batch{batch_num}' in task IDs to ensure uniqueness across batches
**IMPORTANT**: Return ONLY a valid JSON array of tasks. No explanatory text, no markdown formatting, no code blocks. Just the raw JSON array starting with [ and ending with ].
Generate exactly {batch_size} tasks:"""
generation_config = {
"temperature": 0.8, # Higher for creativity and diversity
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
}
try:
response = await asyncio.wait_for(
gemini_client.model.generate_content_async(
batch_prompt,
generation_config=generation_config
),
timeout=120.0 # 120 seconds per batch for larger datasets
)
return response.text, None
except Exception as e:
return None, str(e)
# Split difficulty and agent type distributions across batches
def split_distribution(total_counts: dict, num_batches: int, batch_num: int, remaining_tasks: int):
"""Split distribution counts across batches fairly"""
batch_counts = {}
for key, total in total_counts.items():
# Calculate fair share for this batch
base_share = total // num_batches
extra = 1 if batch_num < (total % num_batches) else 0
batch_counts[key] = min(base_share + extra, remaining_tasks)
return batch_counts
# Generate all batches in parallel
batch_tasks = []
remaining_tasks = num_tasks
for batch_num in range(num_batches):
batch_size = min(TASKS_PER_BATCH, remaining_tasks)
# Calculate distributions for this batch
batch_difficulty = split_distribution(difficulty_counts, num_batches, batch_num, batch_size)
batch_agent_type = split_distribution(agent_type_counts, num_batches, batch_num, batch_size)
batch_tasks.append(generate_batch(batch_num, batch_size, batch_difficulty, batch_agent_type))
remaining_tasks -= batch_size
print(f"[GENERATE_SYNTHETIC_DATASET] Executing {num_batches} parallel Gemini API calls...")
# Execute all batches in parallel
batch_results = await asyncio.gather(*batch_tasks)
# Combine and validate results
all_tasks = []
errors = []
for batch_num, (response_text, error) in enumerate(batch_results):
if error:
errors.append(f"Batch {batch_num} failed: {error}")
continue
try:
# Clean response (remove markdown if present)
cleaned_response = response_text.strip()
if cleaned_response.startswith("```"):
import re
match = re.search(r'```(?:json)?\s*\n(.*?)\n```', cleaned_response, re.DOTALL)
if match:
cleaned_response = match.group(1)
# Parse JSON
batch_tasks_parsed = json.loads(cleaned_response)
if not isinstance(batch_tasks_parsed, list):
errors.append(f"Batch {batch_num} did not return a JSON array")
continue
all_tasks.extend(batch_tasks_parsed)
except json.JSONDecodeError as e:
errors.append(f"Batch {batch_num} JSON parsing failed: {str(e)}")
# Check if we got enough tasks
if len(all_tasks) == 0:
return json.dumps({
"error": "All batches failed to generate tasks",
"batch_errors": errors,
"suggestion": "Check GEMINI_API_KEY and try again"
}, indent=2)
if errors:
print(f"[GENERATE_SYNTHETIC_DATASET] Warning: Some batches failed: {errors}")
print(f"[GENERATE_SYNTHETIC_DATASET] Successfully generated {len(all_tasks)} tasks across {num_batches} batch(es)")
# Validate required fields for all tasks
synthetic_tasks = all_tasks
required_fields = ["id", "prompt", "expected_tool", "difficulty", "agent_type"]
for i, task in enumerate(synthetic_tasks):
missing_fields = [field for field in required_fields if field not in task]
if missing_fields:
return json.dumps({
"error": f"Task {i} is missing required fields: {missing_fields}",
"task": task
}, indent=2)
# Return formatted dataset with metadata
result = {
"dataset_info": {
"domain": domain,
"tools": tools,
"num_tasks_requested": num_tasks,
"num_tasks_generated": len(synthetic_tasks),
"num_batches": num_batches,
"batches_succeeded": num_batches - len(errors),
"batches_failed": len(errors) if errors else 0,
"batch_errors": errors if errors else None,
"difficulty_distribution": difficulty_counts,
"agent_type_distribution": agent_type_counts,
"generated_at": datetime.now().isoformat(),
"smoltrace_naming_convention": f"{{username}}/smoltrace-{domain.lower()}-tasks",
"warning": f"⚠️ {len(errors)} batch(es) failed. Generated {len(synthetic_tasks)}/{num_tasks} tasks." if errors else None
},
"tasks": synthetic_tasks,
"usage_instructions": {
"format": "SMOLTRACE task dataset format",
"naming_convention": f"Follow SMOLTRACE naming: {{username}}/smoltrace-{domain.lower()}-tasks or {{username}}/smoltrace-{domain.lower()}-tasks-v1 for versioning",
"how_to_upload": [
"Option 1: Use the push_dataset_to_hub tool in this MCP server",
"Option 2: Manual upload with Python code (see example_code below)"
],
"example_code": f"""from datasets import Dataset
# Extract tasks from this response
tasks = result["tasks"]
# Create and push to HuggingFace (following SMOLTRACE naming convention)
dataset = Dataset.from_list(tasks)
dataset.push_to_hub("your-username/smoltrace-{domain.lower()}-tasks")
# Use in SMOLTRACE evaluation
# smoltrace-eval --model openai/gpt-4 --dataset-name your-username/smoltrace-{domain.lower()}-tasks"""
}
}
return json.dumps(result, indent=2, default=str)
except Exception as e:
return json.dumps({
"error": f"Failed to generate synthetic dataset: {str(e)}",
"domain": domain,
"tools": tool_names
}, indent=2)
@gr.mcp.tool()
async def push_dataset_to_hub(
dataset_json: str,
repo_name: str,
hf_token: str = None,
private: bool = False,
prompt_template: str = None
) -> str:
"""
Push a generated synthetic dataset to HuggingFace Hub with optional prompt template.
This tool uploads datasets created by generate_synthetic_dataset (or any SMOLTRACE-format
dataset) to HuggingFace Hub, making them ready for use in SMOLTRACE evaluations.
Optionally includes a customized prompt template in the dataset card.
**Naming Convention**: Repo name should follow SMOLTRACE convention:
- Format: {username}/smoltrace-{domain}-tasks or {username}/smoltrace-{domain}-tasks-v{version}
- Examples: "mycompany/smoltrace-finance-tasks", "alice/smoltrace-healthcare-tasks-v2"
**Security**: Requires valid HuggingFace token with write permissions. If not provided,
will use HF_TOKEN from environment variables or Settings.
Args:
dataset_json (str): JSON string containing the tasks array (from generate_synthetic_dataset output, use the "tasks" field)
repo_name (str): HuggingFace repository name following SMOLTRACE naming: {username}/smoltrace-{domain}-tasks
hf_token (str): HuggingFace API token with write permissions (optional - uses HF_TOKEN env var if not provided)
private (bool): Whether to create a private dataset. Default: False (public)
prompt_template (str): Optional YAML prompt template to include in dataset card (from generate_prompt_template)
Returns:
str: JSON response with upload status, dataset URL, and next steps
"""
try:
import os
from huggingface_hub import HfApi
# Use provided token or fallback to environment variable
token = hf_token or os.environ.get("HF_TOKEN")
if not token:
return json.dumps({
"error": "HuggingFace token required",
"message": "Please provide hf_token parameter or set HF_TOKEN environment variable in Settings",
"get_token": "https://huggingface.co/settings/tokens"
}, indent=2)
# Validate repo name follows SMOLTRACE convention
if "smoltrace-" not in repo_name and "-tasks" not in repo_name:
return json.dumps({
"warning": "Repository name doesn't follow SMOLTRACE naming convention",
"expected_format": "{username}/smoltrace-{domain}-tasks or {username}/smoltrace-{domain}-tasks-v{version}",
"your_repo_name": repo_name,
"recommendation": "Consider renaming to follow the convention for consistency with SMOLTRACE ecosystem",
"proceeding": "Continuing with upload..."
}, indent=2)
# Parse dataset JSON
try:
tasks = json.loads(dataset_json)
if not isinstance(tasks, list):
return json.dumps({
"error": "dataset_json must be a JSON array of tasks",
"type_received": str(type(tasks))
}, indent=2)
except json.JSONDecodeError as e:
return json.dumps({
"error": "Invalid JSON in dataset_json",
"parse_error": str(e)
}, indent=2)
# Validate task structure
required_fields = ["id", "prompt", "expected_tool", "difficulty", "agent_type"]
for i, task in enumerate(tasks):
missing_fields = [field for field in required_fields if field not in task]
if missing_fields:
return json.dumps({
"error": f"Task {i} is missing required SMOLTRACE fields: {missing_fields}",
"task": task
}, indent=2)
# Create dataset and push to hub
from datasets import Dataset
dataset = Dataset.from_list(tasks)
print(f"[PUSH_DATASET_TO_HUB] Uploading {len(tasks)} tasks to {repo_name}...")
# Push to hub
dataset.push_to_hub(
repo_name,
token=token,
private=private
)
# If prompt template provided, add it to the dataset card
if prompt_template and prompt_template.strip():
try:
print(f"[PUSH_DATASET_TO_HUB] Adding prompt template to dataset card...")
# Create enhanced README with prompt template
readme_content = f"""---
tags:
- smoltrace
- synthetic-data
- agent-evaluation
- mcp-generated
license: mit
---
# SMOLTRACE Synthetic Dataset
This dataset was generated using the TraceMind MCP Server's synthetic data generation tools.
## Dataset Info
- **Tasks**: {len(tasks)}
- **Format**: SMOLTRACE evaluation format
- **Generated**: AI-powered synthetic task generation
## Usage with SMOLTRACE
```python
from datasets import load_dataset
# Load dataset
dataset = load_dataset("{repo_name}")
# Use with SMOLTRACE
# smoltrace-eval --model openai/gpt-4 --dataset-name {repo_name}
```
## Prompt Template
This dataset includes a customized agent prompt template optimized for the domain and tools used.
### Template File
Save the following as `prompt_template.yaml`:
```yaml
{prompt_template}
```
### Using the Template
```python
from smolagents import ToolCallingAgent # or CodeAgent
agent = ToolCallingAgent(
tools=[...], # Your tools
model="openai/gpt-4",
system_prompt_path="prompt_template.yaml"
)
```
## Dataset Structure
Each task contains:
- `id`: Unique task identifier
- `prompt`: Task description
- `expected_tool`: Tool the agent should use
- `difficulty`: Task complexity (easy/medium/hard)
- `agent_type`: Type of agent (tool/code)
## Generated with TraceMind MCP Server
🔗 [TraceMind MCP Server](https://huggingface.co/spaces/MCP-1st-Birthday/TraceMind-mcp-server)
Part of the MCP's 1st Birthday Hackathon project.
"""
# Upload README to dataset repository
api = HfApi()
api.upload_file(
path_or_fileobj=readme_content.encode('utf-8'),
path_in_repo="README.md",
repo_id=repo_name,
repo_type="dataset",
token=token
)
print(f"[PUSH_DATASET_TO_HUB] Prompt template added to dataset card successfully")
except Exception as readme_error:
print(f"[WARNING] Failed to add prompt template to README: {readme_error}")
# Don't fail the whole operation if README update fails
# Return success response
result = {
"status": "success",
"message": f"Successfully uploaded {len(tasks)} tasks to HuggingFace Hub" + (" with prompt template" if prompt_template else ""),
"dataset_info": {
"repository": repo_name,
"num_tasks": len(tasks),
"visibility": "private" if private else "public",
"dataset_url": f"https://huggingface.co/datasets/{repo_name}",
"includes_prompt_template": bool(prompt_template)
},
"next_steps": {
"view_dataset": f"https://huggingface.co/datasets/{repo_name}",
"use_in_smoltrace": f"smoltrace-eval --model openai/gpt-4 --dataset-name {repo_name}",
"use_prompt_template": "Check the README.md for the customized prompt template" if prompt_template else "Generate a prompt template using generate_prompt_template tool",
"share_with_team": f"Team members can access at https://huggingface.co/datasets/{repo_name}" if not private else "Dataset is private - share access via HuggingFace settings"
}
}
return json.dumps(result, indent=2)
except ImportError:
return json.dumps({
"error": "Required packages not installed",
"missing_packages": "datasets, huggingface_hub",
"install_command": "pip install datasets huggingface_hub"
}, indent=2)
except Exception as e:
return json.dumps({
"error": f"Failed to push dataset to hub: {str(e)}",
"repo_name": repo_name
}, indent=2)
# Helper functions for synthetic dataset generation
def _calculate_difficulty_distribution(num_tasks: int, difficulty_distribution: str) -> dict:
"""Calculate how many tasks of each difficulty to generate."""
if difficulty_distribution == "balanced":
easy = int(num_tasks * 0.4)
medium = int(num_tasks * 0.4)
hard = num_tasks - easy - medium
elif difficulty_distribution == "easy_only":
easy, medium, hard = num_tasks, 0, 0
elif difficulty_distribution == "medium_only":
easy, medium, hard = 0, num_tasks, 0
elif difficulty_distribution == "hard_only":
easy, medium, hard = 0, 0, num_tasks
elif difficulty_distribution == "progressive":
easy = int(num_tasks * 0.5)
medium = int(num_tasks * 0.3)
hard = num_tasks - easy - medium
else:
# Default to balanced
easy = int(num_tasks * 0.4)
medium = int(num_tasks * 0.4)
hard = num_tasks - easy - medium
return {"easy": easy, "medium": medium, "hard": hard}
def _calculate_agent_type_distribution(num_tasks: int, agent_type: str) -> dict:
"""Calculate how many tasks for each agent type to generate."""
if agent_type == "tool":
return {"tool": num_tasks, "code": 0}
elif agent_type == "code":
return {"tool": 0, "code": num_tasks}
elif agent_type == "both":
tool_count = num_tasks // 2
code_count = num_tasks - tool_count
return {"tool": tool_count, "code": code_count}
else:
# Default to both
tool_count = num_tasks // 2
code_count = num_tasks - tool_count
return {"tool": tool_count, "code": code_count}
@gr.mcp.tool()
async def generate_prompt_template(
domain: str,
tool_names: str,
agent_type: str = "tool"
) -> str:
"""
Generate customized smolagents prompt template for a specific domain and tool set.
This tool fetches the base prompt template from smolagents GitHub repository and uses
Gemini AI to adapt it for your specific domain and tools. The result is a ready-to-use
prompt template that you can use with SMOLTRACE evaluations.
**Use Case**: When you generate synthetic datasets with `generate_synthetic_dataset`,
use this tool to create a matching prompt template that agents can use during evaluation.
This ensures your evaluation setup is complete and ready to run.
**Integration**: The generated prompt template can be included in your HuggingFace dataset
card, making it easy for anyone to run evaluations with your dataset.
Args:
domain (str): The domain for the prompt template (e.g., "finance", "healthcare", "customer_support")
tool_names (str): Comma-separated list of tool names (e.g., "get_stock_price,calculate_roi,fetch_company_info")
agent_type (str): Agent type - "tool" for ToolCallingAgent or "code" for CodeAgent. Default: "tool"
Returns:
str: JSON response containing the customized YAML prompt template and metadata
"""
try:
import aiohttp
# Initialize Gemini client
gemini_client = GeminiClient()
# Validate agent_type
if agent_type not in ["tool", "code"]:
return json.dumps({
"error": "agent_type must be 'tool' or 'code'",
"agent_type_provided": agent_type
}, indent=2)
# Parse tool names
tools = [tool.strip() for tool in tool_names.split(",") if tool.strip()]
if len(tools) == 0:
return json.dumps({
"error": "At least one tool name must be provided",
"tool_names_provided": tool_names
}, indent=2)
# Determine which template to fetch
if agent_type == "tool":
template_url = "https://raw.githubusercontent.com/huggingface/smolagents/refs/heads/main/src/smolagents/prompts/toolcalling_agent.yaml"
template_name = "ToolCallingAgent"
else: # code
template_url = "https://raw.githubusercontent.com/huggingface/smolagents/refs/heads/main/src/smolagents/prompts/code_agent.yaml"
template_name = "CodeAgent"
# Fetch the base template from GitHub
async with aiohttp.ClientSession() as session:
async with session.get(template_url) as response:
if response.status != 200:
return json.dumps({
"error": f"Failed to fetch template from GitHub (status {response.status})",
"template_url": template_url
}, indent=2)
base_template = await response.text()
# Create customization prompt for Gemini
customization_prompt = f"""You are an expert at creating agent prompt templates for smolagents.
I have a base {template_name} prompt template and need to customize it for a specific domain and set of tools.
**Domain**: {domain}
**Tools Available**: {", ".join(tools)}
**Agent Type**: {template_name}
**Base Template**:
```yaml
{base_template}
```
**Your Task**:
1. Analyze the base template structure
2. Customize it for the {domain} domain
3. Integrate the provided tools ({", ".join(tools)}) into the template
4. Add domain-specific instructions and examples
5. Ensure the tool descriptions are clear and domain-relevant
**Customization Guidelines**:
- Keep the YAML structure intact
- Update the introduction/system message to be domain-specific
- Add clear descriptions for each tool in the context of the {domain} domain
- Include domain-specific examples where appropriate
- Maintain the same placeholder variables (e.g., {{{{tool_descriptions}}}}, {{{{tools}}}})
- Ensure the template is immediately usable with SMOLTRACE
**Output Format**: Return ONLY the customized YAML template. No explanations, no markdown code blocks, just the raw YAML content.
Start your response with the YAML content immediately."""
# Call Gemini to customize the template
generation_config = {
"temperature": 0.3, # Lower temperature for more consistent formatting
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 4096,
}
response = await gemini_client.model.generate_content_async(
customization_prompt,
generation_config=generation_config
)
customized_template = response.text
# Clean up the response (remove any markdown formatting if present)
customized_template = customized_template.strip()
if customized_template.startswith("```yaml"):
customized_template = customized_template.replace("```yaml\n", "").replace("```", "").strip()
elif customized_template.startswith("```"):
customized_template = customized_template.replace("```\n", "").replace("```", "").strip()
# Return response with metadata
return json.dumps({
"template_info": {
"domain": domain,
"tools": tools,
"agent_type": agent_type,
"template_name": template_name,
"base_template_url": template_url,
"customization_method": "Google Gemini 2.5 Flash"
},
"prompt_template": customized_template,
"usage_instructions": f"""
# How to Use This Prompt Template
## In SMOLTRACE Evaluations
1. Save this template to a file (e.g., `{domain}_{agent_type}_agent.yaml`)
2. Use it with SMOLTRACE:
```python
from smolagents import {template_name}
agent = {template_name}(
tools=[...], # Your tools: {", ".join(tools)}
model="openai/gpt-4", # Or your preferred model
system_prompt_path="{domain}_{agent_type}_agent.yaml"
)
```
## In HuggingFace Dataset Card
Add this template to your dataset's README.md:
```markdown
## Agent Prompt Template
This dataset was designed for the following agent configuration:
- **Agent Type**: {template_name}
- **Domain**: {domain}
- **Tools**: {", ".join(tools)}
### Prompt Template (YAML)
See the `prompt_template.yaml` file in this repository.
```
## Testing the Template
Use this template when evaluating with the synthetic dataset you generated.
The template is pre-configured for the {domain} domain and includes all necessary
tool descriptions and examples.
"""
}, indent=2)
except Exception as e:
import traceback
error_details = traceback.format_exc()
return json.dumps({
"error": f"Failed to generate prompt template: {str(e)}",
"error_details": error_details
}, indent=2)
|