File size: 102,559 Bytes
1928410 e9b1b6e f6af075 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 1f06354 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 e9b1b6e 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 e9b1b6e 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 5a7c030 1928410 5d9aa53 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 5d9aa53 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 ea6e5e7 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 04a8a14 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 f6af075 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 d1dcbba 1928410 c824df8 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 45fa14d 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 45fa14d 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 45fa14d 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 aedac73 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 45fa14d 1928410 4bc618b 1928410 4bc618b 1928410 c824df8 1928410 4bc618b 1928410 d1dcbba 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 f6af075 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 04a8a14 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 5472f27 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 8464d11 1928410 4bc618b 1928410 4bc618b 1928410 61cede0 1928410 4bc618b 1928410 f6af075 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 f6af075 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 a9dab13 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 4bc618b 1928410 a9dab13 1928410 4bc618b a9dab13 1928410 4bc618b 1928410 2887934 1928410 2887934 1928410 2887934 4bc618b 1928410 2887934 1928410 2887934 1928410 4bc618b 1f06354 1928410 4bc618b 1928410 |
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 |
import asyncio
import os
import json
import logging
import numpy as np
import pickle
import gzip
from typing import Dict, List, Optional, Any, Tuple
from datetime import datetime
import uuid
import httpx
import base64
from dataclasses import dataclass
# LightRAG imports
from lightrag import LightRAG, QueryParam
from lightrag.utils import EmbeddingFunc
from lightrag.kg.shared_storage import initialize_pipeline_status
# Database imports
import asyncpg
from redis import Redis
# Environment validation
REQUIRED_ENV_VARS = [
'CLOUDFLARE_API_KEY',
'CLOUDFLARE_ACCOUNT_ID',
'DATABASE_URL',
'BLOB_READ_WRITE_TOKEN',
'REDIS_URL',
'JWT_SECRET'
]
class EnvironmentError(Exception):
"""Raised when required environment variables are missing"""
pass
def validate_environment():
"""Validate all required environment variables are present"""
missing_vars = []
for var in REQUIRED_ENV_VARS:
if not os.getenv(var):
missing_vars.append(var)
if missing_vars:
raise EnvironmentError(f"Missing required environment variables: {', '.join(missing_vars)}")
@dataclass
class RAGConfig:
"""Configuration for RAG instances"""
ai_type: str
user_id: Optional[str] = None
ai_id: Optional[str] = None
name: Optional[str] = None
description: Optional[str] = None
def get_cache_key(self) -> str:
"""Generate cache key for this RAG configuration"""
return f"rag_{self.ai_type}_{self.user_id or 'system'}_{self.ai_id or 'default'}"
class CloudflareWorker:
def __init__(self, cloudflare_api_key: str, api_base_url: str, llm_model_name: str, embedding_model_name: str,
max_tokens: int = 4080):
self.cloudflare_api_key = cloudflare_api_key
self.api_base_url = api_base_url
self.max_tokens = max_tokens
self.logger = logging.getLogger(__name__)
self.llm_model_name = llm_model_name
self.embedding_model_name = embedding_model_name
self.llm_models = [
"@cf/meta/llama-3.1-8b-instruct",
"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
"@cf/mistralai/mistral-small-3.1-24b-instruct",
"@cf/meta/llama-4-scout-17b-16e-instruct",
"@cf/meta/llama-3.2-11b-vision-instruct",
"@cf/meta/llama-3-8b-instruct", # β
VERY GOOD - Llama 3, 8B params
"@cf/mistral/mistral-7b-instruct-v0.1", # β
GOOD - Mistral, excellent reasoning
"@cf/meta/llama-2-7b-chat-int8", # β
RELIABLE - Stable Llama 2
"@cf/microsoft/phi-2", # β
FAST - Microsoft's small but powerful
"@cf/meta/llama-3.2-3b-instruct", # β
CURRENT - Your working model
"@cf/google/gemma-3-12b-it",
"@cf/google/gemma-7b-it", # β
GOOD - Google's model
"@cf/qwen/qwen1.5-7b-chat-awq", # β
ALTERNATIVE - Chinese but works
"@cf/tiiuae/falcon-7b-instruct",
"@cf/microsoft/dialoGPT-medium",
]
# VERIFIED WORKING embedding models
self.embedding_models = [
"@cf/baai/bge-large-en-v1.5", # π BEST - Largest, most accurate
"@cf/baai/bge-base-en-v1.5", # β
GOOD - Standard choice
"@cf/baai/bge-small-en-v1.5", # β
FAST - Smaller but decent
"@cf/baai/bge-m3", # β
CURRENT - Multilingual
]
self.current_llm_index = 0
self.current_embedding_index = 0
async def query(self, prompt: str, system_prompt: str = "", **kwargs) -> str:
"""Enhanced query with better entity extraction prompting"""
# ENHANCED: Better system prompt for entity extraction
if not system_prompt:
system_prompt = """You are an expert technical document analyzer. Your main goal is to identify and extract important technical entities, concepts, and objects from specialized documents. Focus on:
- Technical terms and concepts
- Equipment and devices
- Procedures and processes
- Standards and requirements
- Physical objects and systems
Be precise and technical in your analysis."""
filtered_kwargs = {k: v for k, v in kwargs.items() if
k not in ['hashing_kv', 'history_messages', 'global_kv', 'text_chunks']}
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt[:self.max_tokens]},
]
# ENHANCED: Better parameters for technical content
input_data = {
"messages": messages,
"max_tokens": min(self.max_tokens, 4096),
"temperature": 0.2, # Lower temperature for more focused, technical responses
"top_p": 0.85, # Slightly focused sampling
**filtered_kwargs
}
response, new_index = await self._send_request_with_fallback(self.llm_models, self.current_llm_index,
input_data)
self.current_llm_index = new_index
# Log which model was used
model_used = self.llm_models[new_index]
self.logger.info(f"π€ Used model: {model_used}")
return response
async def _send_request_with_fallback(self, model_list: List[str], current_index: int, input_: dict) -> Tuple[
Any, int]:
"""Send request with model fallback"""
for i in range(len(model_list)):
model_index = (current_index + i) % len(model_list)
model_name = model_list[model_index]
try:
headers = {"Authorization": f"Bearer {self.cloudflare_api_key}"}
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.api_base_url}{model_name}",
headers=headers,
json=input_
)
response.raise_for_status()
result = response.json().get("result", {})
if "data" in result:
return np.array(result["data"]), model_index
elif "response" in result:
return result["response"], model_index
else:
continue
except Exception as e:
self.logger.warning(f"Model {model_name} failed: {e}")
continue
raise Exception("All models failed")
async def query(self, prompt: str, system_prompt: str = "", **kwargs) -> str:
filtered_kwargs = {k: v for k, v in kwargs.items() if
k not in ['hashing_kv', 'history_messages', 'global_kv', 'text_chunks']}
messages = [
{"role": "system",
"content": system_prompt or "You are a helpful AI assistant. Your main goal is to help with the knowledge you have from LightRAG files"},
{"role": "user", "content": prompt[:self.max_tokens]},
]
input_data = {"messages": messages, "max_tokens": min(self.max_tokens, 4096), **filtered_kwargs}
response, new_index = await self._send_request_with_fallback(self.llm_models, self.current_llm_index,
input_data)
self.current_llm_index = new_index
return response
async def embedding_chunk(self, texts: List[str]) -> np.ndarray:
truncated_texts = [text[:2000] for text in texts]
input_data = {"text": truncated_texts}
response, new_index = await self._send_request_with_fallback(self.embedding_models,
self.current_embedding_index, input_data)
self.current_embedding_index = new_index
return response
class VercelBlobClient:
"""Vercel Blob storage client for RAG state persistence"""
def __init__(self, token: str):
self.token = token
self.logger = logging.getLogger(__name__)
async def put(self, filename: str, data: bytes) -> str:
"""Upload data to Vercel Blob"""
try:
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.put(
f"https://blob.vercel-storage.com/{filename}",
headers={"Authorization": f"Bearer {self.token}"},
content=data
)
response.raise_for_status()
result = response.json()
return result.get('url', f"https://blob.vercel-storage.com/{filename}")
except Exception as e:
self.logger.error(f"Failed to upload to Vercel Blob: {e}")
raise
async def get(self, url: str) -> bytes:
"""Download data from Vercel Blob"""
try:
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.get(url)
response.raise_for_status()
return response.content
except Exception as e:
self.logger.error(f"Failed to download from Vercel Blob: {e}")
raise
class DatabaseManager:
"""Database manager with complete RAG persistence"""
def __init__(self, database_url: str, redis_url: str):
self.database_url = database_url
self.redis_url = redis_url
self.pool = None
self.redis = None
self.logger = logging.getLogger(__name__)
async def connect(self):
"""Initialize database connections"""
try:
self.pool = await asyncpg.create_pool(
self.database_url,
min_size=2,
max_size=20,
command_timeout=60
)
self.redis = Redis.from_url(self.redis_url, decode_responses=True)
self.logger.info("Database connections established successfully")
await self._create_tables()
except Exception as e:
self.logger.error(f"Database connection failed: {e}")
raise
async def _create_tables(self):
"""Create necessary tables for RAG persistence"""
async with self.pool.acquire() as conn:
await conn.execute("""
CREATE TABLE IF NOT EXISTS rag_instances (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
ai_type VARCHAR(50) NOT NULL,
user_id VARCHAR(100),
ai_id VARCHAR(100),
name VARCHAR(255) NOT NULL,
description TEXT,
graph_blob_url TEXT,
vector_blob_url TEXT,
config_blob_url TEXT,
total_chunks INTEGER DEFAULT 0,
total_tokens INTEGER DEFAULT 0,
file_count INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
last_accessed_at TIMESTAMP DEFAULT NOW(),
status VARCHAR(20) DEFAULT 'active',
UNIQUE(ai_type, user_id, ai_id)
);
CREATE TABLE IF NOT EXISTS knowledge_files (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
rag_instance_id UUID REFERENCES rag_instances(id) ON DELETE CASCADE,
filename VARCHAR(255) NOT NULL,
original_filename VARCHAR(255),
file_type VARCHAR(50),
file_size INTEGER,
blob_url TEXT,
content_text TEXT,
processed_at TIMESTAMP DEFAULT NOW(),
processing_status VARCHAR(20) DEFAULT 'processed',
token_count INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id VARCHAR(100) NOT NULL,
ai_type VARCHAR(50) NOT NULL,
ai_id VARCHAR(100),
title VARCHAR(255),
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
is_active BOOLEAN DEFAULT TRUE
);
CREATE TABLE IF NOT EXISTS conversation_messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
conversation_id UUID REFERENCES conversations(id) ON DELETE CASCADE,
role VARCHAR(20) NOT NULL,
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}',
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS system_stats (
id VARCHAR(50) PRIMARY KEY DEFAULT gen_random_uuid()::text,
total_users INTEGER NOT NULL DEFAULT 0,
total_ais INTEGER NOT NULL DEFAULT 0,
total_messages INTEGER NOT NULL DEFAULT 0,
date TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_system_stats_date ON system_stats(date DESC);
CREATE UNIQUE INDEX IF NOT EXISTS idx_system_stats_date_unique
ON system_stats(DATE(date));
-- Insert initial stats if table is empty
INSERT INTO system_stats (id, total_users, total_ais, total_messages, date)
SELECT 'initial', 0, 0, 0, NOW()
WHERE NOT EXISTS (SELECT 1 FROM system_stats);
CREATE INDEX IF NOT EXISTS idx_rag_instances_lookup ON rag_instances(ai_type, user_id, ai_id);
CREATE INDEX IF NOT EXISTS idx_conversations_user ON conversations(user_id);
CREATE INDEX IF NOT EXISTS idx_conversation_messages_conv ON conversation_messages(conversation_id);
""")
self.logger.info("Database tables created/verified successfully")
async def initialize_system_stats(self):
"""Initialize system stats with current counts from database"""
try:
async with self.pool.acquire() as conn:
# Count actual users
user_count = await conn.fetchval("""
SELECT COUNT(*) FROM users WHERE is_active = true
""") or 0
# Count actual custom AIs
ai_count = await conn.fetchval("""
SELECT COUNT(*) FROM custom_ais WHERE is_active = true
""") or 0
# Count actual messages from both tables
message_count = await conn.fetchval("""
SELECT
(SELECT COUNT(*) FROM messages) +
(SELECT COUNT(*) FROM conversation_messages)
""") or 0
# Check if today's stats already exist
today = datetime.now().date()
existing_stats = await conn.fetchrow("""
SELECT id FROM system_stats WHERE DATE(date) = $1
""", today)
if existing_stats:
# Update existing record
await conn.execute("""
UPDATE system_stats
SET total_users = $1, total_ais = $2, total_messages = $3, date = NOW()
WHERE DATE(date) = $4
""", user_count, ai_count, message_count, today)
else:
# Insert new record for today
await conn.execute("""
INSERT INTO system_stats (id, total_users, total_ais, total_messages, date)
VALUES ($1, $2, $3, $4, NOW())
""", f"stats_{today}", user_count, ai_count, message_count)
self.logger.info(
f"π Initialized system stats: {user_count} users, {ai_count} AIs, {message_count} messages")
except Exception as e:
self.logger.error(f"Failed to initialize system stats: {e}")
async def update_system_stat(self, stat_type: str, increment: int = 1):
"""Update a specific system statistic"""
try:
async with self.pool.acquire() as conn:
today = datetime.now().date()
# Map stat types to column names
column_map = {
'users': 'total_users',
'ais': 'total_ais',
'messages': 'total_messages'
}
if stat_type not in column_map:
self.logger.warning(f"Unknown stat type: {stat_type}")
return
column_name = column_map[stat_type]
# Upsert today's record
await conn.execute(f"""
INSERT INTO system_stats (id, total_users, total_ais, total_messages, date)
VALUES ($1,
CASE WHEN '{column_name}' = 'total_users' THEN $2 ELSE 0 END,
CASE WHEN '{column_name}' = 'total_ais' THEN $2 ELSE 0 END,
CASE WHEN '{column_name}' = 'total_messages' THEN $2 ELSE 0 END,
NOW())
ON CONFLICT (DATE(date)) DO UPDATE SET
{column_name} = system_stats.{column_name} + $2,
date = NOW()
""", f"stats_{today}", increment)
self.logger.debug(f"π Updated {stat_type} by {increment}")
except Exception as e:
self.logger.error(f"Failed to update {stat_type} stat: {e}")
async def get_current_stats(self):
"""Get current system statistics"""
try:
async with self.pool.acquire() as conn:
# Get latest stats
stats_row = await conn.fetchrow("""
SELECT total_users, total_ais, total_messages, date
FROM system_stats
ORDER BY date DESC
LIMIT 1
""")
if not stats_row:
# Initialize if no stats exist
await self.initialize_system_stats()
return await self.get_current_stats()
# Calculate total characters (lines of code)
total_characters = await conn.fetchval("""
SELECT COALESCE(
(SELECT SUM(LENGTH(content)) FROM messages) +
(SELECT SUM(LENGTH(content)) FROM conversation_messages),
0
)
""")
return {
'total_users': stats_row['total_users'],
'total_ais': stats_row['total_ais'],
'total_messages': stats_row['total_messages'],
'lines_of_code_generated': total_characters or 0,
'last_updated': stats_row['date'].isoformat()
}
except Exception as e:
self.logger.error(f"Failed to get current stats: {e}")
# Return default stats on error
return {
'total_users': 0,
'total_ais': 0,
'total_messages': 0,
'lines_of_code_generated': 0,
'last_updated': datetime.now().isoformat()
}
async def save_rag_instance(self, config: RAGConfig, graph_blob_url: str, vector_blob_url: str,
config_blob_url: str, metadata: Dict[str, Any]) -> str:
async with self.pool.acquire() as conn:
rag_instance_id = await conn.fetchval("""
INSERT INTO rag_instances (
ai_type, user_id, ai_id, name, description,
graph_blob_url, vector_blob_url, config_blob_url,
total_chunks, total_tokens, file_count,
created_at, updated_at, last_accessed_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, NOW(), NOW(), NOW())
ON CONFLICT (ai_type, user_id, ai_id) DO UPDATE SET
graph_blob_url = EXCLUDED.graph_blob_url,
vector_blob_url = EXCLUDED.vector_blob_url,
config_blob_url = EXCLUDED.config_blob_url,
total_chunks = EXCLUDED.total_chunks,
total_tokens = EXCLUDED.total_tokens,
file_count = EXCLUDED.file_count,
updated_at = NOW()
RETURNING id;
""",
config.ai_type, config.user_id, config.ai_id,
config.name, config.description,
graph_blob_url, vector_blob_url, config_blob_url,
metadata.get('total_chunks', 0),
metadata.get('total_tokens', 0),
metadata.get('file_count', 0)
)
return str(rag_instance_id)
async def cleanup_duplicate_rag_instances(self, ai_type: str, keep_latest: bool = True):
"""Clean up duplicate RAG instances, keeping only the latest one"""
async with self.pool.acquire() as conn:
if keep_latest:
# Keep the latest instance, deactivate others
await conn.execute("""
UPDATE rag_instances
SET status = 'duplicate_cleanup'
WHERE ai_type = $1 AND user_id IS NULL AND ai_id IS NULL
AND status = 'active'
AND id NOT IN (
SELECT id FROM rag_instances
WHERE ai_type = $1 AND user_id IS NULL AND ai_id IS NULL AND status = 'active'
ORDER BY created_at DESC LIMIT 1
)
""", ai_type)
count = await conn.fetchval("""
SELECT COUNT(*) FROM rag_instances
WHERE ai_type = $1 AND status = 'duplicate_cleanup'
""", ai_type)
self.logger.info(f"π§Ή Cleaned up {count} duplicate {ai_type} RAG instances")
# Return the active instance info
active_instance = await conn.fetchrow("""
SELECT id, name, created_at FROM rag_instances
WHERE ai_type = $1 AND user_id IS NULL AND ai_id IS NULL AND status = 'active'
ORDER BY created_at DESC LIMIT 1
""", ai_type)
if active_instance:
self.logger.info(f"β
Active {ai_type} RAG: {active_instance['name']} (ID: {active_instance['id']})")
return active_instance
async def get_rag_instance(self, config: RAGConfig) -> Optional[Dict[str, Any]]:
"""Get RAG instance from database with FIXED cache key matching"""
async with self.pool.acquire() as conn:
# Handle NULL/None matching properly for PostgreSQL
if config.user_id is None and config.ai_id is None:
# System-level RAG (fire-safety, general, etc.)
result = await conn.fetchrow("""
SELECT id, ai_type, user_id, ai_id, name, description,
graph_blob_url, vector_blob_url, config_blob_url,
total_chunks, total_tokens, file_count,
created_at, updated_at, last_accessed_at, status
FROM rag_instances
WHERE ai_type = $1 AND user_id IS NULL AND ai_id IS NULL AND status = 'active'
ORDER BY created_at DESC
LIMIT 1
""", config.ai_type)
elif config.user_id is not None and config.ai_id is None:
# User-specific RAG (user's general AI)
result = await conn.fetchrow("""
SELECT id, ai_type, user_id, ai_id, name, description,
graph_blob_url, vector_blob_url, config_blob_url,
total_chunks, total_tokens, file_count,
created_at, updated_at, last_accessed_at, status
FROM rag_instances
WHERE ai_type = $1 AND user_id = $2 AND ai_id IS NULL AND status = 'active'
ORDER BY created_at DESC
LIMIT 1
""", config.ai_type, config.user_id)
else:
# Custom AI RAG
result = await conn.fetchrow("""
SELECT id, ai_type, user_id, ai_id, name, description,
graph_blob_url, vector_blob_url, config_blob_url,
total_chunks, total_tokens, file_count,
created_at, updated_at, last_accessed_at, status
FROM rag_instances
WHERE ai_type = $1 AND user_id = $2 AND ai_id = $3 AND status = 'active'
ORDER BY created_at DESC
LIMIT 1
""", config.ai_type, config.user_id, config.ai_id)
if result:
# Update last accessed time
await conn.execute("""
UPDATE rag_instances SET last_accessed_at = NOW() WHERE id = $1
""", result['id'])
self.logger.info(f"π― Database lookup SUCCESS: Found {result['name']} (ID: {result['id']})")
return dict(result)
self.logger.info(
f"π Database lookup: No RAG found for ai_type='{config.ai_type}', user_id={config.user_id}, ai_id={config.ai_id}")
return None
async def save_conversation_message(
self,
conversation_id: str,
role: str,
content: str,
metadata: Optional[Dict[str, Any]] = None
) -> str:
"""Save conversation message to database"""
async with self.pool.acquire() as conn:
await conn.execute("""
INSERT INTO conversations (id, user_id, ai_type, ai_id, title)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO NOTHING
""", conversation_id,
metadata.get('user_id', 'anonymous'),
metadata.get('ai_type', 'unknown'),
metadata.get('ai_id'),
metadata.get('title', 'New Conversation')
)
message_id = await conn.fetchval("""
INSERT INTO conversation_messages (conversation_id, role, content, metadata)
VALUES ($1, $2, $3, $4)
RETURNING id
""", conversation_id, role, content, json.dumps(metadata or {}))
return str(message_id)
async def get_conversation_messages(
self,
conversation_id: str,
limit: int = 50
) -> List[Dict[str, Any]]:
"""Get conversation messages from database"""
async with self.pool.acquire() as conn:
messages = await conn.fetch("""
SELECT id, role, content, metadata, created_at
FROM conversation_messages
WHERE conversation_id = $1
ORDER BY created_at DESC
LIMIT $2
""", conversation_id, limit)
return [dict(msg) for msg in reversed(messages)]
async def close(self):
"""Close database connections"""
if self.pool:
await self.pool.close()
if self.redis:
self.redis.close()
class PersistentLightRAGManager:
"""
Complete LightRAG manager with Vercel-only persistence
Zero dependency on HuggingFace ephemeral storage
"""
def __init__(
self,
cloudflare_worker: CloudflareWorker,
database_manager: DatabaseManager,
blob_client: VercelBlobClient
):
self.cloudflare_worker = cloudflare_worker
self.db = database_manager
self.blob_client = blob_client
self.rag_instances: Dict[str, LightRAG] = {}
self.processing_locks: Dict[str, asyncio.Lock] = {}
self.conversation_memory: Dict[str, List[Dict[str, Any]]] = {}
self.logger = logging.getLogger(__name__)
async def get_or_create_rag_instance(self, ai_type: str, user_id: Optional[str] = None, ai_id: Optional[str] = None,
name: Optional[str] = None, description: Optional[str] = None) -> LightRAG:
config = RAGConfig(ai_type=ai_type, user_id=user_id, ai_id=ai_id, name=name or f"{ai_type} AI",
description=description)
cache_key = config.get_cache_key()
if cache_key in self.rag_instances:
self.logger.info(f"Returning cached RAG instance: {cache_key}")
return self.rag_instances[cache_key]
if cache_key not in self.processing_locks:
self.processing_locks[cache_key] = asyncio.Lock()
async with self.processing_locks[cache_key]:
if cache_key in self.rag_instances:
return self.rag_instances[cache_key]
try:
self.logger.info(f"Checking for existing RAG instance: {cache_key}")
instance_data = await self.db.get_rag_instance(config)
if instance_data:
self.logger.info(
f"Found existing RAG instance: {instance_data['name']} (ID: {instance_data['id']})")
async with self.db.pool.acquire() as conn:
storage_check = await conn.fetchrow("""
SELECT filename, file_size, processing_status, token_count
FROM knowledge_files
WHERE rag_instance_id = $1 AND filename = 'lightrag_storage.json'
LIMIT 1
""", instance_data['id'])
if storage_check:
self.logger.info(
f"Found storage data: {storage_check['file_size']} bytes, {storage_check['token_count']} tokens, status: {storage_check['processing_status']}")
rag_instance = await self._load_from_database(config)
if rag_instance:
self.rag_instances[cache_key] = rag_instance
self.logger.info(f"Successfully loaded existing RAG from database: {cache_key}")
return rag_instance
else:
self.logger.error(f"Failed to load RAG from database despite having storage data")
else:
self.logger.warning(f"RAG instance exists but no storage data found")
else:
self.logger.info(f"No existing RAG instance found in database for: {cache_key}")
except Exception as e:
self.logger.error(f"Error checking/loading existing RAG instance: {e}")
self.logger.info(f"Creating new RAG instance: {cache_key}")
rag_instance = await self._create_new_rag_instance(config)
await self._save_to_database(config, rag_instance)
self.rag_instances[cache_key] = rag_instance
return rag_instance
async def _create_new_rag_instance(self, config: RAGConfig) -> LightRAG:
"""Create new RAG instance with CORRECT LightRAG 1.3.7 configuration"""
working_dir = f"/tmp/rag_memory_{config.get_cache_key()}_{uuid.uuid4()}"
os.makedirs(working_dir, exist_ok=True)
# FIXED: Use only valid LightRAG 1.3.7 parameters
rag = LightRAG(
working_dir=working_dir,
max_parallel_insert=1, # Reduce for stability
llm_model_func=self.cloudflare_worker.query,
llm_model_name=self.cloudflare_worker.llm_models[0],
llm_model_max_token_size=4080,
embedding_func=EmbeddingFunc(
embedding_dim=1024,
max_token_size=2048,
func=self.cloudflare_worker.embedding_chunk,
),
graph_storage="NetworkXStorage",
vector_storage="NanoVectorDBStorage",
# REMOVED invalid parameters:
# enable_entity_extraction=True, # NOT VALID IN 1.3.7
# chunk_token_size=1200, # NOT VALID IN 1.3.7
# entity_extract_max_gleaning=1, # NOT VALID IN 1.3.7
# entity_summarization_enabled=True, # NOT VALID IN 1.3.7
)
# Initialize storages
await rag.initialize_storages()
await initialize_pipeline_status()
self.logger.info(f"β
Initialized LightRAG 1.3.7 with working directory: {working_dir}")
# Verify configuration
self.logger.info(f"π§ LightRAG Configuration:")
self.logger.info(f" - Working dir: {rag.working_dir}")
self.logger.info(f" - LLM model: {rag.llm_model_name}")
self.logger.info(f" - Graph storage: {type(rag.graph_storage).__name__}")
self.logger.info(f" - Vector storage: {type(rag.vector_storage).__name__}")
# Initialize pipeline status properly
if not hasattr(rag, 'pipeline_status') or rag.pipeline_status is None:
rag.pipeline_status = {"history_messages": []}
elif "history_messages" not in rag.pipeline_status:
rag.pipeline_status["history_messages"] = []
self.logger.info(f"β
Pipeline status initialized for {config.get_cache_key()}")
# Load knowledge based on AI type
if config.ai_type == "fire-safety":
self.logger.info(f"π₯ Loading fire safety knowledge for {config.get_cache_key()}")
success = await self._load_fire_safety_knowledge(rag)
if success:
# CRITICAL: Wait for entity extraction to complete
self.logger.info("β³ Waiting for entity extraction to complete...")
await asyncio.sleep(10) # Give LightRAG time to process entities
# Check what was actually created
await self._check_storage_contents(rag)
else:
self.logger.warning("β οΈ Fire safety knowledge loading reported failure")
return rag
async def _check_storage_contents(self, rag: LightRAG):
"""Check what was actually stored after document insertion"""
try:
self.logger.info("π Checking storage contents after insertion...")
# Check all storage files
storage_files = {
'vdb_entities.json': 'entities',
'vdb_chunks.json': 'chunks',
'vdb_relationships.json': 'relationships'
}
total_items = 0
for filename, storage_type in storage_files.items():
file_path = f"{rag.working_dir}/{filename}"
if os.path.exists(file_path):
try:
file_size = os.path.getsize(file_path)
with open(file_path, 'r') as f:
data = json.load(f)
item_count = len(data.get('data', []))
has_matrix = bool(data.get('matrix', ''))
total_items += item_count
if item_count > 0:
self.logger.info(
f"β
{storage_type}: {item_count} items, {file_size} bytes, matrix: {has_matrix}")
# Show sample for debugging
if item_count > 0 and len(data['data']) > 0:
sample_item = data['data'][0]
if isinstance(sample_item, dict):
sample_keys = list(sample_item.keys())[:5] # Show first 5 keys
self.logger.info(f" Sample item keys: {sample_keys}")
else:
self.logger.info(f" Sample item type: {type(sample_item)}")
else:
self.logger.warning(f"β οΈ {storage_type}: EMPTY ({file_size} bytes)")
except Exception as e:
self.logger.error(f"β Failed to read {filename}: {e}")
else:
self.logger.warning(f"β οΈ {filename} doesn't exist")
self.logger.info(f"π Total items across all storage: {total_items}")
# Test if entity extraction is working by checking entities specifically
if total_items > 0:
await self._test_entity_extraction_quality(rag)
except Exception as e:
self.logger.error(f"β Storage content check failed: {e}")
async def _test_entity_extraction_quality(self, rag: LightRAG):
"""Test the quality of entity extraction"""
try:
self.logger.info("π§ͺ Testing entity extraction quality...")
# Check entities file specifically
entities_file = f"{rag.working_dir}/vdb_entities.json"
if os.path.exists(entities_file):
with open(entities_file, 'r') as f:
entities_data = json.load(f)
entities_count = len(entities_data.get('data', []))
if entities_count > 0:
self.logger.info(f"β
Found {entities_count} entities")
# Show some sample entities
for i, entity in enumerate(entities_data['data'][:3]): # Show first 3
if isinstance(entity, dict):
entity_name = entity.get('content', entity.get('name', str(entity)))
self.logger.info(f" Entity {i + 1}: {entity_name}")
return True
else:
self.logger.warning("β οΈ No entities found - this will break HYBRID mode")
return False
else:
self.logger.warning("β οΈ Entities file doesn't exist")
return False
except Exception as e:
self.logger.error(f"β Entity extraction test failed: {e}")
return False
async def debug_entity_extraction(self, rag: LightRAG):
"""Debug why entities aren't being extracted"""
try:
self.logger.info("π Debugging entity extraction process...")
# Check if entity extraction is working at all
test_content = """
Fire safety regulations require that all commercial buildings have fire extinguishers.
Emergency exits must be clearly marked with illuminated signs.
Sprinkler systems are mandatory in buildings over 15,000 square feet.
"""
# Try manual entity extraction
try:
# This should trigger entity extraction
await rag.ainsert(test_content)
# Wait for processing
await asyncio.sleep(3)
# Check what was created
entities_file = f"{rag.working_dir}/vdb_entities.json"
relationships_file = f"{rag.working_dir}/vdb_relationships.json"
for file_path in [entities_file, relationships_file]:
if os.path.exists(file_path):
with open(file_path, 'r') as f:
data = json.load(f)
filename = os.path.basename(file_path)
item_count = len(data.get('data', []))
self.logger.info(f"π {filename}: {item_count} items")
if item_count > 0:
# Show sample data
sample = data['data'][0]
self.logger.info(f"π Sample {filename} item: {sample}")
else:
self.logger.warning(f"β οΈ {filename} is still empty after insertion")
else:
self.logger.warning(f"β οΈ {file_path} doesn't exist")
except Exception as e:
self.logger.error(f"β Entity extraction test failed: {e}")
# Check LightRAG configuration
self.logger.info(f"π§ LightRAG config:")
self.logger.info(f" - Working dir: {rag.working_dir}")
self.logger.info(f" - LLM model: {getattr(rag, 'llm_model_name', 'unknown')}")
self.logger.info(f" - Graph storage: {type(rag.graph_storage).__name__}")
self.logger.info(f" - Vector storage: {type(rag.vector_storage).__name__}")
# Check if extraction is enabled
if hasattr(rag, 'enable_entity_extraction'):
self.logger.info(f" - Entity extraction enabled: {rag.enable_entity_extraction}")
return True
except Exception as e:
self.logger.error(f"β Debug entity extraction failed: {e}")
return False
async def validate_extracted_entities(self, rag: LightRAG, original_content: str) -> bool:
"""Validate that extracted entities actually exist in the source content"""
try:
entities_file = f"{rag.working_dir}/vdb_entities.json"
if not os.path.exists(entities_file):
return True # No entities to validate
with open(entities_file, 'r') as f:
entities_data = json.load(f)
entities = entities_data.get('data', [])
invalid_entities = []
valid_entities = []
self.logger.info(f"π Validating {len(entities)} extracted entities against source content...")
for entity in entities:
if isinstance(entity, dict):
entity_name = entity.get('entity_name', '').strip()
# Skip empty or placeholder entities
if not entity_name or entity_name in ['<entity_name>', '', 'Unknown']:
invalid_entities.append(f"Empty/placeholder: '{entity_name}'")
continue
# Check if entity name appears in the original content
if entity_name.lower() in original_content.lower():
valid_entities.append(entity_name)
self.logger.info(f" β
Valid entity: '{entity_name}'")
else:
invalid_entities.append(f"Not found in content: '{entity_name}'")
self.logger.warning(f" β INVALID entity: '{entity_name}' - NOT FOUND in source content!")
self.logger.info(f"π Entity validation results:")
self.logger.info(f" β
Valid entities: {len(valid_entities)}")
self.logger.info(f" β Invalid entities: {len(invalid_entities)}")
if invalid_entities:
self.logger.error(f"π¨ ENTITY HALLUCINATION DETECTED!")
for invalid in invalid_entities[:5]: # Show first 5
self.logger.error(f" {invalid}")
if len(invalid_entities) > 5:
self.logger.error(f" ... and {len(invalid_entities) - 5} more invalid entities")
return False
return True
except Exception as e:
self.logger.error(f"β Entity validation failed: {e}")
return False
async def clean_hallucinated_entities(self, rag: LightRAG, original_content: str):
"""Remove entities that don't exist in the source content"""
try:
entities_file = f"{rag.working_dir}/vdb_entities.json"
if not os.path.exists(entities_file):
return
with open(entities_file, 'r') as f:
entities_data = json.load(f)
original_entities = entities_data.get('data', [])
cleaned_entities = []
removed_count = 0
self.logger.info(f"π§Ή Cleaning hallucinated entities from {len(original_entities)} total entities...")
for entity in original_entities:
if isinstance(entity, dict):
entity_name = entity.get('entity_name', '').strip()
# Remove empty/placeholder entities
if not entity_name or entity_name in ['<entity_name>', '', 'Unknown']:
removed_count += 1
continue
# Remove entities not found in content
if entity_name.lower() not in original_content.lower():
self.logger.warning(f" ποΈ Removing hallucinated entity: '{entity_name}'")
removed_count += 1
continue
# Keep valid entities
cleaned_entities.append(entity)
# Update the entities file with cleaned data
entities_data['data'] = cleaned_entities
with open(entities_file, 'w') as f:
json.dump(entities_data, f)
self.logger.info(f"β
Entity cleaning complete:")
self.logger.info(f" π Original entities: {len(original_entities)}")
self.logger.info(f" ποΈ Removed: {removed_count}")
self.logger.info(f" β
Remaining: {len(cleaned_entities)}")
except Exception as e:
self.logger.error(f"β Entity cleaning failed: {e}")
async def _load_fire_safety_knowledge(self, rag: LightRAG):
"""Load fire safety knowledge with FIXED insertion process"""
self.logger.info(f"π₯ Loading fire safety knowledge for {rag.working_dir}")
# Prepare knowledge content
base_knowledge = """
FIRE SAFETY REGULATIONS AND BUILDING CODES
1. Emergency Exit Requirements:
- All buildings must have at least two exits on each floor
- Maximum travel distance to exit: 75 feet in unsprinklered buildings, 100 feet in sprinklered buildings
- Exit doors must swing in direction of egress travel
- All exits must be clearly marked with illuminated exit signs
- Exit routes must be free of obstructions at all times
- Minimum width for exits: 32 inches for single doors, 64 inches for double doors
2. Fire Extinguisher Requirements:
- Type A: For ordinary combustible materials (wood, paper, cloth, rubber, plastic)
- Type B: For flammable and combustible liquids (gasoline, oil, paint, grease)
- Type C: For energized electrical equipment (motors, generators, switches)
- Type D: For combustible metals (magnesium, titanium, zirconium, lithium)
- Type K: For cooking oils and fats in commercial kitchen equipment
- Distribution: Maximum travel distance of 75 feet to nearest extinguisher
- Inspection: Monthly visual inspections and annual professional service
3. Fire Detection and Alarm Systems:
- Smoke detectors required in all sleeping areas and hallways
- Heat detectors required in areas where smoke detectors unsuitable
- Manual fire alarm pull stations required near all exits
- Central monitoring systems required in commercial buildings over 10,000 sq ft
- Backup power systems required for all alarm components
- Testing schedule: Monthly for batteries, annually for full system
4. Sprinkler System Requirements:
- Required in all buildings over 3 stories or 15,000 sq ft
- Wet pipe systems: Most common, water-filled pipes
- Dry pipe systems: For areas subject to freezing temperatures
- Deluge systems: For high-hazard areas with rapid fire spread potential
- Inspection: Quarterly for valves, annually for full system testing
"""
all_content = [base_knowledge]
# Load additional files if they exist
book_files = ['/app/book.pdf', '/app/book.txt']
for file_path in book_files:
if os.path.exists(file_path):
try:
if file_path.endswith('.pdf'):
try:
import PyPDF2
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page_num in range(min(20, len(pdf_reader.pages))): # Reduced from 50 to 20
page_text = pdf_reader.pages[page_num].extract_text()
if page_text and len(page_text.strip()) > 100:
all_content.append(
f"PDF Page {page_num + 1}: {page_text[:3000]}") # Reduced chunk size
except Exception as e:
self.logger.warning(f"PDF processing failed: {e}")
continue
else:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
txt_content = file.read()
# Split into smaller chunks
for i in range(0, min(len(txt_content), 60000), 3000): # Reduced chunk size and total
chunk = txt_content[i:i + 3000]
if chunk.strip():
all_content.append(f"TXT Section {i // 3000 + 1}: {chunk}")
self.logger.info(f"β
Loaded {file_path}")
except Exception as e:
self.logger.error(f"β Failed to load {file_path}: {e}")
self.logger.info(f"π Starting insertion of {len(all_content)} documents")
# CRITICAL: Insert documents with better error handling and timeout
successful_insertions = 0
for i, content in enumerate(all_content):
try:
self.logger.info(f"π Inserting document {i + 1}/{len(all_content)} ({len(content)} chars)")
entities_before = await self._count_entities(rag)
# Insert with timeout
insertion_task = asyncio.create_task(rag.ainsert(content))
try:
await asyncio.wait_for(insertion_task, timeout=45.0) # 30 second timeout per document
successful_insertions += 1
self.logger.info(f"β
Document {i + 1} inserted successfully")
# Brief pause between insertions
await asyncio.sleep(2)
entities_after = await self._count_entities(rag)
entities_added = entities_after - entities_before
self.logger.info(
f"β
Document {i + 1} inserted - Entities added: {entities_added} (total: {entities_after})")
await asyncio.sleep(1)
except asyncio.TimeoutError:
self.logger.error(f"β° Document {i + 1} insertion timed out after 30 seconds")
insertion_task.cancel()
continue
except Exception as e:
self.logger.error(f"β Failed to insert document {i + 1}: {e}")
continue
self.logger.info(f"π Insertion complete: {successful_insertions}/{len(all_content)} documents successful")
# Force storage verification with timeout
if successful_insertions > 0:
self.logger.info("π Final validation and cleaning...")
await asyncio.sleep(5) # Wait for processing
is_valid = await self.validate_extracted_entities(rag, all_content)
if not is_valid:
self.logger.warning("π§Ή Cleaning hallucinated entities...")
await self.clean_hallucinated_entities(rag, all_content)
# Final verification
final_entities = await self._count_entities(rag)
final_relationships = await self._count_relationships(rag)
final_chunks = await self._count_chunks(rag)
self.logger.info(
f"π Final counts after cleaning: {final_chunks} chunks, {final_entities} entities, {final_relationships} relationships")
if final_entities > 0:
self.logger.info("π Entity extraction SUCCESS - HYBRID mode should work!")
else:
self.logger.warning("β οΈ No entities extracted - HYBRID mode will fail")
# Show cleaned entities
try:
# Check storage files
storage_verified = False
for storage_file in ['vdb_chunks.json', 'vdb_entities.json', 'vdb_relationships.json']:
file_path = f"{rag.working_dir}/{storage_file}"
if os.path.exists(file_path) and os.path.getsize(file_path) > 100:
with open(file_path, 'r') as f:
data = json.load(f)
if data.get('data') and len(data['data']) > 0:
storage_verified = True
self.logger.info(f"β
{storage_file}: {len(data['data'])} items")
if storage_verified:
self.logger.info("π Storage verification PASSED")
else:
self.logger.error("β Storage verification FAILED")
except Exception as e:
self.logger.error(f"β Storage verification error: {e}")
self.logger.info("π Starting entity extraction debugging...")
await self.debug_entity_extraction(rag)
return successful_insertions > 0
async def _count_entities(self, rag: LightRAG) -> int:
"""Count entities in storage"""
try:
entities_file = f"{rag.working_dir}/vdb_entities.json"
if os.path.exists(entities_file):
with open(entities_file, 'r') as f:
data = json.load(f)
return len(data.get('data', []))
return 0
except:
return 0
async def _count_relationships(self, rag: LightRAG) -> int:
"""Count relationships in storage"""
try:
relationships_file = f"{rag.working_dir}/vdb_relationships.json"
if os.path.exists(relationships_file):
with open(relationships_file, 'r') as f:
data = json.load(f)
return len(data.get('data', []))
return 0
except:
return 0
async def _count_chunks(self, rag: LightRAG) -> int:
"""Count chunks in storage"""
try:
chunks_file = f"{rag.working_dir}/vdb_chunks.json"
if os.path.exists(chunks_file):
with open(chunks_file, 'r') as f:
data = json.load(f)
return len(data.get('data', []))
return 0
except:
return 0
# Add this method to your PersistentLightRAGManager class
async def fix_entity_extraction_for_custom_ai(self, rag: LightRAG, content_list: List[str]):
"""Fix entity extraction issues for custom AI"""
try:
self.logger.info("π§ Starting entity extraction fix...")
# Clear existing corrupted data
for storage_file in ['vdb_entities.json', 'vdb_chunks.json', 'vdb_relationships.json']:
file_path = f"{rag.working_dir}/{storage_file}"
if os.path.exists(file_path):
# Backup the file
backup_path = f"{file_path}.backup"
shutil.copy2(file_path, backup_path)
self.logger.info(f"π Backed up {storage_file}")
# Try different LLM models for entity extraction
successful_extractions = 0
for i, content in enumerate(content_list):
if len(content.strip()) < 50: # Skip very short content
continue
try:
self.logger.info(f"π Processing content chunk {i + 1}/{len(content_list)}")
# Use a more explicit prompt for entity extraction
enhanced_prompt = f"""
You are an expert at extracting entities and relationships from text.
Extract entities and relationships from this text and return ONLY the extracted information in the exact format requested.
Text to analyze:
{content[:2000]}
Requirements:
1. Extract ALL important entities (people, organizations, locations, concepts, objects)
2. For each entity provide: name, type (person/organization/geo/event/category), description
3. Extract relationships between entities with descriptions and strength (1-10)
4. Identify high-level keywords that summarize main concepts
5. Ignore all entities and do not add if they are: Alex, Taylor, Jordan, Cruz, The Device.
6. Make sure to double check if this entity is actually in the text or are you just hallucinating it.
Return the results in this exact format:
entity<entity_name><|><entity_type><|><entity_description>
relationship<source_entity><|><target_entity><|><relationship_description><|><keywords><|><strength>
content_keywords<high_level_keywords>
Use ## to separate each item.
"""
# Use timeout for entity extraction
extraction_task = asyncio.create_task(
rag.ainsert(content[:3000]) # Limit content size
)
try:
await asyncio.wait_for(extraction_task, timeout=45.0)
successful_extractions += 1
self.logger.info(f"β
Successfully processed chunk {i + 1}")
# Add delay between insertions
await asyncio.sleep(2)
except asyncio.TimeoutError:
self.logger.warning(f"β° Chunk {i + 1} timed out")
extraction_task.cancel()
continue
except Exception as e:
self.logger.error(f"β Failed to process chunk {i + 1}: {e}")
continue
# Verify the results
await asyncio.sleep(5) # Wait for processing
entities_count = await self._count_entities(rag)
chunks_count = await self._count_chunks(rag)
relationships_count = await self._count_relationships(rag)
self.logger.info(
f"π Extraction results: {chunks_count} chunks, {entities_count} entities, {relationships_count} relationships")
if entities_count > 0:
self.logger.info("π Entity extraction fix SUCCESS!")
return True
else:
self.logger.error("β Entity extraction fix FAILED - no entities found")
return False
except Exception as e:
self.logger.error(f"β Entity extraction fix failed: {e}")
return False
# Also add this helper method
async def force_rebuild_custom_ai(self, ai_id: str, user_id: str):
"""Force rebuild a custom AI from scratch"""
try:
self.logger.info(f"π§ Force rebuilding custom AI: {ai_id}")
# Get the AI details and uploaded files
async with self.db.pool.acquire() as conn:
# Get custom AI info
ai_info = await conn.fetchrow("""
SELECT * FROM rag_instances
WHERE ai_id = $1 AND user_id = $2 AND ai_type = 'custom'
""", ai_id, user_id)
if not ai_info:
self.logger.error(f"β Custom AI not found: {ai_id}")
return False
# Get uploaded files
files = await conn.fetch("""
SELECT content_text, original_name FROM knowledge_files
WHERE rag_instance_id = $1 AND filename != 'lightrag_storage.json'
AND processing_status = 'processed'
""", ai_info['id'])
if not files:
self.logger.error(f"β No files found for custom AI: {ai_id}")
return False
# Create new RAG config
config = RAGConfig(
ai_type="custom",
user_id=user_id,
ai_id=ai_id,
name=ai_info['name'],
description=ai_info['description']
)
# Create fresh RAG instance
rag = await self._create_new_rag_instance(config)
# Re-process all files with fixed entity extraction
content_list = []
for file_record in files:
if file_record['content_text']:
content_list.append(file_record['content_text'])
# Use the fixed entity extraction
success = await self.fix_entity_extraction_for_custom_ai(rag, content_list)
if success:
# Save the rebuilt RAG
await self._save_to_database(config, rag)
# Clear cache
cache_key = config.get_cache_key()
if cache_key in self.rag_instances:
del self.rag_instances[cache_key]
self.logger.info(f"β
Successfully rebuilt custom AI: {ai_id}")
return True
else:
self.logger.error(f"β Failed to rebuild custom AI: {ai_id}")
return False
except Exception as e:
self.logger.error(f"β Force rebuild failed: {e}")
return False
async def _force_storage_to_database(self, rag: LightRAG, rag_instance_id: str):
try:
entities_file = f"{rag.working_dir}/vdb_entities.json"
chunks_file = f"{rag.working_dir}/vdb_chunks.json"
relationships_file = f"{rag.working_dir}/vdb_relationships.json"
storage_data = {}
total_items = 0
storage_files = {
'vdb_entities': entities_file,
'vdb_chunks': chunks_file,
'vdb_relationships': relationships_file
}
for storage_key, file_path in storage_files.items():
if os.path.exists(file_path):
try:
with open(file_path, 'r') as f:
file_data = json.load(f)
if isinstance(file_data, dict) and 'data' in file_data:
item_count = len(file_data.get('data', []))
total_items += item_count
storage_data[storage_key] = file_data
self.logger.info(f"β
Read {storage_key}: {item_count} items")
else:
self.logger.warning(f"β οΈ Invalid format in {file_path}")
storage_data[storage_key] = {"data": [], "matrix": ""}
except Exception as e:
self.logger.error(f"β Failed to read {file_path}: {e}")
storage_data[storage_key] = {"data": [], "matrix": ""}
else:
self.logger.warning(f"β οΈ Storage file not found: {file_path}")
storage_data[storage_key] = {"data": [], "matrix": ""}
# Only proceed if we have some data
if total_items > 0 and storage_data:
try:
async with self.db.pool.acquire() as conn:
# Check if rag_instance exists
instance_exists = await conn.fetchval("""
SELECT COUNT(*) FROM rag_instances WHERE id = $1::uuid
""", rag_instance_id)
if not instance_exists:
self.logger.error(f"β RAG instance {rag_instance_id} does not exist")
return False
# Insert the knowledge file record with complete storage data
await conn.execute("""
INSERT INTO knowledge_files (
id, user_id, rag_instance_id, filename, original_name,
file_type, file_size, blob_url, content_text,
processing_status, token_count, created_at, updated_at
) VALUES (
gen_random_uuid(), 'system', $1::uuid, 'lightrag_storage.json',
'LightRAG Storage Data', 'json', $2, 'database://storage', $3,
'processed', $4, NOW(), NOW()
) ON CONFLICT (rag_instance_id, filename) DO UPDATE SET
content_text = EXCLUDED.content_text,
file_size = EXCLUDED.file_size,
token_count = EXCLUDED.token_count,
updated_at = NOW()
""", rag_instance_id, len(json.dumps(storage_data)), json.dumps(storage_data), total_items)
self.logger.info(
f"β
Stored LightRAG data for instance {rag_instance_id}: {total_items} total items")
# Log detailed breakdown
for key, data in storage_data.items():
item_count = len(data.get('data', []))
self.logger.info(f" - {key}: {item_count} items")
return True
except Exception as e:
self.logger.error(f"β Database storage failed: {e}")
import traceback
self.logger.error(f"Full traceback: {traceback.format_exc()}")
return False
else:
if not storage_data:
self.logger.warning("β οΈ No storage data to save")
else:
self.logger.warning(f"β οΈ No items found in storage data (total: {total_items})")
return False
except Exception as e:
self.logger.error(f"β Failed to store to database: {e}")
import traceback
self.logger.error(f"Full traceback: {traceback.format_exc()}")
return False
async def _wait_for_pipeline_completion(self, rag: LightRAG, doc_name: str, max_wait_time: int = 30):
"""Wait for LightRAG 1.3.7 pipeline to complete processing"""
for attempt in range(max_wait_time):
try:
await asyncio.sleep(1)
if hasattr(rag, 'doc_status') and rag.doc_status:
status_data = await rag.doc_status.get_all()
if status_data:
completed_docs = [doc for doc in status_data if 'completed' in str(doc).lower()]
if completed_docs:
self.logger.info(f"Pipeline processing detected for {doc_name}")
return True
if hasattr(rag.vector_storage, '_data') and rag.vector_storage._data:
data_count = len(rag.vector_storage._data)
if data_count > 0:
self.logger.info(f"Vector storage contains {data_count} items after {doc_name}")
return True
if hasattr(rag, 'chunks') and rag.chunks:
chunks_data = await rag.chunks.get_all()
if chunks_data and len(chunks_data) > 0:
self.logger.info(f"Chunks storage contains {len(chunks_data)} items after {doc_name}")
return True
except Exception as e:
self.logger.debug(f"Pipeline check attempt {attempt + 1} failed: {e}")
continue
self.logger.warning(f"Pipeline completion check timed out for {doc_name}")
return False
async def _verify_knowledge_base_state(self, rag: LightRAG):
"""Verify the final state of the knowledge base"""
try:
storage_stats = {}
if hasattr(rag.vector_storage, '_data'):
storage_stats['vector_items'] = len(rag.vector_storage._data) if rag.vector_storage._data else 0
if hasattr(rag, 'chunks') and rag.chunks:
try:
chunks_data = await rag.chunks.get_all()
storage_stats['chunks'] = len(chunks_data) if chunks_data else 0
except:
storage_stats['chunks'] = 0
if hasattr(rag, 'entities') and rag.entities:
try:
entities_data = await rag.entities.get_all()
storage_stats['entities'] = len(entities_data) if entities_data else 0
except:
storage_stats['entities'] = 0
if hasattr(rag, 'relationships') and rag.relationships:
try:
relationships_data = await rag.relationships.get_all()
storage_stats['relationships'] = len(relationships_data) if relationships_data else 0
except:
storage_stats['relationships'] = 0
self.logger.info(f"Knowledge base state: {storage_stats}")
return any(count > 0 for count in storage_stats.values())
except Exception as e:
self.logger.error(f"Failed to verify knowledge base state: {e}")
return False
def _intelligent_chunk_split(self, content: str, max_chunk_size: int = 8000) -> List[str]:
"""Split content intelligently on sentence and paragraph boundaries"""
if len(content) <= max_chunk_size:
return [content]
chunks = []
current_chunk = ""
paragraphs = content.split('\n\n')
for paragraph in paragraphs:
if len(paragraph) > max_chunk_size:
sentences = paragraph.split('. ')
for sentence in sentences:
if len(current_chunk) + len(sentence) + 2 <= max_chunk_size:
current_chunk += sentence + '. '
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence + '. '
else:
if len(current_chunk) + len(paragraph) + 2 <= max_chunk_size:
current_chunk += paragraph + '\n\n'
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = paragraph + '\n\n'
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def _split_content_into_chunks(self, content: str, max_length: int) -> List[str]:
"""Split content into manageable chunks"""
chunks = []
words = content.split()
current_chunk = []
current_length = 0
for word in words:
if current_length + len(word) + 1 <= max_length:
current_chunk.append(word)
current_length += len(word) + 1
else:
if current_chunk:
chunks.append(' '.join(current_chunk))
current_chunk = [word]
current_length = len(word)
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunks
async def _save_to_database(self, config: RAGConfig, rag: LightRAG):
"""Save RAG instance to Database with CORRECT order of operations"""
try:
self.logger.info("πΎ Starting database save process...")
# STEP 1: Calculate metadata from actual storage files
metadata = await self._calculate_storage_metadata(rag)
# STEP 2: Create empty blob URLs (we're using database storage)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
base_filename = f"rag_{config.ai_type}_{config.user_id or 'system'}_{config.ai_id or 'default'}_{timestamp}"
fake_blob_urls = {
'graph_blob_url': f"database://graph_{base_filename}",
'vector_blob_url': f"database://vector_{base_filename}",
'config_blob_url': f"database://config_{base_filename}"
}
# STEP 3: CREATE the rag_instance record FIRST
rag_instance_id = await self.db.save_rag_instance(
config,
fake_blob_urls['graph_blob_url'],
fake_blob_urls['vector_blob_url'],
fake_blob_urls['config_blob_url'],
metadata
)
self.logger.info(f"β
Created RAG instance in database: {rag_instance_id}")
# STEP 4: NOW store the RAG data (with existing rag_instance_id)
storage_success = await self._force_storage_to_database(rag, str(rag_instance_id))
if storage_success:
self.logger.info(f"β
Successfully saved complete RAG to database: {rag_instance_id}")
else:
self.logger.warning(f"β οΈ RAG instance created but storage data save failed: {rag_instance_id}")
except Exception as e:
self.logger.error(f"β Failed to save RAG to database: {e}")
import traceback
self.logger.error(f"Full traceback: {traceback.format_exc()}")
raise
async def _calculate_storage_metadata(self, rag: LightRAG) -> Dict[str, Any]:
"""Calculate metadata from RAG storage"""
try:
total_chunks = 0
total_tokens = 0
file_count = 0
# Check storage files
for storage_file in ['vdb_chunks.json', 'vdb_entities.json', 'vdb_relationships.json']:
file_path = f"{rag.working_dir}/{storage_file}"
if os.path.exists(file_path):
try:
with open(file_path, 'r') as f:
data = json.load(f)
if data.get('data'):
chunk_count = len(data['data'])
total_chunks += chunk_count
# Estimate tokens (rough calculation)
for item in data['data']:
if isinstance(item, dict) and 'content' in item:
# Rough token estimation: ~4 chars per token
total_tokens += len(str(item['content'])) // 4
file_count += 1
except Exception as e:
self.logger.warning(f"Failed to read {storage_file}: {e}")
return {
'total_chunks': total_chunks,
'total_tokens': max(total_tokens, 100), # Minimum 100 tokens
'file_count': file_count
}
except Exception as e:
self.logger.error(f"Failed to calculate metadata: {e}")
return {
'total_chunks': 0,
'total_tokens': 100,
'file_count': 0
}
async def _load_from_database(self, config: RAGConfig) -> Optional[LightRAG]:
"""Load RAG from database with PROPER NanoVectorDB restoration"""
try:
# Get RAG instance metadata
instance_data = await self.db.get_rag_instance(config)
if not instance_data:
self.logger.info(f"No RAG instance found in database for {config.get_cache_key()}")
return None
self.logger.info(f"π Found RAG instance: {instance_data['name']} (ID: {instance_data['id']})")
# Check if we have storage data in knowledge_files table
async with self.db.pool.acquire() as conn:
storage_record = await conn.fetchrow("""
SELECT content_text, file_size, token_count
FROM knowledge_files
WHERE rag_instance_id = $1 AND filename = 'lightrag_storage.json'
AND processing_status = 'processed'
ORDER BY created_at DESC
LIMIT 1
""", instance_data['id'])
if not storage_record or not storage_record['content_text']:
self.logger.warning(f"β οΈ No storage data found in database for RAG {instance_data['id']}")
return None
self.logger.info(
f"π― Found database storage: {storage_record['file_size']} bytes, {storage_record['token_count']} tokens")
try:
# Parse the JSON storage data
storage_data = json.loads(storage_record['content_text'])
self.logger.info(f"π Parsed storage data with keys: {list(storage_data.keys())}")
# CRITICAL: Check if we have chunks with actual content
chunks_data = storage_data.get('vdb_chunks', {})
if not chunks_data.get('data') or len(chunks_data['data']) == 0:
self.logger.warning("β No chunk data found in storage")
return None
chunk_count = len(chunks_data['data'])
self.logger.info(f"π¦ Found {chunk_count} chunks in storage")
# Create working directory
working_dir = f"/tmp/rag_restored_{uuid.uuid4()}"
os.makedirs(working_dir, exist_ok=True)
# Write storage files with PROPER NanoVectorDB format
for filename, file_data in storage_data.items():
try:
file_path = f"{working_dir}/{filename}.json"
# CRITICAL: Ensure proper NanoVectorDB format
if isinstance(file_data, dict) and 'data' in file_data:
# NanoVectorDB expects the EXACT format from your data
with open(file_path, 'w') as f:
json.dump(file_data, f)
file_size = os.path.getsize(file_path)
self.logger.info(f"β
Wrote {filename}.json: {file_size} bytes")
else:
self.logger.warning(f"β οΈ Skipping {filename}: invalid format")
except Exception as e:
self.logger.error(f"β Failed to write {filename}: {e}")
# Create LightRAG instance
self.logger.info("π Creating LightRAG instance with restored files")
rag = LightRAG(
working_dir=working_dir,
max_parallel_insert=2,
llm_model_func=self.cloudflare_worker.query,
llm_model_name=self.cloudflare_worker.llm_models[0],
llm_model_max_token_size=4080,
embedding_func=EmbeddingFunc(
embedding_dim=1024,
max_token_size=2048,
func=self.cloudflare_worker.embedding_chunk,
),
graph_storage="NetworkXStorage",
vector_storage="NanoVectorDBStorage",
)
# Initialize storages
await rag.initialize_storages()
self.logger.info("π Initialized storages")
# Initialize pipeline status
if not hasattr(rag, 'pipeline_status') or rag.pipeline_status is None:
rag.pipeline_status = {"history_messages": []}
elif "history_messages" not in rag.pipeline_status:
rag.pipeline_status["history_messages"] = []
# CRITICAL: Test with multiple query types
self.logger.info("π§ͺ Testing RAG with comprehensive queries...")
# Test 1: Simple fire safety query
try:
from lightrag import QueryParam
test_response = await rag.aquery(
"What are fire exit requirements?",
QueryParam(mode="hybrid")
)
if test_response and len(test_response.strip()) > 50 and not test_response.startswith("Sorry"):
self.logger.info(f"π SUCCESS: Hybrid query test passed - {len(test_response)} chars")
return rag
else:
self.logger.warning(f"β οΈ Hybrid query failed: '{test_response[:100]}'")
except Exception as e:
self.logger.error(f"β Hybrid query test failed: {e}")
# Test 2: Try local mode
try:
local_response = await rag.aquery("fire safety", QueryParam(mode="local"))
if local_response and len(local_response.strip()) > 20 and not local_response.startswith("Sorry"):
self.logger.info(f"β
LOCAL query worked: {local_response[:100]}...")
return rag
except Exception as e:
self.logger.error(f"β Local query failed: {e}")
# Test 3: Try naive mode
try:
naive_response = await rag.aquery("fire", QueryParam(mode="naive"))
if naive_response and len(naive_response.strip()) > 10 and not naive_response.startswith("Sorry"):
self.logger.info(f"β
NAIVE query worked: {naive_response[:100]}...")
return rag
except Exception as e:
self.logger.error(f"β Naive query failed: {e}")
# If all queries fail, return None
self.logger.error("β ALL query tests failed - RAG is not functional")
return None
except json.JSONDecodeError as e:
self.logger.error(f"β Failed to parse JSON storage data: {e}")
return None
except Exception as e:
self.logger.error(f"β Failed to restore from database storage: {e}")
import traceback
self.logger.error(f"Full traceback: {traceback.format_exc()}")
return None
except Exception as e:
self.logger.error(f"β Database loading failed: {e}")
return None
async def _verify_rag_storage(self, rag: LightRAG) -> bool:
"""Verify that RAG storage has been properly loaded with actual data"""
try:
# Check vector storage
vector_count = 0
if hasattr(rag.vector_storage, '_data') and rag.vector_storage._data:
vector_count = len(rag.vector_storage._data)
# Check chunks storage
chunks_count = 0
if hasattr(rag, 'chunks') and rag.chunks:
try:
chunks_data = await rag.chunks.get_all()
chunks_count = len(chunks_data) if chunks_data else 0
except:
pass
# Check entities storage
entities_count = 0
if hasattr(rag, 'entities') and rag.entities:
try:
entities_data = await rag.entities.get_all()
entities_count = len(entities_data) if entities_data else 0
except:
pass
# Check relationships storage
relationships_count = 0
if hasattr(rag, 'relationships') and rag.relationships:
try:
relationships_data = await rag.relationships.get_all()
relationships_count = len(relationships_data) if relationships_data else 0
except:
pass
self.logger.info(
f"π RAG storage verification: vectors={vector_count}, chunks={chunks_count}, entities={entities_count}, relationships={relationships_count}")
# Consider RAG loaded if ANY storage has data
has_data = vector_count > 0 or chunks_count > 0 or entities_count > 0 or relationships_count > 0
if has_data:
self.logger.info("β
RAG verification PASSED - has working data")
else:
self.logger.warning("β RAG verification FAILED - no data found")
return has_data
except Exception as e:
self.logger.error(f"Failed to verify RAG storage: {e}")
return False
async def _serialize_rag_state(self, rag: LightRAG) -> Dict[str, Any]:
"""Serialize RAG state for storage in Vercel Blob + Database"""
try:
rag_state = {
'graph': {},
'vectors': {},
'config': {}
}
# Serialize graph storage (NetworkX)
if hasattr(rag, 'graph_storage') and rag.graph_storage:
try:
# Get the NetworkX graph data
if hasattr(rag.graph_storage, '_graph'):
import networkx as nx
graph_data = nx.node_link_data(rag.graph_storage._graph)
rag_state['graph'] = graph_data
self.logger.info(
f"π Serialized graph: {len(graph_data.get('nodes', []))} nodes, {len(graph_data.get('links', []))} edges")
else:
rag_state['graph'] = {}
except Exception as e:
self.logger.warning(f"Failed to serialize graph storage: {e}")
rag_state['graph'] = {}
# Serialize vector storage (NanoVectorDB)
if hasattr(rag, 'vector_storage') and rag.vector_storage:
try:
vectors_data = {
'embeddings': [],
'metadata': [],
'config': {
'embedding_dim': getattr(rag.vector_storage, 'embedding_dim', 1024),
'metric': getattr(rag.vector_storage, 'metric', 'cosine')
}
}
# Get vector data
if hasattr(rag.vector_storage, '_data') and rag.vector_storage._data:
vectors_data['embeddings'] = rag.vector_storage._data.tolist() if hasattr(
rag.vector_storage._data, 'tolist') else list(rag.vector_storage._data)
if hasattr(rag.vector_storage, '_metadata') and rag.vector_storage._metadata:
vectors_data['metadata'] = rag.vector_storage._metadata
rag_state['vectors'] = vectors_data
self.logger.info(f"π Serialized vectors: {len(vectors_data['embeddings'])} embeddings")
except Exception as e:
self.logger.warning(f"Failed to serialize vector storage: {e}")
rag_state['vectors'] = {'embeddings': [], 'metadata': [], 'config': {}}
# Serialize configuration and metadata
rag_state['config'] = {
'working_dir': rag.working_dir,
'llm_model_name': getattr(rag, 'llm_model_name', ''),
'llm_model_max_token_size': getattr(rag, 'llm_model_max_token_size', 4080),
'graph_storage_type': 'NetworkXStorage',
'vector_storage_type': 'NanoVectorDBStorage',
'embedding_dim': 1024,
'created_at': datetime.now().isoformat()
}
# Add pipeline status if available
if hasattr(rag, 'pipeline_status') and rag.pipeline_status:
rag_state['config']['pipeline_status'] = rag.pipeline_status
self.logger.info(f"β
Successfully serialized RAG state")
return rag_state
except Exception as e:
self.logger.error(f"Failed to serialize RAG state: {e}")
# Return minimal state to avoid complete failure
return {
'graph': {},
'vectors': {'embeddings': [], 'metadata': [], 'config': {}},
'config': {
'working_dir': getattr(rag, 'working_dir', '/tmp/unknown'),
'created_at': datetime.now().isoformat()
}
}
async def _deserialize_rag_state(self, rag_state: Dict[str, Any], working_dir: str) -> LightRAG:
"""Deserialize RAG state from Vercel Blob storage"""
try:
# Create new RAG instance
rag = LightRAG(
working_dir=working_dir,
max_parallel_insert=2,
llm_model_func=self.cloudflare_worker.query,
llm_model_name=self.cloudflare_worker.llm_models[0],
llm_model_max_token_size=4080,
embedding_func=EmbeddingFunc(
embedding_dim=1024,
max_token_size=2048,
func=self.cloudflare_worker.embedding_chunk,
),
graph_storage="NetworkXStorage",
vector_storage="NanoVectorDBStorage",
)
# Initialize storages
await rag.initialize_storages()
# Restore graph data
if rag_state.get('graph') and hasattr(rag, 'graph_storage'):
try:
import networkx as nx
graph_data = rag_state['graph']
if graph_data and 'nodes' in graph_data:
restored_graph = nx.node_link_graph(graph_data)
rag.graph_storage._graph = restored_graph
self.logger.info(f"π Restored graph: {len(graph_data.get('nodes', []))} nodes")
except Exception as e:
self.logger.warning(f"Failed to restore graph: {e}")
# Restore vector data
if rag_state.get('vectors') and hasattr(rag, 'vector_storage'):
try:
vectors_data = rag_state['vectors']
if vectors_data.get('embeddings'):
embeddings = np.array(vectors_data['embeddings'])
rag.vector_storage._data = embeddings
if vectors_data.get('metadata'):
rag.vector_storage._metadata = vectors_data['metadata']
self.logger.info(f"π Restored vectors: {len(vectors_data.get('embeddings', []))} embeddings")
except Exception as e:
self.logger.warning(f"Failed to restore vectors: {e}")
# Restore configuration
if rag_state.get('config'):
config = rag_state['config']
if config.get('pipeline_status'):
rag.pipeline_status = config['pipeline_status']
# Ensure pipeline status is initialized
if not hasattr(rag, 'pipeline_status') or rag.pipeline_status is None:
rag.pipeline_status = {"history_messages": []}
self.logger.info("β
Successfully deserialized RAG state")
return rag
except Exception as e:
self.logger.error(f"Failed to deserialize RAG state: {e}")
raise
async def _estimate_tokens(self, rag_state: Dict[str, Any]) -> int:
"""Estimate token count from RAG state"""
try:
token_count = 0
# Count tokens from vector embeddings
if rag_state.get('vectors', {}).get('embeddings'):
embeddings = rag_state['vectors']['embeddings']
token_count += len(embeddings) * 10 # Rough estimate: 10 tokens per embedding
# Count tokens from graph nodes
if rag_state.get('graph', {}).get('nodes'):
nodes = rag_state['graph']['nodes']
token_count += len(nodes) * 5 # Rough estimate: 5 tokens per node
# Count tokens from graph edges
if rag_state.get('graph', {}).get('links'):
links = rag_state['graph']['links']
token_count += len(links) * 3 # Rough estimate: 3 tokens per edge
return max(token_count, 100) # Minimum 100 tokens
except Exception as e:
self.logger.warning(f"Failed to estimate tokens: {e}")
return 100
async def query_with_memory(
self,
ai_type: str,
question: str,
conversation_id: str,
user_id: str,
ai_id: Optional[str] = None,
mode: str = "hybrid"
) -> str:
"""Query RAG with conversation memory"""
try:
# Get or create RAG instance
rag_instance = await self.get_or_create_rag_instance(
ai_type=ai_type,
user_id=user_id if ai_type == "custom" else None,
ai_id=ai_id,
name=f"{ai_type.title()} AI",
description=f"AI assistant for {ai_type}"
)
# Save user message to database
await self.db.save_conversation_message(
conversation_id, "user", question, {
"user_id": user_id,
"ai_type": ai_type,
"ai_id": ai_id
}
)
# Query RAG with LightRAG QueryParam
from lightrag import QueryParam
response = await rag_instance.aquery(question, QueryParam(mode=mode))
# Save assistant response to database
await self.db.save_conversation_message(
conversation_id, "assistant", response, {
"mode": mode,
"ai_type": ai_type,
"ai_id": ai_id,
"user_id": user_id
}
)
return response
except Exception as e:
self.logger.error(f"Query with memory failed: {e}")
# Fallback to direct Cloudflare query
fallback_response = await self.cloudflare_worker.query(
question,
f"You are a helpful {ai_type} AI assistant."
)
# Save fallback response
await self.db.save_conversation_message(
conversation_id, "assistant", fallback_response, {
"mode": "fallback",
"ai_type": ai_type,
"user_id": user_id,
"error": str(e)
}
)
return fallback_response
async def _load_from_blob_storage(self, instance_data: Dict[str, Any]) -> Optional[LightRAG]:
"""Load RAG from Vercel Blob storage (fallback method)"""
try:
self.logger.info("π Loading RAG from Vercel Blob storage")
# Download RAG state from Vercel Blob
self.logger.info("π₯ Downloading RAG state from Vercel Blob...")
graph_data = await self.blob_client.get(instance_data['graph_blob_url'])
vector_data = await self.blob_client.get(instance_data['vector_blob_url'])
config_data = await self.blob_client.get(instance_data['config_blob_url'])
# Decompress and deserialize
graph_state = pickle.loads(gzip.decompress(graph_data))
vector_state = pickle.loads(gzip.decompress(vector_data))
config_state = pickle.loads(gzip.decompress(config_data))
rag_state = {
'graph': graph_state,
'vectors': vector_state,
'config': config_state
}
self.logger.info("β
Successfully downloaded and deserialized RAG state")
# Create working directory
working_dir = f"/tmp/rag_restored_{uuid.uuid4()}"
os.makedirs(working_dir, exist_ok=True)
# Deserialize RAG instance
rag = await self._deserialize_rag_state(rag_state, working_dir)
return rag
except Exception as e:
self.logger.error(f"β Failed to load RAG from Vercel Blob: {e}")
return None
async def test_model_entity_extraction(self):
"""Test different models to see which extracts entities best"""
test_content = """
Fire extinguishers are required in commercial buildings. Type A fire extinguishers are used for ordinary combustible materials like wood and paper. Emergency exits must be clearly marked with illuminated exit signs. Sprinkler systems are mandatory in buildings over 15,000 square feet. Building codes require fire-resistant construction materials.
"""
results = {}
for i, model in enumerate(self.llm_models[:5]): # Test top 5 models
try:
self.logger.info(f"π§ͺ Testing entity extraction with {model}")
# Temporarily switch to this model
original_index = self.current_llm_index
self.current_llm_index = i
# Test entity extraction
response = await self.query(
f"Extract all important technical entities, concepts, and objects from this text. List each entity with a brief description:\n\n{test_content}",
"You are an expert at identifying technical entities and concepts in specialized documents."
)
# Count how many entities it found (rough estimate)
entity_count = response.count('\n') if response else 0
results[model] = {
"response_length": len(response) if response else 0,
"estimated_entities": entity_count,
"response_preview": response[:200] if response else "No response"
}
self.logger.info(
f" π {model}: {entity_count} estimated entities, {len(response) if response else 0} chars")
# Restore original index
self.current_llm_index = original_index
except Exception as e:
results[model] = {"error": str(e)}
self.logger.error(f" β {model} failed: {e}")
# Find the best model
best_model = None
best_score = 0
for model, result in results.items():
if "error" not in result:
score = result.get("estimated_entities", 0) + (result.get("response_length", 0) // 100)
if score > best_score:
best_score = score
best_model = model
if best_model:
self.logger.info(f"π Best model for entity extraction: {best_model}")
# Switch to the best model
self.current_llm_index = self.llm_models.index(best_model)
return results
# Global instance
lightrag_manager: Optional[PersistentLightRAGManager] = None
# Replace the initialize_lightrag_manager function with correct logger usage
async def initialize_lightrag_manager() -> PersistentLightRAGManager:
"""Initialize with OPTIMIZED models for entity extraction"""
global lightrag_manager
if lightrag_manager is None:
# Get logger for this function
func_logger = logging.getLogger(__name__)
# Validate environment
validate_environment()
# Get environment variables
cloudflare_api_key = os.getenv("CLOUDFLARE_API_KEY")
cloudflare_account_id = os.getenv("CLOUDFLARE_ACCOUNT_ID")
database_url = os.getenv("DATABASE_URL")
redis_url = os.getenv("REDIS_URL")
blob_token = os.getenv("BLOB_READ_WRITE_TOKEN")
# Initialize Cloudflare worker with BEST models
api_base_url = f"https://api.cloudflare.com/client/v4/accounts/{cloudflare_account_id}/ai/run/"
cloudflare_worker = CloudflareWorker(
cloudflare_api_key=cloudflare_api_key,
api_base_url=api_base_url,
llm_model_name="@cf/meta/llama-3.1-8b-instruct", # Start with BEST model
embedding_model_name="@cf/baai/bge-large-en-v1.5" # Start with BEST embedding
)
# Test the enhanced model
func_logger.info("π§ͺ Testing enhanced model configuration...")
try:
test_response = await cloudflare_worker.query(
"Extract entities from: Fire extinguishers are required in commercial buildings.",
"You are an expert at identifying technical entities and concepts."
)
func_logger.info(f"β
Model test successful: {test_response[:100]}...")
except Exception as e:
func_logger.warning(f"β οΈ Model test failed: {e}")
# Initialize database manager
db_manager = DatabaseManager(database_url, redis_url)
await db_manager.connect()
# Initialize blob client
blob_client = VercelBlobClient(blob_token)
# Create manager
lightrag_manager = PersistentLightRAGManager(
cloudflare_worker, db_manager, blob_client
)
return lightrag_manager
def get_lightrag_manager() -> Optional[PersistentLightRAGManager]:
"""Get the current LightRAG manager instance"""
return lightrag_manager |