File size: 102,866 Bytes
979853c | 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 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 | #!/usr/bin/env bash
set -euo pipefail
if [[ -z "${BASH_VERSINFO+x}" || "${BASH_VERSINFO[0]}" -lt 4 ]]; then
echo "Error: scripts/setup/setup.sh requires Bash 4 or newer." >&2
echo "Hint: install a newer bash and run it via 'bash scripts/setup/setup.sh ...'." >&2
exit 1
fi
SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="$SCRIPT_DIR/lib"
# shellcheck disable=SC2034
TEMPLATES_DIR="$SCRIPT_DIR/templates"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
declare -A ENV_VALUES
declare -A ORIGINAL_ENV_VALUES
declare -A COMPOSE_ENV_OVERRIDES
declare -A COMPOSE_REWRITE_SERVICE_SET
declare -A REQUIRED_DB_TYPES
declare -A DOCKER_SERVICE_SET
declare -A EXISTING_MANAGED_ROOT_SERVICE_SET
declare -a DOCKER_SERVICES
SSL_CERT_SOURCE_PATH=""
SSL_KEY_SOURCE_PATH=""
LIGHTRAG_COMPOSE_SERVER_PORT_MAPPING=""
NORMALIZED_SERVER_HOST_FOR_COMPOSE=""
FORCE_REWRITE_COMPOSE="no"
DEBUG="${DEBUG:-false}"
PRESET_VLLM_EMBEDDING=(
"EMBEDDING_BINDING=openai"
"EMBEDDING_BINDING_HOST=http://localhost:8001/v1"
"EMBEDDING_MODEL=BAAI/bge-m3"
"EMBEDDING_DIM=1024"
"VLLM_EMBED_MODEL=BAAI/bge-m3"
"VLLM_EMBED_PORT=8001"
"VLLM_EMBED_DEVICE=cpu"
)
PRESET_VLLM_RERANKER=(
"RERANK_BINDING=cohere"
"LIGHTRAG_SETUP_RERANK_PROVIDER=vllm"
"RERANK_MODEL=BAAI/bge-reranker-v2-m3"
"RERANK_BINDING_HOST=http://localhost:8000/rerank"
"VLLM_RERANK_MODEL=BAAI/bge-reranker-v2-m3"
"VLLM_RERANK_PORT=8000"
"VLLM_RERANK_DEVICE=cpu"
)
VLLM_SERVICES=(
"vllm-embed"
"vllm-rerank"
)
STORAGE_SERVICES=(
"postgres"
"neo4j"
"mongodb"
"redis"
"milvus"
"qdrant"
"memgraph"
"opensearch"
)
DEFAULT_RUNTIME_TARGET="host"
COMPOSE_LIGHTRAG_WORKING_DIR="/app/data/rag_storage"
COMPOSE_LIGHTRAG_INPUT_DIR="/app/data/inputs"
# shellcheck disable=SC2034
COLOR_RESET=""
COLOR_BOLD=""
COLOR_BLUE=""
COLOR_GREEN=""
COLOR_YELLOW=""
# shellcheck disable=SC2034
COLOR_RED=""
# shellcheck disable=SC1091
source "$LIB_DIR/storage_requirements.sh"
# shellcheck disable=SC1091
source "$LIB_DIR/validation.sh"
# shellcheck disable=SC1091
source "$LIB_DIR/prompts.sh"
# shellcheck disable=SC1091
source "$LIB_DIR/file_ops.sh"
# shellcheck disable=SC1091
source "$LIB_DIR/presets.sh"
init_colors() {
if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
COLOR_RESET=$'\033[0m'
COLOR_BOLD=$'\033[1m'
COLOR_BLUE=$'\033[34m'
COLOR_GREEN=$'\033[32m'
COLOR_YELLOW=$'\033[33m'
# shellcheck disable=SC2034
COLOR_RED=$'\033[31m'
fi
}
reset_state() {
ENV_VALUES=()
ORIGINAL_ENV_VALUES=()
COMPOSE_ENV_OVERRIDES=()
COMPOSE_REWRITE_SERVICE_SET=()
REQUIRED_DB_TYPES=()
DOCKER_SERVICE_SET=()
EXISTING_MANAGED_ROOT_SERVICE_SET=()
DOCKER_SERVICES=()
SSL_CERT_SOURCE_PATH=""
SSL_KEY_SOURCE_PATH=""
LIGHTRAG_COMPOSE_SERVER_PORT_MAPPING=""
NORMALIZED_SERVER_HOST_FOR_COMPOSE=""
}
validate_runtime_target() {
local runtime_target="${1:-$DEFAULT_RUNTIME_TARGET}"
case "$runtime_target" in
host|compose)
return 0
;;
*)
format_error \
"Invalid LIGHTRAG_RUNTIME_TARGET: ${runtime_target}" \
"Use 'host' or 'compose', or rerun the setup wizard to regenerate .env."
return 1
;;
esac
}
set_runtime_target() {
local runtime_target="${1:-$DEFAULT_RUNTIME_TARGET}"
if ! validate_runtime_target "$runtime_target"; then
return 1
fi
ENV_VALUES["LIGHTRAG_RUNTIME_TARGET"]="$runtime_target"
}
clear_deprecated_vllm_dtype_state() {
unset 'ENV_VALUES[VLLM_EMBED_DTYPE]'
unset 'ENV_VALUES[VLLM_RERANK_DTYPE]'
}
normalize_vllm_rerank_binding_state() {
if [[ "${ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]:-}" == "vllm" ]]; then
ENV_VALUES["RERANK_BINDING"]="cohere"
fi
}
load_existing_env_if_present() {
local env_file="${REPO_ROOT}/.env"
if [[ -f "$env_file" ]]; then
log_debug "Loading existing .env defaults from $env_file"
load_env_file "$env_file"
clear_deprecated_vllm_dtype_state
normalize_vllm_rerank_binding_state
if [[ "${ENV_VALUES[SSL]:-false}" == "true" ]]; then
SSL_CERT_SOURCE_PATH="${ENV_VALUES[SSL_CERTFILE]:-}"
SSL_KEY_SOURCE_PATH="${ENV_VALUES[SSL_KEYFILE]:-}"
fi
snapshot_original_env_values
fi
}
snapshot_original_env_values() {
local key
ORIGINAL_ENV_VALUES=()
for key in "${!ENV_VALUES[@]}"; do
ORIGINAL_ENV_VALUES["$key"]="${ENV_VALUES[$key]}"
done
}
prepare_compose_output_from_existing() {
local output_file="$1"
local existing_file="$2"
if [[ -z "$existing_file" || "$existing_file" == "$output_file" || -f "$output_file" ]]; then
return 0
fi
if ! cp "$existing_file" "$output_file"; then
format_error "Failed to prepare compose output at ${output_file}" \
"Check file permissions and available disk space, then rerun setup."
return 1
fi
log_success "Using ${existing_file} as merge input for ${output_file}"
}
log_debug() {
if [[ "$DEBUG" == "true" ]]; then
echo "${COLOR_YELLOW}[debug]${COLOR_RESET} $*"
fi
}
log_info() {
echo "${COLOR_BLUE}${COLOR_BOLD}$*${COLOR_RESET}"
}
log_warn() {
echo "${COLOR_YELLOW}$*${COLOR_RESET}"
}
log_success() {
echo "${COLOR_GREEN}$*${COLOR_RESET}"
}
log_step() {
echo "${COLOR_BLUE}${COLOR_BOLD}$*${COLOR_RESET}"
}
normalize_loopback_uri_for_compose() {
local uri="$1"
if [[ "$uri" =~ ^([a-zA-Z][a-zA-Z0-9+.-]*://)([^/?#]+@)?(localhost|127\.0\.0\.1|0\.0\.0\.0)([/:?].*)?$ ]]; then
printf '%s%shost.docker.internal%s' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[4]}"
return 0
fi
printf '%s' "$uri"
}
normalize_mongodb_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^mongodb://([^/?#]+@)?(mongodb|localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?([/?#].*)?$ ]]; then
printf 'mongodb://localhost:27017%s' "${BASH_REMATCH[4]:-/}"
return 0
fi
printf '%s' "$uri"
}
normalize_neo4j_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^([a-zA-Z][a-zA-Z0-9+.-]*://)([^/?#]+@)?(neo4j|localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?([/?#].*)?$ ]]; then
printf '%s%slocalhost:7687%s' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[5]}"
return 0
fi
printf '%s' "$uri"
}
normalize_redis_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^rediss?://([^/?#]+@)?(redis|localhost|127\.0\.0\.1|0\.0\.0\.0)(:([0-9]+))?(/.*)?$ ]]; then
printf 'redis://localhost:6379%s' "${BASH_REMATCH[5]:-/}"
return 0
fi
printf '%s' "$uri"
}
normalize_milvus_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^(https?://)([^/?#]+@)?(milvus|localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?([/?#].*)?$ ]]; then
printf '%slocalhost:19530%s' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[5]}"
return 0
fi
printf '%s' "$uri"
}
normalize_qdrant_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^(https?://)([^/?#]+@)?(qdrant|localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?([/?#].*)?$ ]]; then
printf '%slocalhost:6333%s' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[5]}"
return 0
fi
printf '%s' "$uri"
}
normalize_memgraph_uri_for_local_service() {
local uri="$1"
if [[ "$uri" =~ ^(bolt://)([^/?#]+@)?(memgraph|localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?([/?#].*)?$ ]]; then
printf 'bolt://localhost:7687%s' "${BASH_REMATCH[5]}"
return 0
fi
printf '%s' "$uri"
}
normalize_loopback_host_for_compose() {
local host="$1"
if [[ "$host" == "localhost" || "$host" == "127.0.0.1" || "$host" == "0.0.0.0" ]]; then
printf 'host.docker.internal'
return 0
fi
printf '%s' "$host"
}
normalize_opensearch_hosts_for_compose() {
local hosts="$1"
local entry=""
local trimmed=""
local normalized_entry=""
local -a raw_entries=()
local -a normalized_entries=()
IFS=',' read -r -a raw_entries <<< "$hosts"
for entry in "${raw_entries[@]}"; do
trimmed="${entry#"${entry%%[![:space:]]*}"}"
trimmed="${trimmed%"${trimmed##*[![:space:]]}"}"
# OPENSEARCH_HOSTS is intentionally limited to bare host[:port] entries.
# TLS is configured separately via OPENSEARCH_USE_SSL, so scheme-bearing
# URLs are rejected during validation rather than normalized here.
normalized_entry="$trimmed"
if [[ "$trimmed" =~ ^(localhost|127\.0\.0\.1|0\.0\.0\.0)(:[0-9]+)?$ ]]; then
normalized_entry="host.docker.internal${BASH_REMATCH[2]}"
fi
normalized_entries+=("$normalized_entry")
done
(
IFS=','
printf '%s' "${normalized_entries[*]}"
)
}
env_value_is_true() {
local value="${1:-}"
case "${value,,}" in
true|1|yes)
return 0
;;
*)
return 1
;;
esac
}
normalize_server_host_for_compose() {
# Keep the published bind address/port configurable through compose-time
# variable expansion, while forcing the container itself to listen on the
# internal service defaults.
LIGHTRAG_COMPOSE_SERVER_PORT_MAPPING='${HOST:-0.0.0.0}:${PORT:-9621}:9621'
NORMALIZED_SERVER_HOST_FOR_COMPOSE="0.0.0.0"
}
host_cuda_available() {
command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi >/dev/null 2>&1
}
resolve_local_device_default() {
local configured_device="${1:-}"
if [[ "$configured_device" == "cpu" || "$configured_device" == "cuda" ]]; then
printf '%s' "$configured_device"
return 0
fi
if host_cuda_available; then
printf 'cuda'
else
printf 'cpu'
fi
}
default_loopback_url() {
local port="$1"
local path="${2:-}"
printf 'http://localhost:%s%s' "$port" "$path"
}
uri_points_to_host() {
local uri="$1"
shift
local host=""
local allowed_host
if [[ "$uri" =~ ^[a-zA-Z][a-zA-Z0-9+.-]*://([^/?#]+@)?(\[[^]]+\]|[^/:?#]+) ]]; then
host="${BASH_REMATCH[2]}"
for allowed_host in "$@"; do
if [[ "$host" == "$allowed_host" ]]; then
return 0
fi
done
fi
return 1
}
prefer_local_service_uri() {
local current_uri="$1"
local default_uri="$2"
shift 2
if [[ -z "$current_uri" ]]; then
printf '%s' "$default_uri"
return 0
fi
if uri_points_to_host "$current_uri" "$@"; then
printf '%s' "$current_uri"
return 0
fi
printf '%s' "$default_uri"
}
set_compose_override() {
local key="$1"
local value="${2:-}"
if [[ -n "$value" ]]; then
COMPOSE_ENV_OVERRIDES["$key"]="$value"
else
unset "COMPOSE_ENV_OVERRIDES[$key]"
fi
}
set_managed_service_compose_overrides() {
local root_service="$1"
case "$root_service" in
postgres)
if [[ -z "${COMPOSE_ENV_OVERRIDES[POSTGRES_HOST]+set}" ]]; then
set_compose_override "POSTGRES_HOST" "postgres"
fi
# The bundled postgres compose service always listens on 5432 internally.
if [[ -z "${COMPOSE_ENV_OVERRIDES[POSTGRES_PORT]+set}" ]]; then
set_compose_override "POSTGRES_PORT" "5432"
fi
;;
neo4j)
if [[ -z "${COMPOSE_ENV_OVERRIDES[NEO4J_URI]+set}" ]]; then
set_compose_override "NEO4J_URI" "neo4j://neo4j:7687"
fi
;;
mongodb)
if [[ -z "${COMPOSE_ENV_OVERRIDES[MONGO_URI]+set}" ]]; then
set_compose_override "MONGO_URI" "mongodb://mongodb:27017/"
fi
;;
redis)
if [[ -z "${COMPOSE_ENV_OVERRIDES[REDIS_URI]+set}" ]]; then
set_compose_override "REDIS_URI" "redis://redis:6379"
fi
;;
milvus)
if [[ -z "${COMPOSE_ENV_OVERRIDES[MILVUS_URI]+set}" ]]; then
set_compose_override "MILVUS_URI" "http://milvus:19530"
fi
;;
qdrant)
if [[ -z "${COMPOSE_ENV_OVERRIDES[QDRANT_URL]+set}" ]]; then
set_compose_override "QDRANT_URL" "http://qdrant:6333"
fi
;;
memgraph)
if [[ -z "${COMPOSE_ENV_OVERRIDES[MEMGRAPH_URI]+set}" ]]; then
set_compose_override "MEMGRAPH_URI" "bolt://memgraph:7687"
fi
;;
opensearch)
if [[ -z "${COMPOSE_ENV_OVERRIDES[OPENSEARCH_HOSTS]+set}" ]]; then
set_compose_override "OPENSEARCH_HOSTS" "opensearch:9200"
fi
;;
esac
}
prepare_compose_runtime_overrides() {
local normalized_value
local key
local root_service
# EMBEDDING_BINDING_HOST: when vllm-embed is part of this compose, the LightRAG
# container must reach it by Docker service name, not by a loopback address.
# This applies even when the wizard did not visit the embedding step (e.g.
# env_server_flow), because vllm-embed is detected and added to DOCKER_SERVICE_SET
# before prepare_compose_env_overrides is called.
if [[ -z "${COMPOSE_ENV_OVERRIDES[EMBEDDING_BINDING_HOST]+set}" ]]; then
if [[ -n "${DOCKER_SERVICE_SET[vllm-embed]+set}" ]]; then
set_compose_override "EMBEDDING_BINDING_HOST" \
"http://vllm-embed:${ENV_VALUES[VLLM_EMBED_PORT]:-8001}/v1"
elif [[ -n "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" ]]; then
normalized_value="$(normalize_loopback_uri_for_compose "${ENV_VALUES[EMBEDDING_BINDING_HOST]}")"
if [[ "$normalized_value" != "${ENV_VALUES[EMBEDDING_BINDING_HOST]}" ]]; then
set_compose_override "EMBEDDING_BINDING_HOST" "$normalized_value"
fi
fi
fi
# RERANK_BINDING_HOST: same pattern for vllm-rerank.
if [[ -z "${COMPOSE_ENV_OVERRIDES[RERANK_BINDING_HOST]+set}" ]]; then
if [[ -n "${DOCKER_SERVICE_SET[vllm-rerank]+set}" ]]; then
set_compose_override "RERANK_BINDING_HOST" \
"http://vllm-rerank:${ENV_VALUES[VLLM_RERANK_PORT]:-8000}/rerank"
elif [[ -n "${ENV_VALUES[RERANK_BINDING_HOST]:-}" ]]; then
normalized_value="$(normalize_loopback_uri_for_compose "${ENV_VALUES[RERANK_BINDING_HOST]}")"
if [[ "$normalized_value" != "${ENV_VALUES[RERANK_BINDING_HOST]}" ]]; then
set_compose_override "RERANK_BINDING_HOST" "$normalized_value"
fi
fi
fi
for root_service in postgres neo4j mongodb redis milvus qdrant memgraph opensearch; do
if [[ -n "${DOCKER_SERVICE_SET[$root_service]+set}" ]]; then
set_managed_service_compose_overrides "$root_service"
fi
done
for key in \
"LLM_BINDING_HOST" \
"REDIS_URI" \
"MONGO_URI" \
"NEO4J_URI" \
"MILVUS_URI" \
"QDRANT_URL" \
"MEMGRAPH_URI"; do
if [[ -n "${COMPOSE_ENV_OVERRIDES[$key]+set}" ]]; then
continue
fi
if [[ -n "${ENV_VALUES[$key]:-}" ]]; then
normalized_value="$(normalize_loopback_uri_for_compose "${ENV_VALUES[$key]}")"
if [[ "$normalized_value" != "${ENV_VALUES[$key]}" ]]; then
set_compose_override "$key" "$normalized_value"
fi
fi
done
for key in "OPENSEARCH_HOSTS"; do
if [[ -n "${COMPOSE_ENV_OVERRIDES[$key]+set}" ]]; then
continue
fi
if [[ -n "${ENV_VALUES[$key]:-}" ]]; then
normalized_value="$(normalize_opensearch_hosts_for_compose "${ENV_VALUES[$key]}")"
if [[ "$normalized_value" != "${ENV_VALUES[$key]}" ]]; then
set_compose_override "$key" "$normalized_value"
fi
fi
done
for key in "POSTGRES_HOST"; do
if [[ -n "${COMPOSE_ENV_OVERRIDES[$key]+set}" ]]; then
continue
fi
if [[ -n "${ENV_VALUES[$key]:-}" ]]; then
normalized_value="$(normalize_loopback_host_for_compose "${ENV_VALUES[$key]}")"
if [[ "$normalized_value" != "${ENV_VALUES[$key]}" ]]; then
set_compose_override "$key" "$normalized_value"
fi
fi
done
normalize_server_host_for_compose "${ENV_VALUES[HOST]:-0.0.0.0}"
normalized_value="$NORMALIZED_SERVER_HOST_FOR_COMPOSE"
set_compose_override "HOST" "$normalized_value"
set_compose_override "PORT" "9621"
}
prepare_compose_ssl_overrides() {
local cert_name=""
local key_name=""
if [[ -n "$SSL_CERT_SOURCE_PATH" ]]; then
cert_name="$(resolve_staged_ssl_basename "cert" "$SSL_CERT_SOURCE_PATH" "$SSL_KEY_SOURCE_PATH")"
set_compose_override "SSL_CERTFILE" "/app/data/certs/${cert_name}"
fi
if [[ -n "$SSL_KEY_SOURCE_PATH" ]]; then
key_name="$(resolve_staged_ssl_basename "key" "$SSL_KEY_SOURCE_PATH" "$SSL_CERT_SOURCE_PATH")"
set_compose_override "SSL_KEYFILE" "/app/data/certs/${key_name}"
fi
}
prepare_compose_data_path_overrides() {
# Compose mounts always bind the data directories into these container paths.
# Force lightrag to use them so values from the mounted .env cannot redirect
# storage into a different location.
set_compose_override "WORKING_DIR" "$COMPOSE_LIGHTRAG_WORKING_DIR"
set_compose_override "INPUT_DIR" "$COMPOSE_LIGHTRAG_INPUT_DIR"
}
prepare_compose_env_overrides() {
prepare_compose_data_path_overrides
prepare_compose_runtime_overrides
prepare_compose_ssl_overrides
}
add_docker_service() {
local service="$1"
if [[ -z "${DOCKER_SERVICE_SET[$service]+set}" ]]; then
DOCKER_SERVICE_SET["$service"]=1
DOCKER_SERVICES+=("$service")
fi
}
restore_storage_docker_services_from_env() {
local db_type
local marker_key=""
local service_name=""
local db_types=("postgresql" "neo4j" "mongodb" "redis" "milvus" "qdrant" "memgraph" "opensearch")
for db_type in "${db_types[@]}"; do
marker_key="$(storage_deployment_marker_key "$db_type")"
if [[ -n "$marker_key" && "${ENV_VALUES[$marker_key]:-}" == "docker" ]]; then
service_name="$(storage_service_name_for_db_type "$db_type")"
if [[ -n "$service_name" ]]; then
add_docker_service "$service_name"
fi
fi
done
}
restore_vllm_docker_services_from_env() {
if [[ "${ENV_VALUES[LIGHTRAG_SETUP_EMBEDDING_PROVIDER]:-}" == "vllm" ]]; then
add_docker_service "vllm-embed"
fi
if [[ "${ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]:-}" == "vllm" ]]; then
add_docker_service "vllm-rerank"
fi
}
compose_has_non_wizard_services() {
local compose_file="$1"
local service_name=""
if [[ -z "$compose_file" || ! -f "$compose_file" ]]; then
return 1
fi
while IFS= read -r service_name; do
if [[ -z "$(_managed_service_root_name "$service_name")" ]]; then
return 0
fi
done < <(detect_compose_services "$compose_file")
return 1
}
resolve_compose_output_action() {
local existing_compose="$1"
local -n action_ref="$2"
local -n runtime_target_ref="$3"
local -n host_hint_ref="$4"
local current_target="${ENV_VALUES[LIGHTRAG_RUNTIME_TARGET]:-$DEFAULT_RUNTIME_TARGET}"
action_ref="write_env_only"
runtime_target_ref="$DEFAULT_RUNTIME_TARGET"
host_hint_ref="no"
if ((${#DOCKER_SERVICES[@]} > 0)); then
action_ref="rewrite_compose"
runtime_target_ref="compose"
return 0
fi
if compose_has_non_wizard_services "$existing_compose"; then
action_ref="rewrite_compose"
runtime_target_ref="compose"
return 0
fi
if [[ -n "$existing_compose" ]]; then
if confirm_default_no "All wizard-managed services have been removed. Remove LightRAG from Docker and switch to host mode?"; then
action_ref="delete_compose_and_switch_host"
runtime_target_ref="host"
host_hint_ref="yes"
else
action_ref="rewrite_compose"
runtime_target_ref="compose"
fi
return 0
fi
if [[ "$current_target" == "compose" ]]; then
if confirm_default_yes "Run LightRAG Server via Docker?"; then
action_ref="rewrite_compose"
runtime_target_ref="compose"
else
host_hint_ref="yes"
fi
else
if confirm_default_no "Run LightRAG Server via Docker?"; then
action_ref="rewrite_compose"
runtime_target_ref="compose"
else
host_hint_ref="yes"
fi
fi
}
mark_compose_service_for_rewrite() {
local service="$1"
local root_service=""
root_service="$(_managed_service_root_name "$service")"
if [[ -n "$root_service" ]]; then
COMPOSE_REWRITE_SERVICE_SET["$root_service"]=1
fi
}
record_existing_managed_root_services() {
local compose_file="$1"
local service_name
local root_service
EXISTING_MANAGED_ROOT_SERVICE_SET=()
if [[ -z "$compose_file" || ! -f "$compose_file" ]]; then
return 0
fi
while IFS= read -r service_name; do
root_service="$(_managed_service_root_name "$service_name")"
if [[ -n "$root_service" ]]; then
EXISTING_MANAGED_ROOT_SERVICE_SET["$root_service"]=1
fi
done < <(detect_managed_root_services "$compose_file")
}
backup_existing_compose_if_generating() {
local generate_compose="${1:-no}"
local existing_compose="${2:-}"
local compose_backup_path=""
if [[ "$generate_compose" != "yes" ]]; then
return 0
fi
compose_backup_path="$(backup_compose_file "$existing_compose")" || return 1
if [[ -n "$compose_backup_path" ]]; then
log_success "Backed up existing compose file to $compose_backup_path"
fi
}
backup_existing_compose_for_action() {
local compose_action="${1:-write_env_only}"
local existing_compose="${2:-}"
local compose_backup_path=""
case "$compose_action" in
rewrite_compose|delete_compose_and_switch_host)
;;
*)
return 0
;;
esac
compose_backup_path="$(backup_compose_file "$existing_compose")" || return 1
if [[ -n "$compose_backup_path" ]]; then
log_success "Backed up existing compose file to $compose_backup_path"
fi
}
remove_existing_compose_file() {
local compose_file="${1:-}"
if [[ -z "$compose_file" || ! -f "$compose_file" ]]; then
return 0
fi
if ! rm "$compose_file"; then
format_error "Failed to remove ${compose_file}" \
"Check file permissions, then remove the compose file manually or rerun setup."
return 1
fi
log_success "Removed ${compose_file}"
}
existing_managed_root_service_present() {
local root_service="$1"
[[ -n "${EXISTING_MANAGED_ROOT_SERVICE_SET[$root_service]+set}" ]]
}
env_value_changed_from_original() {
local key="$1"
local missing_marker="__LIGHTRAG_MISSING__"
local current_value="${ENV_VALUES[$key]-$missing_marker}"
local original_value="${ORIGINAL_ENV_VALUES[$key]-$missing_marker}"
[[ "$current_value" != "$original_value" ]]
}
any_env_value_changed_from_original() {
local key
for key in "$@"; do
if env_value_changed_from_original "$key"; then
return 0
fi
done
return 1
}
compose_template_variant_for_service() {
local service="$1"
local snapshot="${2:-current}"
local device=""
case "$service" in
milvus)
if [[ "$snapshot" == "original" ]]; then
device="${ORIGINAL_ENV_VALUES[MILVUS_DEVICE]:-cpu}"
else
device="${ENV_VALUES[MILVUS_DEVICE]:-cpu}"
fi
;;
qdrant)
if [[ "$snapshot" == "original" ]]; then
device="${ORIGINAL_ENV_VALUES[QDRANT_DEVICE]:-cpu}"
else
device="${ENV_VALUES[QDRANT_DEVICE]:-cpu}"
fi
;;
vllm-embed)
if [[ "$snapshot" == "original" ]]; then
device="${ORIGINAL_ENV_VALUES[VLLM_EMBED_DEVICE]:-cpu}"
else
device="${ENV_VALUES[VLLM_EMBED_DEVICE]:-cpu}"
fi
;;
vllm-rerank)
if [[ "$snapshot" == "original" ]]; then
device="${ORIGINAL_ENV_VALUES[VLLM_RERANK_DEVICE]:-cpu}"
else
device="${ENV_VALUES[VLLM_RERANK_DEVICE]:-cpu}"
fi
;;
*)
printf 'default'
return 0
;;
esac
if [[ "$device" == "cuda" ]]; then
printf 'gpu'
else
printf 'cpu'
fi
}
configure_base_compose_rewrites() {
if [[ "$FORCE_REWRITE_COMPOSE" == "yes" ]]; then
return 0
fi
if existing_managed_root_service_present "vllm-embed" && \
[[ -n "${DOCKER_SERVICE_SET[vllm-embed]+set}" ]] && \
[[ "$(compose_template_variant_for_service "vllm-embed" "current")" != \
"$(compose_template_variant_for_service "vllm-embed" "original")" ]]; then
mark_compose_service_for_rewrite "vllm-embed"
fi
if existing_managed_root_service_present "vllm-rerank" && \
[[ -n "${DOCKER_SERVICE_SET[vllm-rerank]+set}" ]] && \
[[ "$(compose_template_variant_for_service "vllm-rerank" "current")" != \
"$(compose_template_variant_for_service "vllm-rerank" "original")" ]]; then
mark_compose_service_for_rewrite "vllm-rerank"
fi
}
configure_storage_compose_rewrites() {
if [[ "$FORCE_REWRITE_COMPOSE" == "yes" ]]; then
return 0
fi
if existing_managed_root_service_present "postgres" && \
[[ -n "${DOCKER_SERVICE_SET[postgres]+set}" ]] && \
any_env_value_changed_from_original "POSTGRES_USER" "POSTGRES_PASSWORD" "POSTGRES_DATABASE"; then
mark_compose_service_for_rewrite "postgres"
fi
if existing_managed_root_service_present "neo4j" && \
[[ -n "${DOCKER_SERVICE_SET[neo4j]+set}" ]] && \
any_env_value_changed_from_original "NEO4J_DATABASE"; then
mark_compose_service_for_rewrite "neo4j"
fi
if existing_managed_root_service_present "milvus" && \
[[ -n "${DOCKER_SERVICE_SET[milvus]+set}" ]] && \
[[ "$(compose_template_variant_for_service "milvus" "current")" != \
"$(compose_template_variant_for_service "milvus" "original")" ]]; then
mark_compose_service_for_rewrite "milvus"
fi
if existing_managed_root_service_present "qdrant" && \
[[ -n "${DOCKER_SERVICE_SET[qdrant]+set}" ]] && \
[[ "$(compose_template_variant_for_service "qdrant" "current")" != \
"$(compose_template_variant_for_service "qdrant" "original")" ]]; then
mark_compose_service_for_rewrite "qdrant"
fi
}
select_storage_backends() {
local deployment_type="$1"
local kv_default="JsonKVStorage"
local vector_default="NanoVectorDBStorage"
local graph_default="NetworkXStorage"
local doc_default="JsonDocStatusStorage"
local kv_storage vector_storage graph_storage doc_storage
if [[ "$deployment_type" == "production" ]]; then
kv_default="PGKVStorage"
vector_default="MilvusVectorDBStorage"
graph_default="Neo4JStorage"
doc_default="PGDocStatusStorage"
fi
kv_default="${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-$kv_default}"
vector_default="${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-$vector_default}"
graph_default="${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]:-$graph_default}"
doc_default="${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]:-$doc_default}"
while true; do
kv_storage="$(prompt_choice "KV storage" "$kv_default" "${KV_STORAGE_OPTIONS[@]}")"
vector_storage="$(prompt_choice "Vector storage" "$vector_default" "${VECTOR_STORAGE_OPTIONS[@]}")"
graph_storage="$(prompt_choice "Graph storage" "$graph_default" "${GRAPH_STORAGE_OPTIONS[@]}")"
doc_storage="$(prompt_choice "Doc status storage" "$doc_default" "${DOC_STATUS_STORAGE_OPTIONS[@]}")"
if check_storage_compatibility "$kv_storage" "$vector_storage" "$graph_storage" "$doc_storage"; then
break
fi
if confirm_default_no "Proceed with these storage selections anyway?"; then
break
fi
done
ENV_VALUES["LIGHTRAG_KV_STORAGE"]="$kv_storage"
ENV_VALUES["LIGHTRAG_VECTOR_STORAGE"]="$vector_storage"
ENV_VALUES["LIGHTRAG_GRAPH_STORAGE"]="$graph_storage"
ENV_VALUES["LIGHTRAG_DOC_STATUS_STORAGE"]="$doc_storage"
for storage in "$kv_storage" "$vector_storage" "$graph_storage" "$doc_storage"; do
if [[ -n "${STORAGE_DB_TYPES[$storage]:-}" ]]; then
REQUIRED_DB_TYPES["${STORAGE_DB_TYPES[$storage]}"]=1
fi
done
}
initialize_default_storage_backends() {
# env-base does not prompt for storage, but its generated .env must remain
# self-consistent for first-run users who have not run env-storage yet.
ENV_VALUES["LIGHTRAG_KV_STORAGE"]="${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-JsonKVStorage}"
ENV_VALUES["LIGHTRAG_VECTOR_STORAGE"]="${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-NanoVectorDBStorage}"
ENV_VALUES["LIGHTRAG_GRAPH_STORAGE"]="${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]:-NetworkXStorage}"
ENV_VALUES["LIGHTRAG_DOC_STATUS_STORAGE"]="${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]:-JsonDocStatusStorage}"
}
storage_service_name_for_db_type() {
local db_type="$1"
case "$db_type" in
postgresql)
printf 'postgres'
;;
neo4j|mongodb|redis|milvus|qdrant|memgraph|opensearch)
printf '%s' "$db_type"
;;
*)
printf ''
;;
esac
}
storage_deployment_marker_key() {
local db_type="$1"
case "$db_type" in
postgresql)
printf 'LIGHTRAG_SETUP_POSTGRES_DEPLOYMENT'
;;
neo4j)
printf 'LIGHTRAG_SETUP_NEO4J_DEPLOYMENT'
;;
mongodb)
printf 'LIGHTRAG_SETUP_MONGODB_DEPLOYMENT'
;;
redis)
printf 'LIGHTRAG_SETUP_REDIS_DEPLOYMENT'
;;
milvus)
printf 'LIGHTRAG_SETUP_MILVUS_DEPLOYMENT'
;;
qdrant)
printf 'LIGHTRAG_SETUP_QDRANT_DEPLOYMENT'
;;
memgraph)
printf 'LIGHTRAG_SETUP_MEMGRAPH_DEPLOYMENT'
;;
opensearch)
printf 'LIGHTRAG_SETUP_OPENSEARCH_DEPLOYMENT'
;;
*)
printf ''
;;
esac
}
storage_default_docker_for_db_type() {
local db_type="$1"
local marker_key
marker_key="$(storage_deployment_marker_key "$db_type")"
if [[ -n "$marker_key" && "${ENV_VALUES[$marker_key]:-}" == "docker" ]]; then
printf 'yes'
else
printf 'no'
fi
}
persist_storage_deployment_choice() {
local db_type="$1"
local deployment_mode="${2:-no}"
local marker_key
marker_key="$(storage_deployment_marker_key "$db_type")"
if [[ -z "$marker_key" ]]; then
return 0
fi
case "$deployment_mode" in
yes|docker)
ENV_VALUES["$marker_key"]="docker"
;;
no|'')
unset "ENV_VALUES[$marker_key]"
;;
*)
ENV_VALUES["$marker_key"]="$deployment_mode"
;;
esac
}
clear_unused_storage_deployment_markers() {
local db_type
for db_type in postgresql neo4j mongodb redis milvus qdrant memgraph opensearch; do
if [[ -z "${REQUIRED_DB_TYPES[$db_type]+set}" ]]; then
persist_storage_deployment_choice "$db_type" "no"
fi
done
}
collect_database_config() {
local db_type="$1"
local default_docker="${2:-no}"
local service_name=""
local deployment_mode="no"
# Storage collector rule for this wizard:
# - Existing ENV_VALUES loaded from .env are user-owned configuration.
# - Collectors should use those values as defaults and preserve them when they
# are already set, even for Docker-managed services.
# - A collector may normalize the stored form, or write a hard default only
# when the key is absent.
# Keep future storage collectors aligned with this behavior so rerunning the
# wizard does not silently erase explicit .env overrides.
case "$db_type" in
postgresql)
collect_postgres_config "$default_docker"
;;
neo4j)
collect_neo4j_config "$default_docker"
;;
mongodb)
collect_mongodb_config "$default_docker"
;;
redis)
collect_redis_config "$default_docker"
;;
milvus)
collect_milvus_config "$default_docker"
;;
qdrant)
collect_qdrant_config "$default_docker"
;;
memgraph)
collect_memgraph_config "$default_docker"
;;
opensearch)
collect_opensearch_config "$default_docker"
;;
*)
echo "Unknown database type: $db_type" >&2
return 1
;;
esac
service_name="$(storage_service_name_for_db_type "$db_type")"
if [[ -n "$service_name" && -n "${DOCKER_SERVICE_SET[$service_name]+set}" ]]; then
deployment_mode="docker"
fi
persist_storage_deployment_choice "$db_type" "$deployment_mode"
}
collect_postgres_config() {
local default_docker="${1:-no}"
local use_docker="no"
local host port user password database
local existing_user="" existing_password="" existing_database=""
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run PostgreSQL locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run PostgreSQL locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "postgres"
host="${ENV_VALUES[POSTGRES_HOST]:-localhost}"
if [[ "$host" != "localhost" && "$host" != "127.0.0.1" && "$host" != "0.0.0.0" && "$host" != "postgres" ]]; then
host="localhost"
elif [[ "$host" == "postgres" ]]; then
host="localhost"
fi
else
host="${ENV_VALUES[POSTGRES_HOST]:-localhost}"
fi
host="$(prompt_with_default "PostgreSQL host" "$host")"
if [[ "$use_docker" == "yes" ]]; then
port="5432"
set_compose_override "POSTGRES_HOST" "postgres"
set_compose_override "POSTGRES_PORT" "5432"
else
port="$(prompt_until_valid "PostgreSQL port" "${ENV_VALUES[POSTGRES_PORT]:-5432}" validate_port)"
set_compose_override "POSTGRES_HOST" ""
set_compose_override "POSTGRES_PORT" ""
fi
existing_user="${ORIGINAL_ENV_VALUES[POSTGRES_USER]-${ENV_VALUES[POSTGRES_USER]:-}}"
existing_password="${ORIGINAL_ENV_VALUES[POSTGRES_PASSWORD]-${ENV_VALUES[POSTGRES_PASSWORD]:-}}"
existing_database="${ORIGINAL_ENV_VALUES[POSTGRES_DATABASE]-${ENV_VALUES[POSTGRES_DATABASE]:-}}"
if [[ "$use_docker" == "yes" && -z "$existing_user" && -z "$existing_password" ]]; then
user="rag"
password="rag"
else
user="$(prompt_with_default "PostgreSQL user" "${existing_user:-rag}")"
password="$(prompt_secret_with_default "PostgreSQL password: " "${existing_password:-rag}")"
fi
if [[ "$use_docker" == "yes" && -z "$existing_database" ]]; then
database="rag"
else
database="$(prompt_with_default "PostgreSQL database" "${existing_database:-lightrag}")"
fi
ENV_VALUES["POSTGRES_HOST"]="$host"
ENV_VALUES["POSTGRES_PORT"]="$port"
ENV_VALUES["POSTGRES_USER"]="$user"
ENV_VALUES["POSTGRES_PASSWORD"]="$password"
ENV_VALUES["POSTGRES_DATABASE"]="$database"
}
collect_neo4j_config() {
local default_docker="${1:-no}"
local use_docker="no"
local uri username password database
local existing_username="" existing_password="" existing_database=""
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run Neo4j locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run Neo4j locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "neo4j"
uri="$(prefer_local_service_uri "${ENV_VALUES[NEO4J_URI]:-}" "neo4j://localhost:7687" "neo4j" "localhost" "127.0.0.1" "0.0.0.0")"
else
uri="${ENV_VALUES[NEO4J_URI]:-neo4j://localhost:7687}"
fi
uri="$(prompt_until_valid "Neo4j URI" "$uri" validate_uri neo4j)"
if [[ "$use_docker" == "yes" ]]; then
uri="$(normalize_neo4j_uri_for_local_service "$uri")"
fi
existing_username="${ORIGINAL_ENV_VALUES[NEO4J_USERNAME]-${ENV_VALUES[NEO4J_USERNAME]:-}}"
existing_password="${ORIGINAL_ENV_VALUES[NEO4J_PASSWORD]-${ENV_VALUES[NEO4J_PASSWORD]:-}}"
existing_database="${ORIGINAL_ENV_VALUES[NEO4J_DATABASE]-${ENV_VALUES[NEO4J_DATABASE]:-}}"
if [[ "$use_docker" == "yes" ]]; then
username="$(prompt_until_valid "Neo4j username" "${existing_username:-neo4j}" validate_non_empty)"
password="$(prompt_secret_until_valid_with_default "Neo4j password: " "${existing_password:-neo4j_password}" validate_non_empty)"
if [[ -n "$existing_database" ]]; then
database="$(prompt_with_default "Neo4j database" "$existing_database")"
else
database="neo4j"
fi
else
username="$(prompt_with_default "Neo4j username" "${existing_username:-neo4j}")"
password="$(prompt_secret_with_default "Neo4j password: " "${existing_password:-neo4j_password}")"
database="$(prompt_with_default "Neo4j database" "${existing_database:-neo4j}")"
fi
ENV_VALUES["NEO4J_URI"]="$uri"
ENV_VALUES["NEO4J_USERNAME"]="$username"
ENV_VALUES["NEO4J_PASSWORD"]="$password"
ENV_VALUES["NEO4J_DATABASE"]="$database"
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "NEO4J_URI" "neo4j://neo4j:7687"
else
set_compose_override "NEO4J_URI" ""
fi
}
collect_mongodb_config() {
local default_docker="${1:-no}"
local use_docker="no"
local uri database
local existing_database=""
local vector_search_required="no"
if [[ "${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-}" == "MongoVectorDBStorage" ]]; then
vector_search_required="yes"
fi
if [[ "$vector_search_required" == "yes" ]]; then
log_warn "MongoVectorDBStorage cannot use the local Docker MongoDB service from this setup wizard."
log_warn "Reason: the bundled local Docker MongoDB service is MongoDB Community Edition, but MongoVectorDBStorage requires Atlas Search / Vector Search support."
log_warn "Provide a MongoDB endpoint that supports Atlas Search / Vector Search, such as MongoDB Atlas or Atlas local."
uri="${ENV_VALUES[MONGO_URI]:-mongodb+srv://cluster.example.mongodb.net/}"
else
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run MongoDB locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run MongoDB locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "mongodb"
uri="$(prefer_local_service_uri "${ENV_VALUES[MONGO_URI]:-}" "mongodb://localhost:27017/" "mongodb" "localhost" "127.0.0.1" "0.0.0.0")"
else
uri="${ENV_VALUES[MONGO_URI]:-mongodb://localhost:27017/}"
fi
fi
if [[ "$vector_search_required" == "yes" ]]; then
uri="$(prompt_until_valid "MongoDB URI (must support Atlas Search / Vector Search)" "$uri" validate_uri mongodb)"
else
uri="$(prompt_until_valid "MongoDB URI" "$uri" validate_uri mongodb)"
fi
if [[ "$use_docker" == "yes" ]]; then
uri="$(normalize_mongodb_uri_for_local_service "$uri")"
fi
existing_database="${ORIGINAL_ENV_VALUES[MONGO_DATABASE]-${ENV_VALUES[MONGO_DATABASE]:-}}"
database="$(prompt_with_default "MongoDB database" "${existing_database:-LightRAG}")"
ENV_VALUES["MONGO_URI"]="$uri"
ENV_VALUES["MONGO_DATABASE"]="$database"
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "MONGO_URI" "mongodb://mongodb:27017/"
else
set_compose_override "MONGO_URI" ""
fi
}
collect_redis_config() {
local default_docker="${1:-no}"
local use_docker="no"
local uri
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run Redis locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run Redis locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "redis"
uri="$(prefer_local_service_uri "${ENV_VALUES[REDIS_URI]:-}" "redis://localhost:6379" "redis" "localhost" "127.0.0.1" "0.0.0.0")"
else
uri="${ENV_VALUES[REDIS_URI]:-redis://localhost:6379}"
fi
uri="$(prompt_until_valid "Redis URI" "$uri" validate_uri redis)"
if [[ "$use_docker" == "yes" ]]; then
uri="$(normalize_redis_uri_for_local_service "$uri")"
fi
ENV_VALUES["REDIS_URI"]="$uri"
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "REDIS_URI" "redis://redis:6379"
else
set_compose_override "REDIS_URI" ""
fi
}
collect_milvus_config() {
local default_docker="${1:-no}"
local use_docker="no"
local uri db_name milvus_device=""
local existing_db_name="" existing_device=""
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run Milvus locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run Milvus locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "milvus"
uri="$(prefer_local_service_uri "${ENV_VALUES[MILVUS_URI]:-}" "http://localhost:19530" "milvus" "localhost" "127.0.0.1" "0.0.0.0")"
else
uri="${ENV_VALUES[MILVUS_URI]:-http://localhost:19530}"
fi
uri="$(prompt_until_valid "Milvus URI" "$uri" validate_uri milvus)"
existing_db_name="${ORIGINAL_ENV_VALUES[MILVUS_DB_NAME]-${ENV_VALUES[MILVUS_DB_NAME]:-}}"
existing_device="${ORIGINAL_ENV_VALUES[MILVUS_DEVICE]-${ENV_VALUES[MILVUS_DEVICE]:-}}"
if [[ "$use_docker" == "yes" ]]; then
milvus_device="$(resolve_local_device_default "$existing_device")"
milvus_device="$(prompt_choice "Milvus device" "$milvus_device" "cpu" "cuda")"
if [[ "$milvus_device" == "cuda" ]] && ! host_cuda_available; then
log_warn "CUDA device selected for Milvus but no NVIDIA driver detected on host."
fi
uri="$(normalize_milvus_uri_for_local_service "$uri")"
if [[ -z "${ENV_VALUES[MINIO_ACCESS_KEY_ID]:-}" ]]; then
ENV_VALUES["MINIO_ACCESS_KEY_ID"]="minioadmin"
fi
if [[ -z "${ENV_VALUES[MINIO_SECRET_ACCESS_KEY]:-}" ]]; then
ENV_VALUES["MINIO_SECRET_ACCESS_KEY"]="minioadmin"
fi
fi
db_name="$(prompt_with_default "Milvus database name" "${existing_db_name:-lightrag}")"
ENV_VALUES["MILVUS_URI"]="$uri"
ENV_VALUES["MILVUS_DB_NAME"]="$db_name"
if [[ -n "$milvus_device" ]]; then
ENV_VALUES["MILVUS_DEVICE"]="$milvus_device"
fi
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "MILVUS_URI" "http://milvus:19530"
else
set_compose_override "MILVUS_URI" ""
fi
}
collect_qdrant_config() {
local default_docker="${1:-no}"
local use_docker="no"
local url qdrant_device=""
local existing_device=""
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run Qdrant locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run Qdrant locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "qdrant"
url="$(prefer_local_service_uri "${ENV_VALUES[QDRANT_URL]:-}" "http://localhost:6333" "qdrant" "localhost" "127.0.0.1" "0.0.0.0")"
else
url="${ENV_VALUES[QDRANT_URL]:-http://localhost:6333}"
fi
url="$(prompt_until_valid "Qdrant URL" "$url" validate_uri qdrant)"
existing_device="${ORIGINAL_ENV_VALUES[QDRANT_DEVICE]-${ENV_VALUES[QDRANT_DEVICE]:-}}"
if [[ "$use_docker" == "yes" ]]; then
qdrant_device="$(resolve_local_device_default "$existing_device")"
qdrant_device="$(prompt_choice "Qdrant device" "$qdrant_device" "cpu" "cuda")"
if [[ "$qdrant_device" == "cuda" ]] && ! host_cuda_available; then
log_warn "CUDA device selected for Qdrant but no NVIDIA driver detected on host."
fi
url="$(normalize_qdrant_uri_for_local_service "$url")"
fi
ENV_VALUES["QDRANT_URL"]="$url"
if [[ -n "$qdrant_device" ]]; then
ENV_VALUES["QDRANT_DEVICE"]="$qdrant_device"
fi
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "QDRANT_URL" "http://qdrant:6333"
else
set_compose_override "QDRANT_URL" ""
fi
}
collect_memgraph_config() {
local default_docker="${1:-no}"
local use_docker="no"
local uri
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run Memgraph locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run Memgraph locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "memgraph"
uri="$(prefer_local_service_uri "${ENV_VALUES[MEMGRAPH_URI]:-}" "bolt://localhost:7687" "memgraph" "localhost" "127.0.0.1" "0.0.0.0")"
else
uri="${ENV_VALUES[MEMGRAPH_URI]:-bolt://localhost:7687}"
fi
uri="$(prompt_until_valid "Memgraph URI" "$uri" validate_uri memgraph)"
if [[ "$use_docker" == "yes" ]]; then
uri="$(normalize_memgraph_uri_for_local_service "$uri")"
fi
ENV_VALUES["MEMGRAPH_URI"]="$uri"
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "MEMGRAPH_URI" "bolt://memgraph:7687"
else
set_compose_override "MEMGRAPH_URI" ""
fi
}
collect_opensearch_config() {
local default_docker="${1:-no}"
local use_docker="no"
local hosts user password
local existing_user="" existing_password=""
local existing_use_ssl="" existing_verify_certs=""
local existing_num_shards="" existing_num_replicas=""
local use_ssl="true"
local verify_certs="false"
local use_ssl_default="yes"
local verify_certs_default="no"
if [[ "$default_docker" == "yes" ]]; then
if confirm_default_yes "Run OpenSearch locally via Docker?"; then
use_docker="yes"
fi
else
if confirm_default_no "Run OpenSearch locally via Docker?"; then
use_docker="yes"
fi
fi
if [[ "$use_docker" == "yes" ]]; then
add_docker_service "opensearch"
hosts="$(prefer_local_service_uri "${ENV_VALUES[OPENSEARCH_HOSTS]:-}" "localhost:9200" "opensearch" "localhost" "127.0.0.1" "0.0.0.0")"
else
hosts="${ENV_VALUES[OPENSEARCH_HOSTS]:-localhost:9200}"
fi
existing_user="${ORIGINAL_ENV_VALUES[OPENSEARCH_USER]-${ENV_VALUES[OPENSEARCH_USER]:-}}"
existing_password="${ORIGINAL_ENV_VALUES[OPENSEARCH_PASSWORD]-${ENV_VALUES[OPENSEARCH_PASSWORD]:-}}"
existing_use_ssl="${ORIGINAL_ENV_VALUES[OPENSEARCH_USE_SSL]-${ENV_VALUES[OPENSEARCH_USE_SSL]:-}}"
existing_verify_certs="${ORIGINAL_ENV_VALUES[OPENSEARCH_VERIFY_CERTS]-${ENV_VALUES[OPENSEARCH_VERIFY_CERTS]:-}}"
existing_num_shards="${ORIGINAL_ENV_VALUES[OPENSEARCH_NUMBER_OF_SHARDS]-${ENV_VALUES[OPENSEARCH_NUMBER_OF_SHARDS]:-}}"
existing_num_replicas="${ORIGINAL_ENV_VALUES[OPENSEARCH_NUMBER_OF_REPLICAS]-${ENV_VALUES[OPENSEARCH_NUMBER_OF_REPLICAS]:-}}"
hosts="$(prompt_until_valid "OpenSearch hosts (host:port, comma-separated)" "$hosts" validate_opensearch_hosts_format)"
user="$(prompt_with_default "OpenSearch user" "${existing_user:-admin}")"
password="$(prompt_secret_until_valid_with_default "OpenSearch password: " "${existing_password:-LightRAG2026_!@}" validate_opensearch_password_strength)"
if [[ "$use_docker" == "yes" ]]; then
if [[ -n "$existing_use_ssl" ]]; then
env_value_is_true "$existing_use_ssl" && use_ssl="true" || use_ssl="false"
else
use_ssl="true"
fi
verify_certs="false"
else
if [[ -n "$existing_use_ssl" ]] && ! env_value_is_true "$existing_use_ssl"; then
use_ssl_default="no"
fi
if [[ "$use_ssl_default" == "yes" ]]; then
confirm_default_yes "Use SSL for OpenSearch?" && use_ssl="true" || use_ssl="false"
else
confirm_default_no "Use SSL for OpenSearch?" && use_ssl="true" || use_ssl="false"
fi
if [[ "$use_ssl" == "true" ]]; then
if [[ -n "$existing_verify_certs" ]] && env_value_is_true "$existing_verify_certs"; then
verify_certs_default="yes"
fi
if [[ "$verify_certs_default" == "yes" ]]; then
confirm_default_yes "Verify OpenSearch TLS certificates?" && verify_certs="true" || verify_certs="false"
else
confirm_default_no "Verify OpenSearch TLS certificates?" && verify_certs="true" || verify_certs="false"
fi
fi
fi
local num_shards num_replicas
if [[ "$use_docker" == "yes" ]]; then
num_shards="1"
num_replicas="0"
else
num_shards="$(prompt_until_valid "Number of index shards" "${existing_num_shards:-1}" validate_positive_integer)"
num_replicas="$(prompt_until_valid "Number of index replicas (use 2 for 3-AZ clusters)" "${existing_num_replicas:-0}" validate_non_negative_integer)"
fi
ENV_VALUES["OPENSEARCH_HOSTS"]="$hosts"
ENV_VALUES["OPENSEARCH_USER"]="$user"
ENV_VALUES["OPENSEARCH_PASSWORD"]="$password"
ENV_VALUES["OPENSEARCH_USE_SSL"]="$use_ssl"
ENV_VALUES["OPENSEARCH_VERIFY_CERTS"]="$verify_certs"
ENV_VALUES["OPENSEARCH_NUMBER_OF_SHARDS"]="$num_shards"
ENV_VALUES["OPENSEARCH_NUMBER_OF_REPLICAS"]="$num_replicas"
if [[ "$use_docker" == "yes" ]]; then
set_compose_override "OPENSEARCH_HOSTS" "opensearch:9200"
else
set_compose_override "OPENSEARCH_HOSTS" ""
fi
}
clear_bedrock_credentials() {
unset 'ENV_VALUES[AWS_ACCESS_KEY_ID]'
unset 'ENV_VALUES[AWS_SECRET_ACCESS_KEY]'
unset 'ENV_VALUES[AWS_SESSION_TOKEN]'
unset 'ENV_VALUES[AWS_REGION]'
}
bedrock_binding_in_use() {
[[ "${ENV_VALUES[LLM_BINDING]:-}" == "aws_bedrock" ||
"${ENV_VALUES[EMBEDDING_BINDING]:-}" == "aws_bedrock" ]]
}
clear_bedrock_credentials_if_unused() {
if ! bedrock_binding_in_use; then
clear_bedrock_credentials
fi
}
collect_bedrock_credentials() {
local access_key secret_key session_token region
log_info "Bedrock uses the AWS credential chain instead of LLM_BINDING_API_KEY/EMBEDDING_BINDING_API_KEY."
if [[ -n "${ENV_VALUES[AWS_ACCESS_KEY_ID]:-}" && -n "${ENV_VALUES[AWS_SECRET_ACCESS_KEY]:-}" ]]; then
if confirm_default_yes "Reuse existing AWS Bedrock credentials?"; then
region="$(prompt_with_default "AWS region" "${ENV_VALUES[AWS_REGION]:-us-east-1}")"
ENV_VALUES["AWS_REGION"]="$region"
return 0
fi
fi
if confirm_default_no "Store explicit AWS Bedrock credentials in .env?"; then
access_key="$(prompt_required_secret "AWS access key ID: ")"
secret_key="$(prompt_required_secret "AWS secret access key: ")"
session_token="$(mask_sensitive_input "AWS session token (optional): ")"
region="$(prompt_with_default "AWS region" "${ENV_VALUES[AWS_REGION]:-us-east-1}")"
ENV_VALUES["AWS_ACCESS_KEY_ID"]="$access_key"
ENV_VALUES["AWS_SECRET_ACCESS_KEY"]="$secret_key"
ENV_VALUES["AWS_REGION"]="$region"
if [[ -n "$session_token" ]]; then
ENV_VALUES["AWS_SESSION_TOKEN"]="$session_token"
else
unset 'ENV_VALUES[AWS_SESSION_TOKEN]'
fi
return 0
fi
log_info "Using the ambient AWS credential chain (for example IAM roles, AWS profiles, or aws sso login)."
clear_bedrock_credentials
region="$(prompt_clearable_with_default "AWS region (optional)" "${ENV_VALUES[AWS_REGION]:-}")"
apply_clearable_env_value "AWS_REGION" "$region"
}
store_optional_env_value() {
local key="$1"
local value="${2:-}"
if [[ -n "$value" ]]; then
ENV_VALUES["$key"]="$value"
else
unset "ENV_VALUES[$key]"
fi
}
provider_default_or_existing() {
local selected_binding="$1"
local existing_binding="${2:-}"
local existing_value="${3:-}"
local default_value="${4:-}"
if [[ "$selected_binding" == "$existing_binding" && -n "$existing_value" ]]; then
printf '%s' "$existing_value"
return 0
fi
printf '%s' "$default_value"
}
default_llm_model_for_binding() {
local binding="$1"
case "$binding" in
openai|azure_openai)
printf 'gpt-5-mini'
;;
ollama|lollms|openai-ollama)
printf 'mistral-nemo:latest'
;;
gemini)
printf 'gemini-flash-latest'
;;
aws_bedrock)
printf 'anthropic.claude-3-5-sonnet-20241022-v2:0'
;;
*)
printf 'gpt-5-mini'
;;
esac
}
default_embedding_model_for_binding() {
local binding="$1"
case "$binding" in
openai|azure_openai)
printf 'text-embedding-3-large'
;;
ollama)
printf 'bge-m3:latest'
;;
jina)
printf 'jina-embeddings-v4'
;;
gemini)
printf 'gemini-embedding-001'
;;
aws_bedrock)
printf 'amazon.titan-embed-text-v2:0'
;;
lollms)
printf 'lollms_embedding_model'
;;
*)
printf 'text-embedding-3-large'
;;
esac
}
default_embedding_dim_for_binding() {
local binding="$1"
case "$binding" in
openai|azure_openai)
printf '3072'
;;
ollama|aws_bedrock|lollms)
printf '1024'
;;
jina)
printf '2048'
;;
gemini)
printf '1536'
;;
*)
printf '3072'
;;
esac
}
collect_llm_config() {
local options=("openai" "azure_openai" "ollama" "openai-ollama" "lollms" "gemini" "aws_bedrock")
local current_binding="${ENV_VALUES[LLM_BINDING]:-openai}"
local binding model model_default host host_default api_key
binding="$(prompt_choice "LLM provider" "$current_binding" "${options[@]}")"
model_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_MODEL]:-}" "$(default_llm_model_for_binding "$binding")")"
model="$(prompt_with_default "LLM model" "$model_default")"
case "$binding" in
ollama)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "$(default_loopback_url 11434)")"
host="$(prompt_with_default "Ollama host" "$host_default")"
api_key=""
;;
openai-ollama)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "$(default_loopback_url 11434 "/v1")")"
host="$(prompt_with_default "OpenAI-compatible Ollama endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "LLM API key: " "${ENV_VALUES[LLM_BINDING_API_KEY]:-}" validate_api_key openai)"
;;
lollms)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "http://localhost:9600")"
host="$(prompt_with_default "LoLLMs host" "$host_default")"
api_key=""
;;
azure_openai)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "https://example.openai.azure.com/")"
host="$(prompt_with_default "Azure OpenAI endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Azure OpenAI API key: " "${ENV_VALUES[LLM_BINDING_API_KEY]:-}" validate_api_key azure_openai)"
;;
gemini)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "https://generativelanguage.googleapis.com")"
host="$(prompt_with_default "Gemini endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Gemini API key: " "${ENV_VALUES[LLM_BINDING_API_KEY]:-}" validate_api_key gemini)"
;;
aws_bedrock)
host="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "https://bedrock.amazonaws.com")"
api_key=""
collect_bedrock_credentials
;;
*)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[LLM_BINDING_HOST]:-}" "https://api.openai.com/v1")"
host="$(prompt_with_default "LLM endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "LLM API key: " "${ENV_VALUES[LLM_BINDING_API_KEY]:-}" validate_api_key "$binding")"
;;
esac
ENV_VALUES["LLM_BINDING"]="$binding"
ENV_VALUES["LLM_MODEL"]="$model"
ENV_VALUES["LLM_BINDING_HOST"]="$host"
store_optional_env_value "LLM_BINDING_API_KEY" "$api_key"
clear_bedrock_credentials_if_unused
}
collect_embedding_config() {
local options=("openai" "azure_openai" "ollama" "jina" "lollms" "gemini" "aws_bedrock")
local current_binding="${ENV_VALUES[EMBEDDING_BINDING]:-openai}"
local binding model model_default host host_default api_key dim dim_default
if [[ "${ENV_VALUES[LLM_BINDING]:-}" == "openai-ollama" ]]; then
binding="ollama"
if [[ "$current_binding" != "ollama" ]]; then
log_info "openai-ollama uses Ollama embeddings. Forcing embedding provider to ollama."
fi
else
binding="$(prompt_choice "Embedding provider" "$current_binding" "${options[@]}")"
fi
model_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_MODEL]:-}" "$(default_embedding_model_for_binding "$binding")")"
dim_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_DIM]:-}" "$(default_embedding_dim_for_binding "$binding")")"
model="$(prompt_with_default "Embedding model" "$model_default")"
dim="$(prompt_with_default "Embedding dimension" "$dim_default")"
local llm_host_fallback="" llm_api_key_default=""
if [[ "$binding" == "${ENV_VALUES[LLM_BINDING]:-}" ]]; then
llm_host_fallback="${ENV_VALUES[LLM_BINDING_HOST]:-}"
llm_api_key_default="${ENV_VALUES[LLM_BINDING_API_KEY]:-}"
fi
case "$binding" in
ollama)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-$(default_loopback_url 11434)}")"
host="$(prompt_with_default "Ollama embedding host" "$host_default")"
api_key=""
;;
lollms)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-http://localhost:9600}")"
host="$(prompt_with_default "LoLLMs embedding host" "$host_default")"
api_key=""
;;
azure_openai)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-https://example.openai.azure.com/}")"
host="$(prompt_with_default "Azure OpenAI endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Azure OpenAI API key: " "${ENV_VALUES[EMBEDDING_BINDING_API_KEY]:-$llm_api_key_default}" validate_api_key azure_openai)"
;;
gemini)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-https://generativelanguage.googleapis.com}")"
host="$(prompt_with_default "Gemini endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Gemini API key: " "${ENV_VALUES[EMBEDDING_BINDING_API_KEY]:-$llm_api_key_default}" validate_api_key gemini)"
;;
aws_bedrock)
host="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-https://bedrock.amazonaws.com}")"
api_key=""
collect_bedrock_credentials
;;
jina)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-https://api.jina.ai/v1/embeddings}")"
host="$(prompt_with_default "Jina endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Jina API key: " "${ENV_VALUES[EMBEDDING_BINDING_API_KEY]:-$llm_api_key_default}" validate_api_key jina)"
;;
*)
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}" "${llm_host_fallback:-https://api.openai.com/v1}")"
host="$(prompt_with_default "Embedding endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Embedding API key: " "${ENV_VALUES[EMBEDDING_BINDING_API_KEY]:-$llm_api_key_default}" validate_api_key "$binding")"
;;
esac
ENV_VALUES["EMBEDDING_BINDING"]="$binding"
ENV_VALUES["EMBEDDING_MODEL"]="$model"
ENV_VALUES["EMBEDDING_DIM"]="$dim"
ENV_VALUES["EMBEDDING_BINDING_HOST"]="$host"
store_optional_env_value "EMBEDDING_BINDING_API_KEY" "$api_key"
clear_bedrock_credentials_if_unused
# User chose a remote provider — clear the Docker deployment marker.
unset 'ENV_VALUES[LIGHTRAG_SETUP_EMBEDDING_PROVIDER]'
}
collect_rerank_config() {
# Pass "yes" to skip the "Enable reranking?" prompt (caller already asked it).
# The optional second argument is retained for caller compatibility.
local skip_enable_check="${1:-no}"
local _docker_choice_override="${2:-prompt}"
local options=("cohere" "jina" "aliyun")
local current_binding="${ENV_VALUES[RERANK_BINDING]:-cohere}"
local binding model host api_key
local default_model="" default_host="" model_default="" host_default=""
local previous_provider="${ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]:-}"
unset 'ENV_VALUES[VLLM_RERANK_DTYPE]'
if [[ "$skip_enable_check" != "yes" ]]; then
local rerank_was_enabled="no"
if [[ -n "${ENV_VALUES[RERANK_BINDING]:-}" && "${ENV_VALUES[RERANK_BINDING]}" != "null" ]]; then
rerank_was_enabled="yes"
fi
local rerank_enabled="no"
if [[ "$rerank_was_enabled" == "yes" ]]; then
confirm_default_yes "Enable reranking?" && rerank_enabled="yes"
else
confirm_default_no "Enable reranking?" && rerank_enabled="yes"
fi
if [[ "$rerank_enabled" != "yes" ]]; then
ENV_VALUES["RERANK_BINDING"]="null"
unset 'ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]'
return
fi
fi
if [[ "$current_binding" == "null" ]]; then
current_binding="cohere"
fi
binding="$(prompt_choice "Rerank provider" "$current_binding" "${options[@]}")"
case "$binding" in
cohere)
default_model="rerank-v3.5"
default_host="https://api.cohere.com/v2/rerank"
;;
jina)
default_model="jina-reranker-v2-base-multilingual"
default_host="https://api.jina.ai/v1/rerank"
;;
aliyun)
default_model="gte-rerank-v2"
default_host="https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
;;
*)
default_model=""
default_host=""
;;
esac
if [[ "$previous_provider" == "vllm" ]]; then
# Switching away from local vLLM should replace stale localhost/model values.
model_default="$default_model"
host_default="$default_host"
else
model_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[RERANK_MODEL]:-}" "$default_model")"
host_default="$(provider_default_or_existing "$binding" "$current_binding" "${ENV_VALUES[RERANK_BINDING_HOST]:-}" "$default_host")"
fi
model="$(prompt_with_default "Rerank model" "$model_default")"
host="$(prompt_with_default "Rerank endpoint" "$host_default")"
api_key="$(prompt_secret_until_valid_with_default "Rerank API key: " "${ENV_VALUES[RERANK_BINDING_API_KEY]:-}" validate_api_key "$binding")"
ENV_VALUES["RERANK_BINDING"]="$binding"
# Only env_base_flow's Docker branch should keep the local vLLM setup marker.
unset 'ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]'
if [[ -n "$model" ]]; then
ENV_VALUES["RERANK_MODEL"]="$model"
fi
if [[ -n "$host" ]]; then
ENV_VALUES["RERANK_BINDING_HOST"]="$host"
fi
store_optional_env_value "RERANK_BINDING_API_KEY" "$api_key"
}
collect_server_config() {
local host port title description summary_language
host="$(prompt_with_default "Server host" "${ENV_VALUES[HOST]:-0.0.0.0}")"
port="$(prompt_until_valid "Server port" "${ENV_VALUES[PORT]:-9621}" validate_port)"
title="$(prompt_with_default "WebUI title" "${ENV_VALUES[WEBUI_TITLE]:-My Graph KB}")"
description="$(prompt_with_default "WebUI description" "${ENV_VALUES[WEBUI_DESCRIPTION]:-Simple and Fast Graph Based RAG System}")"
summary_language="$(prompt_with_default "Summary language" "${ENV_VALUES[SUMMARY_LANGUAGE]:-English}")"
ENV_VALUES["HOST"]="$host"
ENV_VALUES["PORT"]="$port"
ENV_VALUES["WEBUI_TITLE"]="$title"
ENV_VALUES["WEBUI_DESCRIPTION"]="$description"
ENV_VALUES["SUMMARY_LANGUAGE"]="$summary_language"
}
collect_ssl_config() {
local cert key
local ssl_enabled_default="no"
case "${ENV_VALUES[SSL]:-}" in
true|TRUE|True|1|yes|YES|Yes|y|Y|on|ON|On|t|T)
ssl_enabled_default="yes"
;;
esac
if [[ "$ssl_enabled_default" == "yes" ]]; then
if ! confirm_default_yes "Enable SSL/TLS for the API server?"; then
unset 'ENV_VALUES[SSL]'
unset 'ENV_VALUES[SSL_CERTFILE]'
unset 'ENV_VALUES[SSL_KEYFILE]'
SSL_CERT_SOURCE_PATH=""
SSL_KEY_SOURCE_PATH=""
return
fi
else
if ! confirm_default_no "Enable SSL/TLS for the API server?"; then
unset 'ENV_VALUES[SSL]'
unset 'ENV_VALUES[SSL_CERTFILE]'
unset 'ENV_VALUES[SSL_KEYFILE]'
SSL_CERT_SOURCE_PATH=""
SSL_KEY_SOURCE_PATH=""
return
fi
fi
cert="$(prompt_until_valid "SSL certificate file" "${ENV_VALUES[SSL_CERTFILE]:-}" validate_existing_file)"
key="$(prompt_until_valid "SSL key file" "${ENV_VALUES[SSL_KEYFILE]:-}" validate_existing_file)"
ENV_VALUES["SSL"]="true"
ENV_VALUES["SSL_CERTFILE"]="$cert"
ENV_VALUES["SSL_KEYFILE"]="$key"
SSL_CERT_SOURCE_PATH="$cert"
SSL_KEY_SOURCE_PATH="$key"
}
collect_security_config() {
local required="${1:-no}"
local default_yes="${2:-no}"
local auth_accounts token_secret token_expire api_key whitelist
local confirm_result=1
local whitelist_default=""
local whitelist_is_set="no"
if [[ -n "${ENV_VALUES[WHITELIST_PATHS]+set}" ]]; then
whitelist_default="${ENV_VALUES[WHITELIST_PATHS]}"
whitelist_is_set="yes"
fi
if [[ "$default_yes" == "yes" ]]; then
if confirm_default_yes "Configure authentication and API key settings?"; then
confirm_result=0
fi
else
if confirm_default_no "Configure authentication and API key settings?"; then
confirm_result=0
fi
fi
if ((confirm_result != 0)); then
if [[ "$required" == "yes" ]]; then
echo "Warning: production deployments should configure AUTH_ACCOUNTS; API keys are optional on top." >&2
fi
return
fi
echo "Press Enter to keep an existing value. Type 'clear' to remove it." >&2
if [[ "$whitelist_is_set" == "no" ]]; then
whitelist_default="/health"
elif [[ "$required" == "yes" && "$whitelist_default" == "/health,/api/*" ]]; then
whitelist_default="/health"
fi
auth_accounts="$(prompt_clearable_with_default "Auth accounts (user:pass,comma-separated)" "${ENV_VALUES[AUTH_ACCOUNTS]:-}")"
token_secret="$(prompt_clearable_secret_with_default "JWT token secret: " "${ENV_VALUES[TOKEN_SECRET]:-}")"
token_expire="$(prompt_clearable_with_default "Token expire hours" "${ENV_VALUES[TOKEN_EXPIRE_HOURS]:-48}")"
api_key="$(prompt_clearable_secret_with_default "LightRAG API key: " "${ENV_VALUES[LIGHTRAG_API_KEY]:-}")"
whitelist="$(prompt_clearable_with_default "Whitelist paths (comma-separated)" "$whitelist_default")"
if [[ "$whitelist_is_set" == "yes" && -z "$whitelist_default" && -z "$whitelist" ]]; then
whitelist="$CLEAR_INPUT_SENTINEL"
fi
if [[ -z "$token_secret" ]]; then
token_secret="$(openssl rand -hex 32 2>/dev/null || LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 64)"
log_info "Generated TOKEN_SECRET and saved to .env."
fi
apply_clearable_env_value "AUTH_ACCOUNTS" "$auth_accounts"
apply_clearable_env_value "TOKEN_SECRET" "$token_secret"
apply_clearable_env_value "TOKEN_EXPIRE_HOURS" "$token_expire"
apply_clearable_env_value "LIGHTRAG_API_KEY" "$api_key"
apply_clearable_env_value "WHITELIST_PATHS" "$whitelist" "empty"
}
apply_clearable_env_value() {
local key="$1"
local value="${2:-}"
local clear_mode="${3:-unset}"
if [[ "$clear_mode" == "empty" && "$value" == "$CLEAR_INPUT_SENTINEL" ]]; then
ENV_VALUES["$key"]=""
return 0
fi
if [[ "$value" == "$CLEAR_INPUT_SENTINEL" || -z "$value" ]]; then
unset "ENV_VALUES[$key]"
return 0
fi
ENV_VALUES["$key"]="$value"
}
collect_observability_config() {
local secret_key public_key host
if ! confirm_default_no "Enable Langfuse observability?"; then
unset 'ENV_VALUES[LANGFUSE_ENABLE_TRACE]'
unset 'ENV_VALUES[LANGFUSE_SECRET_KEY]'
unset 'ENV_VALUES[LANGFUSE_PUBLIC_KEY]'
unset 'ENV_VALUES[LANGFUSE_HOST]'
return
fi
secret_key="$(prompt_secret_until_valid_with_default "Langfuse secret key: " "${ENV_VALUES[LANGFUSE_SECRET_KEY]:-}" validate_api_key langfuse)"
public_key="$(prompt_secret_until_valid_with_default "Langfuse public key: " "${ENV_VALUES[LANGFUSE_PUBLIC_KEY]:-}" validate_api_key langfuse)"
host="$(prompt_with_default "Langfuse host" "${ENV_VALUES[LANGFUSE_HOST]:-https://cloud.langfuse.com}")"
if [[ -n "$secret_key" ]]; then
ENV_VALUES["LANGFUSE_SECRET_KEY"]="$secret_key"
fi
if [[ -n "$public_key" ]]; then
ENV_VALUES["LANGFUSE_PUBLIC_KEY"]="$public_key"
fi
if [[ -n "$host" ]]; then
ENV_VALUES["LANGFUSE_HOST"]="$host"
fi
ENV_VALUES["LANGFUSE_ENABLE_TRACE"]="true"
}
show_summary() {
local key
local value
echo
log_info "Configuration summary:"
if ((${#ENV_VALUES[@]} > 0)); then
local -a sorted_keys
mapfile -t sorted_keys < <(printf '%s\n' "${!ENV_VALUES[@]}" | sort)
for key in "${sorted_keys[@]}"; do
value="${ENV_VALUES[$key]}"
if is_sensitive_env_key "$key"; then
value="***"
fi
printf ' %s=%s\n' "$key" "$value"
done
fi
if ((${#DOCKER_SERVICES[@]} > 0)); then
echo
log_info "Docker services to include:"
for service in "${DOCKER_SERVICES[@]}"; do
echo " - $service"
done
echo " Compose file: docker-compose.final.yml"
fi
}
# Preserve already-staged SSL mounts when regenerating compose output. The
# setup wizards treat .env as the configuration for the current target runtime,
# not as a single file guaranteed to work for both host and Docker Compose at
# the same time. A later wizard run may rewrite .env again when the operator
# switches between host and compose workflows.
prepare_inherited_ssl_assets_for_compose() {
local existing_compose="${1:-}"
local staged_cert_source="$SSL_CERT_SOURCE_PATH"
local staged_key_source="$SSL_KEY_SOURCE_PATH"
local preserved_cert_path=""
local preserved_key_path=""
if [[ -n "$SSL_CERT_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_CERT_SOURCE_PATH"; then
if [[ -n "$existing_compose" ]]; then
preserved_cert_path="$(read_service_environment_value "$existing_compose" "lightrag" "SSL_CERTFILE" || true)"
fi
if [[ "$preserved_cert_path" == /app/data/certs/* ]]; then
log_warn "SSL_CERTFILE source is missing; preserving the existing compose SSL certificate mount."
staged_cert_source=""
ENV_VALUES["SSL_CERTFILE"]="$preserved_cert_path"
set_compose_override "SSL_CERTFILE" "$preserved_cert_path"
else
format_error "Invalid SSL_CERTFILE" \
"Set it to an existing certificate file, disable SSL, or rerun the wizard to choose a new certificate."
return 1
fi
fi
if [[ -n "$SSL_KEY_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_KEY_SOURCE_PATH"; then
if [[ -n "$existing_compose" ]]; then
preserved_key_path="$(read_service_environment_value "$existing_compose" "lightrag" "SSL_KEYFILE" || true)"
fi
if [[ "$preserved_key_path" == /app/data/certs/* ]]; then
log_warn "SSL_KEYFILE source is missing; preserving the existing compose SSL key mount."
staged_key_source=""
ENV_VALUES["SSL_KEYFILE"]="$preserved_key_path"
set_compose_override "SSL_KEYFILE" "$preserved_key_path"
else
format_error "Invalid SSL_KEYFILE" \
"Set it to an existing private key file, disable SSL, or rerun the wizard to choose a new key."
return 1
fi
fi
SSL_CERT_SOURCE_PATH="$staged_cert_source"
SSL_KEY_SOURCE_PATH="$staged_key_source"
if [[ -n "$SSL_CERT_SOURCE_PATH" || -n "$SSL_KEY_SOURCE_PATH" ]]; then
stage_ssl_assets "$SSL_CERT_SOURCE_PATH" "$SSL_KEY_SOURCE_PATH"
fi
}
prepare_managed_service_assets_for_compose() {
local existing_compose="${1:-}"
if ! prepare_inherited_ssl_assets_for_compose "$existing_compose"; then
return 1
fi
if [[ -n "${DOCKER_SERVICE_SET[redis]:-}" ]]; then
stage_redis_config_asset || return 1
fi
}
env_base_flow() {
local vllm_embed_api_key=""
local vllm_rerank_api_key=""
local existing_vllm_embed_model=""
local existing_embedding_dim=""
local existing_vllm_embed_port=""
local existing_vllm_embed_host=""
local existing_vllm_embed_device=""
local previous_embedding_provider=""
local existing_vllm_rerank_model=""
local existing_vllm_rerank_port=""
local existing_vllm_rerank_host=""
local existing_vllm_rerank_device=""
local previous_rerank_provider=""
if host_cuda_available; then
log_info "GPU detected: NVIDIA GPU found. New local vLLM services default to CUDA (GPU image + float16)."
else
log_info "GPU detection: no NVIDIA GPU found. New local vLLM services default to CPU image + float32."
fi
reset_state
load_existing_env_if_present
initialize_default_storage_backends
log_info "Base configuration wizard (LLM / Embedding / Reranker)"
echo "This wizard only modifies LLM, embedding, and reranker settings."
echo "Storage, server, and security settings are preserved."
echo ""
log_step "LLM configuration"
collect_llm_config
echo ""
# ── Embedding ────────────────────────────────────────────────────────────────
log_step "Embedding configuration"
local docker_embed_default="no"
previous_embedding_provider="${ENV_VALUES[LIGHTRAG_SETUP_EMBEDDING_PROVIDER]:-}"
if [[ "$previous_embedding_provider" == "vllm" ]]; then
docker_embed_default="yes"
fi
local use_docker_embed="no"
if [[ "$docker_embed_default" == "yes" ]]; then
confirm_default_yes "Run embedding model locally via Docker (vLLM)?" && use_docker_embed="yes" || use_docker_embed="no"
else
confirm_default_no "Run embedding model locally via Docker (vLLM)?" && use_docker_embed="yes" || use_docker_embed="no"
fi
if [[ "$use_docker_embed" == "yes" ]]; then
existing_vllm_embed_model="${ENV_VALUES[VLLM_EMBED_MODEL]:-}"
existing_embedding_dim="${ENV_VALUES[EMBEDDING_DIM]:-}"
existing_vllm_embed_port="${ENV_VALUES[VLLM_EMBED_PORT]:-}"
existing_vllm_embed_host="${ENV_VALUES[EMBEDDING_BINDING_HOST]:-}"
existing_vllm_embed_device="${ENV_VALUES[VLLM_EMBED_DEVICE]:-}"
apply_preset_overwrite "${PRESET_VLLM_EMBEDDING[@]}"
if [[ -n "$existing_vllm_embed_port" ]]; then
ENV_VALUES["VLLM_EMBED_PORT"]="$existing_vllm_embed_port"
fi
if [[ -n "$existing_embedding_dim" ]]; then
ENV_VALUES["EMBEDDING_DIM"]="$existing_embedding_dim"
fi
if [[ "$previous_embedding_provider" == "vllm" && -n "$existing_vllm_embed_host" ]]; then
ENV_VALUES["EMBEDDING_BINDING_HOST"]="$existing_vllm_embed_host"
else
ENV_VALUES["EMBEDDING_BINDING_HOST"]="http://localhost:${ENV_VALUES[VLLM_EMBED_PORT]:-8001}/v1"
fi
local embed_model
embed_model="$(prompt_with_default "Embedding model" "${existing_vllm_embed_model:-${ENV_VALUES[VLLM_EMBED_MODEL]:-BAAI/bge-m3}}")"
ENV_VALUES["VLLM_EMBED_MODEL"]="$embed_model"
ENV_VALUES["EMBEDDING_MODEL"]="$embed_model"
local vllm_embed_device
vllm_embed_device="$(resolve_local_device_default "$existing_vllm_embed_device")"
ENV_VALUES["VLLM_EMBED_DEVICE"]="$vllm_embed_device"
ENV_VALUES["LIGHTRAG_SETUP_EMBEDDING_PROVIDER"]="vllm"
vllm_embed_api_key="${ENV_VALUES[VLLM_EMBED_API_KEY]:-${ENV_VALUES[EMBEDDING_BINDING_API_KEY]:-}}"
if [[ -z "$vllm_embed_api_key" ]]; then
vllm_embed_api_key="$(openssl rand -hex 16 2>/dev/null || LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32)"
fi
ENV_VALUES["VLLM_EMBED_API_KEY"]="$vllm_embed_api_key"
ENV_VALUES["EMBEDDING_BINDING_API_KEY"]="$vllm_embed_api_key"
add_docker_service "vllm-embed"
set_compose_override "EMBEDDING_BINDING_HOST" \
"http://vllm-embed:${ENV_VALUES[VLLM_EMBED_PORT]:-8001}/v1"
else
collect_embedding_config
fi
echo ""
# ── Reranker ─────────────────────────────────────────────────────────────────
log_step "Reranker configuration"
local rerank_enabled_default="no"
if [[ -n "${ENV_VALUES[RERANK_BINDING]:-}" && "${ENV_VALUES[RERANK_BINDING]}" != "null" ]]; then
rerank_enabled_default="yes"
fi
previous_rerank_provider="${ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]:-}"
local enable_reranking="no"
if [[ "$rerank_enabled_default" == "yes" ]]; then
confirm_default_yes "Enable reranking?" && enable_reranking="yes" || enable_reranking="no"
else
confirm_default_no "Enable reranking?" && enable_reranking="yes" || enable_reranking="no"
fi
if [[ "$enable_reranking" == "yes" ]]; then
local docker_rerank_default="no"
if [[ "$previous_rerank_provider" == "vllm" ]]; then
docker_rerank_default="yes"
fi
local use_docker_rerank="no"
if [[ "$docker_rerank_default" == "yes" ]]; then
confirm_default_yes "Run rerank service locally via Docker?" && use_docker_rerank="yes" || use_docker_rerank="no"
else
confirm_default_no "Run rerank service locally via Docker?" && use_docker_rerank="yes" || use_docker_rerank="no"
fi
if [[ "$use_docker_rerank" == "yes" ]]; then
existing_vllm_rerank_model="${ENV_VALUES[VLLM_RERANK_MODEL]:-}"
existing_vllm_rerank_port="${ENV_VALUES[VLLM_RERANK_PORT]:-}"
existing_vllm_rerank_host="${ENV_VALUES[RERANK_BINDING_HOST]:-}"
existing_vllm_rerank_device="${ENV_VALUES[VLLM_RERANK_DEVICE]:-}"
apply_preset_overwrite "${PRESET_VLLM_RERANKER[@]}"
local rerank_model rerank_port
if [[ -n "$existing_vllm_rerank_port" ]]; then
ENV_VALUES["VLLM_RERANK_PORT"]="$existing_vllm_rerank_port"
fi
if [[ "$previous_rerank_provider" == "vllm" && -n "$existing_vllm_rerank_host" ]]; then
ENV_VALUES["RERANK_BINDING_HOST"]="$existing_vllm_rerank_host"
else
ENV_VALUES["RERANK_BINDING_HOST"]="http://localhost:${ENV_VALUES[VLLM_RERANK_PORT]:-8000}/rerank"
fi
rerank_model="$(prompt_with_default "Rerank model" "${existing_vllm_rerank_model:-${ENV_VALUES[VLLM_RERANK_MODEL]:-BAAI/bge-reranker-v2-m3}}")"
rerank_port="${ENV_VALUES[VLLM_RERANK_PORT]:-8000}"
ENV_VALUES["VLLM_RERANK_MODEL"]="$rerank_model"
ENV_VALUES["RERANK_MODEL"]="$rerank_model"
ENV_VALUES["VLLM_RERANK_PORT"]="$rerank_port"
local vllm_rerank_device
vllm_rerank_device="$(resolve_local_device_default "$existing_vllm_rerank_device")"
ENV_VALUES["VLLM_RERANK_DEVICE"]="$vllm_rerank_device"
ENV_VALUES["LIGHTRAG_SETUP_RERANK_PROVIDER"]="vllm"
vllm_rerank_api_key="${ENV_VALUES[VLLM_RERANK_API_KEY]:-${ENV_VALUES[RERANK_BINDING_API_KEY]:-}}"
if [[ -z "$vllm_rerank_api_key" ]]; then
vllm_rerank_api_key="$(openssl rand -hex 16 2>/dev/null || LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32)"
fi
ENV_VALUES["VLLM_RERANK_API_KEY"]="$vllm_rerank_api_key"
ENV_VALUES["RERANK_BINDING_API_KEY"]="$vllm_rerank_api_key"
add_docker_service "vllm-rerank"
set_compose_override "RERANK_BINDING_HOST" \
"http://vllm-rerank:${rerank_port}/rerank"
else
# Reranking enabled but not via Docker — ask provider/host/model/api_key
collect_rerank_config "yes" "no"
fi
else
ENV_VALUES["RERANK_BINDING"]="null"
unset 'ENV_VALUES[LIGHTRAG_SETUP_RERANK_PROVIDER]'
fi
echo ""
finalize_base_setup
}
finalize_base_setup() {
local backup_path
local compose_file
local existing_compose
local compose_action="write_env_only"
local runtime_target="$DEFAULT_RUNTIME_TARGET"
local show_host_start_hint="no"
local svc_names=""
if [[ ! -f "${REPO_ROOT}/env.example" ]]; then
format_error "env.example is missing in $REPO_ROOT" "Restore env.example before running setup."
return 1
fi
if [[ ! -w "$REPO_ROOT" ]]; then
format_error "No write permission in $REPO_ROOT" "Run the setup from a writable directory."
return 1
fi
if ! validate_sensitive_env_literals; then
return 1
fi
show_summary
if ! confirm_required_yes_no "${COLOR_YELLOW}Ready to proceed and write .env${COLOR_RESET}"; then
log_warn "Setup cancelled."
return 1
fi
existing_compose="$(find_generated_compose_file)"
compose_file="${REPO_ROOT}/docker-compose.final.yml"
record_existing_managed_root_services "$existing_compose"
restore_storage_docker_services_from_env
configure_base_compose_rewrites
if ((${#DOCKER_SERVICES[@]} > 0)); then
# LightRAG depends on managed Docker services; it must run via Docker.
svc_names="$(printf '%s ' "${DOCKER_SERVICES[@]}")"
svc_names="${svc_names% }"
echo "LightRAG requires Docker services: ${svc_names}"
if ! confirm_default_yes "${COLOR_YELLOW}The compose file will be created/updated. Continue?${COLOR_RESET}"; then
log_warn "Setup cancelled."
return 1
fi
compose_action="rewrite_compose"
runtime_target="compose"
else
resolve_compose_output_action \
"$existing_compose" \
compose_action \
runtime_target \
show_host_start_hint
fi
if [[ "$compose_action" == "rewrite_compose" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
if ! prepare_managed_service_assets_for_compose "$existing_compose"; then
return 1
fi
prepare_compose_env_overrides
elif [[ "$compose_action" == "delete_compose_and_switch_host" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
fi
backup_path="$(backup_env_file)"
if [[ -n "$backup_path" ]]; then
log_success "Backed up existing .env to $backup_path"
fi
clear_deprecated_vllm_dtype_state
set_runtime_target "$runtime_target" || return 1
generate_env_file "${REPO_ROOT}/env.example" "${REPO_ROOT}/.env"
log_success "Wrote .env"
case "$compose_action" in
rewrite_compose)
prepare_compose_output_from_existing "$compose_file" "$existing_compose" || return 1
generate_docker_compose "$compose_file"
log_success "Wrote ${compose_file}"
echo " To start: docker compose -f ${compose_file} up -d"
;;
delete_compose_and_switch_host)
remove_existing_compose_file "$existing_compose" || return 1
echo " To start: lightrag-server"
;;
*)
if [[ "$show_host_start_hint" == "yes" ]]; then
echo " To start: lightrag-server"
fi
;;
esac
}
env_storage_flow() {
local env_file="${REPO_ROOT}/.env"
local db_type
local db_order=("postgresql" "neo4j" "mongodb" "redis" "milvus" "qdrant" "memgraph" "opensearch")
if [[ ! -f "$env_file" ]]; then
format_error "No .env file found." "Run 'make env-base' first to configure LLM and embedding."
return 1
fi
reset_state
load_existing_env_if_present
log_info "Storage configuration wizard"
echo "This wizard only modifies storage backend settings."
echo "LLM, embedding, reranker, server, and security settings are preserved."
echo ""
log_step "Storage backend selection"
select_storage_backends "custom"
log_debug "Storage selections: kv=${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-} vector=${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-} graph=${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]:-} doc=${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]:-}"
clear_unused_storage_deployment_markers
log_step "Database configuration"
for db_type in "${db_order[@]}"; do
if [[ -n "${REQUIRED_DB_TYPES[$db_type]+set}" ]]; then
collect_database_config "$db_type" "$(storage_default_docker_for_db_type "$db_type")"
echo ""
fi
done
finalize_storage_setup
}
finalize_storage_setup() {
local backup_path
local compose_file
local existing_compose
local compose_action="write_env_only"
local runtime_target="$DEFAULT_RUNTIME_TARGET"
local show_host_start_hint="no"
if [[ ! -f "${REPO_ROOT}/env.example" ]]; then
format_error "env.example is missing in $REPO_ROOT" "Restore env.example before running setup."
return 1
fi
if [[ ! -w "$REPO_ROOT" ]]; then
format_error "No write permission in $REPO_ROOT" "Run the setup from a writable directory."
return 1
fi
if [[ -n "${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-}" ]]; then
if ! validate_required_variables \
"${ENV_VALUES[LIGHTRAG_KV_STORAGE]}" \
"${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]}" \
"${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]}" \
"${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]}"; then
return 1
fi
fi
if ! validate_mongo_vector_storage_config \
"${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-}" \
"${ENV_VALUES[MONGO_URI]:-}" \
"${ENV_VALUES[LIGHTRAG_SETUP_MONGODB_DEPLOYMENT]:-}"; then
return 1
fi
if ! validate_sensitive_env_literals; then
return 1
fi
show_summary
if ! confirm_required_yes_no "${COLOR_YELLOW}Ready to proceed and write .env${COLOR_RESET}"; then
log_warn "Setup cancelled."
return 1
fi
existing_compose="$(find_generated_compose_file)"
compose_file="${REPO_ROOT}/docker-compose.final.yml"
record_existing_managed_root_services "$existing_compose"
restore_vllm_docker_services_from_env
configure_storage_compose_rewrites
resolve_compose_output_action \
"$existing_compose" \
compose_action \
runtime_target \
show_host_start_hint
if [[ "$compose_action" == "rewrite_compose" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
if ! prepare_managed_service_assets_for_compose "$existing_compose"; then
return 1
fi
prepare_compose_env_overrides
elif [[ "$compose_action" == "delete_compose_and_switch_host" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
fi
backup_path="$(backup_env_file)"
if [[ -n "$backup_path" ]]; then
log_success "Backed up existing .env to $backup_path"
fi
clear_deprecated_vllm_dtype_state
set_runtime_target "$runtime_target" || return 1
generate_env_file "${REPO_ROOT}/env.example" "${REPO_ROOT}/.env"
log_success "Wrote .env"
case "$compose_action" in
rewrite_compose)
prepare_compose_output_from_existing "$compose_file" "$existing_compose" || return 1
generate_docker_compose "$compose_file"
log_success "Wrote ${compose_file}"
echo " To start: docker compose -f ${compose_file} up -d"
;;
delete_compose_and_switch_host)
remove_existing_compose_file "$existing_compose" || return 1
echo " To start: lightrag-server"
;;
*)
if [[ "$show_host_start_hint" == "yes" ]]; then
echo " To start: lightrag-server"
fi
;;
esac
}
env_server_flow() {
local env_file="${REPO_ROOT}/.env"
if [[ ! -f "$env_file" ]]; then
format_error "No .env file found." "Run 'make env-base' first to configure LLM and embedding."
return 1
fi
reset_state
load_existing_env_if_present
log_info "Server configuration wizard"
echo "This wizard only modifies server, security, and SSL settings."
echo "LLM, embedding, reranker, and storage settings are preserved."
echo ""
log_step "Server configuration"
collect_server_config
echo ""
log_step "Security configuration"
collect_security_config "no" "no"
echo ""
log_step "SSL configuration"
collect_ssl_config
echo ""
finalize_server_setup
}
finalize_server_setup() {
local backup_path
local compose_file
local existing_compose
local compose_action="write_env_only"
local runtime_target="$DEFAULT_RUNTIME_TARGET"
local show_host_start_hint="no"
if [[ ! -f "${REPO_ROOT}/env.example" ]]; then
format_error "env.example is missing in $REPO_ROOT" "Restore env.example before running setup."
return 1
fi
if [[ ! -w "$REPO_ROOT" ]]; then
format_error "No write permission in $REPO_ROOT" "Run the setup from a writable directory."
return 1
fi
if ! validate_sensitive_env_literals; then
return 1
fi
if ! validate_auth_accounts_runtime_config \
"${ENV_VALUES[AUTH_ACCOUNTS]:-}"; then
return 1
fi
show_summary
if ! confirm_required_yes_no "${COLOR_YELLOW}Ready to proceed and write .env${COLOR_RESET}"; then
log_warn "Setup cancelled."
return 1
fi
existing_compose="$(find_generated_compose_file)"
compose_file="${REPO_ROOT}/docker-compose.final.yml"
record_existing_managed_root_services "$existing_compose"
restore_storage_docker_services_from_env
restore_vllm_docker_services_from_env
resolve_compose_output_action \
"$existing_compose" \
compose_action \
runtime_target \
show_host_start_hint
if [[ "$compose_action" == "rewrite_compose" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
if ! prepare_managed_service_assets_for_compose "$existing_compose"; then
return 1
fi
prepare_compose_env_overrides
elif [[ "$compose_action" == "delete_compose_and_switch_host" ]]; then
backup_existing_compose_for_action "$compose_action" "$existing_compose" || return 1
if [[ -n "$SSL_CERT_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_CERT_SOURCE_PATH"; then
format_error "Invalid SSL_CERTFILE" \
"Set it to an existing certificate file, disable SSL, or rerun the wizard to choose a new certificate."
return 1
fi
if [[ -n "$SSL_KEY_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_KEY_SOURCE_PATH"; then
format_error "Invalid SSL_KEYFILE" \
"Set it to an existing private key file, disable SSL, or rerun the wizard to choose a new key."
return 1
fi
else
if [[ -n "$SSL_CERT_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_CERT_SOURCE_PATH"; then
format_error "Invalid SSL_CERTFILE" \
"Set it to an existing certificate file, disable SSL, or rerun the wizard to choose a new certificate."
return 1
fi
if [[ -n "$SSL_KEY_SOURCE_PATH" ]] && ! validate_existing_file "$SSL_KEY_SOURCE_PATH"; then
format_error "Invalid SSL_KEYFILE" \
"Set it to an existing private key file, disable SSL, or rerun the wizard to choose a new key."
return 1
fi
fi
backup_path="$(backup_env_file)"
if [[ -n "$backup_path" ]]; then
log_success "Backed up existing .env to $backup_path"
fi
clear_deprecated_vllm_dtype_state
set_runtime_target "$runtime_target" || return 1
generate_env_file "${REPO_ROOT}/env.example" "${REPO_ROOT}/.env"
log_success "Wrote .env"
case "$compose_action" in
rewrite_compose)
prepare_compose_output_from_existing "$compose_file" "$existing_compose" || return 1
generate_docker_compose "$compose_file"
log_success "Wrote ${compose_file}"
log_success "Server port and security settings updated in compose."
echo " To restart: docker compose -f ${compose_file} up -d --force-recreate lightrag"
;;
delete_compose_and_switch_host)
remove_existing_compose_file "$existing_compose" || return 1
echo " To start: lightrag-server"
;;
*)
if [[ "$show_host_start_hint" == "yes" ]]; then
echo " To start: lightrag-server"
fi
;;
esac
}
load_env_file() {
local env_file="$1"
local line key value
if [[ ! -f "$env_file" ]]; then
format_error ".env file not found at $env_file" "Run make env-base to generate it."
return 1
fi
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^[A-Za-z0-9_]+= ]]; then
key="${line%%=*}"
value="${line#*=}"
if [[ "$value" =~ ^\".*\"$ ]]; then
value="${value:1:${#value}-2}"
value="${value//\\\$/\$}"
value="${value//\\\"/\"}"
value="${value//\\\\/\\}"
elif [[ "$value" =~ ^\'.*\'$ ]]; then
value="${value:1:${#value}-2}"
fi
ENV_VALUES["$key"]="$value"
fi
done < "$env_file"
}
validate_ssl_runtime_path() {
local path="$1"
local runtime_target="${ENV_VALUES[LIGHTRAG_RUNTIME_TARGET]:-$DEFAULT_RUNTIME_TARGET}"
local staged_path=""
if validate_existing_file "$path"; then
return 0
fi
if [[ "$runtime_target" == "compose" && "$path" == /app/data/certs/* ]]; then
staged_path="${REPO_ROOT}/data/certs/${path#/app/data/certs/}"
validate_existing_file "$staged_path"
return $?
fi
return 1
}
validate_env_file() {
local env_file="${REPO_ROOT}/.env"
local errors=0
local kv vector graph doc_status
local runtime_target
local storage db_type
local -A referenced_db_types=()
reset_state
if ! load_env_file "$env_file"; then
return 1
fi
kv="${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-}"
vector="${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-}"
graph="${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]:-}"
doc_status="${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]:-}"
runtime_target="${ENV_VALUES[LIGHTRAG_RUNTIME_TARGET]:-$DEFAULT_RUNTIME_TARGET}"
for storage in "$kv" "$vector" "$graph" "$doc_status"; do
if [[ -z "$storage" ]]; then
continue
fi
db_type="${STORAGE_DB_TYPES[$storage]:-}"
if [[ -n "$db_type" ]]; then
referenced_db_types["$db_type"]=1
fi
done
if ! validate_runtime_target "$runtime_target"; then
errors=1
fi
if [[ -z "$kv" || -z "$vector" || -z "$graph" || -z "$doc_status" ]]; then
format_error "Storage selections are missing in .env" "Set LIGHTRAG_*_STORAGE variables."
return 1
fi
if ! validate_mongo_vector_storage_config \
"$vector" \
"${ENV_VALUES[MONGO_URI]:-}" \
"${ENV_VALUES[LIGHTRAG_SETUP_MONGODB_DEPLOYMENT]:-}"; then
errors=1
fi
if ! validate_required_variables "$kv" "$vector" "$graph" "$doc_status"; then
errors=1
fi
if ! validate_auth_accounts_runtime_config \
"${ENV_VALUES[AUTH_ACCOUNTS]:-}"; then
errors=1
fi
if ! validate_sensitive_env_literals; then
errors=1
fi
if [[ "${ENV_VALUES[SSL]:-false}" == "true" ]]; then
if ! validate_ssl_runtime_path "${ENV_VALUES[SSL_CERTFILE]:-}"; then
format_error "Invalid SSL_CERTFILE" "Set it to an existing certificate file when SSL=true."
errors=1
fi
if ! validate_ssl_runtime_path "${ENV_VALUES[SSL_KEYFILE]:-}"; then
format_error "Invalid SSL_KEYFILE" "Set it to an existing private key file when SSL=true."
errors=1
fi
fi
if [[ -n "${referenced_db_types[neo4j]+set}" ]] && [[ -n "${ENV_VALUES[NEO4J_URI]:-}" ]] && ! validate_uri "${ENV_VALUES[NEO4J_URI]}" neo4j; then
format_error "Invalid NEO4J_URI" "Use neo4j:// or bolt:// format."
errors=1
fi
if [[ -n "${referenced_db_types[mongodb]+set}" ]] && [[ -n "${ENV_VALUES[MONGO_URI]:-}" ]] && ! validate_uri "${ENV_VALUES[MONGO_URI]}" mongodb; then
format_error "Invalid MONGO_URI" "Use mongodb:// or mongodb+srv:// format."
errors=1
fi
if [[ -n "${referenced_db_types[redis]+set}" ]] && [[ -n "${ENV_VALUES[REDIS_URI]:-}" ]] && ! validate_uri "${ENV_VALUES[REDIS_URI]}" redis; then
format_error "Invalid REDIS_URI" "Use redis:// or rediss:// format."
errors=1
fi
if [[ -n "${referenced_db_types[milvus]+set}" ]] && [[ -n "${ENV_VALUES[MILVUS_URI]:-}" ]] && ! validate_uri "${ENV_VALUES[MILVUS_URI]}" milvus; then
format_error "Invalid MILVUS_URI" "Use http://host:port format."
errors=1
fi
if [[ -n "${referenced_db_types[qdrant]+set}" ]] && [[ -n "${ENV_VALUES[QDRANT_URL]:-}" ]] && ! validate_uri "${ENV_VALUES[QDRANT_URL]}" qdrant; then
format_error "Invalid QDRANT_URL" "Use http://host:port format."
errors=1
fi
if [[ -n "${referenced_db_types[memgraph]+set}" ]] && [[ -n "${ENV_VALUES[MEMGRAPH_URI]:-}" ]] && ! validate_uri "${ENV_VALUES[MEMGRAPH_URI]}" memgraph; then
format_error "Invalid MEMGRAPH_URI" "Use bolt://host:port format."
errors=1
fi
if [[ -n "${referenced_db_types[postgresql]+set}" ]] && [[ -n "${ENV_VALUES[POSTGRES_PORT]:-}" ]] && ! validate_port "${ENV_VALUES[POSTGRES_PORT]}"; then
format_error "Invalid POSTGRES_PORT" "Use a port between 1 and 65535."
errors=1
fi
if [[ -n "${referenced_db_types[opensearch]+set}" ]] && [[ -v 'ENV_VALUES[OPENSEARCH_HOSTS]' ]] && [[ -z "${ENV_VALUES[OPENSEARCH_HOSTS]}" ]]; then
format_error "Empty OPENSEARCH_HOSTS" "Set it to host:port (e.g. localhost:9200)."
errors=1
fi
if [[ -n "${referenced_db_types[opensearch]+set}" ]]; then
if ! validate_opensearch_config \
"${ENV_VALUES[LIGHTRAG_SETUP_OPENSEARCH_DEPLOYMENT]:-}" \
"${ENV_VALUES[OPENSEARCH_HOSTS]:-}" \
"${ENV_VALUES[OPENSEARCH_USER]:-}" \
"${ENV_VALUES[OPENSEARCH_PASSWORD]:-}" \
"${ENV_VALUES[OPENSEARCH_NUMBER_OF_SHARDS]-1}" \
"${ENV_VALUES[OPENSEARCH_NUMBER_OF_REPLICAS]-0}"; then
errors=1
fi
fi
if ((errors != 0)); then
return 1
fi
log_success "Validation passed."
}
report_security_issue() {
local message="$1"
local suggestion="${2:-}"
echo "${COLOR_YELLOW:-}Security issue:${COLOR_RESET:-} $message"
if [[ -n "$suggestion" ]]; then
echo " Suggestion: $suggestion"
fi
}
security_check_env_file() {
local env_file="${REPO_ROOT}/.env"
local findings=0
local auth_accounts=""
local token_secret=""
local api_key=""
local whitelist_paths=""
local whitelist_is_set="no"
local effective_whitelist=""
local kv=""
local vector=""
local graph=""
local doc_status=""
local storage=""
local db_type=""
local opensearch_in_use="no"
local key value
local invalid_sensitive_keys=()
local -A referenced_db_types=()
reset_state
if ! load_env_file "$env_file"; then
return 1
fi
auth_accounts="${ENV_VALUES[AUTH_ACCOUNTS]:-}"
token_secret="${ENV_VALUES[TOKEN_SECRET]:-}"
api_key="${ENV_VALUES[LIGHTRAG_API_KEY]:-}"
kv="${ENV_VALUES[LIGHTRAG_KV_STORAGE]:-}"
vector="${ENV_VALUES[LIGHTRAG_VECTOR_STORAGE]:-}"
graph="${ENV_VALUES[LIGHTRAG_GRAPH_STORAGE]:-}"
doc_status="${ENV_VALUES[LIGHTRAG_DOC_STATUS_STORAGE]:-}"
if [[ -n "${ENV_VALUES[WHITELIST_PATHS]+set}" ]]; then
whitelist_paths="${ENV_VALUES[WHITELIST_PATHS]}"
whitelist_is_set="yes"
fi
for storage in "$kv" "$vector" "$graph" "$doc_status"; do
if [[ -z "$storage" ]]; then
continue
fi
if [[ ! -v "STORAGE_DB_TYPES[$storage]" ]]; then
continue
fi
db_type="${STORAGE_DB_TYPES[$storage]}"
if [[ -n "$db_type" ]]; then
referenced_db_types["$db_type"]=1
fi
done
if [[ -n "${referenced_db_types[opensearch]+set}" || "${ENV_VALUES[LIGHTRAG_SETUP_OPENSEARCH_DEPLOYMENT]:-}" == "docker" ]]; then
opensearch_in_use="yes"
fi
for key in "${!ENV_VALUES[@]}"; do
if ! is_sensitive_env_key "$key"; then
continue
fi
value="${ENV_VALUES[$key]:-}"
if [[ -n "$value" ]] && contains_env_interpolation_syntax "$value"; then
invalid_sensitive_keys+=("$key")
fi
done
if ((${#invalid_sensitive_keys[@]} > 0)); then
report_security_issue \
"Sensitive values still contain \${...} interpolation syntax: ${invalid_sensitive_keys[*]}" \
"Replace them with literal values or inject those secrets at runtime."
findings=$((findings + 1))
fi
if [[ -z "$auth_accounts" && -z "$api_key" ]]; then
report_security_issue \
"No API protection is configured." \
"Set AUTH_ACCOUNTS and TOKEN_SECRET, add LIGHTRAG_API_KEY, or put the service behind a trusted reverse proxy."
findings=$((findings + 1))
fi
if [[ -n "$auth_accounts" ]]; then
if ! validate_auth_accounts_format "$auth_accounts"; then
report_security_issue \
"AUTH_ACCOUNTS is malformed." \
"Use comma-separated user:password pairs such as admin:{bcrypt}<hash> or admin:secret,reader:another-secret."
findings=$((findings + 1))
elif ! validate_auth_accounts_password_safety "$auth_accounts"; then
report_security_issue \
"AUTH_ACCOUNTS uses a predictable password prefix." \
"Passwords must not start with 'admin' or 'pass'. Choose a stronger password or use lightrag-hash-password."
findings=$((findings + 1))
fi
if [[ -z "$token_secret" ]]; then
report_security_issue \
"AUTH_ACCOUNTS is set but TOKEN_SECRET is missing." \
"Set a non-empty JWT signing secret before enabling account-based authentication."
findings=$((findings + 1))
elif [[ "$token_secret" == "lightrag-jwt-default-secret" ]]; then
report_security_issue \
"TOKEN_SECRET still uses the built-in default value." \
"Generate a unique JWT signing secret and update TOKEN_SECRET."
findings=$((findings + 1))
fi
effective_whitelist="$whitelist_paths"
if [[ "$whitelist_is_set" != "yes" ]]; then
effective_whitelist="/health,/api/*"
fi
if whitelist_exposes_api_routes "$effective_whitelist"; then
report_security_issue \
"WHITELIST_PATHS exposes /api routes while AUTH_ACCOUNTS is enabled." \
"Use a minimal whitelist such as /health,/docs and keep /api routes authenticated."
findings=$((findings + 1))
fi
fi
if [[ -z "$auth_accounts" && -n "$api_key" ]]; then
effective_whitelist="$whitelist_paths"
if [[ "$whitelist_is_set" != "yes" ]]; then
effective_whitelist="/health,/api/*"
fi
if whitelist_exposes_api_routes "$effective_whitelist"; then
report_security_issue \
"WHITELIST_PATHS exposes /api routes while LIGHTRAG_API_KEY is the only active auth mechanism." \
"Use a minimal whitelist such as /health,/docs and keep /api routes protected by the API key."
findings=$((findings + 1))
fi
fi
if [[ "$opensearch_in_use" == "yes" ]] && [[ -n "${ENV_VALUES[OPENSEARCH_PASSWORD]:-}" ]]; then
local os_pass="${ENV_VALUES[OPENSEARCH_PASSWORD]}"
if [[ "$os_pass" == "admin" || "$os_pass" == "LightRAG2026_!@" ]]; then
report_security_issue \
"OPENSEARCH_PASSWORD uses a well-known default value." \
"Set a unique, strong password for the OpenSearch admin account."
findings=$((findings + 1))
fi
fi
if ((findings == 0)); then
log_success "No obvious security issues found in ${env_file}."
return 0
fi
log_warn "Security check found ${findings} issue(s) in ${env_file}."
return 1
}
backup_only() {
local backup_path
local compose_backup_path
backup_path="$(backup_env_file)"
if [[ -z "$backup_path" ]]; then
format_error "No .env file found to back up." "Create one with make env-base first."
return 1
fi
echo "Backed up .env to $backup_path"
compose_backup_path="$(backup_compose_file)" || return 1
if [[ -n "$compose_backup_path" ]]; then
echo "Backed up compose file to $compose_backup_path"
fi
}
print_help() {
cat <<'HELP'
Usage: scripts/setup/setup.sh [--base|--storage|--server|--validate|--security-check|--backup] [--rewrite-compose]
Options:
--base Configure LLM, embedding, and reranker (run first)
--storage Configure storage backends and databases (requires .env)
--server Configure server, security, and SSL (requires .env)
--validate Validate an existing .env file
--security-check Audit an existing .env for security risks
--backup Backup the current .env and generated compose file when present
--rewrite-compose Force regeneration of all wizard-managed compose services
--debug Enable debug logging
--help Show this help message
HELP
}
_sigint_handler() {
echo ""
echo "Setup interrupted."
exit 130
}
main() {
trap '_sigint_handler' INT
init_colors
local mode="help"
while [[ $# -gt 0 ]]; do
case "$1" in
--base)
mode="base"
;;
--storage)
mode="storage"
;;
--server)
mode="server"
;;
--validate)
mode="validate"
;;
--security-check)
mode="security-check"
;;
--backup)
mode="backup"
;;
--debug)
DEBUG="true"
;;
--rewrite-compose)
FORCE_REWRITE_COMPOSE="yes"
;;
--help|-h)
mode="help"
;;
*)
echo "Unknown option: $1" >&2
print_help
return 1
;;
esac
shift
done
case "$mode" in
base)
env_base_flow
;;
storage)
env_storage_flow
;;
server)
env_server_flow
;;
validate)
validate_env_file
;;
security-check)
security_check_env_file
;;
backup)
backup_only
;;
*)
print_help
;;
esac
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi
|