Spaces:
Paused
Paused
File size: 162,046 Bytes
f6686e1 | 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 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="generator" content="pdoc 0.10.0" />
<title>tinytroupe.utils.llm API documentation</title>
<meta name="description" content="" />
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>tinytroupe.utils.llm</code></h1>
</header>
<section id="section-intro">
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">import re
import json
import ast
import os
import chevron
from typing import Collection, Dict, List, Union
from pydantic import BaseModel
import copy
import functools
import inspect
import pprint
import textwrap
from tinytroupe import utils
from tinytroupe.utils import logger
from tinytroupe.utils.rendering import break_text_at_length
################################################################################
# Model input utilities
################################################################################
def compose_initial_LLM_messages_with_templates(system_template_name:str, user_template_name:str=None,
base_module_folder:str=None,
rendering_configs:dict={}) -> list:
"""
Composes the initial messages for the LLM model call, under the assumption that it always involves
a system (overall task description) and an optional user message (specific task description).
These messages are composed using the specified templates and rendering configurations.
"""
# ../ to go to the base library folder, because that's the most natural reference point for the user
if base_module_folder is None:
sub_folder = "../prompts/"
else:
sub_folder = f"../{base_module_folder}/prompts/"
base_template_folder = os.path.join(os.path.dirname(__file__), sub_folder)
system_prompt_template_path = os.path.join(base_template_folder, f'{system_template_name}')
user_prompt_template_path = os.path.join(base_template_folder, f'{user_template_name}')
messages = []
messages.append({"role": "system",
"content": chevron.render(
open(system_prompt_template_path).read(),
rendering_configs)})
# optionally add a user message
if user_template_name is not None:
messages.append({"role": "user",
"content": chevron.render(
open(user_prompt_template_path).read(),
rendering_configs)})
return messages
#
# Data structures to enforce output format during LLM API call.
#
class LLMScalarWithJustificationResponse(BaseModel):
"""
Represents a typed response from an LLM (Language Learning Model) including justification.
Attributes:
justification (str): The justification or explanation for the response.
value (str, int, float, bool): The value of the response.
confidence (float): The confidence level of the response.
"""
justification: str
value: Union[str, int, float, bool]
confidence: float
class LLMScalarWithJustificationAndReasoningResponse(BaseModel):
"""
Represents a typed response from an LLM (Language Learning Model) including justification and reasoning.
Attributes:
reasoning (str): The reasoning behind the response.
justification (str): The justification or explanation for the response.
value (str, int, float, bool): The value of the response.
confidence (float): The confidence level of the response.
"""
reasoning: str
# we need to repeat these fields here, instead of inheriting from LLMScalarWithJustificationResponse,
# because we need to ensure `reasoning` is always the first field in the JSON object.
justification: str
value: Union[str, int, float, bool]
confidence: float
###########################################################################
# Model calling helpers
###########################################################################
class LLMChat:
"""
A class that represents an ongoing LLM conversation. It maintains the conversation history,
allows adding new messages, and handles model output type coercion.
"""
def __init__(self, system_template_name:str=None, system_prompt:str=None,
user_template_name:str=None, user_prompt:str=None,
base_module_folder=None,
output_type=None,
enable_json_output_format:bool=True,
enable_justification_step:bool=True,
enable_reasoning_step:bool=False,
**model_params):
"""
Initializes an LLMChat instance with the specified system and user templates, or the system and user prompts.
If a template is specified, the corresponding prompt must be None, and vice versa.
Args:
system_template_name (str): Name of the system template file.
system_prompt (str): System prompt content.
user_template_name (str): Name of the user template file.
user_prompt (str): User prompt content.
base_module_folder (str): Optional subfolder path within the library where templates are located.
output_type (type): Expected type of the model output.
enable_reasoning_step (bool): Flag to enable reasoning step in the conversation. This IS NOT the use of "reasoning models" (e.g., o1, o3),
but rather the use of an additional reasoning step in the regular text completion.
enable_justification_step (bool): Flag to enable justification step in the conversation. Must be True if reasoning step is enabled as well.
enable_json_output_format (bool): Flag to enable JSON output format for the model response. Must be True if reasoning or justification steps are enabled.
**model_params: Additional parameters for the LLM model call.
"""
if (system_template_name is not None and system_prompt is not None) or \
(user_template_name is not None and user_prompt is not None) or\
(system_template_name is None and system_prompt is None) or \
(user_template_name is None and user_prompt is None):
raise ValueError("Either the template or the prompt must be specified, but not both.")
self.base_module_folder = base_module_folder
self.system_template_name = system_template_name
self.user_template_name = user_template_name
self.system_prompt = textwrap.dedent(system_prompt) if system_prompt is not None else None
self.user_prompt = textwrap.dedent(user_prompt) if user_prompt is not None else None
self.output_type = output_type
self.enable_reasoning_step = enable_reasoning_step
self.enable_justification_step = enable_justification_step
self.enable_json_output_format = enable_json_output_format
self.model_params = model_params
# Conversation history
self.messages = []
self.conversation_history = []
# Response tracking
self.response_raw = None
self.response_json = None
self.response_reasoning = None
self.response_value = None
self.response_justification = None
self.response_confidence = None
def __call__(self, *args, **kwds):
return self.call(*args, **kwds)
def _render_template(self, template_name, base_module_folder=None, rendering_configs={}):
"""
Helper method to render templates for messages.
Args:
template_name: Name of the template file
base_module_folder: Optional subfolder path within the library
rendering_configs: Configuration variables for template rendering
Returns:
Rendered template content
"""
if base_module_folder is None:
sub_folder = "../prompts/"
else:
sub_folder = f"../{base_module_folder}/prompts/"
base_template_folder = os.path.join(os.path.dirname(__file__), sub_folder)
template_path = os.path.join(base_template_folder, template_name)
return chevron.render(open(template_path).read(), rendering_configs)
def add_user_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a user message to the conversation.
Args:
message: The direct message content from the user (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "user", "content": content})
return self
def add_system_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a system message to the conversation.
Args:
message: The direct message content from the system (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "system", "content": content})
return self
def add_assistant_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add an assistant message to the conversation.
Args:
message: The direct message content from the assistant (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "assistant", "content": content})
return self
def call(self, output_type="default",
enable_json_output_format:bool=None,
enable_justification_step:bool=None,
enable_reasoning_step:bool=None,
**rendering_configs):
"""
Initiates or continues the conversation with the LLM model using the current message history.
Args:
output_type: Optional parameter to override the output type for this specific call. If set to "default", it uses the instance's output_type.
If set to None, removes all output formatting and coercion.
enable_json_output_format: Optional flag to enable JSON output format for the model response. If None, uses the instance's setting.
enable_justification_step: Optional flag to enable justification step in the conversation. If None, uses the instance's setting.
enable_reasoning_step: Optional flag to enable reasoning step in the conversation. If None, uses the instance's setting.
rendering_configs: The rendering configurations (template variables) to use when composing the initial messages.
Returns:
The content of the model response.
"""
from tinytroupe.openai_utils import client # import here to avoid circular import
try:
# Initialize the conversation if this is the first call
if not self.messages:
if self.system_template_name is not None and self.user_template_name is not None:
self.messages = utils.compose_initial_LLM_messages_with_templates(
self.system_template_name,
self.user_template_name,
base_module_folder=self.base_module_folder,
rendering_configs=rendering_configs
)
else:
if self.system_prompt:
self.messages.append({"role": "system", "content": self.system_prompt})
if self.user_prompt:
self.messages.append({"role": "user", "content": self.user_prompt})
# Use the provided output_type if specified, otherwise fall back to the instance's output_type
current_output_type = output_type if output_type != "default" else self.output_type
# Set up typing for the output
if current_output_type is not None:
# TODO obsolete?
#
## Add type coercion instructions if not already added
#if not any(msg.get("content", "").startswith("In your response, you **MUST** provide a value")
# for msg in self.messages if msg.get("role") == "system"):
# the user can override the response format by specifying it in the model_params, otherwise
# we will use the default response format
if "response_format" not in self.model_params:
if utils.first_non_none(enable_json_output_format, self.enable_json_output_format):
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction = {"role": "system",
"content": "Your response **MUST** be a JSON object."}
# Special justification format can be used (will also include confidence level)
if utils.first_non_none(enable_justification_step, self.enable_justification_step):
# Add reasoning step if enabled provides further mechanism to think step-by-step
if not (utils.first_non_none(enable_reasoning_step, self.enable_reasoning_step)):
# Default structured output
self.model_params["response_format"] = LLMScalarWithJustificationResponse
typing_instruction = {"role": "system",
"content": "In your response, you **MUST** provide a value, along with a justification and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence). "+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"justification\": justification, \"value\": value, \"confidence\": confidence}. "+
"Note that \"justification\" comes first in order to help you think about the value you are providing."}
else:
# Override the response format to also use a reasoning step
self.model_params["response_format"] = LLMScalarWithJustificationAndReasoningResponse
typing_instruction = {"role": "system",
"content": \
"In your response, you **FIRST** think step-by-step on how you are going to compute the value, and you put this reasoning in the \"reasoning\" field (which must come before all others). "+
"This allows you to think carefully as much as you need to deduce the best and most correct value. "+
"After that, you **MUST** provide the resulting value, along with a justification (which can tap into the previous reasoning), and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence)."+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"reasoning\": reasoning, \"justification\": justification, \"value\": value, \"confidence\": confidence}." +
" Note that \"justification\" comes after \"reasoning\" but before \"value\" to help with further formulation of the resulting \"value\"."}
# Specify the value type
if current_output_type == bool:
typing_instruction["content"] += " " + self._request_bool_llm_message()["content"]
elif current_output_type == int:
typing_instruction["content"] += " " + self._request_integer_llm_message()["content"]
elif current_output_type == float:
typing_instruction["content"] += " " + self._request_float_llm_message()["content"]
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
typing_instruction["content"] += " " + self._request_enumerable_llm_message(current_output_type)["content"]
elif current_output_type == List[Dict[str, any]]:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_of_dict_llm_message()["content"]
elif current_output_type == dict or current_output_type == "json":
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_dict_llm_message()["content"]
elif current_output_type == list:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_llm_message()["content"]
# Check if it is actually a pydantic model
elif issubclass(current_output_type, BaseModel):
# Completely override the response format
self.model_params["response_format"] = current_output_type
typing_instruction = {"role": "system", "content": "Your response **MUST** be a JSON object."}
elif current_output_type == str:
typing_instruction["content"] += " " + self._request_str_llm_message()["content"]
#pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
self.messages.append(typing_instruction)
else: # output_type is None
self.model_params["response_format"] = None
typing_instruction = {"role": "system", "content": \
"If you were given instructions before about the **format** of your response, please ignore them from now on. "+
"The needs of the user have changed. You **must** now use regular text -- not numbers, not booleans, not JSON. "+
"There are no fields, no types, no special formats. Just regular text appropriate to respond to the last user request."}
self.messages.append(typing_instruction)
#pass # nothing here for now
# Call the LLM model with all messages in the conversation
model_output = client().send_message(self.messages, **self.model_params)
if 'content' in model_output:
self.response_raw = self.response_value = model_output['content']
logger.debug(f"Model raw 'content' response: {self.response_raw}")
# Add the assistant's response to the conversation history
self.add_assistant_message(self.response_raw)
self.conversation_history.append({"messages": copy.deepcopy(self.messages)})
# Type coercion if output type is specified
if current_output_type is not None:
if self.enable_json_output_format:
# output is supposed to be a JSON object
self.response_json = self.response_value = utils.extract_json(self.response_raw)
logger.debug(f"Model output JSON response: {self.response_json}")
if self.enable_justification_step and not (hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj')):
# if justification step is enabled, we expect a JSON object with reasoning (optionally), justification, value, and confidence
# BUT not for Pydantic models which expect direct JSON structure
self.response_reasoning = self.response_json.get("reasoning", None)
self.response_value = self.response_json.get("value", None)
self.response_justification = self.response_json.get("justification", None)
self.response_confidence = self.response_json.get("confidence", None)
else:
# For direct JSON output (like Pydantic models), use the whole JSON as the value
self.response_value = self.response_json
# if output type was specified, we need to coerce the response value
if self.response_value is not None:
if current_output_type == bool:
self.response_value = self._coerce_to_bool(self.response_value)
elif current_output_type == int:
self.response_value = self._coerce_to_integer(self.response_value)
elif current_output_type == float:
self.response_value = self._coerce_to_float(self.response_value)
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
self.response_value = self._coerce_to_enumerable(self.response_value, current_output_type)
elif current_output_type == List[Dict[str, any]]:
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == dict or current_output_type == "json":
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == list:
self.response_value = self._coerce_to_list(self.response_value)
elif hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj'):
# Handle Pydantic model - try modern approach first, then fallback
try:
if hasattr(current_output_type, 'model_validate'):
self.response_value = current_output_type.model_validate(self.response_json)
else:
self.response_value = current_output_type.parse_obj(self.response_json)
except Exception as e:
logger.error(f"Failed to parse Pydantic model: {e}")
raise
elif current_output_type == str:
pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
else:
logger.error(f"Model output is None: {self.response_raw}")
logger.debug(f"Model output coerced response value: {self.response_value}")
logger.debug(f"Model output coerced response justification: {self.response_justification}")
logger.debug(f"Model output coerced response confidence: {self.response_confidence}")
return self.response_value
else:
logger.error(f"Model output does not contain 'content' key: {model_output}")
return None
except ValueError as ve:
# Re-raise ValueError exceptions (like unsupported output type) instead of catching them
if "Unsupported output type" in str(ve):
raise
else:
logger.error(f"Error during LLM call: {ve}. Will return None instead of failing.")
return None
except Exception as e:
logger.error(f"Error during LLM call: {e}. Will return None instead of failing.")
return None
def continue_conversation(self, user_message=None, **rendering_configs):
"""
Continue the conversation with a new user message and get a response.
Args:
user_message: The new message from the user
rendering_configs: Additional rendering configurations
Returns:
The content of the model response
"""
if user_message:
self.add_user_message(user_message)
return self.call(**rendering_configs)
def reset_conversation(self):
"""
Reset the conversation state but keep the initial configuration.
Returns:
self for method chaining
"""
self.messages = []
self.response_raw = None
self.response_json = None
self.response_value = None
self.response_justification = None
self.response_confidence = None
return self
def get_conversation_history(self):
"""
Get the full conversation history.
Returns:
List of all messages in the conversation
"""
return self.messages
# Keep all the existing coercion methods
def _coerce_to_bool(self, llm_output):
"""
Coerces the LLM output to a boolean value.
This method looks for the string "True", "False", "Yes", "No", "Positive", "Negative" in the LLM output, such that
- case is neutralized;
- the first occurrence of the string is considered, the rest is ignored. For example, " Yes, that is true" will be considered "Yes";
- if no such string is found, the method raises an error. So it is important that the prompts actually requests a boolean value.
Args:
llm_output (str, bool): The LLM output to coerce.
Returns:
The boolean value of the LLM output.
"""
# if the LLM output is already a boolean, we return it
if isinstance(llm_output, bool):
return llm_output
# let's extract the first occurrence of the string "True", "False", "Yes", "No", "Positive", "Negative" in the LLM output.
# using a regular expression
import re
match = re.search(r'\b(?:True|False|Yes|No|Positive|Negative)\b', llm_output, re.IGNORECASE)
if match:
first_match = match.group(0).lower()
if first_match in ["true", "yes", "positive"]:
return True
elif first_match in ["false", "no", "negative"]:
return False
raise ValueError("Cannot convert the LLM output to a boolean value.")
def _request_str_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate from now on has no special format, it can be any string you find appropriate to the current conversation. "+
"Make sure you move to `value` **all** relevant information you used in reasoning or justification, so that it is not lost. "}
def _request_bool_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be either 'True' or 'False'. This is critical for later processing. If you don't know the correct answer, just output 'False'."}
def _coerce_to_integer(self, llm_output:str):
"""
Coerces the LLM output to an integer value.
This method looks for the first occurrence of an integer in the LLM output, such that
- the first occurrence of the integer is considered, the rest is ignored. For example, "There are 3 cats" will be considered 3;
- if no integer is found, the method raises an error. So it is important that the prompts actually requests an integer value.
Args:
llm_output (str, int): The LLM output to coerce.
Returns:
The integer value of the LLM output.
"""
# if the LLM output is already an integer, we return it
if isinstance(llm_output, int):
return llm_output
# if it's a float that represents a whole number, convert it
if isinstance(llm_output, float):
if llm_output.is_integer():
return int(llm_output)
else:
raise ValueError("Cannot convert the LLM output to an integer value.")
# Convert to string for regex processing
llm_output_str = str(llm_output)
# let's extract the first occurrence of an integer in the LLM output.
# using a regular expression
import re
# Match integers that are not part of a decimal number
# First check if the string contains a decimal point - if so, reject it for integer coercion
if '.' in llm_output_str and any(c.isdigit() for c in llm_output_str.split('.')[1]):
# This looks like a decimal number, not a pure integer
raise ValueError("Cannot convert the LLM output to an integer value.")
match = re.search(r'-?\b\d+\b', llm_output_str)
if match:
return int(match.group(0))
raise ValueError("Cannot convert the LLM output to an integer value.")
def _request_integer_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be an integer number (e.g., '1'). This is critical for later processing.."}
def _coerce_to_float(self, llm_output:str):
"""
Coerces the LLM output to a float value.
This method looks for the first occurrence of a float in the LLM output, such that
- the first occurrence of the float is considered, the rest is ignored. For example, "The price is $3.50" will be considered 3.50;
- if no float is found, the method raises an error. So it is important that the prompts actually requests a float value.
Args:
llm_output (str, float): The LLM output to coerce.
Returns:
The float value of the LLM output.
"""
# if the LLM output is already a float, we return it
if isinstance(llm_output, float):
return llm_output
# if it's an integer, convert to float
if isinstance(llm_output, int):
return float(llm_output)
# let's extract the first occurrence of a number (float or int) in the LLM output.
# using a regular expression that handles negative numbers and both int/float formats
import re
match = re.search(r'-?\b\d+(?:\.\d+)?\b', llm_output)
if match:
return float(match.group(0))
raise ValueError("Cannot convert the LLM output to a float value.")
def _request_float_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a float number (e.g., '980.16'). This is critical for later processing."}
def _coerce_to_enumerable(self, llm_output:str, options:list):
"""
Coerces the LLM output to one of the specified options.
This method looks for the first occurrence of one of the specified options in the LLM output, such that
- the first occurrence of the option is considered, the rest is ignored. For example, "I prefer cats" will be considered "cats";
- if no option is found, the method raises an error. So it is important that the prompts actually requests one of the specified options.
Args:
llm_output (str): The LLM output to coerce.
options (list): The list of options to consider.
Returns:
The option value of the LLM output.
"""
# let's extract the first occurrence of one of the specified options in the LLM output.
# using a regular expression
import re
match = re.search(r'\b(?:' + '|'.join(options) + r')\b', llm_output, re.IGNORECASE)
if match:
# Return the canonical option (from the options list) instead of the matched text
matched_text = match.group(0).lower()
for option in options:
if option.lower() == matched_text:
return option
return match.group(0) # fallback
raise ValueError("Cannot find any of the specified options in the LLM output.")
def _request_enumerable_llm_message(self, options:list):
options_list_as_string = ', '.join([f"'{o}'" for o in options])
return {"role": "user",
"content": f"The `value` field you generate **must** be exactly one of the following strings: {options_list_as_string}. This is critical for later processing."}
def _coerce_to_dict_or_list(self, llm_output:str):
"""
Coerces the LLM output to a list or dictionary, i.e., a JSON structure.
This method looks for a JSON object in the LLM output, such that
- the JSON object is considered;
- if no JSON object is found, the method raises an error. So it is important that the prompts actually requests a JSON object.
Args:
llm_output (str): The LLM output to coerce.
Returns:
The dictionary value of the LLM output.
"""
# if the LLM output is already a dictionary or list, we return it
if isinstance(llm_output, (dict, list)):
return llm_output
try:
result = utils.extract_json(llm_output)
# extract_json returns {} on failure, but we need dict or list
if result == {} and not (isinstance(llm_output, str) and ('{}' in llm_output or '{' in llm_output and '}' in llm_output)):
raise ValueError("Cannot convert the LLM output to a dict or list value.")
# Check if result is actually dict or list
if not isinstance(result, (dict, list)):
raise ValueError("Cannot convert the LLM output to a dict or list value.")
return result
except Exception:
raise ValueError("Cannot convert the LLM output to a dict or list value.")
def _request_dict_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a JSON structure embedded in a string. This is critical for later processing."}
def _request_list_of_dict_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a list of dictionaries, specified as a JSON structure embedded in a string. For example, `[\{...\}, \{...\}, ...]`. This is critical for later processing."}
def _coerce_to_list(self, llm_output:str):
"""
Coerces the LLM output to a list.
This method looks for a list in the LLM output, such that
- the list is considered;
- if no list is found, the method raises an error. So it is important that the prompts actually requests a list.
Args:
llm_output (str): The LLM output to coerce.
Returns:
The list value of the LLM output.
"""
# if the LLM output is already a list, we return it
if isinstance(llm_output, list):
return llm_output
# must make sure there's actually a list. Let's start with regex
import re
match = re.search(r'\[.*\]', llm_output)
if match:
return json.loads(match.group(0))
raise ValueError("Cannot convert the LLM output to a list.")
def _request_list_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a JSON **list** (e.g., [\"apple\", 1, 0.9]), NOT a dictionary, always embedded in a string. This is critical for later processing."}
def __repr__(self):
return f"LLMChat(messages={self.messages}, model_params={self.model_params})"
def llm(enable_json_output_format:bool=True, enable_justification_step:bool=True, enable_reasoning_step:bool=False, **model_overrides):
"""
Decorator that turns the decorated function into an LLM-based function.
The decorated function must either return a string (the instruction to the LLM)
or a one-argument function that will be used to post-process the LLM response.
If the function returns a string, the function's docstring will be used as the system prompt,
and the returned string will be used as the user prompt. If the function returns a function,
the parameters of the function will be used instead as the system instructions to the LLM,
and the returned function will be used to post-process the LLM response.
The LLM response is coerced to the function's annotated return type, if present.
Usage example:
@llm(model="gpt-4-0613", temperature=0.5, max_tokens=100)
def joke():
return "Tell me a joke."
Usage example with post-processing:
@llm()
def unique_joke_list():
\"\"\"Creates a list of unique jokes.\"\"\"
return lambda x: list(set(x.split("\n")))
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
sig = inspect.signature(func)
return_type = sig.return_annotation if sig.return_annotation != inspect.Signature.empty else str
postprocessing_func = lambda x: x # by default, no post-processing
system_prompt = "You are an AI system that executes a computation as defined below.\n\n"
if func.__doc__ is not None:
system_prompt += func.__doc__.strip()
#
# Setup user prompt
#
if isinstance(result, str):
user_prompt = "EXECUTE THE INSTRUCTIONS BELOW:\n\n " + result
else:
# if there's a parameter named "self" in the function signature, remove it from args
if "self" in sig.parameters:
args = args[1:]
# TODO obsolete?
#
# if we are relying on parameters, they must be named
#if len(args) > 0:
# raise ValueError("Positional arguments are not allowed in LLM-based functions whose body does not return a string.")
user_prompt = f"Execute your computation as best as you can using the following input parameter values.\n\n"
user_prompt += f" ## Unnamed parameters\n{json.dumps(args, indent=4)}\n\n"
user_prompt += f" ## Named parameters\n{json.dumps(kwargs, indent=4)}\n\n"
#
# Set the post-processing function if the function returns a function
#
if inspect.isfunction(result):
# uses the returned function as a post-processing function
postprocessing_func = result
llm_req = LLMChat(system_prompt=system_prompt,
user_prompt=user_prompt,
output_type=return_type,
enable_json_output_format=enable_json_output_format,
enable_justification_step=enable_justification_step,
enable_reasoning_step=enable_reasoning_step,
**model_overrides)
llm_result = postprocessing_func(llm_req.call())
return llm_result
return wrapper
return decorator
################################################################################
# Model output utilities
################################################################################
def extract_json(text: str) -> dict:
"""
Extracts a JSON object from a string, ignoring: any text before the first
opening curly brace; and any Markdown opening (```json) or closing(```) tags.
"""
try:
logger.debug(f"Extracting JSON from text: {text}")
# if it already is a dictionary or list, return it
if isinstance(text, dict) or isinstance(text, list):
# validate that all the internal contents are indeed JSON-like
try:
json.dumps(text)
except Exception as e:
logger.error(f"Error occurred while validating JSON: {e}. Input text: {text}.")
return {}
logger.debug(f"Text is already a dictionary. Returning it.")
return text
filtered_text = ""
# remove any text before the first opening curly or square braces, using regex. Leave the braces.
filtered_text = re.sub(r'^.*?({|\[)', r'\1', text, flags=re.DOTALL)
# remove any trailing text after the LAST closing curly or square braces, using regex. Leave the braces.
filtered_text = re.sub(r'(}|\])(?!.*(\]|\})).*$', r'\1', filtered_text, flags=re.DOTALL)
# remove invalid escape sequences, which show up sometimes
filtered_text = re.sub("\\'", "'", filtered_text) # replace \' with just '
filtered_text = re.sub("\\,", ",", filtered_text)
# parse the final JSON in a robust manner, to account for potentially messy LLM outputs
try:
# First try standard JSON parsing
# use strict=False to correctly parse new lines, tabs, etc.
parsed = json.loads(filtered_text, strict=False)
except json.JSONDecodeError:
# If JSON parsing fails, try ast.literal_eval which accepts single quotes
try:
parsed = ast.literal_eval(filtered_text)
logger.debug("Used ast.literal_eval as fallback for single-quoted JSON-like text")
except:
# If both fail, try converting single quotes to double quotes and parse again
# Replace single-quoted keys and values with double quotes, without using look-behind
# This will match single-quoted strings that are keys or values in JSON-like structures
# It may not be perfect for all edge cases, but works for most LLM outputs
converted_text = re.sub(r"'([^']*)'", r'"\1"', filtered_text)
parsed = json.loads(converted_text, strict=False)
logger.debug("Converted single quotes to double quotes before parsing")
# return the parsed JSON object
return parsed
except Exception as e:
logger.error(f"Error occurred while extracting JSON: {e}. Input text: {text}. Filtered text: {filtered_text}")
return {}
def extract_code_block(text: str) -> str:
"""
Extracts a code block from a string, ignoring any text before the first
opening triple backticks and any text after the closing triple backticks.
"""
try:
# remove any text before the first opening triple backticks, using regex. Leave the backticks.
text = re.sub(r'^.*?(```)', r'\1', text, flags=re.DOTALL)
# remove any trailing text after the LAST closing triple backticks, using regex. Leave the backticks.
text = re.sub(r'(```)(?!.*```).*$', r'\1', text, flags=re.DOTALL)
return text
except Exception:
return ""
################################################################################
# Model control utilities
################################################################################
def repeat_on_error(retries:int, exceptions:list):
"""
Decorator that repeats the specified function call if an exception among those specified occurs,
up to the specified number of retries. If that number of retries is exceeded, the
exception is raised. If no exception occurs, the function returns normally.
Args:
retries (int): The number of retries to attempt.
exceptions (list): The list of exception classes to catch.
"""
def decorator(func):
def wrapper(*args, **kwargs):
for i in range(retries):
try:
return func(*args, **kwargs)
except tuple(exceptions) as e:
logger.debug(f"Exception occurred: {e}")
if i == retries - 1:
raise e
else:
logger.debug(f"Retrying ({i+1}/{retries})...")
continue
return wrapper
return decorator
def try_function(func, postcond_func=None, retries=5, exceptions=[Exception]):
@repeat_on_error(retries=retries, exceptions=exceptions)
def aux_apply_func():
logger.debug(f"Trying function {func.__name__}...")
result = func()
logger.debug(f"Result of function {func.__name__}: {result}")
if postcond_func is not None:
if not postcond_func(result):
# must raise an exception if the postcondition is not met.
raise ValueError(f"Postcondition not met for function {func.__name__}!")
return result
return aux_apply_func()
################################################################################
# Prompt engineering
################################################################################
def add_rai_template_variables_if_enabled(template_variables: dict) -> dict:
"""
Adds the RAI template variables to the specified dictionary, if the RAI disclaimers are enabled.
These can be configured in the config.ini file. If enabled, the variables will then load the RAI disclaimers from the
appropriate files in the prompts directory. Otherwise, the variables will be set to None.
Args:
template_variables (dict): The dictionary of template variables to add the RAI variables to.
Returns:
dict: The updated dictionary of template variables.
"""
from tinytroupe import config # avoids circular import
rai_harmful_content_prevention = config["Simulation"].getboolean(
"RAI_HARMFUL_CONTENT_PREVENTION", True
)
rai_copyright_infringement_prevention = config["Simulation"].getboolean(
"RAI_COPYRIGHT_INFRINGEMENT_PREVENTION", True
)
# Harmful content
with open(os.path.join(os.path.dirname(__file__), "prompts/rai_harmful_content_prevention.md"), "r") as f:
rai_harmful_content_prevention_content = f.read()
template_variables['rai_harmful_content_prevention'] = rai_harmful_content_prevention_content if rai_harmful_content_prevention else None
# Copyright infringement
with open(os.path.join(os.path.dirname(__file__), "prompts/rai_copyright_infringement_prevention.md"), "r") as f:
rai_copyright_infringement_prevention_content = f.read()
template_variables['rai_copyright_infringement_prevention'] = rai_copyright_infringement_prevention_content if rai_copyright_infringement_prevention else None
return template_variables
################################################################################
# Truncation
################################################################################
def truncate_actions_or_stimuli(list_of_actions_or_stimuli: Collection[dict], max_content_length: int) -> Collection[str]:
"""
Truncates the content of actions or stimuli at the specified maximum length. Does not modify the original list.
Args:
list_of_actions_or_stimuli (Collection[dict]): The list of actions or stimuli to truncate.
max_content_length (int): The maximum length of the content.
Returns:
Collection[str]: The truncated list of actions or stimuli. It is a new list, not a reference to the original list,
to avoid unexpected side effects.
"""
cloned_list = copy.deepcopy(list_of_actions_or_stimuli)
for element in cloned_list:
# the external wrapper of the LLM message: {'role': ..., 'content': ...}
if "content" in element and "role" in element and element["role"] != "system":
msg_content = element["content"]
# now the actual action or stimulus content
# has action, stimuli or stimulus as key?
if isinstance(msg_content, dict):
if "action" in msg_content:
# is content there?
if "content" in msg_content["action"]:
msg_content["action"]["content"] = break_text_at_length(msg_content["action"]["content"], max_content_length)
elif "stimulus" in msg_content:
# is content there?
if "content" in msg_content["stimulus"]:
msg_content["stimulus"]["content"] = break_text_at_length(msg_content["stimulus"]["content"], max_content_length)
elif "stimuli" in msg_content:
# for each element in the list
for stimulus in msg_content["stimuli"]:
# is content there?
if "content" in stimulus:
stimulus["content"] = break_text_at_length(stimulus["content"], max_content_length)
# if no condition was met, we just ignore it. It is not an action or a stimulus.
return cloned_list</code></pre>
</details>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-functions">Functions</h2>
<dl>
<dt id="tinytroupe.utils.llm.add_rai_template_variables_if_enabled"><code class="name flex">
<span>def <span class="ident">add_rai_template_variables_if_enabled</span></span>(<span>template_variables: dict) ‑> dict</span>
</code></dt>
<dd>
<div class="desc"><p>Adds the RAI template variables to the specified dictionary, if the RAI disclaimers are enabled.
These can be configured in the config.ini file. If enabled, the variables will then load the RAI disclaimers from the
appropriate files in the prompts directory. Otherwise, the variables will be set to None.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>template_variables</code></strong> : <code>dict</code></dt>
<dd>The dictionary of template variables to add the RAI variables to.</dd>
</dl>
<h2 id="returns">Returns</h2>
<dl>
<dt><code>dict</code></dt>
<dd>The updated dictionary of template variables.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def add_rai_template_variables_if_enabled(template_variables: dict) -> dict:
"""
Adds the RAI template variables to the specified dictionary, if the RAI disclaimers are enabled.
These can be configured in the config.ini file. If enabled, the variables will then load the RAI disclaimers from the
appropriate files in the prompts directory. Otherwise, the variables will be set to None.
Args:
template_variables (dict): The dictionary of template variables to add the RAI variables to.
Returns:
dict: The updated dictionary of template variables.
"""
from tinytroupe import config # avoids circular import
rai_harmful_content_prevention = config["Simulation"].getboolean(
"RAI_HARMFUL_CONTENT_PREVENTION", True
)
rai_copyright_infringement_prevention = config["Simulation"].getboolean(
"RAI_COPYRIGHT_INFRINGEMENT_PREVENTION", True
)
# Harmful content
with open(os.path.join(os.path.dirname(__file__), "prompts/rai_harmful_content_prevention.md"), "r") as f:
rai_harmful_content_prevention_content = f.read()
template_variables['rai_harmful_content_prevention'] = rai_harmful_content_prevention_content if rai_harmful_content_prevention else None
# Copyright infringement
with open(os.path.join(os.path.dirname(__file__), "prompts/rai_copyright_infringement_prevention.md"), "r") as f:
rai_copyright_infringement_prevention_content = f.read()
template_variables['rai_copyright_infringement_prevention'] = rai_copyright_infringement_prevention_content if rai_copyright_infringement_prevention else None
return template_variables</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.compose_initial_LLM_messages_with_templates"><code class="name flex">
<span>def <span class="ident">compose_initial_LLM_messages_with_templates</span></span>(<span>system_template_name: str, user_template_name: str = None, base_module_folder: str = None, rendering_configs: dict = {}) ‑> list</span>
</code></dt>
<dd>
<div class="desc"><p>Composes the initial messages for the LLM model call, under the assumption that it always involves
a system (overall task description) and an optional user message (specific task description).
These messages are composed using the specified templates and rendering configurations.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def compose_initial_LLM_messages_with_templates(system_template_name:str, user_template_name:str=None,
base_module_folder:str=None,
rendering_configs:dict={}) -> list:
"""
Composes the initial messages for the LLM model call, under the assumption that it always involves
a system (overall task description) and an optional user message (specific task description).
These messages are composed using the specified templates and rendering configurations.
"""
# ../ to go to the base library folder, because that's the most natural reference point for the user
if base_module_folder is None:
sub_folder = "../prompts/"
else:
sub_folder = f"../{base_module_folder}/prompts/"
base_template_folder = os.path.join(os.path.dirname(__file__), sub_folder)
system_prompt_template_path = os.path.join(base_template_folder, f'{system_template_name}')
user_prompt_template_path = os.path.join(base_template_folder, f'{user_template_name}')
messages = []
messages.append({"role": "system",
"content": chevron.render(
open(system_prompt_template_path).read(),
rendering_configs)})
# optionally add a user message
if user_template_name is not None:
messages.append({"role": "user",
"content": chevron.render(
open(user_prompt_template_path).read(),
rendering_configs)})
return messages</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.extract_code_block"><code class="name flex">
<span>def <span class="ident">extract_code_block</span></span>(<span>text: str) ‑> str</span>
</code></dt>
<dd>
<div class="desc"><p>Extracts a code block from a string, ignoring any text before the first
opening triple backticks and any text after the closing triple backticks.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def extract_code_block(text: str) -> str:
"""
Extracts a code block from a string, ignoring any text before the first
opening triple backticks and any text after the closing triple backticks.
"""
try:
# remove any text before the first opening triple backticks, using regex. Leave the backticks.
text = re.sub(r'^.*?(```)', r'\1', text, flags=re.DOTALL)
# remove any trailing text after the LAST closing triple backticks, using regex. Leave the backticks.
text = re.sub(r'(```)(?!.*```).*$', r'\1', text, flags=re.DOTALL)
return text
except Exception:
return ""</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.extract_json"><code class="name flex">
<span>def <span class="ident">extract_json</span></span>(<span>text: str) ‑> dict</span>
</code></dt>
<dd>
<div class="desc"><p>Extracts a JSON object from a string, ignoring: any text before the first
opening curly brace; and any Markdown opening (<code>json) or closing(</code>) tags.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def extract_json(text: str) -> dict:
"""
Extracts a JSON object from a string, ignoring: any text before the first
opening curly brace; and any Markdown opening (```json) or closing(```) tags.
"""
try:
logger.debug(f"Extracting JSON from text: {text}")
# if it already is a dictionary or list, return it
if isinstance(text, dict) or isinstance(text, list):
# validate that all the internal contents are indeed JSON-like
try:
json.dumps(text)
except Exception as e:
logger.error(f"Error occurred while validating JSON: {e}. Input text: {text}.")
return {}
logger.debug(f"Text is already a dictionary. Returning it.")
return text
filtered_text = ""
# remove any text before the first opening curly or square braces, using regex. Leave the braces.
filtered_text = re.sub(r'^.*?({|\[)', r'\1', text, flags=re.DOTALL)
# remove any trailing text after the LAST closing curly or square braces, using regex. Leave the braces.
filtered_text = re.sub(r'(}|\])(?!.*(\]|\})).*$', r'\1', filtered_text, flags=re.DOTALL)
# remove invalid escape sequences, which show up sometimes
filtered_text = re.sub("\\'", "'", filtered_text) # replace \' with just '
filtered_text = re.sub("\\,", ",", filtered_text)
# parse the final JSON in a robust manner, to account for potentially messy LLM outputs
try:
# First try standard JSON parsing
# use strict=False to correctly parse new lines, tabs, etc.
parsed = json.loads(filtered_text, strict=False)
except json.JSONDecodeError:
# If JSON parsing fails, try ast.literal_eval which accepts single quotes
try:
parsed = ast.literal_eval(filtered_text)
logger.debug("Used ast.literal_eval as fallback for single-quoted JSON-like text")
except:
# If both fail, try converting single quotes to double quotes and parse again
# Replace single-quoted keys and values with double quotes, without using look-behind
# This will match single-quoted strings that are keys or values in JSON-like structures
# It may not be perfect for all edge cases, but works for most LLM outputs
converted_text = re.sub(r"'([^']*)'", r'"\1"', filtered_text)
parsed = json.loads(converted_text, strict=False)
logger.debug("Converted single quotes to double quotes before parsing")
# return the parsed JSON object
return parsed
except Exception as e:
logger.error(f"Error occurred while extracting JSON: {e}. Input text: {text}. Filtered text: {filtered_text}")
return {}</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.llm"><code class="name flex">
<span>def <span class="ident">llm</span></span>(<span>enable_json_output_format: bool = True, enable_justification_step: bool = True, enable_reasoning_step: bool = False, **model_overrides)</span>
</code></dt>
<dd>
<div class="desc"><p>Decorator that turns the decorated function into an LLM-based function.
The decorated function must either return a string (the instruction to the LLM)
or a one-argument function that will be used to post-process the LLM response.</p>
<pre><code>If the function returns a string, the function's docstring will be used as the system prompt,
and the returned string will be used as the user prompt. If the function returns a function,
the parameters of the function will be used instead as the system instructions to the LLM,
and the returned function will be used to post-process the LLM response.
The LLM response is coerced to the function's annotated return type, if present.
Usage example:
@llm(model="gpt-4-0613", temperature=0.5, max_tokens=100)
def joke():
return "Tell me a joke."
Usage example with post-processing:
@llm()
def unique_joke_list():
"""Creates a list of unique jokes."""
return lambda x: list(set(x.split("
</code></pre>
<p>")))</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def llm(enable_json_output_format:bool=True, enable_justification_step:bool=True, enable_reasoning_step:bool=False, **model_overrides):
"""
Decorator that turns the decorated function into an LLM-based function.
The decorated function must either return a string (the instruction to the LLM)
or a one-argument function that will be used to post-process the LLM response.
If the function returns a string, the function's docstring will be used as the system prompt,
and the returned string will be used as the user prompt. If the function returns a function,
the parameters of the function will be used instead as the system instructions to the LLM,
and the returned function will be used to post-process the LLM response.
The LLM response is coerced to the function's annotated return type, if present.
Usage example:
@llm(model="gpt-4-0613", temperature=0.5, max_tokens=100)
def joke():
return "Tell me a joke."
Usage example with post-processing:
@llm()
def unique_joke_list():
\"\"\"Creates a list of unique jokes.\"\"\"
return lambda x: list(set(x.split("\n")))
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
sig = inspect.signature(func)
return_type = sig.return_annotation if sig.return_annotation != inspect.Signature.empty else str
postprocessing_func = lambda x: x # by default, no post-processing
system_prompt = "You are an AI system that executes a computation as defined below.\n\n"
if func.__doc__ is not None:
system_prompt += func.__doc__.strip()
#
# Setup user prompt
#
if isinstance(result, str):
user_prompt = "EXECUTE THE INSTRUCTIONS BELOW:\n\n " + result
else:
# if there's a parameter named "self" in the function signature, remove it from args
if "self" in sig.parameters:
args = args[1:]
# TODO obsolete?
#
# if we are relying on parameters, they must be named
#if len(args) > 0:
# raise ValueError("Positional arguments are not allowed in LLM-based functions whose body does not return a string.")
user_prompt = f"Execute your computation as best as you can using the following input parameter values.\n\n"
user_prompt += f" ## Unnamed parameters\n{json.dumps(args, indent=4)}\n\n"
user_prompt += f" ## Named parameters\n{json.dumps(kwargs, indent=4)}\n\n"
#
# Set the post-processing function if the function returns a function
#
if inspect.isfunction(result):
# uses the returned function as a post-processing function
postprocessing_func = result
llm_req = LLMChat(system_prompt=system_prompt,
user_prompt=user_prompt,
output_type=return_type,
enable_json_output_format=enable_json_output_format,
enable_justification_step=enable_justification_step,
enable_reasoning_step=enable_reasoning_step,
**model_overrides)
llm_result = postprocessing_func(llm_req.call())
return llm_result
return wrapper
return decorator</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.repeat_on_error"><code class="name flex">
<span>def <span class="ident">repeat_on_error</span></span>(<span>retries: int, exceptions: list)</span>
</code></dt>
<dd>
<div class="desc"><p>Decorator that repeats the specified function call if an exception among those specified occurs,
up to the specified number of retries. If that number of retries is exceeded, the
exception is raised. If no exception occurs, the function returns normally.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>retries</code></strong> : <code>int</code></dt>
<dd>The number of retries to attempt.</dd>
<dt><strong><code>exceptions</code></strong> : <code>list</code></dt>
<dd>The list of exception classes to catch.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def repeat_on_error(retries:int, exceptions:list):
"""
Decorator that repeats the specified function call if an exception among those specified occurs,
up to the specified number of retries. If that number of retries is exceeded, the
exception is raised. If no exception occurs, the function returns normally.
Args:
retries (int): The number of retries to attempt.
exceptions (list): The list of exception classes to catch.
"""
def decorator(func):
def wrapper(*args, **kwargs):
for i in range(retries):
try:
return func(*args, **kwargs)
except tuple(exceptions) as e:
logger.debug(f"Exception occurred: {e}")
if i == retries - 1:
raise e
else:
logger.debug(f"Retrying ({i+1}/{retries})...")
continue
return wrapper
return decorator</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.truncate_actions_or_stimuli"><code class="name flex">
<span>def <span class="ident">truncate_actions_or_stimuli</span></span>(<span>list_of_actions_or_stimuli: Collection[dict], max_content_length: int) ‑> Collection[str]</span>
</code></dt>
<dd>
<div class="desc"><p>Truncates the content of actions or stimuli at the specified maximum length. Does not modify the original list.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>list_of_actions_or_stimuli</code></strong> : <code>Collection[dict]</code></dt>
<dd>The list of actions or stimuli to truncate.</dd>
<dt><strong><code>max_content_length</code></strong> : <code>int</code></dt>
<dd>The maximum length of the content.</dd>
</dl>
<h2 id="returns">Returns</h2>
<dl>
<dt><code>Collection[str]</code></dt>
<dd>The truncated list of actions or stimuli. It is a new list, not a reference to the original list, </dd>
</dl>
<p>to avoid unexpected side effects.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def truncate_actions_or_stimuli(list_of_actions_or_stimuli: Collection[dict], max_content_length: int) -> Collection[str]:
"""
Truncates the content of actions or stimuli at the specified maximum length. Does not modify the original list.
Args:
list_of_actions_or_stimuli (Collection[dict]): The list of actions or stimuli to truncate.
max_content_length (int): The maximum length of the content.
Returns:
Collection[str]: The truncated list of actions or stimuli. It is a new list, not a reference to the original list,
to avoid unexpected side effects.
"""
cloned_list = copy.deepcopy(list_of_actions_or_stimuli)
for element in cloned_list:
# the external wrapper of the LLM message: {'role': ..., 'content': ...}
if "content" in element and "role" in element and element["role"] != "system":
msg_content = element["content"]
# now the actual action or stimulus content
# has action, stimuli or stimulus as key?
if isinstance(msg_content, dict):
if "action" in msg_content:
# is content there?
if "content" in msg_content["action"]:
msg_content["action"]["content"] = break_text_at_length(msg_content["action"]["content"], max_content_length)
elif "stimulus" in msg_content:
# is content there?
if "content" in msg_content["stimulus"]:
msg_content["stimulus"]["content"] = break_text_at_length(msg_content["stimulus"]["content"], max_content_length)
elif "stimuli" in msg_content:
# for each element in the list
for stimulus in msg_content["stimuli"]:
# is content there?
if "content" in stimulus:
stimulus["content"] = break_text_at_length(stimulus["content"], max_content_length)
# if no condition was met, we just ignore it. It is not an action or a stimulus.
return cloned_list</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.try_function"><code class="name flex">
<span>def <span class="ident">try_function</span></span>(<span>func, postcond_func=None, retries=5, exceptions=[<class 'Exception'>])</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def try_function(func, postcond_func=None, retries=5, exceptions=[Exception]):
@repeat_on_error(retries=retries, exceptions=exceptions)
def aux_apply_func():
logger.debug(f"Trying function {func.__name__}...")
result = func()
logger.debug(f"Result of function {func.__name__}: {result}")
if postcond_func is not None:
if not postcond_func(result):
# must raise an exception if the postcondition is not met.
raise ValueError(f"Postcondition not met for function {func.__name__}!")
return result
return aux_apply_func()</code></pre>
</details>
</dd>
</dl>
</section>
<section>
<h2 class="section-title" id="header-classes">Classes</h2>
<dl>
<dt id="tinytroupe.utils.llm.LLMChat"><code class="flex name class">
<span>class <span class="ident">LLMChat</span></span>
<span>(</span><span>system_template_name: str = None, system_prompt: str = None, user_template_name: str = None, user_prompt: str = None, base_module_folder=None, output_type=None, enable_json_output_format: bool = True, enable_justification_step: bool = True, enable_reasoning_step: bool = False, **model_params)</span>
</code></dt>
<dd>
<div class="desc"><p>A class that represents an ongoing LLM conversation. It maintains the conversation history,
allows adding new messages, and handles model output type coercion.</p>
<p>Initializes an LLMChat instance with the specified system and user templates, or the system and user prompts.
If a template is specified, the corresponding prompt must be None, and vice versa.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>system_template_name</code></strong> : <code>str</code></dt>
<dd>Name of the system template file.</dd>
<dt><strong><code>system_prompt</code></strong> : <code>str</code></dt>
<dd>System prompt content.</dd>
<dt><strong><code>user_template_name</code></strong> : <code>str</code></dt>
<dd>Name of the user template file.</dd>
<dt><strong><code>user_prompt</code></strong> : <code>str</code></dt>
<dd>User prompt content.</dd>
<dt><strong><code>base_module_folder</code></strong> : <code>str</code></dt>
<dd>Optional subfolder path within the library where templates are located.</dd>
<dt><strong><code>output_type</code></strong> : <code>type</code></dt>
<dd>Expected type of the model output.</dd>
<dt><strong><code>enable_reasoning_step</code></strong> : <code>bool</code></dt>
<dd>Flag to enable reasoning step in the conversation. This IS NOT the use of "reasoning models" (e.g., o1, o3),
but rather the use of an additional reasoning step in the regular text completion.</dd>
<dt><strong><code>enable_justification_step</code></strong> : <code>bool</code></dt>
<dd>Flag to enable justification step in the conversation. Must be True if reasoning step is enabled as well.</dd>
<dt><strong><code>enable_json_output_format</code></strong> : <code>bool</code></dt>
<dd>Flag to enable JSON output format for the model response. Must be True if reasoning or justification steps are enabled.</dd>
<dt><strong><code>**model_params</code></strong></dt>
<dd>Additional parameters for the LLM model call.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class LLMChat:
"""
A class that represents an ongoing LLM conversation. It maintains the conversation history,
allows adding new messages, and handles model output type coercion.
"""
def __init__(self, system_template_name:str=None, system_prompt:str=None,
user_template_name:str=None, user_prompt:str=None,
base_module_folder=None,
output_type=None,
enable_json_output_format:bool=True,
enable_justification_step:bool=True,
enable_reasoning_step:bool=False,
**model_params):
"""
Initializes an LLMChat instance with the specified system and user templates, or the system and user prompts.
If a template is specified, the corresponding prompt must be None, and vice versa.
Args:
system_template_name (str): Name of the system template file.
system_prompt (str): System prompt content.
user_template_name (str): Name of the user template file.
user_prompt (str): User prompt content.
base_module_folder (str): Optional subfolder path within the library where templates are located.
output_type (type): Expected type of the model output.
enable_reasoning_step (bool): Flag to enable reasoning step in the conversation. This IS NOT the use of "reasoning models" (e.g., o1, o3),
but rather the use of an additional reasoning step in the regular text completion.
enable_justification_step (bool): Flag to enable justification step in the conversation. Must be True if reasoning step is enabled as well.
enable_json_output_format (bool): Flag to enable JSON output format for the model response. Must be True if reasoning or justification steps are enabled.
**model_params: Additional parameters for the LLM model call.
"""
if (system_template_name is not None and system_prompt is not None) or \
(user_template_name is not None and user_prompt is not None) or\
(system_template_name is None and system_prompt is None) or \
(user_template_name is None and user_prompt is None):
raise ValueError("Either the template or the prompt must be specified, but not both.")
self.base_module_folder = base_module_folder
self.system_template_name = system_template_name
self.user_template_name = user_template_name
self.system_prompt = textwrap.dedent(system_prompt) if system_prompt is not None else None
self.user_prompt = textwrap.dedent(user_prompt) if user_prompt is not None else None
self.output_type = output_type
self.enable_reasoning_step = enable_reasoning_step
self.enable_justification_step = enable_justification_step
self.enable_json_output_format = enable_json_output_format
self.model_params = model_params
# Conversation history
self.messages = []
self.conversation_history = []
# Response tracking
self.response_raw = None
self.response_json = None
self.response_reasoning = None
self.response_value = None
self.response_justification = None
self.response_confidence = None
def __call__(self, *args, **kwds):
return self.call(*args, **kwds)
def _render_template(self, template_name, base_module_folder=None, rendering_configs={}):
"""
Helper method to render templates for messages.
Args:
template_name: Name of the template file
base_module_folder: Optional subfolder path within the library
rendering_configs: Configuration variables for template rendering
Returns:
Rendered template content
"""
if base_module_folder is None:
sub_folder = "../prompts/"
else:
sub_folder = f"../{base_module_folder}/prompts/"
base_template_folder = os.path.join(os.path.dirname(__file__), sub_folder)
template_path = os.path.join(base_template_folder, template_name)
return chevron.render(open(template_path).read(), rendering_configs)
def add_user_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a user message to the conversation.
Args:
message: The direct message content from the user (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "user", "content": content})
return self
def add_system_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a system message to the conversation.
Args:
message: The direct message content from the system (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "system", "content": content})
return self
def add_assistant_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add an assistant message to the conversation.
Args:
message: The direct message content from the assistant (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "assistant", "content": content})
return self
def call(self, output_type="default",
enable_json_output_format:bool=None,
enable_justification_step:bool=None,
enable_reasoning_step:bool=None,
**rendering_configs):
"""
Initiates or continues the conversation with the LLM model using the current message history.
Args:
output_type: Optional parameter to override the output type for this specific call. If set to "default", it uses the instance's output_type.
If set to None, removes all output formatting and coercion.
enable_json_output_format: Optional flag to enable JSON output format for the model response. If None, uses the instance's setting.
enable_justification_step: Optional flag to enable justification step in the conversation. If None, uses the instance's setting.
enable_reasoning_step: Optional flag to enable reasoning step in the conversation. If None, uses the instance's setting.
rendering_configs: The rendering configurations (template variables) to use when composing the initial messages.
Returns:
The content of the model response.
"""
from tinytroupe.openai_utils import client # import here to avoid circular import
try:
# Initialize the conversation if this is the first call
if not self.messages:
if self.system_template_name is not None and self.user_template_name is not None:
self.messages = utils.compose_initial_LLM_messages_with_templates(
self.system_template_name,
self.user_template_name,
base_module_folder=self.base_module_folder,
rendering_configs=rendering_configs
)
else:
if self.system_prompt:
self.messages.append({"role": "system", "content": self.system_prompt})
if self.user_prompt:
self.messages.append({"role": "user", "content": self.user_prompt})
# Use the provided output_type if specified, otherwise fall back to the instance's output_type
current_output_type = output_type if output_type != "default" else self.output_type
# Set up typing for the output
if current_output_type is not None:
# TODO obsolete?
#
## Add type coercion instructions if not already added
#if not any(msg.get("content", "").startswith("In your response, you **MUST** provide a value")
# for msg in self.messages if msg.get("role") == "system"):
# the user can override the response format by specifying it in the model_params, otherwise
# we will use the default response format
if "response_format" not in self.model_params:
if utils.first_non_none(enable_json_output_format, self.enable_json_output_format):
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction = {"role": "system",
"content": "Your response **MUST** be a JSON object."}
# Special justification format can be used (will also include confidence level)
if utils.first_non_none(enable_justification_step, self.enable_justification_step):
# Add reasoning step if enabled provides further mechanism to think step-by-step
if not (utils.first_non_none(enable_reasoning_step, self.enable_reasoning_step)):
# Default structured output
self.model_params["response_format"] = LLMScalarWithJustificationResponse
typing_instruction = {"role": "system",
"content": "In your response, you **MUST** provide a value, along with a justification and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence). "+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"justification\": justification, \"value\": value, \"confidence\": confidence}. "+
"Note that \"justification\" comes first in order to help you think about the value you are providing."}
else:
# Override the response format to also use a reasoning step
self.model_params["response_format"] = LLMScalarWithJustificationAndReasoningResponse
typing_instruction = {"role": "system",
"content": \
"In your response, you **FIRST** think step-by-step on how you are going to compute the value, and you put this reasoning in the \"reasoning\" field (which must come before all others). "+
"This allows you to think carefully as much as you need to deduce the best and most correct value. "+
"After that, you **MUST** provide the resulting value, along with a justification (which can tap into the previous reasoning), and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence)."+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"reasoning\": reasoning, \"justification\": justification, \"value\": value, \"confidence\": confidence}." +
" Note that \"justification\" comes after \"reasoning\" but before \"value\" to help with further formulation of the resulting \"value\"."}
# Specify the value type
if current_output_type == bool:
typing_instruction["content"] += " " + self._request_bool_llm_message()["content"]
elif current_output_type == int:
typing_instruction["content"] += " " + self._request_integer_llm_message()["content"]
elif current_output_type == float:
typing_instruction["content"] += " " + self._request_float_llm_message()["content"]
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
typing_instruction["content"] += " " + self._request_enumerable_llm_message(current_output_type)["content"]
elif current_output_type == List[Dict[str, any]]:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_of_dict_llm_message()["content"]
elif current_output_type == dict or current_output_type == "json":
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_dict_llm_message()["content"]
elif current_output_type == list:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_llm_message()["content"]
# Check if it is actually a pydantic model
elif issubclass(current_output_type, BaseModel):
# Completely override the response format
self.model_params["response_format"] = current_output_type
typing_instruction = {"role": "system", "content": "Your response **MUST** be a JSON object."}
elif current_output_type == str:
typing_instruction["content"] += " " + self._request_str_llm_message()["content"]
#pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
self.messages.append(typing_instruction)
else: # output_type is None
self.model_params["response_format"] = None
typing_instruction = {"role": "system", "content": \
"If you were given instructions before about the **format** of your response, please ignore them from now on. "+
"The needs of the user have changed. You **must** now use regular text -- not numbers, not booleans, not JSON. "+
"There are no fields, no types, no special formats. Just regular text appropriate to respond to the last user request."}
self.messages.append(typing_instruction)
#pass # nothing here for now
# Call the LLM model with all messages in the conversation
model_output = client().send_message(self.messages, **self.model_params)
if 'content' in model_output:
self.response_raw = self.response_value = model_output['content']
logger.debug(f"Model raw 'content' response: {self.response_raw}")
# Add the assistant's response to the conversation history
self.add_assistant_message(self.response_raw)
self.conversation_history.append({"messages": copy.deepcopy(self.messages)})
# Type coercion if output type is specified
if current_output_type is not None:
if self.enable_json_output_format:
# output is supposed to be a JSON object
self.response_json = self.response_value = utils.extract_json(self.response_raw)
logger.debug(f"Model output JSON response: {self.response_json}")
if self.enable_justification_step and not (hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj')):
# if justification step is enabled, we expect a JSON object with reasoning (optionally), justification, value, and confidence
# BUT not for Pydantic models which expect direct JSON structure
self.response_reasoning = self.response_json.get("reasoning", None)
self.response_value = self.response_json.get("value", None)
self.response_justification = self.response_json.get("justification", None)
self.response_confidence = self.response_json.get("confidence", None)
else:
# For direct JSON output (like Pydantic models), use the whole JSON as the value
self.response_value = self.response_json
# if output type was specified, we need to coerce the response value
if self.response_value is not None:
if current_output_type == bool:
self.response_value = self._coerce_to_bool(self.response_value)
elif current_output_type == int:
self.response_value = self._coerce_to_integer(self.response_value)
elif current_output_type == float:
self.response_value = self._coerce_to_float(self.response_value)
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
self.response_value = self._coerce_to_enumerable(self.response_value, current_output_type)
elif current_output_type == List[Dict[str, any]]:
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == dict or current_output_type == "json":
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == list:
self.response_value = self._coerce_to_list(self.response_value)
elif hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj'):
# Handle Pydantic model - try modern approach first, then fallback
try:
if hasattr(current_output_type, 'model_validate'):
self.response_value = current_output_type.model_validate(self.response_json)
else:
self.response_value = current_output_type.parse_obj(self.response_json)
except Exception as e:
logger.error(f"Failed to parse Pydantic model: {e}")
raise
elif current_output_type == str:
pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
else:
logger.error(f"Model output is None: {self.response_raw}")
logger.debug(f"Model output coerced response value: {self.response_value}")
logger.debug(f"Model output coerced response justification: {self.response_justification}")
logger.debug(f"Model output coerced response confidence: {self.response_confidence}")
return self.response_value
else:
logger.error(f"Model output does not contain 'content' key: {model_output}")
return None
except ValueError as ve:
# Re-raise ValueError exceptions (like unsupported output type) instead of catching them
if "Unsupported output type" in str(ve):
raise
else:
logger.error(f"Error during LLM call: {ve}. Will return None instead of failing.")
return None
except Exception as e:
logger.error(f"Error during LLM call: {e}. Will return None instead of failing.")
return None
def continue_conversation(self, user_message=None, **rendering_configs):
"""
Continue the conversation with a new user message and get a response.
Args:
user_message: The new message from the user
rendering_configs: Additional rendering configurations
Returns:
The content of the model response
"""
if user_message:
self.add_user_message(user_message)
return self.call(**rendering_configs)
def reset_conversation(self):
"""
Reset the conversation state but keep the initial configuration.
Returns:
self for method chaining
"""
self.messages = []
self.response_raw = None
self.response_json = None
self.response_value = None
self.response_justification = None
self.response_confidence = None
return self
def get_conversation_history(self):
"""
Get the full conversation history.
Returns:
List of all messages in the conversation
"""
return self.messages
# Keep all the existing coercion methods
def _coerce_to_bool(self, llm_output):
"""
Coerces the LLM output to a boolean value.
This method looks for the string "True", "False", "Yes", "No", "Positive", "Negative" in the LLM output, such that
- case is neutralized;
- the first occurrence of the string is considered, the rest is ignored. For example, " Yes, that is true" will be considered "Yes";
- if no such string is found, the method raises an error. So it is important that the prompts actually requests a boolean value.
Args:
llm_output (str, bool): The LLM output to coerce.
Returns:
The boolean value of the LLM output.
"""
# if the LLM output is already a boolean, we return it
if isinstance(llm_output, bool):
return llm_output
# let's extract the first occurrence of the string "True", "False", "Yes", "No", "Positive", "Negative" in the LLM output.
# using a regular expression
import re
match = re.search(r'\b(?:True|False|Yes|No|Positive|Negative)\b', llm_output, re.IGNORECASE)
if match:
first_match = match.group(0).lower()
if first_match in ["true", "yes", "positive"]:
return True
elif first_match in ["false", "no", "negative"]:
return False
raise ValueError("Cannot convert the LLM output to a boolean value.")
def _request_str_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate from now on has no special format, it can be any string you find appropriate to the current conversation. "+
"Make sure you move to `value` **all** relevant information you used in reasoning or justification, so that it is not lost. "}
def _request_bool_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be either 'True' or 'False'. This is critical for later processing. If you don't know the correct answer, just output 'False'."}
def _coerce_to_integer(self, llm_output:str):
"""
Coerces the LLM output to an integer value.
This method looks for the first occurrence of an integer in the LLM output, such that
- the first occurrence of the integer is considered, the rest is ignored. For example, "There are 3 cats" will be considered 3;
- if no integer is found, the method raises an error. So it is important that the prompts actually requests an integer value.
Args:
llm_output (str, int): The LLM output to coerce.
Returns:
The integer value of the LLM output.
"""
# if the LLM output is already an integer, we return it
if isinstance(llm_output, int):
return llm_output
# if it's a float that represents a whole number, convert it
if isinstance(llm_output, float):
if llm_output.is_integer():
return int(llm_output)
else:
raise ValueError("Cannot convert the LLM output to an integer value.")
# Convert to string for regex processing
llm_output_str = str(llm_output)
# let's extract the first occurrence of an integer in the LLM output.
# using a regular expression
import re
# Match integers that are not part of a decimal number
# First check if the string contains a decimal point - if so, reject it for integer coercion
if '.' in llm_output_str and any(c.isdigit() for c in llm_output_str.split('.')[1]):
# This looks like a decimal number, not a pure integer
raise ValueError("Cannot convert the LLM output to an integer value.")
match = re.search(r'-?\b\d+\b', llm_output_str)
if match:
return int(match.group(0))
raise ValueError("Cannot convert the LLM output to an integer value.")
def _request_integer_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be an integer number (e.g., '1'). This is critical for later processing.."}
def _coerce_to_float(self, llm_output:str):
"""
Coerces the LLM output to a float value.
This method looks for the first occurrence of a float in the LLM output, such that
- the first occurrence of the float is considered, the rest is ignored. For example, "The price is $3.50" will be considered 3.50;
- if no float is found, the method raises an error. So it is important that the prompts actually requests a float value.
Args:
llm_output (str, float): The LLM output to coerce.
Returns:
The float value of the LLM output.
"""
# if the LLM output is already a float, we return it
if isinstance(llm_output, float):
return llm_output
# if it's an integer, convert to float
if isinstance(llm_output, int):
return float(llm_output)
# let's extract the first occurrence of a number (float or int) in the LLM output.
# using a regular expression that handles negative numbers and both int/float formats
import re
match = re.search(r'-?\b\d+(?:\.\d+)?\b', llm_output)
if match:
return float(match.group(0))
raise ValueError("Cannot convert the LLM output to a float value.")
def _request_float_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a float number (e.g., '980.16'). This is critical for later processing."}
def _coerce_to_enumerable(self, llm_output:str, options:list):
"""
Coerces the LLM output to one of the specified options.
This method looks for the first occurrence of one of the specified options in the LLM output, such that
- the first occurrence of the option is considered, the rest is ignored. For example, "I prefer cats" will be considered "cats";
- if no option is found, the method raises an error. So it is important that the prompts actually requests one of the specified options.
Args:
llm_output (str): The LLM output to coerce.
options (list): The list of options to consider.
Returns:
The option value of the LLM output.
"""
# let's extract the first occurrence of one of the specified options in the LLM output.
# using a regular expression
import re
match = re.search(r'\b(?:' + '|'.join(options) + r')\b', llm_output, re.IGNORECASE)
if match:
# Return the canonical option (from the options list) instead of the matched text
matched_text = match.group(0).lower()
for option in options:
if option.lower() == matched_text:
return option
return match.group(0) # fallback
raise ValueError("Cannot find any of the specified options in the LLM output.")
def _request_enumerable_llm_message(self, options:list):
options_list_as_string = ', '.join([f"'{o}'" for o in options])
return {"role": "user",
"content": f"The `value` field you generate **must** be exactly one of the following strings: {options_list_as_string}. This is critical for later processing."}
def _coerce_to_dict_or_list(self, llm_output:str):
"""
Coerces the LLM output to a list or dictionary, i.e., a JSON structure.
This method looks for a JSON object in the LLM output, such that
- the JSON object is considered;
- if no JSON object is found, the method raises an error. So it is important that the prompts actually requests a JSON object.
Args:
llm_output (str): The LLM output to coerce.
Returns:
The dictionary value of the LLM output.
"""
# if the LLM output is already a dictionary or list, we return it
if isinstance(llm_output, (dict, list)):
return llm_output
try:
result = utils.extract_json(llm_output)
# extract_json returns {} on failure, but we need dict or list
if result == {} and not (isinstance(llm_output, str) and ('{}' in llm_output or '{' in llm_output and '}' in llm_output)):
raise ValueError("Cannot convert the LLM output to a dict or list value.")
# Check if result is actually dict or list
if not isinstance(result, (dict, list)):
raise ValueError("Cannot convert the LLM output to a dict or list value.")
return result
except Exception:
raise ValueError("Cannot convert the LLM output to a dict or list value.")
def _request_dict_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a JSON structure embedded in a string. This is critical for later processing."}
def _request_list_of_dict_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a list of dictionaries, specified as a JSON structure embedded in a string. For example, `[\{...\}, \{...\}, ...]`. This is critical for later processing."}
def _coerce_to_list(self, llm_output:str):
"""
Coerces the LLM output to a list.
This method looks for a list in the LLM output, such that
- the list is considered;
- if no list is found, the method raises an error. So it is important that the prompts actually requests a list.
Args:
llm_output (str): The LLM output to coerce.
Returns:
The list value of the LLM output.
"""
# if the LLM output is already a list, we return it
if isinstance(llm_output, list):
return llm_output
# must make sure there's actually a list. Let's start with regex
import re
match = re.search(r'\[.*\]', llm_output)
if match:
return json.loads(match.group(0))
raise ValueError("Cannot convert the LLM output to a list.")
def _request_list_llm_message(self):
return {"role": "user",
"content": "The `value` field you generate **must** be a JSON **list** (e.g., [\"apple\", 1, 0.9]), NOT a dictionary, always embedded in a string. This is critical for later processing."}
def __repr__(self):
return f"LLMChat(messages={self.messages}, model_params={self.model_params})"</code></pre>
</details>
<h3>Methods</h3>
<dl>
<dt id="tinytroupe.utils.llm.LLMChat.add_assistant_message"><code class="name flex">
<span>def <span class="ident">add_assistant_message</span></span>(<span>self, message=None, template_name=None, base_module_folder=None, rendering_configs={})</span>
</code></dt>
<dd>
<div class="desc"><p>Add an assistant message to the conversation.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>message</code></strong></dt>
<dd>The direct message content from the assistant (mutually exclusive with template_name)</dd>
<dt><strong><code>template_name</code></strong></dt>
<dd>Optional template file name to use for the message</dd>
<dt><strong><code>base_module_folder</code></strong></dt>
<dd>Optional subfolder for template location</dd>
<dt><strong><code>rendering_configs</code></strong></dt>
<dd>Configuration variables for template rendering</dd>
</dl>
<h2 id="returns">Returns</h2>
<p>self for method chaining</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def add_assistant_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add an assistant message to the conversation.
Args:
message: The direct message content from the assistant (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "assistant", "content": content})
return self</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.add_system_message"><code class="name flex">
<span>def <span class="ident">add_system_message</span></span>(<span>self, message=None, template_name=None, base_module_folder=None, rendering_configs={})</span>
</code></dt>
<dd>
<div class="desc"><p>Add a system message to the conversation.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>message</code></strong></dt>
<dd>The direct message content from the system (mutually exclusive with template_name)</dd>
<dt><strong><code>template_name</code></strong></dt>
<dd>Optional template file name to use for the message</dd>
<dt><strong><code>base_module_folder</code></strong></dt>
<dd>Optional subfolder for template location</dd>
<dt><strong><code>rendering_configs</code></strong></dt>
<dd>Configuration variables for template rendering</dd>
</dl>
<h2 id="returns">Returns</h2>
<p>self for method chaining</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def add_system_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a system message to the conversation.
Args:
message: The direct message content from the system (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "system", "content": content})
return self</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.add_user_message"><code class="name flex">
<span>def <span class="ident">add_user_message</span></span>(<span>self, message=None, template_name=None, base_module_folder=None, rendering_configs={})</span>
</code></dt>
<dd>
<div class="desc"><p>Add a user message to the conversation.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>message</code></strong></dt>
<dd>The direct message content from the user (mutually exclusive with template_name)</dd>
<dt><strong><code>template_name</code></strong></dt>
<dd>Optional template file name to use for the message</dd>
<dt><strong><code>base_module_folder</code></strong></dt>
<dd>Optional subfolder for template location</dd>
<dt><strong><code>rendering_configs</code></strong></dt>
<dd>Configuration variables for template rendering</dd>
</dl>
<h2 id="returns">Returns</h2>
<p>self for method chaining</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def add_user_message(self, message=None, template_name=None, base_module_folder=None, rendering_configs={}):
"""
Add a user message to the conversation.
Args:
message: The direct message content from the user (mutually exclusive with template_name)
template_name: Optional template file name to use for the message
base_module_folder: Optional subfolder for template location
rendering_configs: Configuration variables for template rendering
Returns:
self for method chaining
"""
if message is not None and template_name is not None:
raise ValueError("Either message or template_name must be specified, but not both.")
if template_name is not None:
content = self._render_template(template_name, base_module_folder, rendering_configs)
else:
content = textwrap.dedent(message)
self.messages.append({"role": "user", "content": content})
return self</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.call"><code class="name flex">
<span>def <span class="ident">call</span></span>(<span>self, output_type='default', enable_json_output_format: bool = None, enable_justification_step: bool = None, enable_reasoning_step: bool = None, **rendering_configs)</span>
</code></dt>
<dd>
<div class="desc"><p>Initiates or continues the conversation with the LLM model using the current message history.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>output_type</code></strong></dt>
<dd>Optional parameter to override the output type for this specific call. If set to "default", it uses the instance's output_type.
If set to None, removes all output formatting and coercion.</dd>
<dt><strong><code>enable_json_output_format</code></strong></dt>
<dd>Optional flag to enable JSON output format for the model response. If None, uses the instance's setting.</dd>
<dt><strong><code>enable_justification_step</code></strong></dt>
<dd>Optional flag to enable justification step in the conversation. If None, uses the instance's setting.</dd>
<dt><strong><code>enable_reasoning_step</code></strong></dt>
<dd>Optional flag to enable reasoning step in the conversation. If None, uses the instance's setting.</dd>
<dt><strong><code>rendering_configs</code></strong></dt>
<dd>The rendering configurations (template variables) to use when composing the initial messages.</dd>
</dl>
<h2 id="returns">Returns</h2>
<p>The content of the model response.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def call(self, output_type="default",
enable_json_output_format:bool=None,
enable_justification_step:bool=None,
enable_reasoning_step:bool=None,
**rendering_configs):
"""
Initiates or continues the conversation with the LLM model using the current message history.
Args:
output_type: Optional parameter to override the output type for this specific call. If set to "default", it uses the instance's output_type.
If set to None, removes all output formatting and coercion.
enable_json_output_format: Optional flag to enable JSON output format for the model response. If None, uses the instance's setting.
enable_justification_step: Optional flag to enable justification step in the conversation. If None, uses the instance's setting.
enable_reasoning_step: Optional flag to enable reasoning step in the conversation. If None, uses the instance's setting.
rendering_configs: The rendering configurations (template variables) to use when composing the initial messages.
Returns:
The content of the model response.
"""
from tinytroupe.openai_utils import client # import here to avoid circular import
try:
# Initialize the conversation if this is the first call
if not self.messages:
if self.system_template_name is not None and self.user_template_name is not None:
self.messages = utils.compose_initial_LLM_messages_with_templates(
self.system_template_name,
self.user_template_name,
base_module_folder=self.base_module_folder,
rendering_configs=rendering_configs
)
else:
if self.system_prompt:
self.messages.append({"role": "system", "content": self.system_prompt})
if self.user_prompt:
self.messages.append({"role": "user", "content": self.user_prompt})
# Use the provided output_type if specified, otherwise fall back to the instance's output_type
current_output_type = output_type if output_type != "default" else self.output_type
# Set up typing for the output
if current_output_type is not None:
# TODO obsolete?
#
## Add type coercion instructions if not already added
#if not any(msg.get("content", "").startswith("In your response, you **MUST** provide a value")
# for msg in self.messages if msg.get("role") == "system"):
# the user can override the response format by specifying it in the model_params, otherwise
# we will use the default response format
if "response_format" not in self.model_params:
if utils.first_non_none(enable_json_output_format, self.enable_json_output_format):
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction = {"role": "system",
"content": "Your response **MUST** be a JSON object."}
# Special justification format can be used (will also include confidence level)
if utils.first_non_none(enable_justification_step, self.enable_justification_step):
# Add reasoning step if enabled provides further mechanism to think step-by-step
if not (utils.first_non_none(enable_reasoning_step, self.enable_reasoning_step)):
# Default structured output
self.model_params["response_format"] = LLMScalarWithJustificationResponse
typing_instruction = {"role": "system",
"content": "In your response, you **MUST** provide a value, along with a justification and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence). "+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"justification\": justification, \"value\": value, \"confidence\": confidence}. "+
"Note that \"justification\" comes first in order to help you think about the value you are providing."}
else:
# Override the response format to also use a reasoning step
self.model_params["response_format"] = LLMScalarWithJustificationAndReasoningResponse
typing_instruction = {"role": "system",
"content": \
"In your response, you **FIRST** think step-by-step on how you are going to compute the value, and you put this reasoning in the \"reasoning\" field (which must come before all others). "+
"This allows you to think carefully as much as you need to deduce the best and most correct value. "+
"After that, you **MUST** provide the resulting value, along with a justification (which can tap into the previous reasoning), and your confidence level that the value and justification are correct (0.0 means no confidence, 1.0 means complete confidence)."+
"Furtheremore, your response **MUST** be a JSON object with the following structure: {\"reasoning\": reasoning, \"justification\": justification, \"value\": value, \"confidence\": confidence}." +
" Note that \"justification\" comes after \"reasoning\" but before \"value\" to help with further formulation of the resulting \"value\"."}
# Specify the value type
if current_output_type == bool:
typing_instruction["content"] += " " + self._request_bool_llm_message()["content"]
elif current_output_type == int:
typing_instruction["content"] += " " + self._request_integer_llm_message()["content"]
elif current_output_type == float:
typing_instruction["content"] += " " + self._request_float_llm_message()["content"]
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
typing_instruction["content"] += " " + self._request_enumerable_llm_message(current_output_type)["content"]
elif current_output_type == List[Dict[str, any]]:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_of_dict_llm_message()["content"]
elif current_output_type == dict or current_output_type == "json":
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_dict_llm_message()["content"]
elif current_output_type == list:
# Override the response format
self.model_params["response_format"] = {"type": "json_object"}
typing_instruction["content"] += " " + self._request_list_llm_message()["content"]
# Check if it is actually a pydantic model
elif issubclass(current_output_type, BaseModel):
# Completely override the response format
self.model_params["response_format"] = current_output_type
typing_instruction = {"role": "system", "content": "Your response **MUST** be a JSON object."}
elif current_output_type == str:
typing_instruction["content"] += " " + self._request_str_llm_message()["content"]
#pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
self.messages.append(typing_instruction)
else: # output_type is None
self.model_params["response_format"] = None
typing_instruction = {"role": "system", "content": \
"If you were given instructions before about the **format** of your response, please ignore them from now on. "+
"The needs of the user have changed. You **must** now use regular text -- not numbers, not booleans, not JSON. "+
"There are no fields, no types, no special formats. Just regular text appropriate to respond to the last user request."}
self.messages.append(typing_instruction)
#pass # nothing here for now
# Call the LLM model with all messages in the conversation
model_output = client().send_message(self.messages, **self.model_params)
if 'content' in model_output:
self.response_raw = self.response_value = model_output['content']
logger.debug(f"Model raw 'content' response: {self.response_raw}")
# Add the assistant's response to the conversation history
self.add_assistant_message(self.response_raw)
self.conversation_history.append({"messages": copy.deepcopy(self.messages)})
# Type coercion if output type is specified
if current_output_type is not None:
if self.enable_json_output_format:
# output is supposed to be a JSON object
self.response_json = self.response_value = utils.extract_json(self.response_raw)
logger.debug(f"Model output JSON response: {self.response_json}")
if self.enable_justification_step and not (hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj')):
# if justification step is enabled, we expect a JSON object with reasoning (optionally), justification, value, and confidence
# BUT not for Pydantic models which expect direct JSON structure
self.response_reasoning = self.response_json.get("reasoning", None)
self.response_value = self.response_json.get("value", None)
self.response_justification = self.response_json.get("justification", None)
self.response_confidence = self.response_json.get("confidence", None)
else:
# For direct JSON output (like Pydantic models), use the whole JSON as the value
self.response_value = self.response_json
# if output type was specified, we need to coerce the response value
if self.response_value is not None:
if current_output_type == bool:
self.response_value = self._coerce_to_bool(self.response_value)
elif current_output_type == int:
self.response_value = self._coerce_to_integer(self.response_value)
elif current_output_type == float:
self.response_value = self._coerce_to_float(self.response_value)
elif isinstance(current_output_type, list) and all(isinstance(option, str) for option in current_output_type):
self.response_value = self._coerce_to_enumerable(self.response_value, current_output_type)
elif current_output_type == List[Dict[str, any]]:
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == dict or current_output_type == "json":
self.response_value = self._coerce_to_dict_or_list(self.response_value)
elif current_output_type == list:
self.response_value = self._coerce_to_list(self.response_value)
elif hasattr(current_output_type, 'model_validate') or hasattr(current_output_type, 'parse_obj'):
# Handle Pydantic model - try modern approach first, then fallback
try:
if hasattr(current_output_type, 'model_validate'):
self.response_value = current_output_type.model_validate(self.response_json)
else:
self.response_value = current_output_type.parse_obj(self.response_json)
except Exception as e:
logger.error(f"Failed to parse Pydantic model: {e}")
raise
elif current_output_type == str:
pass # no coercion needed, it is already a string
else:
raise ValueError(f"Unsupported output type: {current_output_type}")
else:
logger.error(f"Model output is None: {self.response_raw}")
logger.debug(f"Model output coerced response value: {self.response_value}")
logger.debug(f"Model output coerced response justification: {self.response_justification}")
logger.debug(f"Model output coerced response confidence: {self.response_confidence}")
return self.response_value
else:
logger.error(f"Model output does not contain 'content' key: {model_output}")
return None
except ValueError as ve:
# Re-raise ValueError exceptions (like unsupported output type) instead of catching them
if "Unsupported output type" in str(ve):
raise
else:
logger.error(f"Error during LLM call: {ve}. Will return None instead of failing.")
return None
except Exception as e:
logger.error(f"Error during LLM call: {e}. Will return None instead of failing.")
return None</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.continue_conversation"><code class="name flex">
<span>def <span class="ident">continue_conversation</span></span>(<span>self, user_message=None, **rendering_configs)</span>
</code></dt>
<dd>
<div class="desc"><p>Continue the conversation with a new user message and get a response.</p>
<h2 id="args">Args</h2>
<dl>
<dt><strong><code>user_message</code></strong></dt>
<dd>The new message from the user</dd>
<dt><strong><code>rendering_configs</code></strong></dt>
<dd>Additional rendering configurations</dd>
</dl>
<h2 id="returns">Returns</h2>
<p>The content of the model response</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def continue_conversation(self, user_message=None, **rendering_configs):
"""
Continue the conversation with a new user message and get a response.
Args:
user_message: The new message from the user
rendering_configs: Additional rendering configurations
Returns:
The content of the model response
"""
if user_message:
self.add_user_message(user_message)
return self.call(**rendering_configs)</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.get_conversation_history"><code class="name flex">
<span>def <span class="ident">get_conversation_history</span></span>(<span>self)</span>
</code></dt>
<dd>
<div class="desc"><p>Get the full conversation history.</p>
<h2 id="returns">Returns</h2>
<p>List of all messages in the conversation</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def get_conversation_history(self):
"""
Get the full conversation history.
Returns:
List of all messages in the conversation
"""
return self.messages</code></pre>
</details>
</dd>
<dt id="tinytroupe.utils.llm.LLMChat.reset_conversation"><code class="name flex">
<span>def <span class="ident">reset_conversation</span></span>(<span>self)</span>
</code></dt>
<dd>
<div class="desc"><p>Reset the conversation state but keep the initial configuration.</p>
<h2 id="returns">Returns</h2>
<p>self for method chaining</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def reset_conversation(self):
"""
Reset the conversation state but keep the initial configuration.
Returns:
self for method chaining
"""
self.messages = []
self.response_raw = None
self.response_json = None
self.response_value = None
self.response_justification = None
self.response_confidence = None
return self</code></pre>
</details>
</dd>
</dl>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse"><code class="flex name class">
<span>class <span class="ident">LLMScalarWithJustificationAndReasoningResponse</span></span>
<span>(</span><span>**data: Any)</span>
</code></dt>
<dd>
<div class="desc"><p>Represents a typed response from an LLM (Language Learning Model) including justification and reasoning.</p>
<h2 id="attributes">Attributes</h2>
<dl>
<dt><strong><code>reasoning</code></strong> : <code>str</code></dt>
<dd>The reasoning behind the response.</dd>
<dt><strong><code>justification</code></strong> : <code>str</code></dt>
<dd>The justification or explanation for the response.</dd>
<dt><strong><code>value</code></strong> : <code>str, int, float, bool</code></dt>
<dd>The value of the response.</dd>
<dt><strong><code>confidence</code></strong> : <code>float</code></dt>
<dd>The confidence level of the response.</dd>
</dl>
<p>Create a new model by parsing and validating input data from keyword arguments.</p>
<p>Raises [<code>ValidationError</code>][pydantic_core.ValidationError] if the input data cannot be
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class LLMScalarWithJustificationAndReasoningResponse(BaseModel):
"""
Represents a typed response from an LLM (Language Learning Model) including justification and reasoning.
Attributes:
reasoning (str): The reasoning behind the response.
justification (str): The justification or explanation for the response.
value (str, int, float, bool): The value of the response.
confidence (float): The confidence level of the response.
"""
reasoning: str
# we need to repeat these fields here, instead of inheriting from LLMScalarWithJustificationResponse,
# because we need to ensure `reasoning` is always the first field in the JSON object.
justification: str
value: Union[str, int, float, bool]
confidence: float</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
</ul>
<h3>Class variables</h3>
<dl>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.confidence"><code class="name">var <span class="ident">confidence</span> : float</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.justification"><code class="name">var <span class="ident">justification</span> : str</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_config"><code class="name">var <span class="ident">model_config</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_fields"><code class="name">var <span class="ident">model_fields</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.reasoning"><code class="name">var <span class="ident">reasoning</span> : str</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.value"><code class="name">var <span class="ident">value</span> : Union[str, int, float, bool]</code></dt>
<dd>
<div class="desc"></div>
</dd>
</dl>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse"><code class="flex name class">
<span>class <span class="ident">LLMScalarWithJustificationResponse</span></span>
<span>(</span><span>**data: Any)</span>
</code></dt>
<dd>
<div class="desc"><p>Represents a typed response from an LLM (Language Learning Model) including justification.</p>
<h2 id="attributes">Attributes</h2>
<dl>
<dt><strong><code>justification</code></strong> : <code>str</code></dt>
<dd>The justification or explanation for the response.</dd>
<dt><strong><code>value</code></strong> : <code>str, int, float, bool</code></dt>
<dd>The value of the response.</dd>
<dt><strong><code>confidence</code></strong> : <code>float</code></dt>
<dd>The confidence level of the response.
</dd>
</dl>
<p>Create a new model by parsing and validating input data from keyword arguments.</p>
<p>Raises [<code>ValidationError</code>][pydantic_core.ValidationError] if the input data cannot be
validated to form a valid model.</p>
<p><code>__init__</code> uses <code>__pydantic_self__</code> instead of the more common <code>self</code> for the first arg to
allow <code>self</code> as a field name.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class LLMScalarWithJustificationResponse(BaseModel):
"""
Represents a typed response from an LLM (Language Learning Model) including justification.
Attributes:
justification (str): The justification or explanation for the response.
value (str, int, float, bool): The value of the response.
confidence (float): The confidence level of the response.
"""
justification: str
value: Union[str, int, float, bool]
confidence: float</code></pre>
</details>
<h3>Ancestors</h3>
<ul class="hlist">
<li>pydantic.main.BaseModel</li>
</ul>
<h3>Class variables</h3>
<dl>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.confidence"><code class="name">var <span class="ident">confidence</span> : float</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.justification"><code class="name">var <span class="ident">justification</span> : str</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_config"><code class="name">var <span class="ident">model_config</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_fields"><code class="name">var <span class="ident">model_fields</span></code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.value"><code class="name">var <span class="ident">value</span> : Union[str, int, float, bool]</code></dt>
<dd>
<div class="desc"></div>
</dd>
</dl>
</dd>
</dl>
</section>
</article>
<nav id="sidebar">
<h1>Index</h1>
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="tinytroupe.utils" href="index.html">tinytroupe.utils</a></code></li>
</ul>
</li>
<li><h3><a href="#header-functions">Functions</a></h3>
<ul class="">
<li><code><a title="tinytroupe.utils.llm.add_rai_template_variables_if_enabled" href="#tinytroupe.utils.llm.add_rai_template_variables_if_enabled">add_rai_template_variables_if_enabled</a></code></li>
<li><code><a title="tinytroupe.utils.llm.compose_initial_LLM_messages_with_templates" href="#tinytroupe.utils.llm.compose_initial_LLM_messages_with_templates">compose_initial_LLM_messages_with_templates</a></code></li>
<li><code><a title="tinytroupe.utils.llm.extract_code_block" href="#tinytroupe.utils.llm.extract_code_block">extract_code_block</a></code></li>
<li><code><a title="tinytroupe.utils.llm.extract_json" href="#tinytroupe.utils.llm.extract_json">extract_json</a></code></li>
<li><code><a title="tinytroupe.utils.llm.llm" href="#tinytroupe.utils.llm.llm">llm</a></code></li>
<li><code><a title="tinytroupe.utils.llm.repeat_on_error" href="#tinytroupe.utils.llm.repeat_on_error">repeat_on_error</a></code></li>
<li><code><a title="tinytroupe.utils.llm.truncate_actions_or_stimuli" href="#tinytroupe.utils.llm.truncate_actions_or_stimuli">truncate_actions_or_stimuli</a></code></li>
<li><code><a title="tinytroupe.utils.llm.try_function" href="#tinytroupe.utils.llm.try_function">try_function</a></code></li>
</ul>
</li>
<li><h3><a href="#header-classes">Classes</a></h3>
<ul>
<li>
<h4><code><a title="tinytroupe.utils.llm.LLMChat" href="#tinytroupe.utils.llm.LLMChat">LLMChat</a></code></h4>
<ul class="">
<li><code><a title="tinytroupe.utils.llm.LLMChat.add_assistant_message" href="#tinytroupe.utils.llm.LLMChat.add_assistant_message">add_assistant_message</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.add_system_message" href="#tinytroupe.utils.llm.LLMChat.add_system_message">add_system_message</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.add_user_message" href="#tinytroupe.utils.llm.LLMChat.add_user_message">add_user_message</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.call" href="#tinytroupe.utils.llm.LLMChat.call">call</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.continue_conversation" href="#tinytroupe.utils.llm.LLMChat.continue_conversation">continue_conversation</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.get_conversation_history" href="#tinytroupe.utils.llm.LLMChat.get_conversation_history">get_conversation_history</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMChat.reset_conversation" href="#tinytroupe.utils.llm.LLMChat.reset_conversation">reset_conversation</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse">LLMScalarWithJustificationAndReasoningResponse</a></code></h4>
<ul class="two-column">
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.confidence" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.confidence">confidence</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.justification" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.justification">justification</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_config" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_config">model_config</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_fields" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.model_fields">model_fields</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.reasoning" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.reasoning">reasoning</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.value" href="#tinytroupe.utils.llm.LLMScalarWithJustificationAndReasoningResponse.value">value</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse">LLMScalarWithJustificationResponse</a></code></h4>
<ul class="">
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.confidence" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse.confidence">confidence</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.justification" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse.justification">justification</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_config" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_config">model_config</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_fields" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse.model_fields">model_fields</a></code></li>
<li><code><a title="tinytroupe.utils.llm.LLMScalarWithJustificationResponse.value" href="#tinytroupe.utils.llm.LLMScalarWithJustificationResponse.value">value</a></code></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
</footer>
</body>
</html> |