Spaces:
Sleeping
Sleeping
File size: 112,423 Bytes
4305b0a d9837be 69f6ed5 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 4305b0a 9d7d2e0 4305b0a 9d7d2e0 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 9d7d2e0 4305b0a d9837be 69f6ed5 d9837be 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 6fba44c 4305b0a 5ebc627 4305b0a ff4c876 4305b0a 5ebc627 4305b0a 69f6ed5 5ebc627 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 a5f4f6d 4305b0a 5ebc627 4305b0a 5ebc627 69f6ed5 4305b0a ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 e33b088 ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a d9837be 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 ff4c876 4305b0a 40b5db7 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 40b5db7 69f6ed5 4305b0a 40b5db7 4305b0a 69f6ed5 4305b0a 3acc06b 69f6ed5 4305b0a 69f6ed5 ff4c876 40b5db7 ff4c876 40b5db7 ff4c876 40b5db7 ff4c876 40b5db7 ff4c876 40b5db7 ff4c876 40b5db7 ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 ff4c876 5ebc627 ff4c876 69f6ed5 5ebc627 ff4c876 4305b0a ff4c876 69f6ed5 4305b0a 69f6ed5 ff4c876 4305b0a ff4c876 4305b0a ff4c876 5ebc627 ff4c876 5ebc627 69f6ed5 4305b0a ff4c876 5ebc627 4305b0a ff4c876 5ebc627 4305b0a ff4c876 4305b0a ff4c876 5ebc627 69f6ed5 4305b0a ff4c876 5ebc627 69f6ed5 ff4c876 4305b0a ff4c876 69f6ed5 5ebc627 69f6ed5 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a ff4c876 5ebc627 ff4c876 5ebc627 9d7d2e0 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 5ebc627 ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 5ebc627 4305b0a 69f6ed5 4305b0a 40b5db7 4305b0a 5ebc627 40b5db7 69f6ed5 40b5db7 4305b0a 40b5db7 4305b0a 40b5db7 69f6ed5 40b5db7 4305b0a 69f6ed5 4305b0a ff4c876 4305b0a ff4c876 69f6ed5 ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 ff4c876 69f6ed5 4305b0a ff4c876 69f6ed5 ff4c876 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 ff4c876 4305b0a ff4c876 4305b0a 69f6ed5 ff4c876 69f6ed5 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 ff4c876 69f6ed5 ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a ff4c876 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 5ebc627 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 40b5db7 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 5ebc627 69f6ed5 4305b0a 5ebc627 9d7d2e0 5ebc627 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 6fba44c e33b088 69f6ed5 4305b0a 69f6ed5 40b5db7 6fba44c e33b088 69f6ed5 4305b0a 69f6ed5 4305b0a 69f6ed5 4305b0a 5ebc627 e33b088 5ebc627 e33b088 5ebc627 |
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 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 |
"""
MAI Diagnostic Orchestrator (MAI-DxO)
This script provides a complete implementation of the "Sequential Diagnosis with Language Models"
paper, using the `swarms` framework. It simulates a virtual panel of physician-agents to perform
iterative medical diagnosis with cost-effectiveness optimization.
Based on the paper: "Sequential Diagnosis with Language Models"
(arXiv:2506.22405v1) by Nori et al.
Key Features:
- Virtual physician panel with specialized roles (Hypothesis, Test-Chooser, Challenger, Stewardship, Checklist)
- Multiple operational modes (instant, question_only, budgeted, no_budget, ensemble)
- Comprehensive cost tracking and budget management
- Clinical accuracy evaluation with 5-point Likert scale
- Gatekeeper system for realistic clinical information disclosure
- Ensemble methods for improved diagnostic accuracy
Example Usage:
# Standard MAI-DxO usage
orchestrator = MaiDxOrchestrator(model_name="gpt-4o")
result = orchestrator.run(initial_case_info, full_case_details, ground_truth)
# Budget-constrained variant
budgeted_orchestrator = MaiDxOrchestrator.create_variant("budgeted", budget=5000)
# Ensemble approach
ensemble_result = orchestrator.run_ensemble(initial_case_info, full_case_details, ground_truth)
"""
# Enable debug mode if environment variable is set
import os
import json
import sys
import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, List, Union, Literal, Optional
from loguru import logger
from pydantic import BaseModel, Field, ValidationError
from swarms import Agent, Conversation
from dotenv import load_dotenv
load_dotenv()
# Configure Loguru with beautiful formatting and features
logger.remove() # Remove default handler
# Console handler with beautiful colors
logger.add(
sys.stdout,
level="INFO",
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
colorize=True,
)
if os.getenv("MAIDX_DEBUG", "").lower() in ("1", "true", "yes"):
logger.add(
"logs/maidx_debug_{time:YYYY-MM-DD}.log",
level="DEBUG",
format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
rotation="1 day",
retention="3 days",
)
logger.info(
"π Debug logging enabled - logs will be written to logs/ directory"
)
# File handler for persistent logging (optional - uncomment if needed)
# logger.add(
# "logs/mai_dxo_{time:YYYY-MM-DD}.log",
# rotation="1 day",
# retention="7 days",
# level="DEBUG",
# format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
# compression="zip"
# )
# --- Data Structures and Enums ---
class AgentRole(Enum):
"""Enumeration of roles for the virtual physician panel."""
HYPOTHESIS = "Dr. Hypothesis"
TEST_CHOOSER = "Dr. Test-Chooser"
CHALLENGER = "Dr. Challenger"
STEWARDSHIP = "Dr. Stewardship"
CHECKLIST = "Dr. Checklist"
CONSENSUS = "Consensus Coordinator"
GATEKEEPER = "Gatekeeper"
JUDGE = "Judge"
@dataclass
class CaseState:
"""Structured state management for diagnostic process - addresses Category 2.1"""
initial_vignette: str
evidence_log: List[str] = field(default_factory=list)
differential_diagnosis: Dict[str, float] = field(default_factory=dict)
tests_performed: List[str] = field(default_factory=list)
questions_asked: List[str] = field(default_factory=list)
cumulative_cost: int = 0
iteration: int = 0
last_actions: List['Action'] = field(default_factory=list) # For stagnation detection
def add_evidence(self, evidence: str):
"""Add new evidence to the case"""
self.evidence_log.append(f"[Turn {self.iteration}] {evidence}")
def update_differential(self, diagnosis_dict: Dict[str, float]):
"""Update differential diagnosis probabilities"""
self.differential_diagnosis.update(diagnosis_dict)
def add_test(self, test_name: str):
"""Record a test that was performed"""
self.tests_performed.append(test_name)
def add_question(self, question: str):
"""Record a question that was asked"""
self.questions_asked.append(question)
def is_stagnating(self, new_action: 'Action') -> bool:
"""Detect if the system is stuck in a loop - addresses Category 1.2"""
if len(self.last_actions) < 2:
return False
# Check if the new action is identical to recent ones
for last_action in self.last_actions[-2:]:
if (last_action.action_type == new_action.action_type and
last_action.content == new_action.content):
return True
return False
def add_action(self, action: 'Action'):
"""Add action to history and maintain sliding window"""
self.last_actions.append(action)
if len(self.last_actions) > 3: # Keep only last 3 actions
self.last_actions.pop(0)
def get_max_confidence(self) -> float:
"""Get the maximum confidence from differential diagnosis"""
if not self.differential_diagnosis:
return 0.0
return max(self.differential_diagnosis.values())
def get_leading_diagnosis(self) -> str:
"""Get the diagnosis with highest confidence"""
if not self.differential_diagnosis:
return "No diagnosis formulated"
return max(self.differential_diagnosis.items(), key=lambda x: x[1])[0]
def summarize_evidence(self) -> str:
"""Create a concise summary of evidence for token efficiency"""
if len(self.evidence_log) <= 5:
return "\n".join(self.evidence_log)
# Keep first 2 and last 3 entries, summarize middle
summary_parts = []
summary_parts.extend(self.evidence_log[:2])
if len(self.evidence_log) > 5:
middle_count = len(self.evidence_log) - 5
summary_parts.append(f"[... {middle_count} additional findings ...]")
summary_parts.extend(self.evidence_log[-3:])
return "\n".join(summary_parts)
@dataclass
class DeliberationState:
"""Structured state for panel deliberation - addresses Category 1.1"""
hypothesis_analysis: str = ""
test_chooser_analysis: str = ""
challenger_analysis: str = ""
stewardship_analysis: str = ""
checklist_analysis: str = ""
situational_context: str = ""
stagnation_detected: bool = False
retry_count: int = 0
def to_consensus_prompt(self) -> str:
"""Generate a structured prompt for the consensus coordinator - no truncation, let agent self-regulate"""
prompt = f"""
You are the Consensus Coordinator. Here is the panel's analysis:
**Differential Diagnosis (Dr. Hypothesis):**
{self.hypothesis_analysis or 'Not yet formulated'}
**Test Recommendations (Dr. Test-Chooser):**
{self.test_chooser_analysis or 'None provided'}
**Critical Challenges (Dr. Challenger):**
{self.challenger_analysis or 'None identified'}
**Cost Assessment (Dr. Stewardship):**
{self.stewardship_analysis or 'Not evaluated'}
**Quality Control (Dr. Checklist):**
{self.checklist_analysis or 'No issues noted'}
"""
if self.stagnation_detected:
prompt += "\n**STAGNATION DETECTED** - The panel is repeating actions. You MUST make a decisive choice or provide final diagnosis."
if self.situational_context:
prompt += f"\n**Situational Context:** {self.situational_context}"
prompt += "\n\nBased on this comprehensive panel input, use the make_consensus_decision function to provide your structured action."
return prompt
@dataclass
class DiagnosisResult:
"""Stores the final result of a diagnostic session."""
final_diagnosis: str
ground_truth: str
accuracy_score: float
accuracy_reasoning: str
total_cost: int
iterations: int
conversation_history: str
class Action(BaseModel):
"""Pydantic model for a structured action decided by the consensus agent."""
action_type: Literal["ask", "test", "diagnose"] = Field(
..., description="The type of action to perform."
)
content: Union[str, List[str]] = Field(
...,
description="The content of the action (question, test name, or diagnosis).",
)
reasoning: str = Field(
..., description="The reasoning behind choosing this action."
)
# ------------------------------------------------------------------
# Strongly-typed models for function-calling arguments (type safety)
# ------------------------------------------------------------------
class ConsensusArguments(BaseModel):
"""Typed model for the `make_consensus_decision` function call."""
action_type: Literal["ask", "test", "diagnose"]
content: Union[str, List[str]]
reasoning: str
class DifferentialDiagnosisItem(BaseModel):
"""Single differential diagnosis item returned by Dr. Hypothesis."""
diagnosis: str
probability: float
rationale: str
class HypothesisArguments(BaseModel):
"""Typed model for the `update_differential_diagnosis` function call."""
summary: str
differential_diagnoses: List[DifferentialDiagnosisItem]
key_evidence: str
contradictory_evidence: Optional[str] = None
# --- Main Orchestrator Class ---
class MaiDxOrchestrator:
"""
Implements the MAI Diagnostic Orchestrator (MAI-DxO) framework.
This class orchestrates a virtual panel of AI agents to perform sequential medical diagnosis,
evaluates the final diagnosis, and tracks costs.
Enhanced with structured deliberation and proper state management as per research paper.
"""
def __init__(
self,
model_name: str = "gpt-4o-mini", # Fixed: Use valid GPT-4 Turbo model name
max_iterations: int = 10,
initial_budget: int = 10000,
mode: str = "no_budget", # "instant", "question_only", "budgeted", "no_budget", "ensemble"
physician_visit_cost: int = 300,
enable_budget_tracking: bool = False,
request_delay: float = 8.0, # seconds to wait between model calls to mitigate rate-limits
):
"""
Initializes the MAI-DxO system with improved architecture.
Args:
model_name (str): The language model to be used by all agents.
max_iterations (int): The maximum number of diagnostic loops.
initial_budget (int): The starting budget for diagnostic tests.
mode (str): The operational mode of MAI-DxO.
physician_visit_cost (int): Cost per physician visit.
enable_budget_tracking (bool): Whether to enable budget tracking.
request_delay (float): Seconds to wait between model calls to mitigate rate-limits.
"""
self.model_name = model_name
self.max_iterations = max_iterations
self.initial_budget = initial_budget
self.mode = mode
self.physician_visit_cost = physician_visit_cost
self.enable_budget_tracking = enable_budget_tracking
# Throttle settings to avoid OpenAI TPM rate-limits
self.request_delay = max(request_delay, 0)
# Token management
self.max_total_tokens_per_request = 25000 # Safety margin below 30k limit
self.cumulative_cost = 0
self.differential_diagnosis = "Not yet formulated."
self.conversation = Conversation(
time_enabled=True, autosave=False, save_enabled=False
)
# Initialize case state for structured state management
self.case_state = None
# Enhanced cost model based on the paper's methodology
self.test_cost_db = {
"default": 150,
"cbc": 50,
"complete blood count": 50,
"fbc": 50,
"chest x-ray": 200,
"chest xray": 200,
"mri": 1500,
"mri brain": 1800,
"mri neck": 1600,
"ct scan": 1200,
"ct chest": 1300,
"ct abdomen": 1400,
"biopsy": 800,
"core biopsy": 900,
"immunohistochemistry": 400,
"fish test": 500,
"fish": 500,
"ultrasound": 300,
"ecg": 100,
"ekg": 100,
"blood glucose": 30,
"liver function tests": 80,
"renal function": 70,
"toxic alcohol panel": 200,
"urinalysis": 40,
"culture": 150,
"pathology": 600,
}
self._init_agents()
logger.info(
f"π₯ MAI Diagnostic Orchestrator initialized successfully in '{mode}' mode with budget ${initial_budget:,}"
)
def _get_agent_max_tokens(self, role: AgentRole) -> int:
"""Get max_tokens for each agent based on their role - agents will self-regulate based on token guidance"""
token_limits = {
# Reasonable limits - agents will adjust their verbosity based on token guidance
AgentRole.HYPOTHESIS: 1200, # Function calling keeps this structured, but allow room for quality
AgentRole.TEST_CHOOSER: 800, # Need space for test rationale
AgentRole.CHALLENGER: 800, # Need space for critical analysis
AgentRole.STEWARDSHIP: 600,
AgentRole.CHECKLIST: 400,
AgentRole.CONSENSUS: 500, # Function calling is efficient
AgentRole.GATEKEEPER: 1000, # Needs to provide detailed clinical findings
AgentRole.JUDGE: 700,
}
return token_limits.get(role, 600)
def _estimate_tokens(self, text: str) -> int:
"""Rough token estimation (1 token β 4 characters for English)"""
return len(text) // 4
def _generate_token_guidance(self, input_tokens: int, max_output_tokens: int, total_tokens: int, agent_role: AgentRole) -> str:
"""Generate dynamic token guidance for agents to self-regulate their responses"""
# Determine urgency level based on token usage
if total_tokens > self.max_total_tokens_per_request:
urgency = "CRITICAL"
strategy = "Be extremely concise. Prioritize only the most essential information."
elif total_tokens > self.max_total_tokens_per_request * 0.8:
urgency = "HIGH"
strategy = "Be concise and focus on key points. Avoid elaborate explanations."
elif total_tokens > self.max_total_tokens_per_request * 0.6:
urgency = "MODERATE"
strategy = "Be reasonably concise while maintaining necessary detail."
else:
urgency = "LOW"
strategy = "You can provide detailed analysis within your allocated tokens."
# Role-specific guidance
role_specific_guidance = {
AgentRole.HYPOTHESIS: "Focus on top 2-3 diagnoses with probabilities. Prioritize summary over detailed pathophysiology.",
AgentRole.TEST_CHOOSER: "Recommend 1-2 highest-yield tests. Focus on which hypotheses they'll help differentiate.",
AgentRole.CHALLENGER: "Identify 1-2 most critical biases or alternative diagnoses. Be direct and specific.",
AgentRole.STEWARDSHIP: "Focus on cost-effectiveness assessment. Recommend cheaper alternatives where applicable.",
AgentRole.CHECKLIST: "Provide concise quality check. Flag critical issues only.",
AgentRole.CONSENSUS: "Function calling enforces structure. Focus on clear reasoning.",
AgentRole.GATEKEEPER: "Provide specific clinical findings. Be factual and complete but not verbose.",
AgentRole.JUDGE: "Provide score and focused justification. Be systematic but concise."
}.get(agent_role, "Be concise and focused.")
guidance = f"""
[TOKEN MANAGEMENT - {urgency} PRIORITY]
Input: {input_tokens} tokens | Your Output Limit: {max_output_tokens} tokens | Total: {total_tokens} tokens
Strategy: {strategy}
Role Focus: {role_specific_guidance}
IMPORTANT: Adjust your response length and detail level based on this guidance. Prioritize the most critical information for your role.
"""
return guidance
def _init_agents(self) -> None:
"""Initializes all required agents with their specific roles and prompts."""
# Define the structured output tool for consensus decisions
consensus_tool = {
"type": "function",
"function": {
"name": "make_consensus_decision",
"description": "Make a structured consensus decision for the next diagnostic action",
"parameters": {
"type": "object",
"properties": {
"action_type": {
"type": "string",
"enum": ["ask", "test", "diagnose"],
"description": "The type of action to perform"
},
"content": {
"type": "string",
"description": "The specific content of the action (question, test name, or diagnosis)"
},
"reasoning": {
"type": "string",
"description": "The detailed reasoning behind this decision, synthesizing panel input"
}
},
"required": ["action_type", "content", "reasoning"]
}
}
}
# Define structured output tool for differential diagnosis
hypothesis_tool = {
"type": "function",
"function": {
"name": "update_differential_diagnosis",
"description": "Update the differential diagnosis with structured probabilities and reasoning",
"parameters": {
"type": "object",
"properties": {
"summary": {
"type": "string",
"description": "One-sentence summary of primary diagnostic conclusion and confidence"
},
"differential_diagnoses": {
"type": "array",
"items": {
"type": "object",
"properties": {
"diagnosis": {"type": "string", "description": "The diagnosis name"},
"probability": {"type": "number", "minimum": 0, "maximum": 1, "description": "Probability as decimal (0.0-1.0)"},
"rationale": {"type": "string", "description": "Brief rationale for this diagnosis"}
},
"required": ["diagnosis", "probability", "rationale"]
},
"minItems": 2,
"maxItems": 5,
"description": "Top 2-5 differential diagnoses with probabilities"
},
"key_evidence": {
"type": "string",
"description": "Key supporting evidence for leading hypotheses"
},
"contradictory_evidence": {
"type": "string",
"description": "Critical contradictory evidence that must be addressed"
}
},
"required": ["summary", "differential_diagnoses", "key_evidence"]
}
}
}
self.agents = {}
for role in AgentRole:
if role == AgentRole.CONSENSUS:
# Use function calling for consensus agent to ensure structured output
self.agents[role] = Agent(
agent_name=role.value,
system_prompt=self._get_prompt_for_role(role),
model_name=self.model_name,
max_loops=1,
tools_list_dictionary=[consensus_tool], # swarms expects tools_list_dictionary
tool_choice="auto", # Let the model choose to use the tool
print_on=True,
max_tokens=self._get_agent_max_tokens(role),
)
elif role == AgentRole.HYPOTHESIS:
# Use function calling for hypothesis agent to ensure structured differential
self.agents[role] = Agent(
agent_name=role.value,
system_prompt=self._get_prompt_for_role(role),
model_name=self.model_name,
max_loops=1,
tools_list_dictionary=[hypothesis_tool],
tool_choice="auto",
print_on=True,
max_tokens=self._get_agent_max_tokens(role),
)
else:
# Regular agents without function calling
self.agents[role] = Agent(
agent_name=role.value,
system_prompt=self._get_prompt_for_role(role),
model_name=self.model_name,
max_loops=1,
output_type="str",
print_on=True,
max_tokens=self._get_agent_max_tokens(role),
)
logger.info(
f"π₯ {len(self.agents)} virtual physician agents initialized and ready for consultation"
)
def _get_dynamic_context(self, role: AgentRole, case_state: CaseState) -> str:
"""Generate dynamic context for agents based on current situation - addresses Category 4.2"""
remaining_budget = self.initial_budget - case_state.cumulative_cost
# Calculate confidence from differential diagnosis
max_confidence = max(case_state.differential_diagnosis.values()) if case_state.differential_diagnosis else 0
context = ""
if role == AgentRole.STEWARDSHIP and remaining_budget < 1000:
context = f"""
**SITUATIONAL CONTEXT: URGENT**
The remaining budget is critically low (${remaining_budget}). All recommendations must be focused on maximum cost-effectiveness. Veto any non-essential or high-cost tests.
"""
elif role == AgentRole.HYPOTHESIS and max_confidence > 0.75:
context = f"""
**SITUATIONAL CONTEXT: FINAL STAGES**
The panel is converging on a diagnosis (current max confidence: {max_confidence:.0%}). Your primary role now is to confirm the leading hypothesis or state what single piece of evidence is needed to reach >85% confidence.
"""
elif role == AgentRole.CONSENSUS and case_state.iteration > 5:
context = f"""
**SITUATIONAL CONTEXT: EXTENDED CASE**
This case has gone through {case_state.iteration} iterations. Focus on decisive actions that will lead to a definitive diagnosis rather than additional exploratory steps.
"""
return context
def _get_prompt_for_role(self, role: AgentRole, case_state: CaseState = None) -> str:
"""Returns the system prompt for a given agent role with dynamic context."""
# Add dynamic context if case_state is provided
dynamic_context = ""
if case_state:
dynamic_context = self._get_dynamic_context(role, case_state)
# --- Compact, token-efficient prompts ---
base_prompts = {
AgentRole.HYPOTHESIS: f"""{dynamic_context}
MANDATE: Keep an up-to-date, probability-ranked differential.
DIRECTIVES:
1. Return 2-5 diagnoses (prob 0-1) with 1-line rationale.
2. List key supporting & contradictory evidence.
You MUST call update_differential_diagnosis().""",
AgentRole.TEST_CHOOSER: f"""{dynamic_context}
MANDATE: Pick the highest-yield tests.
DIRECTIVES:
1. Suggest β€3 tests that best separate current diagnoses.
2. Note target hypothesis & info-gain vs cost.
Limit: focus on top 1-2 critical points.""",
AgentRole.CHALLENGER: f"""{dynamic_context}
MANDATE: Expose the biggest flaw or bias.
DIRECTIVES:
1. Name the key bias/contradiction.
2. Propose an alternate diagnosis or falsifying test.
Reply concisely (top 2 issues).""",
AgentRole.STEWARDSHIP: f"""{dynamic_context}
MANDATE: Ensure cost-effective care.
DIRECTIVES:
1. Rate proposed tests (High/Mod/Low value).
2. Suggest cheaper equivalents where possible.
Be brief; highlight savings.""",
AgentRole.CHECKLIST: f"""{dynamic_context}
MANDATE: Guarantee quality & consistency.
DIRECTIVES:
1. Flag invalid tests or logic gaps.
2. Note safety concerns.
Return bullet list of critical items.""",
AgentRole.CONSENSUS: f"""{dynamic_context}
MANDATE: Decide the next action.
DECISION RULES:
1. If confidence >85% & no major objection β diagnose.
2. Else address Challenger's top concern.
3. Else order highest info-gain (cheapest) test.
4. Else ask the most informative question.
You MUST call make_consensus_decision().""",
}
# Use existing prompts for other roles, just add dynamic context
if role not in base_prompts:
return dynamic_context + self._get_original_prompt_for_role(role)
return base_prompts[role]
def _get_original_prompt_for_role(self, role: AgentRole) -> str:
"""Returns original system prompts for roles not yet updated"""
prompts = {
AgentRole.HYPOTHESIS: (
"""
You are Dr. Hypothesis, a specialist in maintaining differential diagnoses. Your role is critical to the diagnostic process.
CORE RESPONSIBILITIES:
- Maintain a probability-ranked differential diagnosis with the top 3 most likely conditions
- Update probabilities using Bayesian reasoning after each new finding
- Consider both common and rare diseases appropriate to the clinical context
- Explicitly track how new evidence changes your diagnostic thinking
APPROACH:
1. Start with the most likely diagnoses based on presenting symptoms
2. For each new piece of evidence, consider:
- How it supports or refutes each hypothesis
- Whether it suggests new diagnoses to consider
- How it changes the relative probabilities
3. Always explain your Bayesian reasoning clearly
OUTPUT FORMAT:
Provide your updated differential diagnosis with:
- Top 3 diagnoses with probability estimates (percentages)
- Brief rationale for each
- Key evidence supporting each hypothesis
- Evidence that contradicts or challenges each hypothesis
Remember: Your differential drives the entire diagnostic process. Be thorough, evidence-based, and adaptive.
"""
),
AgentRole.TEST_CHOOSER: (
"""
You are Dr. Test-Chooser, a specialist in diagnostic test selection and information theory.
CORE RESPONSIBILITIES:
- Select up to 3 diagnostic tests per round that maximally discriminate between leading hypotheses
- Optimize for information value, not just clinical reasonableness
- Consider test characteristics: sensitivity, specificity, positive/negative predictive values
- Balance diagnostic yield with patient burden and resource utilization
SELECTION CRITERIA:
1. Information Value: How much will this test change diagnostic probabilities?
2. Discriminatory Power: How well does it distinguish between competing hypotheses?
3. Clinical Impact: Will the result meaningfully alter management?
4. Sequential Logic: What should we establish first before ordering more complex tests?
APPROACH:
- For each proposed test, explicitly state which hypotheses it will help confirm or exclude
- Consider both positive and negative results and their implications
- Think about test sequences (e.g., basic labs before advanced imaging)
- Avoid redundant tests that won't add new information
OUTPUT FORMAT:
For each recommended test:
- Test name (be specific)
- Primary hypotheses it will help evaluate
- Expected information gain
- How results will change management decisions
Focus on tests that will most efficiently narrow the differential diagnosis.
"""
),
AgentRole.CHALLENGER: (
"""
You are Dr. Challenger, the critical thinking specialist and devil's advocate.
CORE RESPONSIBILITIES:
- Identify and challenge cognitive biases in the diagnostic process
- Highlight contradictory evidence that might be overlooked
- Propose alternative hypotheses and falsifying tests
- Guard against premature diagnostic closure
COGNITIVE BIASES TO WATCH FOR:
1. Anchoring: Over-reliance on initial impressions
2. Confirmation bias: Seeking only supporting evidence
3. Availability bias: Overestimating probability of recently seen conditions
4. Representativeness: Ignoring base rates and prevalence
5. Search satisficing: Stopping at "good enough" explanations
YOUR APPROACH:
- Ask "What else could this be?" and "What doesn't fit?"
- Challenge assumptions and look for alternative explanations
- Propose tests that could disprove the leading hypothesis
- Consider rare diseases when common ones don't fully explain the picture
- Advocate for considering multiple conditions simultaneously
OUTPUT FORMAT:
- Specific biases you've identified in the current reasoning
- Evidence that contradicts the leading hypotheses
- Alternative diagnoses to consider
- Tests that could falsify current assumptions
- Red flags or concerning patterns that need attention
Be constructively critical - your role is to strengthen diagnostic accuracy through rigorous challenge.
"""
),
AgentRole.STEWARDSHIP: (
"""
You are Dr. Stewardship, the resource optimization and cost-effectiveness specialist.
CORE RESPONSIBILITIES:
- Enforce cost-conscious, high-value care
- Advocate for cheaper alternatives when diagnostically equivalent
- Challenge low-yield, expensive tests
- Balance diagnostic thoroughness with resource stewardship
COST-VALUE FRAMEWORK:
1. High-Value Tests: Low cost, high diagnostic yield, changes management
2. Moderate-Value Tests: Moderate cost, specific indication, incremental value
3. Low-Value Tests: High cost, low yield, minimal impact on decisions
4. No-Value Tests: Any cost, no diagnostic value, ordered out of habit
ALTERNATIVE STRATEGIES:
- Could patient history/physical exam provide this information?
- Is there a less expensive test with similar diagnostic value?
- Can we use a staged approach (cheap test first, expensive if needed)?
- Does the test result actually change management?
YOUR APPROACH:
- Review all proposed tests for necessity and value
- Suggest cost-effective alternatives
- Question tests that don't clearly advance diagnosis
- Advocate for asking questions before ordering expensive tests
- Consider the cumulative cost burden
OUTPUT FORMAT:
- Assessment of proposed tests (high/moderate/low/no value)
- Specific cost-effective alternatives
- Questions that might obviate need for testing
- Recommended modifications to testing strategy
- Cumulative cost considerations
Your goal: Maximum diagnostic accuracy at minimum necessary cost.
"""
),
AgentRole.CHECKLIST: (
"""
You are Dr. Checklist, the quality assurance and consistency specialist.
CORE RESPONSIBILITIES:
- Perform silent quality control on all panel deliberations
- Ensure test names are valid and properly specified
- Check internal consistency of reasoning across panel members
- Flag logical errors or contradictions in the diagnostic approach
QUALITY CHECKS:
1. Test Validity: Are proposed tests real and properly named?
2. Logical Consistency: Do the recommendations align with the differential?
3. Evidence Integration: Are all findings being considered appropriately?
4. Process Adherence: Is the panel following proper diagnostic methodology?
5. Safety Checks: Are any critical possibilities being overlooked?
SPECIFIC VALIDATIONS:
- Test names match standard medical terminology
- Proposed tests are appropriate for the clinical scenario
- No contradictions between different panel members' reasoning
- All significant findings are being addressed
- No gaps in the diagnostic logic
OUTPUT FORMAT:
- Brief validation summary (β Clear / β Issues noted)
- Any test name corrections needed
- Logical inconsistencies identified
- Missing considerations or gaps
- Process improvement suggestions
Keep your feedback concise but comprehensive. Flag any issues that could compromise diagnostic quality.
"""
),
AgentRole.CONSENSUS: (
"""
You are the Consensus Coordinator, responsible for synthesizing the virtual panel's expertise into a single, optimal decision.
CORE RESPONSIBILITIES:
- Integrate input from Dr. Hypothesis, Dr. Test-Chooser, Dr. Challenger, Dr. Stewardship, and Dr. Checklist
- Decide on the single best next action: 'ask', 'test', or 'diagnose'
- Balance competing priorities: accuracy, cost, efficiency, and thoroughness
- Ensure the chosen action advances the diagnostic process optimally
DECISION FRAMEWORK:
1. DIAGNOSE: Choose when diagnostic certainty is sufficiently high (>85%) for the leading hypothesis
2. TEST: Choose when tests will meaningfully discriminate between hypotheses
3. ASK: Choose when history/exam questions could provide high-value information
SYNTHESIS PROCESS:
- Weight Dr. Hypothesis's confidence level and differential
- Consider Dr. Test-Chooser's information value analysis
- Incorporate Dr. Challenger's alternative perspectives
- Respect Dr. Stewardship's cost-effectiveness concerns
- Address any quality issues raised by Dr. Checklist
OUTPUT REQUIREMENTS:
Provide a JSON object with this exact structure:
{
"action_type": "ask" | "test" | "diagnose",
"content": "specific question(s), test name(s), or final diagnosis",
"reasoning": "clear justification synthesizing panel input"
}
For action_type "ask": content should be specific patient history or physical exam questions
For action_type "test": content should be properly named diagnostic tests (up to 3)
For action_type "diagnose": content should be the complete, specific final diagnosis
Make the decision that best advances accurate, cost-effective diagnosis.
"""
),
AgentRole.GATEKEEPER: (
"""
You are the Gatekeeper, the clinical information oracle with complete access to the patient case file.
CORE RESPONSIBILITIES:
- Provide objective, specific clinical findings when explicitly requested
- Serve as the authoritative source for all patient information
- Generate realistic synthetic findings for tests not in the original case
- Maintain clinical realism while preventing information leakage
RESPONSE PRINCIPLES:
1. OBJECTIVITY: Provide only factual findings, never interpretations or impressions
2. SPECIFICITY: Give precise, detailed results when tests are properly ordered
3. REALISM: Ensure all responses reflect realistic clinical scenarios
4. NO HINTS: Never provide diagnostic clues or suggestions
5. CONSISTENCY: Maintain coherence across all provided information
HANDLING REQUESTS:
- Patient History Questions: Provide relevant history from case file or realistic details
- Physical Exam: Give specific examination findings as would be documented
- Diagnostic Tests: Provide exact results as specified or realistic synthetic values
- Vague Requests: Politely ask for more specific queries
- Invalid Requests: Explain why the request cannot be fulfilled
SYNTHETIC FINDINGS GUIDELINES:
When generating findings not in the original case:
- Ensure consistency with established diagnosis and case details
- Use realistic reference ranges and values
- Maintain clinical plausibility
- Avoid pathognomonic findings unless specifically diagnostic
RESPONSE FORMAT:
- Direct, clinical language
- Specific measurements with reference ranges when applicable
- Clear organization of findings
- Professional medical terminology
Your role is crucial: provide complete, accurate clinical information while maintaining the challenge of the diagnostic process.
"""
),
AgentRole.JUDGE: (
"""
You are the Judge, the diagnostic accuracy evaluation specialist.
CORE RESPONSIBILITIES:
- Evaluate candidate diagnoses against ground truth using a rigorous clinical rubric
- Provide fair, consistent scoring based on clinical management implications
- Consider diagnostic substance over terminology differences
- Account for acceptable medical synonyms and equivalent formulations
EVALUATION RUBRIC (5-point Likert scale):
SCORE 5 (Perfect/Clinically Superior):
- Clinically identical to reference diagnosis
- May be more specific than reference (adding relevant detail)
- No incorrect or unrelated additions
- Treatment approach would be identical
SCORE 4 (Mostly Correct - Minor Incompleteness):
- Core disease correctly identified
- Minor qualifier or component missing/mis-specified
- Overall management largely unchanged
- Clinically appropriate diagnosis
SCORE 3 (Partially Correct - Major Error):
- Correct general disease category
- Major error in etiology, anatomic site, or critical specificity
- Would significantly alter workup or prognosis
- Partially correct but clinically concerning gaps
SCORE 2 (Largely Incorrect):
- Shares only superficial features with correct diagnosis
- Wrong fundamental disease process
- Would misdirect clinical workup
- Partially contradicts case details
SCORE 1 (Completely Incorrect):
- No meaningful overlap with correct diagnosis
- Wrong organ system or disease category
- Would likely lead to harmful care
- Completely inconsistent with clinical presentation
EVALUATION PROCESS:
1. Compare core disease entity
2. Assess etiology/causative factors
3. Evaluate anatomic specificity
4. Consider diagnostic completeness
5. Judge clinical management implications
OUTPUT FORMAT:
- Score (1-5) with clear label
- Detailed justification referencing specific rubric criteria
- Explanation of how diagnosis would affect clinical management
- Note any acceptable medical synonyms or equivalent terminology
Maintain high standards while recognizing legitimate diagnostic variability in medical practice.
"""
),
}
return prompts[role]
def _parse_json_response(self, response: str, retry_count: int = 0) -> Dict[str, Any]:
"""Safely parses a JSON string with retry logic - addresses Category 3.2"""
try:
# Handle agent response wrapper - extract actual content
if isinstance(response, dict):
# Handle swarms Agent response format
if 'role' in response and 'content' in response:
response = response['content']
elif 'content' in response:
response = response['content']
else:
# Try to extract any string value from dict
response = str(response)
elif hasattr(response, 'content'):
response = response.content
elif not isinstance(response, str):
# Convert to string if it's some other type
response = str(response)
# Extract the actual response content from the agent response
if isinstance(response, str):
# Handle markdown-formatted JSON
if "```json" in response:
# Extract JSON content between ```json and ```
start_marker = "```json"
end_marker = "```"
start_idx = response.find(start_marker)
if start_idx != -1:
start_idx += len(start_marker)
end_idx = response.find(end_marker, start_idx)
if end_idx != -1:
json_content = response[
start_idx:end_idx
].strip()
return json.loads(json_content)
# Try to find JSON-like content in the response
lines = response.split("\n")
json_lines = []
in_json = False
brace_count = 0
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith("{") and not in_json:
in_json = True
json_lines = [line] # Start fresh
brace_count = line.count("{") - line.count(
"}"
)
elif in_json:
json_lines.append(line)
brace_count += line.count("{") - line.count(
"}"
)
if (
brace_count <= 0
): # Balanced braces, end of JSON
break
if json_lines and in_json:
json_content = "\n".join(json_lines)
return json.loads(json_content)
# Try to extract JSON from text that might contain other content
import re
# Look for JSON pattern in the text - more comprehensive regex
json_pattern = r'\{(?:[^{}]|(?:\{[^{}]*\}))*\}'
matches = re.findall(json_pattern, response, re.DOTALL)
for match in matches:
try:
parsed = json.loads(match)
# Validate that it has the expected action structure
if isinstance(parsed, dict) and 'action_type' in parsed:
return parsed
except json.JSONDecodeError:
continue
# Direct parsing attempt as fallback
try:
return json.loads(response)
except json.JSONDecodeError:
# --- Fallback Sanitization ---
# Attempt to strip any leading table/frame characters (e.g., β, β, β°) that may wrap each line
try:
# Extract everything between the first '{' and last '}'
start_curly = response.index('{')
end_curly = response.rindex('}')
candidate = response[start_curly:end_curly + 1]
sanitized_lines = []
for line in candidate.splitlines():
# Remove common frame characters and leading whitespace
line = line.lstrip('β|ββ°β―βββ€ ').rstrip('β|ββ°β―βββ€ ')
sanitized_lines.append(line)
candidate_clean = '\n'.join(sanitized_lines)
return json.loads(candidate_clean)
except Exception as inner_e:
# Still failing, raise original error to trigger retry logic
try:
# --- Ultimate Fallback: Regex extraction ---
import re
atype = re.search(r'"action_type"\s*:\s*"(ask|test|diagnose)"', response, re.IGNORECASE)
content_match = re.search(r'"content"\s*:\s*"([^"]+?)"', response, re.IGNORECASE | re.DOTALL)
reasoning_match = re.search(r'"reasoning"\s*:\s*"([^"]+?)"', response, re.IGNORECASE | re.DOTALL)
if atype and content_match and reasoning_match:
return {
"action_type": atype.group(1).lower(),
"content": content_match.group(1).strip(),
"reasoning": reasoning_match.group(1).strip(),
}
except Exception:
pass
raise e
except (
json.JSONDecodeError,
IndexError,
AttributeError,
) as e:
logger.error(f"Failed to parse JSON response. Error: {e}")
logger.debug(
f"Response content: {response[:500]}..."
) # Log first 500 chars
# Return the error for potential retry instead of immediately falling back
raise e
def _parse_json_with_retry(self, consensus_agent: Agent, consensus_prompt: str, max_retries: int = 3) -> Dict[str, Any]:
"""Parse JSON with retry logic for robustness - addresses Category 3.2"""
for attempt in range(max_retries + 1):
try:
if attempt == 0:
response = consensus_agent.run(consensus_prompt)
else:
# Retry with error feedback
retry_prompt = f"""
{consensus_prompt}
**CRITICAL: RETRY REQUIRED - ATTEMPT {attempt + 1}**
Your previous response could not be parsed as JSON. You MUST respond with ONLY a valid JSON object in exactly this format:
{{
"action_type": "ask" | "test" | "diagnose",
"content": "your content here",
"reasoning": "your reasoning here"
}}
Do NOT include any other text, markdown formatting, or explanations. Only the raw JSON object.
NO SYSTEM MESSAGES, NO WRAPPER FORMAT. JUST THE JSON.
"""
response = consensus_agent.run(retry_prompt)
# Handle different response types from swarms Agent
response_text = ""
if hasattr(response, 'content'):
response_text = response.content
elif isinstance(response, dict):
# Handle swarms Agent response wrapper
if 'role' in response and 'content' in response:
response_text = response['content']
elif 'content' in response:
response_text = response['content']
else:
response_text = str(response)
elif isinstance(response, str):
response_text = response
else:
response_text = str(response)
# Log the response for debugging
logger.debug(f"Parsing attempt {attempt + 1}, response type: {type(response)}")
logger.debug(f"Response content preview: {str(response_text)[:200]}...")
return self._parse_json_response(response_text, attempt)
except Exception as e:
logger.warning(f"JSON parsing attempt {attempt + 1} failed: {e}")
if attempt == max_retries:
# Final fallback after all retries
logger.error("All JSON parsing attempts failed, using fallback")
return {
"action_type": "ask",
"content": "Could you please clarify the next best step? The previous analysis was inconclusive.",
"reasoning": f"Fallback due to JSON parsing error after {max_retries + 1} attempts.",
}
# Should never reach here, but just in case
return {
"action_type": "ask",
"content": "Please provide more information about the patient's condition.",
"reasoning": "Unexpected fallback in JSON parsing.",
}
def _estimate_cost(self, tests: Union[List[str], str]) -> int:
"""Estimates the cost of diagnostic tests."""
if isinstance(tests, str):
tests = [tests]
cost = 0
for test in tests:
test_lower = test.lower().strip()
# Enhanced cost matching with multiple strategies
cost_found = False
# Strategy 1: Exact match
if test_lower in self.test_cost_db:
cost += self.test_cost_db[test_lower]
cost_found = True
continue
# Strategy 2: Partial match (find best matching key)
best_match = None
best_match_length = 0
for cost_key in self.test_cost_db:
if cost_key in test_lower or test_lower in cost_key:
if len(cost_key) > best_match_length:
best_match = cost_key
best_match_length = len(cost_key)
if best_match:
cost += self.test_cost_db[best_match]
cost_found = True
continue
# Strategy 3: Keyword-based matching
if any(
keyword in test_lower
for keyword in ["biopsy", "tissue"]
):
cost += self.test_cost_db.get("biopsy", 800)
cost_found = True
elif any(
keyword in test_lower
for keyword in ["mri", "magnetic"]
):
cost += self.test_cost_db.get("mri", 1500)
cost_found = True
elif any(
keyword in test_lower
for keyword in ["ct", "computed tomography"]
):
cost += self.test_cost_db.get("ct scan", 1200)
cost_found = True
elif any(
keyword in test_lower
for keyword in ["xray", "x-ray", "radiograph"]
):
cost += self.test_cost_db.get("chest x-ray", 200)
cost_found = True
elif any(
keyword in test_lower
for keyword in ["blood", "serum", "plasma"]
):
cost += 100 # Basic blood test cost
cost_found = True
elif any(
keyword in test_lower
for keyword in ["culture", "sensitivity"]
):
cost += self.test_cost_db.get("culture", 150)
cost_found = True
elif any(
keyword in test_lower
for keyword in ["immunohistochemistry", "ihc"]
):
cost += self.test_cost_db.get(
"immunohistochemistry", 400
)
cost_found = True
# Strategy 4: Default cost for unknown tests
if not cost_found:
cost += self.test_cost_db["default"]
logger.debug(
f"Using default cost for unknown test: {test}"
)
return cost
def _run_panel_deliberation(self, case_state: CaseState) -> Action:
"""Orchestrates one round of structured debate among the virtual panel - addresses Category 1.1"""
logger.info(
"π©Ί Virtual medical panel deliberation commenced - analyzing patient case"
)
logger.debug(
"Panel members: Dr. Hypothesis, Dr. Test-Chooser, Dr. Challenger, Dr. Stewardship, Dr. Checklist"
)
# Initialize structured deliberation state instead of conversational chaining
deliberation_state = DeliberationState()
# Prepare concise case context for each agent (token-optimized)
remaining_budget = self.initial_budget - case_state.cumulative_cost
budget_status = (
"EXCEEDED"
if remaining_budget < 0
else f"${remaining_budget:,}"
)
# Full context - let agents self-regulate based on token guidance
base_context = f"""
=== DIAGNOSTIC CASE STATUS - ROUND {case_state.iteration} ===
INITIAL PRESENTATION:
{case_state.initial_vignette}
EVIDENCE GATHERED:
{case_state.summarize_evidence()}
CURRENT STATE:
- Tests Performed: {', '.join(case_state.tests_performed) if case_state.tests_performed else 'None'}
- Questions Asked: {len(case_state.questions_asked)}
- Cumulative Cost: ${case_state.cumulative_cost:,}
- Remaining Budget: {budget_status}
- Mode: {self.mode}
"""
# Check mode-specific constraints
if self.mode == "instant":
# For instant mode, skip deliberation and go straight to diagnosis
action_dict = {
"action_type": "diagnose",
"content": case_state.get_leading_diagnosis(),
"reasoning": (
"Instant diagnosis mode - providing immediate assessment based on initial presentation"
),
}
return Action(**action_dict)
# Check for stagnation before running deliberation
stagnation_detected = False
if len(case_state.last_actions) >= 2:
last_action = case_state.last_actions[-1]
stagnation_detected = case_state.is_stagnating(last_action)
deliberation_state.stagnation_detected = stagnation_detected
if stagnation_detected:
logger.warning("π Stagnation detected - will force different action")
# Generate dynamic situational context for all agents
deliberation_state.situational_context = self._generate_situational_context(case_state, remaining_budget)
# Run each specialist agent in parallel-like fashion with structured output
# Each agent gets the same base context plus their role-specific dynamic prompt
try:
# Dr. Hypothesis - Differential diagnosis and probability assessment
logger.info("π§ Dr. Hypothesis analyzing differential diagnosis...")
hypothesis_prompt = self._get_prompt_for_role(AgentRole.HYPOTHESIS, case_state) + "\n\n" + base_context
hypothesis_response = self._safe_agent_run(
self.agents[AgentRole.HYPOTHESIS], hypothesis_prompt, agent_role=AgentRole.HYPOTHESIS
)
# Update case state with new differential (supports both function calls and text)
self._update_differential_from_hypothesis(case_state, hypothesis_response)
# Store the analysis for deliberation state (convert to text format for other agents)
if hasattr(hypothesis_response, 'content'):
deliberation_state.hypothesis_analysis = hypothesis_response.content
else:
deliberation_state.hypothesis_analysis = str(hypothesis_response)
# Dr. Test-Chooser - Information value optimization
logger.info("π¬ Dr. Test-Chooser selecting optimal tests...")
test_chooser_prompt = self._get_prompt_for_role(AgentRole.TEST_CHOOSER, case_state) + "\n\n" + base_context
if self.mode == "question_only":
test_chooser_prompt += "\n\nIMPORTANT: This is QUESTION-ONLY mode. You may ONLY recommend patient questions, not diagnostic tests."
deliberation_state.test_chooser_analysis = self._safe_agent_run(
self.agents[AgentRole.TEST_CHOOSER], test_chooser_prompt, agent_role=AgentRole.TEST_CHOOSER
)
# Dr. Challenger - Bias identification and alternative hypotheses
logger.info("π€ Dr. Challenger challenging assumptions...")
challenger_prompt = self._get_prompt_for_role(AgentRole.CHALLENGER, case_state) + "\n\n" + base_context
deliberation_state.challenger_analysis = self._safe_agent_run(
self.agents[AgentRole.CHALLENGER], challenger_prompt, agent_role=AgentRole.CHALLENGER
)
# Dr. Stewardship - Cost-effectiveness analysis
logger.info("π° Dr. Stewardship evaluating cost-effectiveness...")
stewardship_prompt = self._get_prompt_for_role(AgentRole.STEWARDSHIP, case_state) + "\n\n" + base_context
if self.enable_budget_tracking:
stewardship_prompt += f"\n\nBUDGET TRACKING ENABLED - Current cost: ${case_state.cumulative_cost}, Remaining: ${remaining_budget}"
deliberation_state.stewardship_analysis = self._safe_agent_run(
self.agents[AgentRole.STEWARDSHIP], stewardship_prompt, agent_role=AgentRole.STEWARDSHIP
)
# Dr. Checklist - Quality assurance
logger.info("β
Dr. Checklist performing quality control...")
checklist_prompt = self._get_prompt_for_role(AgentRole.CHECKLIST, case_state) + "\n\n" + base_context
deliberation_state.checklist_analysis = self._safe_agent_run(
self.agents[AgentRole.CHECKLIST], checklist_prompt, agent_role=AgentRole.CHECKLIST
)
# Consensus Coordinator - Final decision synthesis using structured state
logger.info("π€ Consensus Coordinator synthesizing panel decision...")
# Generate the structured consensus prompt
consensus_prompt = deliberation_state.to_consensus_prompt()
# Add mode-specific constraints to consensus
if self.mode == "budgeted" and remaining_budget <= 0:
consensus_prompt += "\n\nBUDGET CONSTRAINT: Budget exceeded - must either ask questions or provide final diagnosis."
# Use function calling with retry logic for robust structured output
action_dict = self._get_consensus_with_retry(consensus_prompt)
# Validate action based on mode constraints
action = Action(**action_dict)
# Apply mode-specific validation and corrections
action = self._validate_and_correct_action(action, case_state, remaining_budget)
return action
except Exception as e:
logger.error(f"Error during panel deliberation: {e}")
# Fallback action
return Action(
action_type="ask",
content="Could you please provide more information about the patient's current condition?",
reasoning=f"Fallback due to panel deliberation error: {str(e)}",
)
def _generate_situational_context(self, case_state: CaseState, remaining_budget: int) -> str:
"""Generate dynamic situational context based on current case state - addresses Category 4.2"""
context_parts = []
# Budget-related context
if remaining_budget < 1000:
context_parts.append(f"URGENT: Remaining budget critically low (${remaining_budget}). Focus on cost-effective actions.")
elif remaining_budget < 2000:
context_parts.append(f"WARNING: Budget running low (${remaining_budget}). Prioritize high-value tests.")
# Diagnostic confidence context
max_confidence = case_state.get_max_confidence()
if max_confidence > 0.85:
context_parts.append(f"FINAL STAGES: High confidence diagnosis available ({max_confidence:.0%}). Consider definitive action.")
elif max_confidence > 0.70:
context_parts.append(f"CONVERGING: Moderate confidence in leading diagnosis ({max_confidence:.0%}). Focus on confirmation.")
# Iteration context
if case_state.iteration > 7:
context_parts.append(f"EXTENDED CASE: {case_state.iteration} rounds completed. Move toward decisive action.")
elif case_state.iteration > 5:
context_parts.append(f"PROLONGED: {case_state.iteration} rounds. Avoid further exploratory steps unless critical.")
# Test/cost context
if len(case_state.tests_performed) > 5:
context_parts.append("EXTENSIVE TESTING: Many tests completed. Focus on synthesis rather than additional testing.")
return " | ".join(context_parts) if context_parts else ""
def _update_differential_from_hypothesis(self, case_state: CaseState, hypothesis_response):
"""Extract and update differential diagnosis from Dr. Hypothesis analysis - now supports both function calls and text"""
try:
# Try to extract structured data from function call first
if hasattr(hypothesis_response, '__dict__') or isinstance(hypothesis_response, dict):
structured_data = self._extract_function_call_output(hypothesis_response)
# Validate the structured data using the HypothesisArguments schema
try:
_ = HypothesisArguments(**structured_data)
except ValidationError as e:
logger.warning(f"HypothesisArguments validation failed: {e}")
# Check if we got structured differential data
if "differential_diagnoses" in structured_data:
# Update case state with structured data
new_differential = {}
for dx in structured_data["differential_diagnoses"]:
new_differential[dx["diagnosis"]] = dx["probability"]
case_state.update_differential(new_differential)
# Update the main differential for backward compatibility
summary = structured_data.get("summary", "Differential diagnosis updated")
dx_text = f"{summary}\n\nTop Diagnoses:\n"
for dx in structured_data["differential_diagnoses"]:
dx_text += f"- {dx['diagnosis']}: {dx['probability']:.0%} - {dx['rationale']}\n"
if "key_evidence" in structured_data:
dx_text += f"\nKey Evidence: {structured_data['key_evidence']}"
if "contradictory_evidence" in structured_data:
dx_text += f"\nContradictory Evidence: {structured_data['contradictory_evidence']}"
self.differential_diagnosis = dx_text
logger.debug(f"Updated differential from function call: {new_differential}")
return
# Fallback to text-based extraction
hypothesis_text = str(hypothesis_response)
if hasattr(hypothesis_response, 'content'):
hypothesis_text = hypothesis_response.content
# Simple extraction - look for percentage patterns in the text
import re
# Update the main differential diagnosis for backward compatibility
self.differential_diagnosis = hypothesis_text
# Try to extract structured probabilities
# Look for patterns like "Diagnosis: 85%" or "Disease (70%)"
percentage_pattern = r'([A-Za-z][^:(\n]*?)[\s:]*[\(]?(\d{1,3})%[\)]?'
matches = re.findall(percentage_pattern, hypothesis_text)
new_differential = {}
for match in matches:
diagnosis = match[0].strip().rstrip(':-()').strip()
probability = float(match[1]) / 100.0
if 0 <= probability <= 1.0 and len(diagnosis) > 3: # Basic validation
new_differential[diagnosis] = probability
if new_differential:
case_state.update_differential(new_differential)
logger.debug(f"Updated differential from text parsing: {new_differential}")
except Exception as e:
logger.debug(f"Could not extract structured differential: {e}")
# Still update the text version for display
hypothesis_text = str(hypothesis_response)
if hasattr(hypothesis_response, 'content'):
hypothesis_text = hypothesis_response.content
self.differential_diagnosis = hypothesis_text
def _validate_and_correct_action(self, action: Action, case_state: CaseState, remaining_budget: int) -> Action:
"""Validate and correct actions based on mode constraints and context"""
# Mode-specific validations
if self.mode == "question_only" and action.action_type == "test":
logger.warning("Test ordering attempted in question-only mode, converting to ask action")
action.action_type = "ask"
action.content = "Can you provide more details about the patient's symptoms and history?"
action.reasoning = "Mode constraint: question-only mode active"
if self.mode == "budgeted" and action.action_type == "test" and remaining_budget <= 0:
logger.warning("Test ordering attempted with insufficient budget, converting to diagnose action")
action.action_type = "diagnose"
action.content = case_state.get_leading_diagnosis()
action.reasoning = "Budget constraint: insufficient funds for additional testing"
# Stagnation handling - ensure we have a valid diagnosis
if case_state.is_stagnating(action):
logger.warning("Stagnation detected, forcing diagnostic decision")
action.action_type = "diagnose"
leading_diagnosis = case_state.get_leading_diagnosis()
# Ensure the diagnosis is meaningful, not corrupted
if leading_diagnosis == "No diagnosis formulated" or len(leading_diagnosis) < 10 or any(char in leading_diagnosis for char in ['x10^9', 'β', '40β']):
# Use a fallback diagnosis based on the case context
action.content = "Unable to establish definitive diagnosis - further evaluation needed"
else:
action.content = leading_diagnosis
action.reasoning = "Forced diagnosis due to detected stagnation in diagnostic process"
# High confidence threshold
if action.action_type != "diagnose" and case_state.get_max_confidence() > 0.90:
logger.info("Very high confidence reached, recommending diagnosis")
action.action_type = "diagnose"
action.content = case_state.get_leading_diagnosis()
action.reasoning = "High confidence threshold reached - proceeding to final diagnosis"
return action
def _interact_with_gatekeeper(
self, action: Action, full_case_details: str
) -> str:
"""Sends the panel's action to the Gatekeeper and returns its response."""
gatekeeper = self.agents[AgentRole.GATEKEEPER]
if action.action_type == "ask":
request = f"Question: {action.content}"
elif action.action_type == "test":
request = f"Tests ordered: {', '.join(action.content)}"
else:
return "No interaction needed for 'diagnose' action."
# The Gatekeeper needs the full case to act as an oracle
prompt = f"""
Full Case Details (for your reference only):
---
{full_case_details}
---
Request from Diagnostic Agent:
{request}
"""
response = self._safe_agent_run(gatekeeper, prompt, agent_role=AgentRole.GATEKEEPER)
return response
def _judge_diagnosis(
self, candidate_diagnosis: str, ground_truth: str
) -> Dict[str, Any]:
"""Uses the Judge agent to evaluate the final diagnosis."""
judge = self.agents[AgentRole.JUDGE]
prompt = f"""
Please evaluate the following diagnosis.
Ground Truth: "{ground_truth}"
Candidate Diagnosis: "{candidate_diagnosis}"
You must provide your evaluation in exactly this format:
Score: [number from 1-5]
Justification: [detailed reasoning for the score]
"""
response = self._safe_agent_run(judge, prompt, agent_role=AgentRole.JUDGE)
# Handle different response types from swarms Agent
response_text = ""
if hasattr(response, 'content'):
response_text = response.content
elif isinstance(response, dict):
if 'role' in response and 'content' in response:
response_text = response['content']
elif 'content' in response:
response_text = response['content']
else:
response_text = str(response)
elif isinstance(response, str):
response_text = response
else:
response_text = str(response)
# Enhanced parsing for demonstration; a more robust solution would use structured output.
try:
# Look for score patterns
import re
# Try multiple score patterns
score_patterns = [
r"Score:\s*(\d+(?:\.\d+)?)",
r"Score\s*(\d+(?:\.\d+)?)",
r"(\d+(?:\.\d+)?)/5",
r"Score.*?(\d+(?:\.\d+)?)",
]
score = 0.0
for pattern in score_patterns:
match = re.search(pattern, response_text, re.IGNORECASE)
if match:
score = float(match.group(1))
break
# Extract reasoning
reasoning_patterns = [
r"Justification:\s*(.+?)(?:\n\n|\Z)",
r"Reasoning:\s*(.+?)(?:\n\n|\Z)",
r"Explanation:\s*(.+?)(?:\n\n|\Z)",
]
reasoning = "Could not parse judge's reasoning."
for pattern in reasoning_patterns:
match = re.search(pattern, response_text, re.IGNORECASE | re.DOTALL)
if match:
reasoning = match.group(1).strip()
break
# If no specific reasoning found, use the whole response after score
if reasoning == "Could not parse judge's reasoning." and score > 0:
# Try to extract everything after the score
score_match = re.search(r"Score:?\s*\d+(?:\.\d+)?", response_text, re.IGNORECASE)
if score_match:
reasoning = response_text[score_match.end():].strip()
# Clean up common prefixes
reasoning = re.sub(r"^(Justification|Reasoning|Explanation):\s*", "", reasoning, flags=re.IGNORECASE)
# Final fallback - use the whole response if we have a score
if reasoning == "Could not parse judge's reasoning." and score > 0:
reasoning = response_text
except (IndexError, ValueError, AttributeError) as e:
logger.error(f"Error parsing judge response: {e}")
score = 0.0
reasoning = f"Could not parse judge's response: {str(e)}"
logger.info(f"Judge evaluation: Score={score}, Reasoning preview: {reasoning[:100]}...")
return {"score": score, "reasoning": reasoning}
def run(
self,
initial_case_info: str,
full_case_details: str,
ground_truth_diagnosis: str,
) -> DiagnosisResult:
"""
Executes the full sequential diagnostic process with structured state management.
Args:
initial_case_info (str): The initial abstract of the case.
full_case_details (str): The complete case file for the Gatekeeper.
ground_truth_diagnosis (str): The correct final diagnosis for evaluation.
Returns:
DiagnosisResult: An object containing the final diagnosis, evaluation, cost, and history.
"""
start_time = time.time()
# Initialize structured case state
case_state = CaseState(initial_vignette=initial_case_info)
case_state.cumulative_cost = self.physician_visit_cost # Add initial visit cost
self.cumulative_cost = case_state.cumulative_cost
# Store for potential use by other methods
self.case_state = case_state
# Add to conversation for history tracking
self.conversation.add(
"Gatekeeper",
f"Initial Case Information: {initial_case_info}",
)
case_state.add_evidence(f"Initial presentation: {initial_case_info}")
logger.info(
f"Initial physician visit cost: ${self.physician_visit_cost}"
)
final_diagnosis = None
iteration_count = 0
for i in range(self.max_iterations):
iteration_count = i + 1
case_state.iteration = iteration_count
logger.info(
f"--- Starting Diagnostic Loop {iteration_count}/{self.max_iterations} ---"
)
logger.info(
f"Current cost: ${case_state.cumulative_cost:,} | Remaining budget: ${self.initial_budget - case_state.cumulative_cost:,}"
)
try:
# Panel deliberates to decide on the next action using structured state
action = self._run_panel_deliberation(case_state)
logger.info(
f"βοΈ Panel decision: {action.action_type.upper()} -> {action.content}"
)
logger.info(
f"π Medical reasoning: {action.reasoning}"
)
# Add action to case state for stagnation detection
case_state.add_action(action)
if action.action_type == "diagnose":
final_diagnosis = action.content
logger.info(
f"Final diagnosis proposed: {final_diagnosis}"
)
break
# Handle mode-specific constraints (most are now handled in validation)
if (
self.mode == "question_only"
and action.action_type == "test"
):
logger.warning(
"Test ordering blocked in question-only mode"
)
continue
if (
self.mode == "budgeted"
and action.action_type == "test"
):
# Check if we can afford the tests
estimated_test_cost = self._estimate_cost(
action.content
)
if (
case_state.cumulative_cost + estimated_test_cost
> self.initial_budget
):
logger.warning(
f"Test cost ${estimated_test_cost} would exceed budget. Skipping tests."
)
continue
# Interact with the Gatekeeper
response = self._interact_with_gatekeeper(
action, full_case_details
)
self.conversation.add("Gatekeeper", response)
case_state.add_evidence(response)
# Update costs and state based on action type
if action.action_type == "test":
test_cost = self._estimate_cost(action.content)
case_state.cumulative_cost += test_cost
case_state.add_test(str(action.content))
self.cumulative_cost = case_state.cumulative_cost # Keep backward compatibility
logger.info(f"Tests ordered: {action.content}")
logger.info(
f"Test cost: ${test_cost:,} | Cumulative cost: ${case_state.cumulative_cost:,}"
)
elif action.action_type == "ask":
case_state.add_question(str(action.content))
# Questions are part of the same visit until tests are ordered
logger.info(f"Questions asked: {action.content}")
logger.info(
"No additional cost for questions in same visit"
)
# Check budget constraints for budgeted mode
if (
self.mode == "budgeted"
and case_state.cumulative_cost >= self.initial_budget
):
logger.warning(
"Budget limit reached. Forcing final diagnosis."
)
# Use current leading diagnosis
final_diagnosis = case_state.get_leading_diagnosis()
break
except Exception as e:
logger.error(
f"Error in diagnostic loop {iteration_count}: {e}"
)
# Continue to next iteration or break if critical error
continue
else:
# Max iterations reached without diagnosis
final_diagnosis = case_state.get_leading_diagnosis()
if final_diagnosis == "No diagnosis formulated":
final_diagnosis = "Diagnosis not reached within maximum iterations."
logger.warning(
f"Max iterations ({self.max_iterations}) reached. Using best available diagnosis."
)
# Ensure we have a final diagnosis
if not final_diagnosis or final_diagnosis.strip() == "":
final_diagnosis = (
"Unable to determine diagnosis within constraints."
)
# Calculate total time
total_time = time.time() - start_time
logger.info(
f"Diagnostic session completed in {total_time:.2f} seconds"
)
# Judge the final diagnosis
logger.info("Evaluating final diagnosis...")
try:
judgement = self._judge_diagnosis(
final_diagnosis, ground_truth_diagnosis
)
except Exception as e:
logger.error(f"Error in diagnosis evaluation: {e}")
judgement = {
"score": 0.0,
"reasoning": f"Evaluation error: {str(e)}",
}
# Create comprehensive result
result = DiagnosisResult(
final_diagnosis=final_diagnosis,
ground_truth=ground_truth_diagnosis,
accuracy_score=judgement["score"],
accuracy_reasoning=judgement["reasoning"],
total_cost=case_state.cumulative_cost,
iterations=iteration_count,
conversation_history=self.conversation.get_str(),
)
logger.info("Diagnostic process completed:")
logger.info(f" Final diagnosis: {final_diagnosis}")
logger.info(f" Ground truth: {ground_truth_diagnosis}")
logger.info(f" Accuracy score: {judgement['score']}/5.0")
logger.info(f" Total cost: ${case_state.cumulative_cost:,}")
logger.info(f" Iterations: {iteration_count}")
return result
def run_ensemble(
self,
initial_case_info: str,
full_case_details: str,
ground_truth_diagnosis: str,
num_runs: int = 3,
) -> DiagnosisResult:
"""
Runs multiple independent diagnostic sessions and aggregates the results.
Args:
initial_case_info (str): The initial abstract of the case.
full_case_details (str): The complete case file for the Gatekeeper.
ground_truth_diagnosis (str): The correct final diagnosis for evaluation.
num_runs (int): Number of independent runs to perform.
Returns:
DiagnosisResult: Aggregated result from ensemble runs.
"""
logger.info(
f"Starting ensemble run with {num_runs} independent sessions"
)
ensemble_results = []
total_cost = 0
for run_id in range(num_runs):
logger.info(
f"=== Ensemble Run {run_id + 1}/{num_runs} ==="
)
# Create a fresh orchestrator instance for each run
run_orchestrator = MaiDxOrchestrator(
model_name=self.model_name,
max_iterations=self.max_iterations,
initial_budget=self.initial_budget,
mode="no_budget", # Use no_budget for ensemble runs
physician_visit_cost=self.physician_visit_cost,
enable_budget_tracking=False,
)
# Run the diagnostic session
result = run_orchestrator.run(
initial_case_info,
full_case_details,
ground_truth_diagnosis,
)
ensemble_results.append(result)
total_cost += result.total_cost
logger.info(
f"Run {run_id + 1} completed: {result.final_diagnosis} (Score: {result.accuracy_score})"
)
# Aggregate results using consensus
final_diagnosis = self._aggregate_ensemble_diagnoses(
[r.final_diagnosis for r in ensemble_results]
)
# Judge the aggregated diagnosis
judgement = self._judge_diagnosis(
final_diagnosis, ground_truth_diagnosis
)
# Calculate average metrics
avg_iterations = sum(
r.iterations for r in ensemble_results
) / len(ensemble_results)
# Combine conversation histories
combined_history = "\n\n=== ENSEMBLE RESULTS ===\n"
for i, result in enumerate(ensemble_results):
combined_history += f"\n--- Run {i+1} ---\n"
combined_history += (
f"Diagnosis: {result.final_diagnosis}\n"
)
combined_history += f"Score: {result.accuracy_score}\n"
combined_history += f"Cost: ${result.total_cost:,}\n"
combined_history += f"Iterations: {result.iterations}\n"
combined_history += "\n--- Aggregated Result ---\n"
combined_history += f"Final Diagnosis: {final_diagnosis}\n"
combined_history += f"Reasoning: {judgement['reasoning']}\n"
ensemble_result = DiagnosisResult(
final_diagnosis=final_diagnosis,
ground_truth=ground_truth_diagnosis,
accuracy_score=judgement["score"],
accuracy_reasoning=judgement["reasoning"],
total_cost=total_cost, # Sum of all runs
iterations=int(avg_iterations),
conversation_history=combined_history,
)
logger.info(
f"Ensemble completed: {final_diagnosis} (Score: {judgement['score']})"
)
return ensemble_result
def _aggregate_ensemble_diagnoses(
self, diagnoses: List[str]
) -> str:
"""Aggregates multiple diagnoses from ensemble runs."""
# Simple majority voting or use the most confident diagnosis
if not diagnoses:
return "No diagnosis available"
# Remove any empty or invalid diagnoses
valid_diagnoses = [
d
for d in diagnoses
if d and d.strip() and "not reached" not in d.lower()
]
if not valid_diagnoses:
return diagnoses[0] if diagnoses else "No valid diagnosis"
# If all diagnoses are the same, return that
if len(set(valid_diagnoses)) == 1:
return valid_diagnoses[0]
# Use an aggregator agent to select the best diagnosis
try:
aggregator_prompt = f"""
You are a medical consensus aggregator. Given multiple diagnostic assessments from independent medical panels,
select the most accurate and complete diagnosis.
Diagnoses to consider:
{chr(10).join(f"{i+1}. {d}" for i, d in enumerate(valid_diagnoses))}
Provide the single best diagnosis that represents the medical consensus.
Consider clinical accuracy, specificity, and completeness.
"""
aggregator = Agent(
agent_name="Ensemble Aggregator",
system_prompt=aggregator_prompt,
model_name=self.model_name,
max_loops=1,
print_on=True, # Enable printing for aggregator agent
)
agg_resp = self._safe_agent_run(aggregator, aggregator_prompt)
if hasattr(agg_resp, "content"):
return agg_resp.content.strip()
return str(agg_resp).strip()
except Exception as e:
logger.error(f"Error in ensemble aggregation: {e}")
# Fallback to most common diagnosis
from collections import Counter
return Counter(valid_diagnoses).most_common(1)[0][0]
@classmethod
def create_variant(
cls, variant: str, **kwargs
) -> "MaiDxOrchestrator":
"""
Factory method to create different MAI-DxO variants as described in the paper.
Args:
variant (str): One of 'instant', 'question_only', 'budgeted', 'no_budget', 'ensemble'
**kwargs: Additional parameters for the orchestrator
Returns:
MaiDxOrchestrator: Configured orchestrator instance
"""
variant_configs = {
"instant": {
"mode": "instant",
"max_iterations": 1,
"enable_budget_tracking": False,
},
"question_only": {
"mode": "question_only",
"max_iterations": 10,
"enable_budget_tracking": False,
},
"budgeted": {
"mode": "budgeted",
"max_iterations": 10,
"enable_budget_tracking": True,
"initial_budget": kwargs.get("budget", 5000), # Fixed: map budget to initial_budget
},
"no_budget": {
"mode": "no_budget",
"max_iterations": 10,
"enable_budget_tracking": False,
},
"ensemble": {
"mode": "no_budget",
"max_iterations": 10,
"enable_budget_tracking": False,
},
}
if variant not in variant_configs:
raise ValueError(
f"Unknown variant: {variant}. Choose from: {list(variant_configs.keys())}"
)
config = variant_configs[variant]
config.update(kwargs) # Allow overrides
# Remove 'budget' parameter if present, as it's mapped to 'initial_budget'
config.pop('budget', None)
return cls(**config)
# ------------------------------------------------------------------
# Helper utilities β throttling & robust JSON parsing
# ------------------------------------------------------------------
def _safe_agent_run(
self,
agent: "Agent", # type: ignore β forward reference
prompt: str,
retries: int = 3,
agent_role: AgentRole = None,
) -> Any:
"""Safely call `agent.run` while respecting OpenAI rate-limits.
Features:
1. Estimates token usage and provides guidance to agents for self-regulation
2. Applies progressive delays to respect rate limits
3. Lets agents dynamically adjust their response strategy based on token constraints
"""
# Get agent role for token calculations
if agent_role is None:
agent_role = AgentRole.CONSENSUS # Default fallback
# Estimate total tokens in the request
estimated_input_tokens = self._estimate_tokens(prompt)
max_output_tokens = self._get_agent_max_tokens(agent_role)
total_estimated_tokens = estimated_input_tokens + max_output_tokens
# Add dynamic token guidance to the prompt instead of truncating
token_guidance = self._generate_token_guidance(
estimated_input_tokens, max_output_tokens, total_estimated_tokens, agent_role
)
# Prepend token guidance to prompt
enhanced_prompt = f"{token_guidance}\n\n{prompt}"
logger.debug(f"Agent {agent_role.value}: Input={estimated_input_tokens}, Output={max_output_tokens}, Total={total_estimated_tokens}")
# Increased base delay for better rate limit compliance
base_delay = max(self.request_delay, 5.0) # Minimum 5 seconds between requests
for attempt in range(retries + 1):
# Progressive delay: 5s, 15s, 45s, 135s
current_delay = base_delay * (3 ** attempt) if attempt > 0 else base_delay
logger.info(f"Request attempt {attempt + 1}/{retries + 1}, waiting {current_delay:.1f}s...")
time.sleep(current_delay)
try:
return agent.run(enhanced_prompt)
except Exception as e:
err_msg = str(e).lower()
if "rate_limit" in err_msg or "ratelimiterror" in err_msg or "429" in str(e):
logger.warning(
f"Rate-limit encountered (attempt {attempt + 1}/{retries + 1}). "
f"Will retry after {base_delay * (3 ** (attempt + 1)):.1f}s..."
)
continue # Next retry applies longer delay
# For non-rate-limit errors, propagate immediately
raise
# All retries exhausted
raise RuntimeError("Maximum retries exceeded for agent.run β aborting call")
def _robust_parse_action(self, raw_response: str) -> Dict[str, Any]:
"""Extract a JSON *action* object from `raw_response`.
The function tries multiple strategies and finally returns a default
*ask* action if no valid JSON can be located.
"""
import json, re
# Strip common markdown fences
if raw_response.strip().startswith("```"):
segments = raw_response.split("```")
for seg in segments:
seg = seg.strip()
if seg.startswith("{") and seg.endswith("}"):
raw_response = seg
break
# 1) Fast path β direct JSON decode
try:
data = json.loads(raw_response)
if isinstance(data, dict) and "action_type" in data:
return data
except Exception:
pass
# 2) Regex search for the first balanced curly block
match = re.search(r"\{[\s\S]*?\}", raw_response)
if match:
candidate = match.group(0)
# Remove leading drawing characters (e.g., table borders)
candidate = "\n".join(line.lstrip("β| ").rstrip("β| ") for line in candidate.splitlines())
try:
data = json.loads(candidate)
if isinstance(data, dict) and "action_type" in data:
return data
except Exception:
pass
logger.error("Failed to parse a valid action JSON. Falling back to default ask action")
return {
"action_type": "ask",
"content": "Could you please clarify the next best step? The previous analysis was inconclusive.",
"reasoning": "Fallback generated due to JSON parsing failure.",
}
def _extract_function_call_output(self, agent_response) -> Dict[str, Any]:
"""Extract structured output from agent function call response.
This method handles the swarms Agent response format when using function calling.
The response should contain tool calls with the structured data.
"""
try:
# Handle different response formats from swarms Agent
if isinstance(agent_response, dict):
# Check for tool calls in the response
if "tool_calls" in agent_response and agent_response["tool_calls"]:
tool_call = agent_response["tool_calls"][0] # Get first tool call
if "function" in tool_call and "arguments" in tool_call["function"]:
arguments = tool_call["function"]["arguments"]
if isinstance(arguments, str):
# Parse JSON string arguments
import json
arguments = json.loads(arguments)
return arguments
# Check for direct arguments in response
if "arguments" in agent_response:
arguments = agent_response["arguments"]
if isinstance(arguments, str):
import json
arguments = json.loads(arguments)
return arguments
# Check if response itself has the expected structure
if all(key in agent_response for key in ["action_type", "content", "reasoning"]):
return {
"action_type": agent_response["action_type"],
"content": agent_response["content"],
"reasoning": agent_response["reasoning"]
}
# Handle Agent object response
elif hasattr(agent_response, "__dict__"):
# Check for tool_calls attribute
if hasattr(agent_response, "tool_calls") and agent_response.tool_calls:
tool_call = agent_response.tool_calls[0]
if hasattr(tool_call, "function") and hasattr(tool_call.function, "arguments"):
arguments = tool_call.function.arguments
if isinstance(arguments, str):
import json
arguments = json.loads(arguments)
return arguments
# Check for direct function call response
if hasattr(agent_response, "function_call"):
function_call = agent_response.function_call
if hasattr(function_call, "arguments"):
arguments = function_call.arguments
if isinstance(arguments, str):
import json
arguments = json.loads(arguments)
return arguments
# Try to extract from response content
if hasattr(agent_response, "content"):
content = agent_response.content
if isinstance(content, dict) and all(key in content for key in ["action_type", "content", "reasoning"]):
return content
# Handle string response (fallback to regex parsing)
elif isinstance(agent_response, str):
# Try to parse as JSON first
try:
import json
parsed = json.loads(agent_response)
if isinstance(parsed, dict) and all(key in parsed for key in ["action_type", "content", "reasoning"]):
return parsed
except:
pass
# Fallback to regex extraction
import re
action_type_match = re.search(r'"action_type":\s*"(ask|test|diagnose)"', agent_response, re.IGNORECASE)
content_match = re.search(r'"content":\s*"([^"]+)"', agent_response, re.IGNORECASE | re.DOTALL)
reasoning_match = re.search(r'"reasoning":\s*"([^"]+)"', agent_response, re.IGNORECASE | re.DOTALL)
if action_type_match and content_match and reasoning_match:
return {
"action_type": action_type_match.group(1).lower(),
"content": content_match.group(1).strip(),
"reasoning": reasoning_match.group(1).strip()
}
logger.warning(f"Could not extract function call output from response type: {type(agent_response)}")
logger.debug(f"Response content: {str(agent_response)[:500]}...")
except Exception as e:
logger.error(f"Error extracting function call output: {e}")
logger.debug(f"Response: {str(agent_response)[:500]}...")
# Final fallback
return {
"action_type": "ask",
"content": "Could you please provide more information to help guide the next diagnostic step?",
"reasoning": "Fallback action due to function call parsing error."
}
def _get_consensus_with_retry(self, consensus_prompt: str, max_retries: int = 2) -> Dict[str, Any]:
"""Get consensus decision with function call retry logic."""
for attempt in range(max_retries + 1):
try:
if attempt == 0:
# First attempt - use original prompt
response = self._safe_agent_run(
self.agents[AgentRole.CONSENSUS], consensus_prompt, agent_role=AgentRole.CONSENSUS
)
else:
# Retry with explicit function call instruction
retry_prompt = f"""
{consensus_prompt}
**CRITICAL: RETRY ATTEMPT {attempt}**
Your previous response failed to use the required `make_consensus_decision` function.
You MUST call the make_consensus_decision function with the appropriate parameters:
- action_type: "ask", "test", or "diagnose"
- content: specific question, test name, or diagnosis
- reasoning: your detailed reasoning
Please try again and ensure you call the function correctly.
"""
response = self._safe_agent_run(
self.agents[AgentRole.CONSENSUS], retry_prompt, agent_role=AgentRole.CONSENSUS
)
logger.debug(f"Consensus attempt {attempt + 1}, response type: {type(response)}")
# Try to extract function call output
action_dict = self._extract_function_call_output(response)
# Validate and enforce schema using ConsensusArguments for type safety
try:
validated_args = ConsensusArguments(**action_dict)
action_dict = validated_args.dict()
except ValidationError as e:
logger.warning(f"ConsensusArguments validation failed: {e}")
# Check if we got a valid response (not a fallback)
if not action_dict.get("reasoning", "").startswith("Fallback action due to function call parsing error"):
logger.debug(f"Consensus function call successful on attempt {attempt + 1}")
return action_dict
logger.warning(f"Function call failed on attempt {attempt + 1}, will retry")
except Exception as e:
logger.error(f"Error in consensus attempt {attempt + 1}: {e}")
# Final fallback to JSON parsing if all function call attempts failed
logger.warning("All function call attempts failed, falling back to JSON parsing")
try:
# Use the last response and try JSON parsing
consensus_text = (
response.content if hasattr(response, "content") else str(response)
)
return self._robust_parse_action(consensus_text)
except Exception as e:
logger.error(f"Both function calling and JSON parsing failed: {e}")
return {
"action_type": "ask",
"content": "Could you please provide more information to guide the diagnostic process?",
"reasoning": f"Final fallback after {max_retries + 1} function call attempts and JSON parsing failure."
}
def run_mai_dxo_demo(
case_info: str = None,
case_details: str = None,
ground_truth: str = None,
) -> Dict[str, DiagnosisResult]:
"""
Convenience function to run a quick demonstration of MAI-DxO variants.
Args:
case_info (str): Initial case information. Uses default if None.
case_details (str): Full case details. Uses default if None.
ground_truth (str): Ground truth diagnosis. Uses default if None.
Returns:
Dict[str, DiagnosisResult]: Results from different MAI-DxO variants
"""
# Use default case if not provided
if not case_info:
case_info = (
"A 29-year-old woman was admitted to the hospital because of sore throat and peritonsillar swelling "
"and bleeding. Symptoms did not abate with antimicrobial therapy."
)
if not case_details:
case_details = """
Patient: 29-year-old female.
History: Onset of sore throat 7 weeks prior to admission. Worsening right-sided pain and swelling.
No fevers, headaches, or gastrointestinal symptoms. Past medical history is unremarkable.
Physical Exam: Right peritonsillar mass, displacing the uvula. No other significant findings.
Initial Labs: FBC, clotting studies normal.
MRI Neck: Showed a large, enhancing mass in the right peritonsillar space.
Biopsy (H&E): Infiltrative round-cell neoplasm with high nuclear-to-cytoplasmic ratio and frequent mitotic figures.
Biopsy (Immunohistochemistry): Desmin and MyoD1 diffusely positive. Myogenin multifocally positive.
Biopsy (FISH): No FOXO1 (13q14) rearrangements detected.
Final Diagnosis from Pathology: Embryonal rhabdomyosarcoma of the pharynx.
"""
if not ground_truth:
ground_truth = "Embryonal rhabdomyosarcoma of the pharynx"
results = {}
# Test key variants
variants = ["no_budget", "budgeted", "question_only"]
for variant in variants:
try:
logger.info(f"Running MAI-DxO variant: {variant}")
if variant == "budgeted":
orchestrator = MaiDxOrchestrator.create_variant(
variant,
budget=3000,
model_name="gemini/gemini-2.5-flash", # Fixed: Use valid model name
max_iterations=3,
)
else:
orchestrator = MaiDxOrchestrator.create_variant(
variant,
model_name="gemini/gemini-2.5-flash", # Fixed: Use valid model name
max_iterations=3,
)
result = orchestrator.run(
case_info, case_details, ground_truth
)
results[variant] = result
except Exception as e:
logger.error(f"Error running variant {variant}: {e}")
results[variant] = None
return results
# if __name__ == "__main__":
# # Example case inspired by the paper's Figure 1
# initial_info = (
# "A 29-year-old woman was admitted to the hospital because of sore throat and peritonsillar swelling "
# "and bleeding. Symptoms did not abate with antimicrobial therapy."
# )
# full_case = """
# Patient: 29-year-old female.
# History: Onset of sore throat 7 weeks prior to admission. Worsening right-sided pain and swelling.
# No fevers, headaches, or gastrointestinal symptoms. Past medical history is unremarkable. No history of smoking or significant alcohol use.
# Physical Exam: Right peritonsillar mass, displacing the uvula. No other significant findings.
# Initial Labs: FBC, clotting studies normal.
# MRI Neck: Showed a large, enhancing mass in the right peritonsillar space.
# Biopsy (H&E): Infiltrative round-cell neoplasm with high nuclear-to-cytoplasmic ratio and frequent mitotic figures.
# Biopsy (Immunohistochemistry for Carcinoma): CD31, D2-40, CD34, ERG, GLUT-1, pan-cytokeratin, CD45, CD20, CD3 all negative. Ki-67: 60% nuclear positivity.
# Biopsy (Immunohistochemistry for Rhabdomyosarcoma): Desmin and MyoD1 diffusely positive. Myogenin multifocally positive.
# Biopsy (FISH): No FOXO1 (13q14) rearrangements detected.
# Final Diagnosis from Pathology: Embryonal rhabdomyosarcoma of the pharynx.
# """
# ground_truth = "Embryonal rhabdomyosarcoma of the pharynx"
# # --- Demonstrate Different MAI-DxO Variants ---
# try:
# print("\n" + "=" * 80)
# print(
# " MAI DIAGNOSTIC ORCHESTRATOR (MAI-DxO) - SEQUENTIAL DIAGNOSIS BENCHMARK"
# )
# print(
# " Implementation based on the NEJM Research Paper"
# )
# print("=" * 80)
# # Test different variants as described in the paper
# variants_to_test = [
# (
# "no_budget",
# "Standard MAI-DxO with no budget constraints",
# ),
# ("budgeted", "Budget-constrained MAI-DxO ($3000 limit)"),
# (
# "question_only",
# "Question-only variant (no diagnostic tests)",
# ),
# ]
# results = {}
# for variant_name, description in variants_to_test:
# print(f"\n{'='*60}")
# print(f"Testing Variant: {variant_name.upper()}")
# print(f"Description: {description}")
# print("=" * 60)
# # Create the variant
# if variant_name == "budgeted":
# orchestrator = MaiDxOrchestrator.create_variant(
# variant_name,
# budget=3000,
# model_name="gpt-4.1", # Fixed: Use valid model name
# max_iterations=3,
# )
# else:
# orchestrator = MaiDxOrchestrator.create_variant(
# variant_name,
# model_name="gpt-4.1", # Fixed: Use valid model name
# max_iterations=3,
# )
# # Run the diagnostic process
# result = orchestrator.run(
# initial_case_info=initial_info,
# full_case_details=full_case,
# ground_truth_diagnosis=ground_truth,
# )
# results[variant_name] = result
# # Display results
# print(f"\nπ Final Diagnosis: {result.final_diagnosis}")
# print(f"π― Ground Truth: {result.ground_truth}")
# print(f"β Accuracy Score: {result.accuracy_score}/5.0")
# print(f" Reasoning: {result.accuracy_reasoning}")
# print(f"π° Total Cost: ${result.total_cost:,}")
# print(f"π Iterations: {result.iterations}")
# print(f"β±οΈ Mode: {orchestrator.mode}")
# # Demonstrate ensemble approach
# print(f"\n{'='*60}")
# print("Testing Variant: ENSEMBLE")
# print(
# "Description: Multiple independent runs with consensus aggregation"
# )
# print("=" * 60)
# ensemble_orchestrator = MaiDxOrchestrator.create_variant(
# "ensemble",
# model_name="gpt-4.1", # Fixed: Use valid model name
# max_iterations=3, # Shorter iterations for ensemble
# )
# ensemble_result = ensemble_orchestrator.run_ensemble(
# initial_case_info=initial_info,
# full_case_details=full_case,
# ground_truth_diagnosis=ground_truth,
# num_runs=2, # Reduced for demo
# )
# results["ensemble"] = ensemble_result
# print(
# f"\nπ Ensemble Diagnosis: {ensemble_result.final_diagnosis}"
# )
# print(f"π― Ground Truth: {ensemble_result.ground_truth}")
# print(
# f"β Ensemble Score: {ensemble_result.accuracy_score}/5.0"
# )
# print(
# f"π° Total Ensemble Cost: ${ensemble_result.total_cost:,}"
# )
# # --- Summary Comparison ---
# print(f"\n{'='*80}")
# print(" RESULTS SUMMARY")
# print("=" * 80)
# print(
# f"{'Variant':<15} {'Diagnosis Match':<15} {'Score':<8} {'Cost':<12} {'Iterations':<12}"
# )
# print("-" * 80)
# for variant_name, result in results.items():
# match_status = (
# "β Match"
# if result.accuracy_score >= 4.0
# else "β No Match"
# )
# print(
# f"{variant_name:<15} {match_status:<15} {result.accuracy_score:<8.1f} ${result.total_cost:<11,} {result.iterations:<12}"
# )
# print(f"\n{'='*80}")
# print(
# "Implementation successfully demonstrates the MAI-DxO framework"
# )
# print(
# "as described in 'Sequential Diagnosis with Language Models' paper"
# )
# print("=" * 80)
# except Exception as e:
# logger.exception(
# f"An error occurred during the diagnostic session: {e}"
# )
# print(f"\nβ Error occurred: {e}")
# print("Please check your model configuration and API keys.")
|