File size: 69,713 Bytes
44951fc 19b525c 44951fc 2d0ccb4 19b525c 2d0ccb4 19b525c 44951fc 19b525c 44951fc 0760868 19b525c 2d0ccb4 3e9f489 21e2154 44951fc 3e9f489 44951fc 21e2154 44951fc 2d0ccb4 19b525c 44951fc 2d0ccb4 19b525c 2d0ccb4 44951fc 19b525c 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 19b525c 2d0ccb4 19b525c 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 19b525c 44951fc 19b525c 2d0ccb4 ac1b0e8 19b525c ac1b0e8 19b525c ac1b0e8 19b525c ac1b0e8 19b525c ac1b0e8 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 ac1b0e8 2d0ccb4 ac1b0e8 2d0ccb4 ac1b0e8 3e9f489 ac1b0e8 3e9f489 ac1b0e8 44951fc ac1b0e8 3e9f489 ac1b0e8 3e9f489 ac1b0e8 3e9f489 44951fc ac1b0e8 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 21e2154 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 21e2154 2d0ccb4 44951fc 21e2154 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 44951fc 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 2d0ccb4 fdf05c6 9b87c26 fdf05c6 9b87c26 4dce431 3e9f489 9b87c26 2d0ccb4 9b87c26 fdf05c6 9b87c26 fdf05c6 9b87c26 fdf05c6 9b87c26 fdf05c6 9b87c26 2d0ccb4 44951fc |
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 |
"""
λλ²κΉ
μ μν μ½λ μΆκ° - κ²½λ‘ κ΄λ ¨ λ¬Έμ ν΄κ²°
"""
import os
import time
import hashlib
import pickle
import json
import logging
import glob
from typing import List, Dict, Tuple, Any, Optional
from logging.handlers import RotatingFileHandler
from pathlib import Path
from langchain.schema import Document
from config import (
PDF_DIRECTORY, CACHE_DIRECTORY, CHUNK_SIZE, CHUNK_OVERLAP,
LLM_MODEL, LOG_LEVEL, LOG_FILE, print_config, validate_config
)
from optimized_document_processor import OptimizedDocumentProcessor
from vector_store import VectorStore
import sys
print("===== Script starting =====")
sys.stdout.flush() # μ¦μ μΆλ ₯ κ°μ
# μ£Όμ ν¨μ/λ©μλ νΈμΆ μ νμλ λλ²κΉ
μΆλ ₯ μΆκ°
print("Loading config...")
sys.stdout.flush()
# from config import ... λ±μ μ½λ
print("Config loaded!")
sys.stdout.flush()
# λ‘κΉ
μ€μ κ°μ
def setup_logging():
"""μ ν리μΌμ΄μ
λ‘κΉ
μ€μ """
# λ‘κ·Έ λ 벨 μ€μ
log_level = getattr(logging, LOG_LEVEL.upper(), logging.INFO)
# λ‘κ·Έ ν¬λ§· μ€μ
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(log_format)
# λ£¨νΈ λ‘κ±° μ€μ
root_logger = logging.getLogger()
root_logger.setLevel(log_level)
# νΈλ€λ¬ μ΄κΈ°ν
# μ½μ νΈλ€λ¬
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
root_logger.addHandler(console_handler)
# νμΌ νΈλ€λ¬ (νμ μ)
try:
file_handler = RotatingFileHandler(
LOG_FILE,
maxBytes=10*1024*1024, # 10 MB
backupCount=5
)
file_handler.setFormatter(formatter)
root_logger.addHandler(file_handler)
except Exception as e:
console_handler.warning(f"λ‘κ·Έ νμΌ μ€μ μ€ν¨: {e}, μ½μ λ‘κΉ
λ§ μ¬μ©ν©λλ€.")
return logging.getLogger("AutoRAG")
# λ‘κ±° μ€μ
logger = setup_logging()
# νμ¬ μμ
λλ ν 리 νμΈμ μν λλ²κΉ
μ½λ
current_dir = os.getcwd()
logger.info(f"νμ¬ μμ
λλ ν 리: {current_dir}")
# μ€μ λ PDF λλ ν 리 νμΈ
abs_pdf_dir = os.path.abspath(PDF_DIRECTORY)
logger.info(f"μ€μ λ PDF λλ ν 리: {PDF_DIRECTORY}")
logger.info(f"μ λ κ²½λ‘λ‘ λ³νλ PDF λλ ν 리: {abs_pdf_dir}")
# PDF λλ ν 리 μ‘΄μ¬ νμΈ
if os.path.exists(abs_pdf_dir):
logger.info(f"PDF λλ ν λ¦¬κ° μ‘΄μ¬ν©λλ€: {abs_pdf_dir}")
# λλ ν 리 λ΄μ© νμΈ
pdf_files = glob.glob(os.path.join(abs_pdf_dir, "*.pdf"))
logger.info(f"λλ ν 리 λ΄ PDF νμΌ λͺ©λ‘: {pdf_files}")
else:
logger.error(f"PDF λλ ν λ¦¬κ° μ‘΄μ¬νμ§ μμ΅λλ€: {abs_pdf_dir}")
# μμ λλ ν 리 λ΄μ© νμΈ
parent_dir = os.path.dirname(abs_pdf_dir)
logger.info(f"μμ λλ ν 리: {parent_dir}")
if os.path.exists(parent_dir):
dir_contents = os.listdir(parent_dir)
logger.info(f"μμ λλ ν 리 λ΄μ©: {dir_contents}")
# μ€μ μν νμΈ
logger.info("μ ν리μΌμ΄μ
μ€μ κ²μ¦ μ€...")
config_status = validate_config()
if config_status["status"] != "valid":
for warning in config_status["warnings"]:
logger.warning(f"μ€μ κ²½κ³ : {warning}")
# μμ ν μν¬νΈ
try:
from rag_chain import RAGChain
RAG_CHAIN_AVAILABLE = True
print("RAG μ²΄μΈ λͺ¨λ λ‘λ μ±κ³΅!")
except ImportError as e:
logger.warning(f"RAG μ²΄μΈ λͺ¨λμ λ‘λν μ μμ΅λλ€: {e}")
RAG_CHAIN_AVAILABLE = False
except Exception as e:
logger.warning(f"RAG μ²΄μΈ λͺ¨λ λ‘λ μ€ μμμΉ λͺ»ν μ€λ₯: {e}")
RAG_CHAIN_AVAILABLE = False
# ν΄λ°± RAG κ΄λ ¨ λͺ¨λλ 미리 νμΈ
try:
from fallback_rag_chain import FallbackRAGChain
FALLBACK_AVAILABLE = True
print("ν΄λ°± RAG μ²΄μΈ λͺ¨λ λ‘λ μ±κ³΅!")
except ImportError as e:
logger.warning(f"ν΄λ°± RAG μ²΄μΈ λͺ¨λμ λ‘λν μ μμ΅λλ€: {e}")
FALLBACK_AVAILABLE = False
try:
from offline_fallback_rag import OfflineFallbackRAG
OFFLINE_FALLBACK_AVAILABLE = True
print("μ€νλΌμΈ ν΄λ°± RAG λͺ¨λ λ‘λ μ±κ³΅!")
except ImportError as e:
logger.warning(f"μ€νλΌμΈ ν΄λ°± RAG λͺ¨λμ λ‘λν μ μμ΅λλ€: {e}")
OFFLINE_FALLBACK_AVAILABLE = False
class DocumentProcessingError(Exception):
"""λ¬Έμ μ²λ¦¬ μ€ λ°μνλ μμΈ"""
pass
class VectorStoreError(Exception):
"""λ²‘ν° μ€ν μ΄ μμ
μ€ λ°μνλ μμΈ"""
pass
class RAGInitializationError(Exception):
"""RAG μ²΄μΈ μ΄κΈ°ν μ€ λ°μνλ μμΈ"""
pass
class ConfigurationError(Exception):
"""μ€μ κ΄λ ¨ μ€λ₯"""
pass
class AutoRAGChatApp:
"""
documents ν΄λμ PDF νμΌμ μλμΌλ‘ μ²λ¦¬νλ RAG μ±λ΄
"""
def __init__(self):
"""
RAG μ±λ΄ μ ν리μΌμ΄μ
μ΄κΈ°ν
"""
try:
logger.info("AutoRAGChatApp μ΄κΈ°ν μμ")
# λ°μ΄ν° λλ ν 리 μ μ (μ€μ μμ κ°μ Έμ΄)
# μ λ κ²½λ‘λ‘ λ³ννμ¬ μ¬μ©
self.pdf_directory = os.path.abspath(PDF_DIRECTORY)
self.cache_directory = os.path.abspath(CACHE_DIRECTORY)
self.index_file = os.path.join(self.cache_directory, "file_index.json")
self.chunks_dir = os.path.join(self.cache_directory, "chunks")
self.vector_index_dir = os.path.join(self.cache_directory, "vector_index")
logger.info(f"μ€μ λ PDF λλ ν 리 (μ λ κ²½λ‘): {self.pdf_directory}")
# λλ ν 리 κ²μ¦
self._verify_pdf_directory()
# λλ ν 리 μμ±
self._ensure_directories_exist()
logger.info(f"PDF λ¬Έμ λλ ν 리: '{self.pdf_directory}'")
logger.info(f"μΊμ λλ ν 리: '{self.cache_directory}'")
# μ»΄ν¬λνΈ μ΄κΈ°ν
try:
self.document_processor = OptimizedDocumentProcessor(
chunk_size=CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP
)
except Exception as e:
logger.error(f"λ¬Έμ μ²λ¦¬κΈ° μ΄κΈ°ν μ€ν¨: {e}")
raise DocumentProcessingError(f"λ¬Έμ μ²λ¦¬κΈ° μ΄κΈ°ν μ€ν¨: {str(e)}")
# λ²‘ν° μ μ₯μ μ΄κΈ°ν
try:
self.vector_store = VectorStore(use_milvus=False)
except Exception as e:
logger.error(f"λ²‘ν° μ μ₯μ μ΄κΈ°ν μ€ν¨: {e}")
raise VectorStoreError(f"λ²‘ν° μ μ₯μ μ΄κΈ°ν μ€ν¨: {str(e)}")
# λ¬Έμ μΈλ±μ€ λ‘λ
self.file_index = self._load_file_index()
# κΈ°λ³Έ λ³μ μ΄κΈ°ν
self.documents = []
self.processed_files = []
self.is_initialized = False
# μμ μ μλμΌλ‘ λ¬Έμ λ‘λ λ° μ²λ¦¬
logger.info("λ¬Έμ μλ λ‘λ λ° μ²λ¦¬ μμ...")
self.auto_process_documents()
logger.info("AutoRAGChatApp μ΄κΈ°ν μλ£")
except Exception as e:
logger.critical(f"μ ν리μΌμ΄μ
μ΄κΈ°ν μ€ μ¬κ°ν μ€λ₯: {e}", exc_info=True)
# κΈ°λ³Έ μν μ€μ μΌλ‘ μ΅μνμ κΈ°λ₯ μ μ§
self.pdf_directory = os.path.abspath(PDF_DIRECTORY)
self.documents = []
self.processed_files = []
self.is_initialized = False
self.file_index = {}
def _ensure_directories_exist(self) -> None:
"""
νμν λλ ν λ¦¬κ° μ‘΄μ¬νλμ§ νμΈνκ³ μμ±
"""
directories = [
self.pdf_directory,
self.cache_directory,
self.chunks_dir,
self.vector_index_dir
]
for directory in directories:
try:
os.makedirs(directory, exist_ok=True)
except Exception as e:
logger.error(f"λλ ν 리 μμ± μ€ν¨ '{directory}': {e}")
raise OSError(f"λλ ν 리 μμ± μ€ν¨ '{directory}': {str(e)}")
def _process_pdf_file(self, file_path: str) -> List[Document]:
"""
PDF νμΌ μ²λ¦¬ - docling μ€ν¨ μ PyPDFLoader μ¬μ©
Args:
file_path: μ²λ¦¬ν PDF νμΌ κ²½λ‘
Returns:
μ²λ¦¬λ λ¬Έμ μ²ν¬ 리μ€νΈ
"""
if not os.path.exists(file_path):
logger.error(f"νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
raise FileNotFoundError(f"νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
try:
logger.info(f"doclingμΌλ‘ μ²λ¦¬ μλ: {file_path}")
# docling μ¬μ© μλ
try:
# 10μ΄ νμμμ μ€μ (μ΅μ
)
import signal
def timeout_handler(signum, frame):
raise TimeoutError("docling μ²λ¦¬ μκ° μ΄κ³Ό (60μ΄)")
# 리λ
μ€/λ§₯μμλ§ μλ (μλμ°μμλ 무μλ¨)
try:
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(60) # 60μ΄ νμμμ
except (AttributeError, ValueError) as se:
logger.warning(f"μκ·Έλ μ€μ μ€ν¨ (μλμ° νκ²½μΌ μ μμ): {se}")
# doclingμΌλ‘ μ²λ¦¬ μλ
chunks = self.document_processor.process_pdf(file_path, use_docling=True)
# νμμμ μ·¨μ
try:
signal.alarm(0)
except (AttributeError, ValueError):
pass
return chunks
except TimeoutError as te:
logger.warning(f"docling μ²λ¦¬ μκ° μ΄κ³Ό: {te}")
logger.info("PyPDFLoaderλ‘ λ체ν©λλ€.")
# PyPDFLoaderλ‘ λ체
try:
return self.document_processor.process_pdf(file_path, use_docling=False)
except Exception as inner_e:
logger.error(f"PyPDFLoader μ²λ¦¬ μ€λ₯: {inner_e}", exc_info=True)
raise DocumentProcessingError(f"PDF λ‘λ© μ€ν¨ (PyPDFLoader): {str(inner_e)}")
except Exception as e:
# docling μ€λ₯ νμΈ
error_str = str(e)
if "Invalid code point" in error_str or "RuntimeError" in error_str:
logger.warning(f"docling μ²λ¦¬ μ€λ₯ (μ½λ ν¬μΈνΈ λ¬Έμ ): {error_str}")
logger.info("PyPDFLoaderλ‘ λ체ν©λλ€.")
else:
logger.warning(f"docling μ²λ¦¬ μ€λ₯: {error_str}")
logger.info("PyPDFLoaderλ‘ λ체ν©λλ€.")
# PyPDFLoaderλ‘ λ체
try:
return self.document_processor.process_pdf(file_path, use_docling=False)
except Exception as inner_e:
logger.error(f"PyPDFLoader μ²λ¦¬ μ€λ₯: {inner_e}", exc_info=True)
raise DocumentProcessingError(f"PDF λ‘λ© μ€ν¨ (PyPDFLoader): {str(inner_e)}")
except DocumentProcessingError:
# μ΄λ―Έ λνλ μμΈλ κ·Έλλ‘ μ λ¬
raise
except Exception as e:
logger.error(f"PDF μ²λ¦¬ μ€ μ¬κ°ν μ€λ₯: {e}", exc_info=True)
# λΉ μ²ν¬λΌλ λ°ννμ¬ μ 체 μ²λ¦¬κ° μ€λ¨λμ§ μλλ‘ ν¨
logger.warning(f"'{file_path}' μ²λ¦¬ μ€ν¨λ‘ λΉ μ²ν¬ λͺ©λ‘ λ°ν")
return []
def _load_file_index(self) -> Dict[str, Dict[str, Any]]:
"""
νμΌ μΈλ±μ€ λ‘λ
Returns:
νμΌ κ²½λ‘ -> λ©νλ°μ΄ν° λ§€ν
"""
if os.path.exists(self.index_file):
try:
with open(self.index_file, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError as e:
logger.error(f"μΈλ±μ€ νμΌ JSON νμ± μ€ν¨: {e}")
logger.warning("μμλ μΈλ±μ€ νμΌ, μλ‘μ΄ μΈλ±μ€λ₯Ό μμ±ν©λλ€.")
return {}
except Exception as e:
logger.error(f"μΈλ±μ€ νμΌ λ‘λ μ€ν¨: {e}")
return {}
return {}
def _save_file_index(self) -> None:
"""
νμΌ μΈλ±μ€ μ μ₯
"""
try:
with open(self.index_file, 'w', encoding='utf-8') as f:
json.dump(self.file_index, f, ensure_ascii=False, indent=2)
logger.debug("νμΌ μΈλ±μ€ μ μ₯ μλ£")
except Exception as e:
logger.error(f"νμΌ μΈλ±μ€ μ μ₯ μ€ν¨: {e}")
raise IOError(f"νμΌ μΈλ±μ€ μ μ₯ μ€ν¨: {str(e)}")
def _calculate_file_hash(self, file_path: str) -> str:
"""
νμΌ ν΄μ κ³μ°
Args:
file_path: νμΌ κ²½λ‘
Returns:
MD5 ν΄μκ°
"""
if not os.path.exists(file_path):
logger.error(f"ν΄μ κ³μ° μ€ν¨ - νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
raise FileNotFoundError(f"νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
try:
hasher = hashlib.md5()
with open(file_path, 'rb') as f:
buf = f.read(65536)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(65536)
return hasher.hexdigest()
except Exception as e:
logger.error(f"νμΌ ν΄μ κ³μ° μ€ μ€λ₯: {e}")
raise IOError(f"νμΌ ν΄μ κ³μ° μ€ν¨: {str(e)}")
def _is_file_processed(self, file_path: str) -> bool:
"""
νμΌμ΄ μ΄λ―Έ μ²λ¦¬λμκ³ λ³κ²½λμ§ μμλμ§ νμΈ
Args:
file_path: νμΌ κ²½λ‘
Returns:
μ²λ¦¬ μ¬λΆ
"""
# νμΌ μ‘΄μ¬ νμΈ
if not os.path.exists(file_path):
logger.warning(f"νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
return False
# μΈλ±μ€μ νμΌ μ‘΄μ¬ μ¬λΆ νμΈ
if file_path not in self.file_index:
return False
try:
# νμ¬ ν΄μκ° κ³μ°
current_hash = self._calculate_file_hash(file_path)
# μ μ₯λ ν΄μκ°κ³Ό λΉκ΅
if self.file_index[file_path]['hash'] != current_hash:
logger.info(f"νμΌ λ³κ²½ κ°μ§: {file_path}")
return False
# μ²ν¬ νμΌ μ‘΄μ¬ νμΈ
chunks_path = self.file_index[file_path]['chunks_path']
if not os.path.exists(chunks_path):
logger.warning(f"μ²ν¬ νμΌμ΄ μ‘΄μ¬νμ§ μμ: {chunks_path}")
return False
return True
except Exception as e:
logger.error(f"νμΌ μ²λ¦¬ μν νμΈ μ€ μ€λ₯: {e}")
return False
def _get_chunks_path(self, file_hash: str) -> str:
"""
μ²ν¬ νμΌ κ²½λ‘ μμ±
Args:
file_hash: νμΌ ν΄μκ°
Returns:
μ²ν¬ νμΌ κ²½λ‘
"""
return os.path.join(self.chunks_dir, f"{file_hash}.pkl")
def _save_chunks(self, file_path: str, chunks: List[Document]) -> None:
"""
μ²ν¬ λ°μ΄ν° μ μ₯
Args:
file_path: μλ³Έ νμΌ κ²½λ‘
chunks: λ¬Έμ μ²ν¬ 리μ€νΈ
"""
try:
# ν΄μ κ³μ°
file_hash = self._calculate_file_hash(file_path)
# μ²ν¬ νμΌ κ²½λ‘
chunks_path = self._get_chunks_path(file_hash)
# μ²ν¬ λ°μ΄ν° μ μ₯
with open(chunks_path, 'wb') as f:
pickle.dump(chunks, f)
# μΈλ±μ€ μ
λ°μ΄νΈ
self.file_index[file_path] = {
'hash': file_hash,
'chunks_path': chunks_path,
'last_processed': time.time(),
'chunks_count': len(chunks),
'file_size': os.path.getsize(file_path),
'file_name': os.path.basename(file_path)
}
# μΈλ±μ€ μ μ₯
self._save_file_index()
logger.info(f"μ²ν¬ μ μ₯ μλ£: {file_path} ({len(chunks)}κ° μ²ν¬)")
except Exception as e:
logger.error(f"μ²ν¬ μ μ₯ μ€ν¨: {e}", exc_info=True)
raise IOError(f"μ²ν¬ μ μ₯ μ€ν¨: {str(e)}")
def _load_chunks(self, file_path: str) -> List[Document]:
"""
μ μ₯λ μ²ν¬ λ°μ΄ν° λ‘λ
Args:
file_path: νμΌ κ²½λ‘
Returns:
λ¬Έμ μ²ν¬ 리μ€νΈ
"""
if file_path not in self.file_index:
logger.error(f"μΈλ±μ€μ νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
raise KeyError(f"μΈλ±μ€μ νμΌμ΄ μ‘΄μ¬νμ§ μμ: {file_path}")
chunks_path = self.file_index[file_path]['chunks_path']
if not os.path.exists(chunks_path):
logger.error(f"μ²ν¬ νμΌμ΄ μ‘΄μ¬νμ§ μμ: {chunks_path}")
raise FileNotFoundError(f"μ²ν¬ νμΌμ΄ μ‘΄μ¬νμ§ μμ: {chunks_path}")
try:
with open(chunks_path, 'rb') as f:
chunks = pickle.load(f)
logger.info(f"μ²ν¬ λ‘λ μλ£: {file_path} ({len(chunks)}κ° μ²ν¬)")
return chunks
except pickle.UnpicklingError as e:
logger.error(f"μ²ν¬ νμΌ μμ§λ ¬ν μ€ν¨: {e}")
raise IOError(f"μ²ν¬ νμΌ μμ: {str(e)}")
except Exception as e:
logger.error(f"μ²ν¬ λ‘λ μ€ν¨: {e}", exc_info=True)
raise IOError(f"μ²ν¬ λ‘λ μ€ν¨: {str(e)}")
def _verify_pdf_directory(self):
"""PDF λλ ν 리 κ²μ¦ λ° νμΌ μ‘΄μ¬ νμΈ"""
try:
# λλ ν 리 μ‘΄μ¬ νμΈ
if not os.path.exists(self.pdf_directory):
try:
logger.warning(f"PDF λλ ν λ¦¬κ° μ‘΄μ¬νμ§ μμ μμ±ν©λλ€: {self.pdf_directory}")
os.makedirs(self.pdf_directory, exist_ok=True)
except Exception as e:
logger.error(f"PDF λλ ν 리 μμ± μ€ν¨: {e}")
raise
# λλ ν 리μΈμ§ νμΈ
if not os.path.isdir(self.pdf_directory):
logger.error(f"PDF κ²½λ‘κ° λλ ν λ¦¬κ° μλλλ€: {self.pdf_directory}")
raise ConfigurationError(f"PDF κ²½λ‘κ° λλ ν λ¦¬κ° μλλλ€: {self.pdf_directory}")
# PDF νμΌ μ‘΄μ¬ νμΈ
pdf_files = [f for f in os.listdir(self.pdf_directory) if f.lower().endswith('.pdf')]
if pdf_files:
logger.info(f"PDF λλ ν 리μμ {len(pdf_files)}κ°μ PDF νμΌμ μ°Ύμμ΅λλ€: {pdf_files}")
else:
# μ¬λ¬ κ²½λ‘μμ PDF νμΌ νμ μλ
alternative_paths = [
"./documents",
"../documents",
"documents",
os.path.join(os.getcwd(), "documents")
]
found_pdfs = False
for alt_path in alternative_paths:
if os.path.exists(alt_path) and os.path.isdir(alt_path):
alt_pdf_files = [f for f in os.listdir(alt_path) if f.lower().endswith('.pdf')]
if alt_pdf_files:
logger.warning(f"λ체 κ²½λ‘ '{alt_path}'μμ PDF νμΌμ μ°Ύμμ΅λλ€. μ΄ κ²½λ‘λ₯Ό μ¬μ©ν©λλ€.")
self.pdf_directory = os.path.abspath(alt_path)
found_pdfs = True
break
if not found_pdfs:
logger.warning(f"PDF λλ ν 리μ PDF νμΌμ΄ μμ΅λλ€: {self.pdf_directory}")
logger.info("PDF νμΌμ λλ ν 리μ μΆκ°ν΄μ£ΌμΈμ.")
except Exception as e:
logger.error(f"PDF λλ ν 리 κ²μ¦ μ€ μ€λ₯: {e}", exc_info=True)
raise
def auto_process_documents(self) -> str:
"""
documents ν΄λμ PDF νμΌ μλ μ²λ¦¬
Returns:
μ²λ¦¬ κ²°κ³Ό λ©μμ§
"""
try:
start_time = time.time()
# PDF νμΌ λͺ©λ‘ μμ§μ κ°μ νμ¬ λ€μν κ²½λ‘ μ²λ¦¬
try:
pdf_files = []
# μ€μ λ λλ ν 리μμ PDF νμΌ μ°ΎκΈ°
logger.info(f"PDF νμΌ κ²μ κ²½λ‘: {self.pdf_directory}")
if os.path.exists(self.pdf_directory) and os.path.isdir(self.pdf_directory):
# λλ ν 리 λ΄μ© μΆλ ₯ (λλ²κΉ
μ©)
dir_contents = os.listdir(self.pdf_directory)
logger.info(f"λλ ν 리 λ΄μ©: {dir_contents}")
# PDF νμΌλ§ νν°λ§
for filename in os.listdir(self.pdf_directory):
if filename.lower().endswith('.pdf'):
file_path = os.path.join(self.pdf_directory, filename)
if os.path.isfile(file_path): # μ€μ νμΌμΈμ§ νμΈ
pdf_files.append(file_path)
logger.info(f"PDF νμΌ μ°Ύμ: {file_path}")
# λ°κ²¬λ λͺ¨λ νμΌ λ‘κ·Έ
logger.info(f"λ°κ²¬λ λͺ¨λ PDF νμΌ: {pdf_files}")
except FileNotFoundError:
logger.error(f"PDF λλ ν 리λ₯Ό μ°Ύμ μ μμ: {self.pdf_directory}")
return f"'{self.pdf_directory}' λλ ν 리λ₯Ό μ°Ύμ μ μμ΅λλ€. λλ ν λ¦¬κ° μ‘΄μ¬νλμ§ νμΈνμΈμ."
except PermissionError:
logger.error(f"PDF λλ ν 리 μ κ·Ό κΆν μμ: {self.pdf_directory}")
return f"'{self.pdf_directory}' λλ ν 리μ μ κ·Όν μ μμ΅λλ€. κΆνμ νμΈνμΈμ."
if not pdf_files:
logger.warning(f"'{self.pdf_directory}' ν΄λμ PDF νμΌμ΄ μμ΅λλ€.")
return f"'{self.pdf_directory}' ν΄λμ PDF νμΌμ΄ μμ΅λλ€."
logger.info(f"λ°κ²¬λ PDF νμΌ: {len(pdf_files)}κ°")
# ν΄λ λ΄ PDF νμΌ μ²λ¦¬
new_files = []
updated_files = []
cached_files = []
failed_files = []
all_chunks = []
for file_path in pdf_files:
try:
if self._is_file_processed(file_path):
# μΊμμμ μ²ν¬ λ‘λ
try:
chunks = self._load_chunks(file_path)
all_chunks.extend(chunks)
cached_files.append(file_path)
self.processed_files.append(os.path.basename(file_path))
except Exception as e:
logger.error(f"μΊμλ μ²ν¬ λ‘λ μ€ν¨: {e}")
# νμΌμ λ€μ μ²λ¦¬
logger.info(f"μΊμ μ€ν¨λ‘ νμΌ μ¬μ²λ¦¬: {file_path}")
chunks = self._process_pdf_file(file_path)
if chunks:
self._save_chunks(file_path, chunks)
all_chunks.extend(chunks)
updated_files.append(file_path)
self.processed_files.append(os.path.basename(file_path))
else:
failed_files.append(file_path)
else:
# μ νμΌ λλ λ³κ²½λ νμΌ μ²λ¦¬
logger.info(f"μ²λ¦¬ μ€: {file_path}")
try:
# κ°μ λ PDF μ²λ¦¬ λ©μλ μ¬μ©
chunks = self._process_pdf_file(file_path)
if chunks: # μ²ν¬κ° μλ κ²½μ°μλ§ μ μ₯
# μ²ν¬ μ μ₯
self._save_chunks(file_path, chunks)
all_chunks.extend(chunks)
if file_path in self.file_index:
updated_files.append(file_path)
else:
new_files.append(file_path)
self.processed_files.append(os.path.basename(file_path))
else:
logger.warning(f"'{file_path}' μ²λ¦¬ μ€ν¨: μΆμΆλ μ²ν¬ μμ")
failed_files.append(file_path)
except Exception as e:
logger.error(f"'{file_path}' μ²λ¦¬ μ€ μ€λ₯: {e}", exc_info=True)
failed_files.append(file_path)
except Exception as e:
logger.error(f"'{file_path}' νμΌ μ²λ¦¬ 루ν μ€ μ€λ₯: {e}", exc_info=True)
failed_files.append(file_path)
# λͺ¨λ μ²ν¬ μ μ₯
self.documents = all_chunks
processing_time = time.time() - start_time
logger.info(f"λ¬Έμ μ²λ¦¬ μλ£: {len(all_chunks)}κ° μ²ν¬, {processing_time:.2f}μ΄")
# λ²‘ν° μΈλ±μ€ μ²λ¦¬
try:
self._process_vector_index(new_files, updated_files)
except Exception as e:
logger.error(f"λ²‘ν° μΈλ±μ€ μ²λ¦¬ μ€ν¨: {e}", exc_info=True)
return f"λ¬Έμλ μ²λ¦¬λμμΌλ λ²‘ν° μΈλ±μ€ μμ±μ μ€ν¨νμ΅λλ€: {str(e)}"
# RAG μ²΄μΈ μ΄κΈ°ν
if RAG_CHAIN_AVAILABLE:
try:
logger.info("RAGChainμΌλ‘ μ΄κΈ°νλ₯Ό μλν©λλ€.")
self.rag_chain = RAGChain(self.vector_store)
self.is_initialized = True
logger.info("RAG μ²΄μΈ μ΄κΈ°ν μ±κ³΅")
except Exception as e:
logger.error(f"RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {e}", exc_info=True)
# FallbackRAGChainμΌλ‘ λ체 μλ
try:
logger.info("FallbackRAGChainμΌλ‘ λ체ν©λλ€...")
from fallback_rag_chain import FallbackRAGChain
self.rag_chain = FallbackRAGChain(self.vector_store)
self.is_initialized = True
logger.info("ν΄λ°± RAG μ²΄μΈ μ΄κΈ°ν μ±κ³΅")
except Exception as fallback_e:
logger.error(f"ν΄λ°± RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {fallback_e}", exc_info=True)
# SimpleRAGChain μλ (μ΅νμ μλ¨)
try:
logger.info("SimpleRAGChainμΌλ‘ λ체ν©λλ€...")
from simple_rag_chain import SimpleRAGChain
# API μ 보 κ°μ Έμ€κΈ°
try:
from config import DEEPSEEK_API_KEY, DEEPSEEK_MODEL, DEEPSEEK_ENDPOINT
logger.info(f"μ€μ νμΌμμ DeepSeek API μ 보λ₯Ό λ‘λνμ΅λλ€: λͺ¨λΈ={DEEPSEEK_MODEL}")
except ImportError:
# μ€μ νμΌμμ κ°μ Έμ¬ μ μλ κ²½μ° νκ²½ λ³μ νμΈ
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "")
DEEPSEEK_MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-chat")
DEEPSEEK_ENDPOINT = os.environ.get("DEEPSEEK_ENDPOINT",
"https://api.deepseek.com/v1/chat/completions")
logger.info(f"νκ²½ λ³μμμ DeepSeek API μ 보λ₯Ό λ‘λνμ΅λλ€: λͺ¨λΈ={DEEPSEEK_MODEL}")
# SimpleRAGChain μ΄κΈ°ν μλ
self.rag_chain = SimpleRAGChain(self.vector_store)
self.is_initialized = True
logger.info("SimpleRAGChain μ΄κΈ°ν μ±κ³΅")
except Exception as simple_e:
logger.error(f"λͺ¨λ RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {simple_e}", exc_info=True)
return f"λ¬Έμμ λ²‘ν° μΈλ±μ€λ μ²λ¦¬λμμΌλ RAG μ²΄μΈ μ΄κΈ°νμ μ€ν¨νμ΅λλ€: {str(e)}"
else:
# RAGChainμ μ¬μ©ν μ μλ κ²½μ°
try:
logger.info("κΈ°λ³Έ RAG Chainμ μ¬μ©ν μ μμ΄ λ체 λ²μ μ μλν©λλ€...")
# FallbackRAGChain μλ
try:
from fallback_rag_chain import FallbackRAGChain
self.rag_chain = FallbackRAGChain(self.vector_store)
self.is_initialized = True
logger.info("ν΄λ°± RAG μ²΄μΈ μ΄κΈ°ν μ±κ³΅")
except Exception as fallback_e:
logger.error(f"ν΄λ°± RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {fallback_e}", exc_info=True)
# SimpleRAGChain μλ (μ΅νμ μλ¨)
try:
from simple_rag_chain import SimpleRAGChain
self.rag_chain = SimpleRAGChain(self.vector_store)
self.is_initialized = True
logger.info("SimpleRAGChain μ΄κΈ°ν μ±κ³΅")
except Exception as simple_e:
logger.error(f"λͺ¨λ RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {simple_e}", exc_info=True)
return f"λ¬Έμμ λ²‘ν° μΈλ±μ€λ μ²λ¦¬λμμΌλ RAG μ²΄μΈ μ΄κΈ°νμ μ€ν¨νμ΅λλ€"
except Exception as e:
logger.error(f"RAG μ²΄μΈ μ΄κΈ°ν μ€ν¨: {e}", exc_info=True)
return f"λ¬Έμμ λ²‘ν° μΈλ±μ€λ μ²λ¦¬λμμΌλ RAG μ²΄μΈ μ΄κΈ°νμ μ€ν¨νμ΅λλ€: {str(e)}"
# μ±κ³΅ λ©μμ§ μμ±
result_message = f"""λ¬Έμ μ²λ¦¬ μλ£!
- μ²λ¦¬λ νμΌ: {len(pdf_files)}κ°
- μΊμλ νμΌ: {len(cached_files)}κ°
- μ νμΌ: {len(new_files)}κ°
- μ
λ°μ΄νΈλ νμΌ: {len(updated_files)}κ°
- μ€ν¨ν νμΌ: {len(failed_files)}κ°
- μ΄ μ²ν¬ μ: {len(all_chunks)}κ°
- μ²λ¦¬ μκ°: {processing_time:.2f}μ΄
μ΄μ μ§λ¬Έν μ€λΉκ° λμμ΅λλ€!"""
return result_message
except Exception as e:
error_message = f"λ¬Έμ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}"
logger.error(error_message, exc_info=True)
return error_message
def _process_vector_index(self, new_files: List[str], updated_files: List[str]) -> None:
"""
λ²‘ν° μΈλ±μ€ μ²λ¦¬
Args:
new_files: μλ‘ μΆκ°λ νμΌ λͺ©λ‘
updated_files: μ
λ°μ΄νΈλ νμΌ λͺ©λ‘
"""
# λ²‘ν° μΈλ±μ€ μ μ₯ κ²½λ‘ νμΈ
if os.path.exists(self.vector_index_dir) and any(os.listdir(self.vector_index_dir)):
# κΈ°μ‘΄ λ²‘ν° μΈλ±μ€ λ‘λ
try:
logger.info("μ μ₯λ λ²‘ν° μΈλ±μ€ λ‘λ μ€...")
vector_store_loaded = self.vector_store.load_local(self.vector_index_dir)
# μΈλ±μ€ λ‘λ μ±κ³΅ νμΈ
if self.vector_store.vector_store is not None:
# μ λ¬Έμλ λ³κ²½λ λ¬Έμκ° μμΌλ©΄ μΈλ±μ€ μ
λ°μ΄νΈ
if new_files or updated_files:
logger.info("λ²‘ν° μΈλ±μ€ μ
λ°μ΄νΈ μ€...")
self.vector_store.add_documents(self.documents)
logger.info("λ²‘ν° μΈλ±μ€ λ‘λ μλ£")
else:
logger.warning("λ²‘ν° μΈλ±μ€λ₯Ό λ‘λνμΌλ μ ν¨νμ§ μμ, μλ‘ μμ±ν©λλ€.")
self.vector_store.create_or_load(self.documents)
except Exception as e:
logger.error(f"λ²‘ν° μΈλ±μ€ λ‘λ μ€ν¨, μλ‘ μμ±ν©λλ€: {e}", exc_info=True)
# μ λ²‘ν° μΈλ±μ€ μμ±
self.vector_store.create_or_load(self.documents)
else:
# μ λ²‘ν° μΈλ±μ€ μμ±
logger.info("μ λ²‘ν° μΈλ±μ€ μμ± μ€...")
self.vector_store.create_or_load(self.documents)
# λ²‘ν° μΈλ±μ€ μ μ₯
if self.vector_store and self.vector_store.vector_store is not None:
try:
logger.info(f"λ²‘ν° μΈλ±μ€ μ μ₯ μ€: {self.vector_index_dir}")
save_result = self.vector_store.save_local(self.vector_index_dir)
logger.info(f"λ²‘ν° μΈλ±μ€ μ μ₯ μλ£: {self.vector_index_dir}")
except Exception as e:
logger.error(f"λ²‘ν° μΈλ±μ€ μ μ₯ μ€ν¨: {e}", exc_info=True)
raise VectorStoreError(f"λ²‘ν° μΈλ±μ€ μ μ₯ μ€ν¨: {str(e)}")
else:
logger.warning("λ²‘ν° μΈλ±μ€κ° μ΄κΈ°νλμ§ μμ μ μ₯νμ§ μμ΅λλ€.")
def reset_cache(self) -> str:
"""
μΊμ μ΄κΈ°ν
Returns:
κ²°κ³Ό λ©μμ§
"""
try:
# μ²ν¬ νμΌ μμ
try:
for filename in os.listdir(self.chunks_dir):
file_path = os.path.join(self.chunks_dir, filename)
if os.path.isfile(file_path):
os.remove(file_path)
logger.info("μ²ν¬ μΊμ νμΌ μμ μλ£")
except Exception as e:
logger.error(f"μ²ν¬ νμΌ μμ μ€ μ€λ₯: {e}")
return f"μ²ν¬ νμΌ μμ μ€ μ€λ₯ λ°μ: {str(e)}"
# μΈλ±μ€ μ΄κΈ°ν
self.file_index = {}
try:
self._save_file_index()
logger.info("νμΌ μΈλ±μ€ μ΄κΈ°ν μλ£")
except Exception as e:
logger.error(f"μΈλ±μ€ νμΌ μ΄κΈ°ν μ€ μ€λ₯: {e}")
return f"μΈλ±μ€ νμΌ μ΄κΈ°ν μ€ μ€λ₯ λ°μ: {str(e)}"
# λ²‘ν° μΈλ±μ€ μμ
try:
for filename in os.listdir(self.vector_index_dir):
file_path = os.path.join(self.vector_index_dir, filename)
if os.path.isfile(file_path):
os.remove(file_path)
logger.info("λ²‘ν° μΈλ±μ€ νμΌ μμ μλ£")
except Exception as e:
logger.error(f"λ²‘ν° μΈλ±μ€ νμΌ μμ μ€ μ€λ₯: {e}")
return f"λ²‘ν° μΈλ±μ€ νμΌ μμ μ€ μ€λ₯ λ°μ: {str(e)}"
self.documents = []
self.processed_files = []
self.is_initialized = False
logger.info("μΊμ μ΄κΈ°ν μλ£")
return "μΊμκ° μ΄κΈ°νλμμ΅λλ€. λ€μ μ€ν μ λͺ¨λ λ¬Έμκ° λ€μ μ²λ¦¬λ©λλ€."
except Exception as e:
error_msg = f"μΊμ μ΄κΈ°ν μ€ μ€λ₯ λ°μ: {str(e)}"
logger.error(error_msg, exc_info=True)
return error_msg
def process_query(self, query: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
"""
μ¬μ©μ 쿼리 μ²λ¦¬
Args:
query: μ¬μ©μ μ§λ¬Έ
chat_history: λν κΈ°λ‘
Returns:
μλ΅ λ° μ
λ°μ΄νΈλ λν κΈ°λ‘
"""
if not query or not query.strip():
response = "μ§λ¬Έμ΄ λΉμ΄ μμ΅λλ€. μ§λ¬Έμ μ
λ ₯ν΄ μ£ΌμΈμ."
chat_history.append((query, response))
return "", chat_history
if not self.is_initialized:
response = "λ¬Έμ λ‘λκ° μ΄κΈ°νλμ§ μμμ΅λλ€. μλ λ‘λλ₯Ό μλν©λλ€."
chat_history.append((query, response))
# μλ λ‘λ μλ
try:
init_result = self.auto_process_documents()
if not self.is_initialized:
response = f"λ¬Έμλ₯Ό λ‘λν μ μμ΅λλ€. 'documents' ν΄λμ PDF νμΌμ΄ μλμ§ νμΈνμΈμ. μ΄κΈ°ν κ²°κ³Ό: {init_result}"
chat_history.append((query, response))
return "", chat_history
except Exception as e:
response = f"λ¬Έμ λ‘λ μ€ μ€λ₯ λ°μ: {str(e)}"
logger.error(f"μλ λ‘λ μ€ν¨: {e}", exc_info=True)
chat_history.append((query, response))
return "", chat_history
try:
# RAG μ²΄μΈ μ€ν λ° μλ΅ μμ±
start_time = time.time()
logger.info(f"쿼리 μ²λ¦¬ μμ: {query}")
# rag_chainμ΄ μ΄κΈ°νλμλμ§ νμΈ
if not hasattr(self, 'rag_chain') or self.rag_chain is None:
raise RAGInitializationError("RAG 체μΈμ΄ μ΄κΈ°νλμ§ μμμ΅λλ€")
# 1. λ¨Όμ νμ€ RAG 체μΈμΌλ‘ μλ
try:
response = self.rag_chain.run(query)
logger.info(f"κΈ°λ³Έ RAG 체μΈμΌλ‘ μλ΅ μμ± μ±κ³΅")
except Exception as rag_error:
logger.error(f"κΈ°λ³Έ RAG μ²΄μΈ μ€ν μ€ν¨: {rag_error}, λμ μλ")
# 2. DeepSeek API μ§μ νΈμΆ μλ (RAG μ²΄μΈ μ°ν)
try:
# DeepSeek API μ 보 κ°μ Έμ€κΈ°
try:
from config import DEEPSEEK_API_KEY, DEEPSEEK_MODEL, DEEPSEEK_ENDPOINT
except ImportError:
# μ€μ λͺ¨λμμ κ°μ Έμ¬ μ μλ κ²½μ° κΈ°λ³Έκ° μ€μ
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "")
DEEPSEEK_MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-chat")
DEEPSEEK_ENDPOINT = os.environ.get("DEEPSEEK_ENDPOINT",
"https://api.deepseek.com/v1/chat/completions")
# μ§μ API νΈμΆ ν¨μ μ μ (μΈλΆ λͺ¨λ μμ‘΄μ± μ κ±°)
def direct_api_call(query, context, api_key, model_name, endpoint, max_retries=3, timeout=60):
"""DeepSeek API μ§μ νΈμΆ ν¨μ"""
import requests
import json
import time
# ν둬ννΈ κΈΈμ΄ μ ν
if len(context) > 6000:
context = context[:2500] + "\n...(μ€λ΅)...\n" + context[-2500:]
# ν둬ννΈ κ΅¬μ±
prompt = f"""
λ€μ μ 보λ₯Ό κΈ°λ°μΌλ‘ μ§λ¬Έμ μ ννκ² λ΅λ³ν΄μ£ΌμΈμ.
μ§λ¬Έ: {query}
μ°Έκ³ μ 보:
{context}
μ°Έκ³ μ 보μ λ΅μ΄ μμΌλ©΄ λ°λμ κ·Έ μ 보λ₯Ό κΈ°λ°μΌλ‘ λ΅λ³νμΈμ.
μ°Έκ³ μ 보μ λ΅μ΄ μλ κ²½μ°μλ μΌλ°μ μΈ μ§μμ νμ©νμ¬ λ΅λ³ν μ μμ§λ§, "μ 곡λ λ¬Έμμλ μ΄ μ λ³΄κ° μμΌλ, μΌλ°μ μΌλ‘λ..." μμΌλ‘ μμνμΈμ.
λ΅λ³μ μ ννκ³ κ°κ²°νκ² μ 곡νλ, κ°λ₯ν μ°Έκ³ μ 보μμ κ·Όκ±°λ₯Ό μ°Ύμ μ€λͺ
ν΄μ£ΌμΈμ.
μ°Έκ³ μ 보μ μΆμ²λ ν¨κ» μλ €μ£ΌμΈμ.
"""
# API μμ² μλ
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": model_name,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.3,
"max_tokens": 1000
}
# μ¬μλ λ‘μ§
retry_delay = 1.0
for attempt in range(max_retries):
try:
logger.info(f"DeepSeek API μ§μ νΈμΆ μλ ({attempt + 1}/{max_retries})...")
response = requests.post(
endpoint,
headers=headers,
json=payload,
timeout=timeout
)
if response.status_code == 200:
result = response.json()
content = result.get("choices", [{}])[0].get("message", {}).get("content", "")
logger.info(f"DeepSeek API μ§μ νΈμΆ μ±κ³΅")
return content
else:
logger.warning(f"API μ€λ₯: μν μ½λ {response.status_code}")
# μμ² νλμΈ κ²½μ° λ μ€λ λκΈ°
if response.status_code == 429:
retry_delay = min(retry_delay * 3, 15)
else:
retry_delay = min(retry_delay * 2, 10)
if attempt < max_retries - 1:
logger.info(f"{retry_delay}μ΄ ν μ¬μλ...")
time.sleep(retry_delay)
except Exception as e:
logger.error(f"API νΈμΆ μ€λ₯: {e}")
if attempt < max_retries - 1:
logger.info(f"{retry_delay}μ΄ ν μ¬μλ...")
time.sleep(retry_delay)
retry_delay = min(retry_delay * 2, 10)
# λͺ¨λ μλ μ€ν¨
raise Exception("μ΅λ μ¬μλ νμ μ΄κ³Ό")
# λ²‘ν° κ²μ μν
if self.vector_store and hasattr(self.vector_store, "similarity_search"):
logger.info("λ²‘ν° κ²μ μν...")
docs = self.vector_store.similarity_search(query, k=5)
# κ²μ κ²°κ³Ό 컨ν
μ€νΈ ꡬμ±
context_parts = []
for i, doc in enumerate(docs, 1):
source = doc.metadata.get("source", "μ μ μλ μΆμ²")
page = doc.metadata.get("page", "")
source_info = f"{source}"
if page:
source_info += f" (νμ΄μ§: {page})"
context_parts.append(f"[μ°Έκ³ μλ£ {i}] - μΆμ²: {source_info}\n{doc.page_content}\n")
context = "\n".join(context_parts)
# μ§μ API νΈμΆ
logger.info("DeepSeek API μ§μ νΈμΆ μλ...")
response = direct_api_call(
query,
context,
DEEPSEEK_API_KEY,
DEEPSEEK_MODEL,
DEEPSEEK_ENDPOINT,
max_retries=3,
timeout=120
)
logger.info("DeepSeek API μ§μ νΈμΆ μ±κ³΅")
else:
raise Exception("λ²‘ν° μ€ν μ΄κ° μ΄κΈ°νλμ§ μμμ΅λλ€")
except Exception as direct_api_error:
logger.error(f"DeepSeek API μ§μ νΈμΆ μ€ν¨: {direct_api_error}, κ²μ κ²°κ³Ό λ°ν")
# 3. κ²μ κ²°κ³Όλ§μ΄λΌλ λ°ν
try:
# λ²‘ν° κ²μ μν
if self.vector_store and hasattr(self.vector_store, "similarity_search"):
docs = self.vector_store.similarity_search(query, k=5)
# κ²μ κ²°κ³Ό 컨ν
μ€νΈ ꡬμ±
context_parts = []
for i, doc in enumerate(docs, 1):
source = doc.metadata.get("source", "μ μ μλ μΆμ²")
page = doc.metadata.get("page", "")
source_info = f"{source}"
if page:
source_info += f" (νμ΄μ§: {page})"
context_parts.append(f"[μ°Έκ³ μλ£ {i}] - μΆμ²: {source_info}\n{doc.page_content}\n")
context = "\n".join(context_parts)
# κ°λ¨ν μλ΅ μμ±
predefined_answers = {
"λνλ―Όκ΅μ μλ": "λνλ―Όκ΅μ μλλ μμΈμ
λλ€.",
"μλ": "λνλ―Όκ΅μ μλλ μμΈμ
λλ€.",
"λꡬμΌ": "μ λ RAG κΈ°λ° μ§μμλ΅ μμ€ν
μ
λλ€. λ¬Έμλ₯Ό κ²μνκ³ κ΄λ ¨ μ 보λ₯Ό μ°Ύμλ립λλ€.",
"μλ
": "μλ
νμΈμ! 무μμ λμλ릴κΉμ?",
"λν΄": "μ¬μ©μμ μ§λ¬Έμ λ΅λ³νκΈ° μν΄ λ¬Έμλ₯Ό κ²μνκ³ μμ΅λλ€. 무μμ μλ €λ릴κΉμ?"
}
# μ§λ¬Έμ λ§λ 미리 μ μλ μλ΅μ΄ μλμ§ νμΈ
for key, answer in predefined_answers.items():
if key in query.lower():
response = answer
logger.info(f"미리 μ μλ μλ΅ μ 곡: {key}")
break
else:
# 미리 μ μλ μλ΅μ΄ μμΌλ©΄ κ²μ κ²°κ³Όλ§ νμ
response = f"""
API μλ² μ°κ²°μ λ¬Έμ κ° μμ΄ κ²μ κ²°κ³Όλ§ νμν©λλ€.
μ§λ¬Έ: {query}
κ²μλ κ΄λ ¨ λ¬Έμ:
{context}
[μ°Έκ³ ] API μ°κ²° λ¬Έμ λ‘ μΈν΄ μλ μμ½μ΄ μ 곡λμ§ μμ΅λλ€. λ€μ μλνκ±°λ λ€λ₯Έ μ§λ¬Έμ ν΄λ³΄μΈμ.
"""
logger.info("κ²μ κ²°κ³Όλ§ νμ")
else:
response = f"API μ°κ²° λ° λ²‘ν° κ²μμ λͺ¨λ μ€ν¨νμ΅λλ€. μμ€ν
κ΄λ¦¬μμκ² λ¬ΈμνμΈμ."
except Exception as fallback_error:
logger.error(f"μ΅μ’
ν΄λ°± μλ΅ μμ± μ€ν¨: {fallback_error}")
# 4. μ΅νμ λ°©λ²: μ€λ₯ λ©μμ§λ₯Ό μλ΅μΌλ‘ λ°ν
if "Connection error" in str(rag_error) or "timeout" in str(rag_error).lower():
response = f"""
API μλ² μ°κ²°μ λ¬Έμ κ° μμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ.
μ§λ¬Έ: {query}
[μ°Έκ³ ] νμ¬ DeepSeek API μλ²μμ μ°κ²°μ΄ μννμ§ μμ΅λλ€. μ΄λ‘ μΈν΄ μ§λ¬Έμ λν μλ΅μ μ 곡ν μ μμ΅λλ€.
"""
else:
response = f"쿼리 μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(rag_error)}"
end_time = time.time()
query_time = end_time - start_time
logger.info(f"쿼리 μ²λ¦¬ μλ£: {query_time:.2f}μ΄")
chat_history.append((query, response))
return "", chat_history
except RAGInitializationError as e:
error_msg = f"RAG μμ€ν
μ΄κΈ°ν μ€λ₯: {str(e)}. 'documents' ν΄λμ PDF νμΌμ΄ μλμ§ νμΈνκ³ , μ¬μμν΄ λ³΄μΈμ."
logger.error(f"쿼리 μ²λ¦¬ μ€ RAG μ΄κΈ°ν μ€λ₯: {e}", exc_info=True)
chat_history.append((query, error_msg))
return "", chat_history
except (VectorStoreError, DocumentProcessingError) as e:
error_msg = f"λ¬Έμ μ²λ¦¬ μμ€ν
μ€λ₯: {str(e)}. λ¬Έμ νμμ΄ μ¬λ°λ₯Έμ§ νμΈν΄ 보μΈμ."
logger.error(f"쿼리 μ²λ¦¬ μ€ λ¬Έμ/λ²‘ν° μ€ν μ΄ μ€λ₯: {e}", exc_info=True)
chat_history.append((query, error_msg))
return "", chat_history
except Exception as e:
error_msg = f"쿼리 μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}"
logger.error(f"쿼리 μ²λ¦¬ μ€ μμμΉ λͺ»ν μ€λ₯: {e}", exc_info=True)
chat_history.append((query, error_msg))
return "", chat_history
def launch_app(self) -> None:
"""
Gradio μ± μ€ν
"""
try:
import gradio as gr
except ImportError:
logger.error("Gradio λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°Ύμ μ μμ΅λλ€. pip install gradioλ‘ μ€μΉνμΈμ.")
print("Gradio λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°Ύμ μ μμ΅λλ€. pip install gradioλ‘ μ€μΉνμΈμ.")
return
try:
with gr.Blocks(title="PDF λ¬Έμ κΈ°λ° RAG μ±λ΄") as app:
gr.Markdown("# PDF λ¬Έμ κΈ°λ° RAG μ±λ΄")
gr.Markdown(f"* μ¬μ© μ€μΈ LLM λͺ¨λΈ: **{LLM_MODEL}**")
# μ¬κΈ°λ₯Ό μμ : μ€μ κ²½λ‘ νμ
actual_pdf_dir = self.pdf_directory.replace('\\', '\\\\') if os.name == 'nt' else self.pdf_directory
gr.Markdown(f"* PDF λ¬Έμ ν΄λ: **{actual_pdf_dir}**")
with gr.Row():
with gr.Column(scale=1):
# λ¬Έμ μν μΉμ
status_box = gr.Textbox(
label="λ¬Έμ μ²λ¦¬ μν",
value=self._get_status_message(),
lines=5,
interactive=False
)
# μΊμ κ΄λ¦¬ λ²νΌ
refresh_button = gr.Button("λ¬Έμ μλ‘ μ½κΈ°", variant="primary")
reset_button = gr.Button("μΊμ μ΄κΈ°ν", variant="stop")
# μν λ° μ€λ₯ νμ
status_info = gr.Markdown(
value=f"μμ€ν
μν: {'μ΄κΈ°νλ¨' if self.is_initialized else 'μ΄κΈ°νλμ§ μμ'}"
)
# μ²λ¦¬λ νμΌ μ 보
with gr.Accordion("μΊμ μΈλΆ μ 보", open=False):
cache_info = gr.Textbox(
label="μΊμλ νμΌ μ 보",
value=self._get_cache_info(),
lines=5,
interactive=False
)
with gr.Column(scale=2):
# μ±ν
μΈν°νμ΄μ€
chatbot = gr.Chatbot(
label="λν λ΄μ©",
bubble_full_width=False,
height=500,
show_copy_button=True
)
# μ§λ¬Έ μ
λ ₯κ³Ό μ μ‘ λ²νΌμ μνμΌλ‘ λ°°μΉ
with gr.Row():
query_box = gr.Textbox(
label="μ§λ¬Έ",
placeholder="μ²λ¦¬λ λ¬Έμ λ΄μ©μ λν΄ μ§λ¬ΈνμΈμ...",
lines=2,
scale=4
)
submit_btn = gr.Button("μ μ‘", variant="primary", scale=1)
clear_chat_button = gr.Button("λν μ΄κΈ°ν")
# μ΄λ²€νΈ νΈλ€λ¬ μ€μ
def update_ui_after_refresh(result):
return (
result, # μν λ©μμ§
self._get_status_message(), # μν λ°μ€ μ
λ°μ΄νΈ
f"μμ€ν
μν: {'μ΄κΈ°νλ¨' if self.is_initialized else 'μ΄κΈ°νλμ§ μμ'}", # μν μ 보 μ
λ°μ΄νΈ
self._get_cache_info() # μΊμ μ 보 μ
λ°μ΄νΈ
)
# λ¬Έμ μλ‘ μ½κΈ° λ²νΌ
refresh_button.click(
fn=lambda: update_ui_after_refresh(self.auto_process_documents()),
inputs=[],
outputs=[status_box, status_box, status_info, cache_info]
)
# μΊμ μ΄κΈ°ν λ²νΌ
def reset_and_process():
reset_result = self.reset_cache()
process_result = self.auto_process_documents()
return update_ui_after_refresh(f"{reset_result}\n\n{process_result}")
reset_button.click(
fn=reset_and_process,
inputs=[],
outputs=[status_box, status_box, status_info, cache_info]
)
# μ μ‘ λ²νΌ ν΄λ¦ μ΄λ²€νΈ
submit_btn.click(
fn=self.process_query,
inputs=[query_box, chatbot],
outputs=[query_box, chatbot]
)
# μν°ν€ μ
λ ₯ μ΄λ²€νΈ
query_box.submit(
fn=self.process_query,
inputs=[query_box, chatbot],
outputs=[query_box, chatbot]
)
# λν μ΄κΈ°ν λ²νΌ
clear_chat_button.click(
fn=lambda: [],
outputs=[chatbot]
)
# μ± μ€ν
app.launch(share=False)
except Exception as e:
logger.error(f"Gradio μ± μ€ν μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
print(f"Gradio μ± μ€ν μ€ μ€λ₯ λ°μ: {e}")
def _get_status_message(self) -> str:
"""
νμ¬ μ²λ¦¬ μν λ©μμ§ μμ±
Returns:
μν λ©μμ§
"""
if not self.processed_files:
return "μ²λ¦¬λ λ¬Έμκ° μμ΅λλ€. 'λ¬Έμ μλ‘ μ½κΈ°' λ²νΌμ ν΄λ¦νμΈμ."
# DeepSeek API μν νμΈ
from config import USE_DEEPSEEK, DEEPSEEK_API_KEY, DEEPSEEK_MODEL
model_info = ""
if USE_DEEPSEEK and DEEPSEEK_API_KEY:
# DeepSeek API ν
μ€νΈ μν
try:
# ν
μ€νΈ ν¨μ κ°μ Έμ€κΈ° μλ
try:
from deepseek_utils import test_deepseek_api
# DeepSeek μ€μ κ°μ Έμ€κΈ°
from config import DEEPSEEK_ENDPOINT
# API ν
μ€νΈ
test_result = test_deepseek_api(DEEPSEEK_API_KEY, DEEPSEEK_ENDPOINT, DEEPSEEK_MODEL)
if test_result["success"]:
model_info = f"\nDeepSeek API μν: μ μ ({DEEPSEEK_MODEL})"
else:
model_info = f"\nDeepSeek API μν: μ€λ₯ - {test_result['message']}"
except ImportError:
# μ§μ ν
μ€νΈ μ€ν
import requests
import json
# DeepSeek μ€μ κ°μ Έμ€κΈ°
from config import DEEPSEEK_ENDPOINT
# ν
μ€νΈμ© κ°λ¨ν ν둬ννΈ
test_prompt = "Hello, please respond with a short greeting."
# API μμ² ν€λ λ° λ°μ΄ν°
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {DEEPSEEK_API_KEY}"
}
payload = {
"model": DEEPSEEK_MODEL,
"messages": [{"role": "user", "content": test_prompt}],
"temperature": 0.7,
"max_tokens": 50
}
# API μμ² μ μ‘
try:
response = requests.post(
DEEPSEEK_ENDPOINT,
headers=headers,
data=json.dumps(payload),
timeout=5 # 5μ΄ νμμμ (UI λ°μμ± μ μ§)
)
# μλ΅ νμΈ
if response.status_code == 200:
model_info = f"\nDeepSeek API μν: μ μ ({DEEPSEEK_MODEL})"
else:
error_message = response.text[:100]
model_info = f"\nDeepSeek API μν: μ€λ₯ (μν μ½λ: {response.status_code})"
except Exception as e:
model_info = f"\nDeepSeek API μν: μ°κ²° μ€ν¨ ({str(e)[:100]})"
except Exception as e:
model_info = f"\nDeepSeek API μν νμΈ μ€ν¨: {str(e)[:100]}"
return f"μ²λ¦¬λ λ¬Έμ ({len(self.processed_files)}κ°): {', '.join(self.processed_files)}{model_info}"
def _get_cache_info(self) -> str:
"""
μΊμ μΈλΆ μ 보 λ©μμ§ μμ±
Returns:
μΊμ μ 보 λ©μμ§
"""
if not self.file_index:
return "μΊμλ νμΌμ΄ μμ΅λλ€."
file_info = ""
for file_path, info in self.file_index.items():
file_name = info.get('file_name', os.path.basename(file_path))
chunks_count = info.get('chunks_count', 0)
file_size = info.get('file_size', 0)
last_processed = info.get('last_processed', 0)
# νμΌ ν¬κΈ°λ₯Ό μ¬λμ΄ μ½κΈ° μ¬μ΄ ννλ‘ λ³ν
if file_size < 1024:
size_str = f"{file_size} bytes"
elif file_size < 1024 * 1024:
size_str = f"{file_size / 1024:.1f} KB"
else:
size_str = f"{file_size / (1024 * 1024):.1f} MB"
# λ§μ§λ§ μ²λ¦¬ μκ°μ λ μ§/μκ° νμμΌλ‘ λ³ν
if last_processed:
from datetime import datetime
last_time = datetime.fromtimestamp(last_processed).strftime('%Y-%m-%d %H:%M:%S')
else:
last_time = "μ μ μμ"
file_info += f"- {file_name}: {chunks_count}κ° μ²ν¬, {size_str}, λ§μ§λ§ μ²λ¦¬: {last_time}\n"
return file_info
def launch_app(self) -> None:
"""
Gradio μ± μ€ν
"""
try:
import gradio as gr
except ImportError:
logger.error("Gradio λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°Ύμ μ μμ΅λλ€. pip install gradioλ‘ μ€μΉνμΈμ.")
print("Gradio λΌμ΄λΈλ¬λ¦¬λ₯Ό μ°Ύμ μ μμ΅λλ€. pip install gradioλ‘ μ€μΉνμΈμ.")
return
try:
with gr.Blocks(title="PDF λ¬Έμ κΈ°λ° RAG μ±λ΄") as app:
gr.Markdown("# PDF λ¬Έμ κΈ°λ° RAG μ±λ΄")
# LLM λͺ¨λΈ μ 보 κ°μ Έμ€κΈ°
from config import LLM_MODEL, USE_OPENAI, USE_DEEPSEEK
# μ¬μ© μ€μΈ λͺ¨λΈ νμ
model_type = "DeepSeek" if USE_DEEPSEEK else "OpenAI" if USE_OPENAI else "Ollama"
gr.Markdown(f"* μ¬μ© μ€μΈ LLM λͺ¨λΈ: **{model_type} - {LLM_MODEL}**")
# μ¬κΈ°λ₯Ό μμ : μ€μ κ²½λ‘ νμ
actual_pdf_dir = self.pdf_directory.replace('\\', '\\\\') if os.name == 'nt' else self.pdf_directory
gr.Markdown(f"* PDF λ¬Έμ ν΄λ: **{actual_pdf_dir}**")
with gr.Row():
with gr.Column(scale=1):
# λ¬Έμ μν μΉμ
status_box = gr.Textbox(
label="λ¬Έμ μ²λ¦¬ μν",
value=self._get_status_message(),
lines=5,
interactive=False
)
# μΊμ κ΄λ¦¬ λ²νΌ
refresh_button = gr.Button("λ¬Έμ μλ‘ μ½κΈ°", variant="primary")
reset_button = gr.Button("μΊμ μ΄κΈ°ν", variant="stop")
# DeepSeek API μν νμΈ λ²νΌ
from config import USE_DEEPSEEK
if USE_DEEPSEEK:
deepseek_button = gr.Button("DeepSeek API μν νμΈ", variant="secondary")
# μν λ° μ€λ₯ νμ
status_info = gr.Markdown(
value=f"μμ€ν
μν: {'μ΄κΈ°νλ¨' if self.is_initialized else 'μ΄κΈ°νλμ§ μμ'}"
)
# μ²λ¦¬λ νμΌ μ 보
with gr.Accordion("μΊμ μΈλΆ μ 보", open=False):
cache_info = gr.Textbox(
label="μΊμλ νμΌ μ 보",
value=self._get_cache_info(),
lines=5,
interactive=False
)
with gr.Column(scale=2):
# μ±ν
μΈν°νμ΄μ€
chatbot = gr.Chatbot(
label="λν λ΄μ©",
bubble_full_width=False,
height=500,
show_copy_button=True
)
# μ§λ¬Έ μ
λ ₯κ³Ό μ μ‘ λ²νΌμ μνμΌλ‘ λ°°μΉ
with gr.Row():
query_box = gr.Textbox(
label="μ§λ¬Έ",
placeholder="μ²λ¦¬λ λ¬Έμ λ΄μ©μ λν΄ μ§λ¬ΈνμΈμ...",
lines=2,
scale=4
)
submit_btn = gr.Button("μ μ‘", variant="primary", scale=1)
clear_chat_button = gr.Button("λν μ΄κΈ°ν")
# μ΄λ²€νΈ νΈλ€λ¬ μ€μ
def update_ui_after_refresh(result):
return (
result, # μν λ©μμ§
self._get_status_message(), # μν λ°μ€ μ
λ°μ΄νΈ
f"μμ€ν
μν: {'μ΄κΈ°νλ¨' if self.is_initialized else 'μ΄κΈ°νλμ§ μμ'}", # μν μ 보 μ
λ°μ΄νΈ
self._get_cache_info() # μΊμ μ 보 μ
λ°μ΄νΈ
)
# λ¬Έμ μλ‘ μ½κΈ° λ²νΌ
refresh_button.click(
fn=lambda: update_ui_after_refresh(self.auto_process_documents()),
inputs=[],
outputs=[status_box, status_box, status_info, cache_info]
)
# μΊμ μ΄κΈ°ν λ²νΌ
def reset_and_process():
reset_result = self.reset_cache()
process_result = self.auto_process_documents()
return update_ui_after_refresh(f"{reset_result}\n\n{process_result}")
reset_button.click(
fn=reset_and_process,
inputs=[],
outputs=[status_box, status_box, status_info, cache_info]
)
# DeepSeek API μν νμΈ λ²νΌ (μ‘΄μ¬νλ κ²½μ°)
if USE_DEEPSEEK:
def check_deepseek_api():
# DeepSeek API μν νμΈ
try:
from config import DEEPSEEK_API_KEY, DEEPSEEK_ENDPOINT, DEEPSEEK_MODEL
try:
from deepseek_utils import test_deepseek_api
test_result = test_deepseek_api(DEEPSEEK_API_KEY, DEEPSEEK_ENDPOINT, DEEPSEEK_MODEL)
if test_result["success"]:
return f"DeepSeek API μν: μ μ\nλͺ¨λΈ: {DEEPSEEK_MODEL}\nμλ΅: {test_result.get('response', '(μλ΅ λ΄μ© μμ)')}"
else:
return f"DeepSeek API μ€λ₯: {test_result['message']}\nμν μ½λ: {test_result.get('status_code', 'N/A')}"
except ImportError:
# μ§μ ν
μ€νΈ
import requests
import json
# ν
μ€νΈμ© κ°λ¨ν ν둬ννΈ
test_prompt = "Hello, please respond with a short greeting."
# API μμ² ν€λ λ° λ°μ΄ν°
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {DEEPSEEK_API_KEY}"
}
payload = {
"model": DEEPSEEK_MODEL,
"messages": [{"role": "user", "content": test_prompt}],
"temperature": 0.7,
"max_tokens": 50
}
# API μμ² μ μ‘
response = requests.post(
DEEPSEEK_ENDPOINT,
headers=headers,
data=json.dumps(payload),
timeout=10
)
# μλ΅ νμΈ
if response.status_code == 200:
response_data = response.json()
message_content = response_data.get("choices", [{}])[0].get("message", {}).get(
"content", "")
return f"DeepSeek API μν: μ μ\nλͺ¨λΈ: {DEEPSEEK_MODEL}\nμλ΅: {message_content[:200]}..."
else:
return f"DeepSeek API μ€λ₯\nμν μ½λ: {response.status_code}\nμλ΅: {response.text[:200]}..."
except Exception as e:
return f"DeepSeek API ν
μ€νΈ μ€ μ€λ₯ λ°μ: {str(e)}"
deepseek_button.click(
fn=check_deepseek_api,
inputs=[],
outputs=[status_box]
)
# μ μ‘ λ²νΌ ν΄λ¦ μ΄λ²€νΈ
submit_btn.click(
fn=self.process_query,
inputs=[query_box, chatbot],
outputs=[query_box, chatbot]
)
# μν°ν€ μ
λ ₯ μ΄λ²€νΈ
query_box.submit(
fn=self.process_query,
inputs=[query_box, chatbot],
outputs=[query_box, chatbot]
)
# λν μ΄κΈ°ν λ²νΌ
clear_chat_button.click(
fn=lambda: [],
outputs=[chatbot]
)
# μ± μ€ν
app.launch(share=False)
except Exception as e:
logger.error(f"Gradio μ± μ€ν μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
print(f"Gradio μ± μ€ν μ€ μ€λ₯ λ°μ: {e}")
if __name__ == "__main__":
app = AutoRAGChatApp()
app.launch_app() |