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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""
American University Academic Advisor Chatbot
===========================================

A RAG-based chatbot system that answers questions about American University academic programs,
leveraging ChromaDB for vector retrieval and Mistral 7B for response generation.

Features:
---------
- Course requirement pattern recognition: Distinguishes between required courses, alternative
  options ("take either X or Y"), option groups, and true electives
- Academic terminology matching: Connects student questions using "required" to program
  descriptions using "must complete"
- Specialized formatting for course requirements: Organizes courses by type with clear labels
- Response generation using Mistral 7B: Creates natural language responses with source citations
- Conversation history tracking: Maintains context across multiple questions

Usage:
------
1. Command line:
   python chatbot.py
   
2. Import in another script:
   from chatbot import ask_question
   result = ask_question("What are the required courses for the Data Science program?")
   print(result["response"])
   
3. Clear conversation history:
   from chatbot import clear_conversation
   clear_conversation()

Requirements:
------------
- Python 3.8+
- ChromaDB for vector storage and retrieval
- Hugging Face API access for Mistral 7B
- Keyring (optional) for secure API key storage

Configuration:
-------------
The system needs a Hugging Face API key for generating responses. Set it using:
    
    keyring.set_password("HF_API_KEY", "rressler", "<your_api_key>")
    
Or create an .env file with:
    
    HF_API_KEY=<your_api_key>

Note:
-----
This implementation is designed specifically for academic program queries that
involve distinguishing between required courses and alternatives. It uses
specialized detection for patterns like "STAT-320 or STAT-302" to correctly
inform students about their course options.
"""

# chatbot.py

import os
import sys
import re
from pathlib import Path
import logging
import requests
import json
import math
import warnings
from typing import List, Dict, Tuple, Any, Optional

# Suppress some unnecessary warnings
warnings.filterwarnings("ignore", category=FutureWarning)

# Local imports
from utils.logging_utils import setup_logging
from utils.chroma_utils import get_chroma_manager
from utils.auth_utils import authenticate_huggingface

# Configure logging
logger = setup_logging(logger_name="Chatbot", log_filename="chatbot.log")

def configure_api_credentials() -> Tuple[Optional[str], str, Optional[Dict[str, str]]]:
    """
    Configure Hugging Face API credentials using a unified method.
    
    Returns:
        Tuple: (API key, Model URL, Headers)
    """
    try:
        hf_api_key, headers = authenticate_huggingface()
        
        model_url = os.getenv(
            "MISTRAL_API_URL",
            "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
        )
        
        return hf_api_key, model_url, headers

    except Exception as e:
        logger.warning(f"Authentication failed: {e}")
        raise

# Global configuration
try:
    HF_API_KEY, MISTRAL_API_URL, MISTRAL_HEADERS = configure_api_credentials()
except Exception as e:
    logger.error(f"Failed to configure API credentials: {e}")
    HF_API_KEY, MISTRAL_API_URL, MISTRAL_HEADERS = None, None, None

# Initialize ChromaDB manager
global_chroma_manager = get_chroma_manager(model_size="medium")
print(type(global_chroma_manager))

def classify_course_level(course_code):
    """
    Classify course level based on course number.
    
    Args:
        course_code (str): The course code (e.g., "MATH-221", "STAT-615")
        
    Returns:
        dict: Dictionary with course_level and level_description
    """
    # Initialize classification metadata
    classification = {
        "course_level": "unknown",
        "level_description": "Unknown course level"
    }
    
    # Extract the course number from the course code
    try:
        # Handle different separator formats (hyphen, space, dot)
        if '-' in course_code:
            parts = course_code.split('-')
        elif ' ' in course_code:
            parts = course_code.split(' ')
        elif '.' in course_code:
            parts = course_code.split('.')
        else:
            # Try to separate letters from numbers
            import re
            match = re.match(r'^([A-Za-z]+)(\d+)$', course_code)
            if match:
                parts = [match.group(1), match.group(2)]
            else:
                return classification
        
        # Get the course number
        if len(parts) < 2:
            return classification
        
        # Extract numeric part and convert to integer
        course_num_str = parts[1].strip()
        # Remove any trailing letters (like in "100A")
        course_num_str = ''.join(c for c in course_num_str if c.isdigit())
        course_num = int(course_num_str)
        
        # Classify based on course number
        if course_num <= 499:
            classification["course_level"] = "undergraduate"
            classification["level_description"] = "Undergraduate course"
        elif 500 <= course_num <= 599:
            classification["course_level"] = "graduate_open"
            classification["level_description"] = "Graduate course open to qualified undergraduate students"
        elif 600 <= course_num <= 699:
            classification["course_level"] = "graduate_core"
            classification["level_description"] = "Core graduate course for the master's degree in the field of study"
        elif 700 <= course_num <= 799:
            classification["course_level"] = "graduate_advanced"
            classification["level_description"] = "Advanced graduate course"
        else:
            classification["course_level"] = "other"
            classification["level_description"] = f"Course number {course_num} outside standard classification"
            
    except Exception as e:
        # If there's any error in parsing, return the default classification
        pass
        
    return classification

def extract_courses_from_results(results):
    """
    Extract course information from the query results with level classification.
    
    Args:
        results (dict): Results from ChromaDB query
        
    Returns:
        list: List of course objects with code, title, credits, type, and level classification
    """
    courses = []
    course_codes_seen = set()
    
    # Parse through each document
    for i, (doc, metadata) in enumerate(zip(results["documents"][0], results["metadatas"][0])):
        # Extract course section type
        section_type = metadata.get("section_type", "unknown")
        
        # Extract course codes using regex
        # Format: DEPT-123 Course Title (3)
        course_pattern = r'([A-Z]{2,4}-\d{3})\s+([^(]+)(?:\s*\((\d+(?:\.\d+)?)\))?'
        
        for line in doc.split('\n'):
            matches = re.findall(course_pattern, line)
            
            for match in matches:
                code = match[0].strip()
                title = match[1].strip() if len(match) > 1 else ""
                credits = match[2] if len(match) > 2 and match[2] else "N/A"
                
                # Skip duplicates
                if code in course_codes_seen:
                    continue
                    
                course_codes_seen.add(code)
                
                # Get course level classification
                classification = classify_course_level(code)
                
                courses.append({
                    "code": code,
                    "title": title,
                    "credits": credits,
                    "type": section_type,
                    "course_level": classification["course_level"],
                    "level_description": classification["level_description"]
                })
    
    return courses

def format_courses_for_display(courses):
    """
    Format the courses into a readable string with level information.
    
    Args:
        courses (list): List of course objects
        
    Returns:
        str: Formatted string with course information grouped by type and level
    """
    if not courses:
        return "No courses found."
    
    # Group courses by type
    grouped_courses = {
        "required_courses": [],
        "elective_courses": [],
        "option_group": [],
        "small_option_group": []
    }
    
    for course in courses:
        course_type = course["type"]
        if course_type in grouped_courses:
            grouped_courses[course_type].append(course)
    
    # Format the output
    output = []
    
    # Add required courses
    if grouped_courses["required_courses"]:
        output.append("**Required Courses:**")
        output.append("These courses must be completed by all students in the program:")
        
        # Sort required courses by level (undergraduate first, then graduate)
        level_priority = {
            "undergraduate": 1,
            "graduate_open": 2,
            "graduate_core": 3,
            "graduate_advanced": 4,
            "other": 5,
            "unknown": 6
        }
        
        # Sort the courses by level priority
        sorted_courses = sorted(
            grouped_courses["required_courses"], 
            key=lambda x: level_priority.get(x.get("course_level", "unknown"), 999)
        )
        
        # Group by level for clearer presentation
        current_level = None
        
        for course in sorted_courses:
            level = course.get("course_level", "unknown")
            level_desc = course.get("level_description", "")
            
            # Add level header if changed
            if level != current_level:
                current_level = level
                if level_desc:
                    output.append(f"\n{level_desc.upper()}:")
            
            output.append(f"- {course['code']} {course['title']} ({course['credits']})")
        
        output.append("")
    
    # Add small option groups (either X or Y)
    if grouped_courses["small_option_group"]:
        output.append("**Alternative Course Options:**")
        output.append("Students must complete ONE course from each of these groups:")
        
        # Group the courses by their option group
        group_id = 1
        # First gather all courses into groups
        groups = {}
        for course in grouped_courses["small_option_group"]:
            # Extract group info from metadata if available, or use sequential numbering
            group_id = course.get("group_id", group_id)
            if group_id not in groups:
                groups[group_id] = []
            groups[group_id].append(course)
            
        # Now display the groups
        for group_id, course_list in groups.items():
            output.append(f"\nOption Group {group_id}:")
            
            # Sort by course level
            level_priority = {
                "undergraduate": 1,
                "graduate_open": 2,
                "graduate_core": 3,
                "graduate_advanced": 4,
                "other": 5,
                "unknown": 6
            }
            
            sorted_courses = sorted(
                course_list, 
                key=lambda x: level_priority.get(x.get("course_level", "unknown"), 999)
            )
            
            for course in sorted_courses:
                level_desc = course.get("level_description", "")
                output.append(f"- {course['code']} {course['title']} ({course['credits']}) - {level_desc}")
        
        output.append("")
    
    # Add option groups (choose one or more)
    if grouped_courses["option_group"]:
        output.append("**Option Groups:**")
        output.append("Students must select courses from the following groups according to program requirements:")
        
        # Sort by course level
        level_priority = {
            "undergraduate": 1,
            "graduate_open": 2,
            "graduate_core": 3,
            "graduate_advanced": 4,
            "other": 5,
            "unknown": 6
        }
        
        sorted_courses = sorted(
            grouped_courses["option_group"], 
            key=lambda x: level_priority.get(x.get("course_level", "unknown"), 999)
        )
        
        # Group by level for clearer presentation
        current_level = None
        
        for course in sorted_courses:
            level = course.get("course_level", "unknown")
            level_desc = course.get("level_description", "")
            
            # Add level header if changed
            if level != current_level:
                current_level = level
                if level_desc:
                    output.append(f"\n{level_desc.upper()}:")
            
            output.append(f"- {course['code']} {course['title']} ({course['credits']})")
        
        output.append("")
    
    # Add elective courses
    if grouped_courses["elective_courses"]:
        output.append("**Elective Courses:**")
        output.append("Students may choose from these optional courses to fulfill elective requirements:")
        
        # Sort by course level
        level_priority = {
            "undergraduate": 1,
            "graduate_open": 2,
            "graduate_core": 3,
            "graduate_advanced": 4,
            "other": 5,
            "unknown": 6
        }
        
        sorted_courses = sorted(
            grouped_courses["elective_courses"], 
            key=lambda x: level_priority.get(x.get("course_level", "unknown"), 999)
        )
        
        # Group by level for clearer presentation
        current_level = None
        
        for course in sorted_courses:
            level = course.get("course_level", "unknown")
            level_desc = course.get("level_description", "")
            
            # Add level header if changed
            if level != current_level:
                current_level = level
                if level_desc:
                    output.append(f"\n{level_desc.upper()}:")
            
            output.append(f"- {course['code']} {course['title']} ({course['credits']})")
    
    return "\n".join(output)

def process_program_query(query, program_name=None):
    """
    Check if the query is about program requirements or courses and extract program name.
    
    Args:
        query (str): The user's query
        program_name (str, optional): Pre-identified program name
        
    Returns:
        dict: Information about the query intent and program
    """
    logger.info(f"Processing query in process_program_query start line 446: {repr(query)}")
    logger.info(f"[process_program_query] Got query: {repr(query)} | Type: {type(query)} | ID: {id(query)}")
    if not isinstance(query, str):
        logger.warning(f"Query is not a string! Got {type(query)}: {repr(query)}")
        return {
            "is_course_query": False,
            "course_type": None,
            "program_name": program_name,
            "query_type": "invalid"
        }

    query_lower = query.lower()
    result = {
        "is_course_query": False,
        "course_type": None,
        "program_name": program_name,
        "query_type": "general"
    }
    
    # Course query patterns
    course_query_patterns = [
        # Direct questions about specific course types
        r'what(?:\s+are)?(?:\s+the)?\s+(required|core|elective|optional|must[\s-]complete)\s+courses\s+for\s+(?:the\s+)?(.+?)(?:\s+program|\s+major|\s+degree|\s+minor)?$',
        
        # Questions about program requirements in general
        r'(?:the\s+)?(.+?)(?:\s+program|\s+major|\s+degree|\s+minor)(?:\s+requirements|(?:\s+)courses)',
        
        # Questions about what courses to take
        r'what\s+courses\s+(?:do\s+I|does\s+one|should\s+I)\s+(?:need\s+to|have\s+to|must)\s+(?:take|complete)\s+for\s+(?:the\s+)?(.+?)(?:\s+program|\s+major|\s+degree|\s+minor)?',
        
        # Alternate phrasing about "must complete" courses
        r'what(?:\s+courses)?\s+(?:do\s+I|does\s+one|should\s+I)\s+(?:have\s+to|need\s+to|must)\s+complete\s+for\s+(?:the\s+)?(.+?)(?:\s+program|\s+major|\s+degree|\s+minor)?'
    ]
    
    # Try each pattern
    for pattern in course_query_patterns:
        match = re.search(pattern, query_lower)
        if match:
            result["is_course_query"] = True
            
            # Extract course type and program name
            if len(match.groups()) > 1:
                course_type = match.group(1)
                program_name = match.group(2)
                
                # Map course type
                if course_type in ['required', 'core', 'must-complete', 'must complete']:
                    result["course_type"] = 'required_courses'
                elif course_type in ['elective', 'optional']:
                    result["course_type"] = 'elective_courses'
                else:
                    result["course_type"] = 'all'
                    
                result["program_name"] = program_name
                result["query_type"] = "course_requirements"
                break
            elif len(match.groups()) == 1:
                # Just program name, no course type specified
                program_name = match.group(1)
                result["program_name"] = program_name
                result["course_type"] = 'all'
                result["query_type"] = "program_requirements"
                break
    
    return result

def expand_query_with_academic_terms(query):
    """
    Expand the query with alternate academic terminology to improve retrieval.
    
    This function identifies key terms in the query and adds synonyms/alternate
    phrasings that are common in academic contexts, focusing especially on
    course requirement terminology.
    
    Args:
        query (str): The original user query
        
    Returns:
        str: Expanded query with alternate terminology
    """
    # Define academic term mappings (original term -> list of synonyms)
    academic_term_mappings = {
        "required": ["must complete", "must take", "mandatory", "core", "required", "requirement", "capstone"],
        "elective": ["optional", "elective", "choice", "select from"],
        "prerequisite": ["prereq", "prerequisite", "before taking", "prior to"],
        "corequisite": ["coreq", "corequisite", "concurrent", "alongside"],
        "credit": ["credit hour", "credit", "unit"],
        "major": ["major", "program", "degree", "concentration"],
        "minor": ["minor", "secondary field"],
        "course": ["course", "class", "subject"]
    }
    
    # Check if the query contains any of our mapped terms
    expanded_terms = []
    logger.info(f"Processing query for mapped terms: {repr(query)}")
    query_lower = query.lower()
    
    for original_term, synonyms in academic_term_mappings.items():
        if original_term in query_lower:
            # Add synonyms of terms that appear in the query
            expanded_terms.extend(synonyms)
    
    # If we found terms to expand
    if expanded_terms:
        # Create an expanded query by adding synonyms
        # We use a format that works well with sentence transformers
        expanded_query = f"{query} {' '.join(expanded_terms)}"
        return expanded_query
    
    # If no expansion needed, return original
    return query

def get_program_courses(program_name, course_type='all', n_results=10):
    """
    Get specific course information for a program based on course type.
    
    Args:
        program_name (str): Name of the academic program
        course_type (str): Type of courses to retrieve ('required_courses', 
                          'elective_courses', 'option_group', 'small_option_group', or 'all')
        n_results (int): Number of results to return
        
    Returns:
        dict: Results containing course information
    """
    # Get ChromaDB manager
    chroma_manager = global_chroma_manager

    
    # Build the where clause based on the requested course type
    if course_type == 'all':
        where_clause = {
            "$or": [
                {"section_type": "required_courses"},
                {"section_type": "elective_courses"},
                {"section_type": "option_group"},
                {"section_type": "small_option_group"}
            ]
        }
    else:
        where_clause = {"section_type": course_type}
    
    # Add program name to the query
    if program_name and program_name.lower() != "any":
        # Use a more flexible approach for program name matching
        query = f"{course_type} for {program_name} program"
        
        # Add program name condition to where clause with flexible matching
        where_clause["$and"] = [
            {"type": "program"},
            {"$or": [
                {"program_name": {"$contains": program_name.lower()}},
                {"parent_title": {"$contains": program_name.lower()}}
            ]}
        ]
    else:
        query = f"{course_type}"
        where_clause["type"] = "program"
    
    # Expand query with academic terminology
    expanded_query = expand_query_with_academic_terms(query)
    
    # Execute the query with filtering
    results = chroma_manager.query(
        query_text=expanded_query,
        where=where_clause,
        n_results=n_results
    )
    
    return results

def get_program_course_information(program_name, course_type='all'):
    """
    Get formatted course information for a program.
    
    Args:
        program_name (str): Name of the academic program
        course_type (str): Type of courses to retrieve
        
    Returns:
        str: Formatted course information
    """
    results = get_program_courses(program_name, course_type, n_results=15)
    courses = extract_courses_from_results(results)
    return format_courses_for_display(courses)

# Enhanced program requirements extraction with better program differentiation
def extract_validated_program_requirements(soup, program_name, department, url, debug_mode=False):
    """
    Extract program requirements with strict validation to avoid mixing electives with requirements.
    Carefully differentiates between similarly named programs.
    
    Args:
        soup (BeautifulSoup): Parsed HTML content
        program_name (str): Name of the program
        department (str): Department name
        url (str): URL of the page
        debug_mode (bool): Whether to log debug information
    
    Returns:
        dict: Validated program requirements
    """
    logger.info(f"Extracting validated requirements for: {program_name}")
    
    # Initialize structured requirements
    requirements = {
        "program_name": program_name,
        "department": department,
        "url": url,
        "core_requirements": [],
        "electives": [],
        "capstone": None,
        "total_credits": 0
    }
    
    # Determine exact program type to avoid confusion between similar programs
    # Normalize program name for comparison
    normalized_program = program_name.lower().strip()
    
    # Identify the specific program
    if normalized_program == "bs data science" or normalized_program == "b.s. data science":
        program_type = "BS_DATA_SCIENCE"
    elif normalized_program == "bs data sciences" or normalized_program == "b.s. data sciences":
        program_type = "BS_DATA_SCIENCES"  # Note the plural
    elif normalized_program == "ms data science" or normalized_program == "m.s. data science":
        program_type = "MS_DATA_SCIENCE"
    elif normalized_program == "ms data sciences" or normalized_program == "m.s. data sciences":
        program_type = "MS_DATA_SCIENCES"  # Note the plural
    else:
        # Generic handling for other programs
        program_type = "OTHER"
    
    requirements["program_type"] = program_type
    
    if debug_mode:
        logger.debug(f"Identified program type: {program_type}")
    
    # Look for specific requirement sections
    requirement_sections = []
    
    # Find headers that likely contain requirement information
    requirement_headers = soup.find_all(['h2', 'h3', 'h4'], string=lambda text: text and any(keyword in text.lower() 
                                     for keyword in ['requirement', 'core', 'foundation', 'required', 'curriculum',
                                                    'major', 'course', 'capstone', 'thesis', 'project', 'elective']))
    
    for header in requirement_headers:
        section_title = header.get_text(strip=True)
        section_content = []
        
        # Get all content until the next header
        current = header.next_sibling
        while current and not (hasattr(current, 'name') and current.name in ['h2', 'h3', 'h4']):
            if hasattr(current, 'get_text'):
                text = current.get_text(strip=True)
                if text:
                    section_content.append(text)
            elif isinstance(current, str) and current.strip():
                section_content.append(current.strip())
            
            current = current.next_sibling
        
        if section_content:
            section_text = ' '.join(section_content)
            
            # Categorize the section based on its title and content
            section_type = "unknown"
            
            # Check for capstone specifically first (highest priority)
            if any(keyword in section_title.lower() for keyword in ['capstone', 'thesis', 'project', 'senior']):
                section_type = "capstone"
                requirements["capstone"] = {
                    "title": section_title,
                    "content": section_text,
                    "courses": extract_course_codes(section_text)
                }
                
                # Validate capstone based on program type
                if program_type == "BS_DATA_SCIENCE":
                    # Check for STAT-427 for BS Data Science
                    if "stat-427" in section_text.lower() or "stat 427" in section_text.lower():
                        requirements["capstone"]["validated"] = True
                        requirements["capstone"]["credits"] = 3
                        requirements["capstone"]["course_title"] = "Statistical Machine Learning"
                    else:
                        requirements["capstone"]["validated"] = False
                else:
                    # For other programs, just extract course information
                    requirements["capstone"]["validated"] = True  # Assume valid for other programs
            
            # Check for electives
            elif any(keyword in section_title.lower() for keyword in ['elective', 'optional', 'choose']):
                section_type = "electives"
                requirements["electives"].append({
                    "title": section_title,
                    "content": section_text,
                    "courses": extract_course_codes(section_text)
                })
            
            # Check for core requirements
            elif any(keyword in section_title.lower() for keyword in ['requirement', 'core', 'required', 'foundation']):
                section_type = "core"
                requirements["core_requirements"].append({
                    "title": section_title,
                    "content": section_text,
                    "courses": extract_course_codes(section_text)
                })
            
            # Add this section to our list
            requirement_sections.append({
                "title": section_title,
                "content": section_text,
                "type": section_type
            })
    
    # Extract total credits information
    credit_patterns = [
        r'total\s+of\s+(\d+)\s+credit',
        r'(\d+)\s+credits?\s+(?:are|is)\s+required',
        r'requires\s+(\d+)\s+credits?',
        r'minimum\s+of\s+(\d+)\s+credits?'
    ]
    
    full_text = soup.get_text()
    for pattern in credit_patterns:
        match = re.search(pattern, full_text, re.IGNORECASE)
        if match:
            try:
                requirements["total_credits"] = int(match.group(1))
                break
            except ValueError:
                pass
    
    # Program-specific validation
    if program_type == "BS_DATA_SCIENCE":
        # Known core courses for BS Data Science at American University
        expected_core_courses = [
            "MATH-221", "MATH-222", "MATH-313", "STAT-203", "STAT-302", 
            "CSC-280", "DATA-320", "STAT-412", "STAT-415"
        ]
        
        # Validate that all expected courses are in our core requirements
        found_courses = []
        for section in requirements["core_requirements"]:
            for course in section["courses"]:
                course_clean = clean_course_code(course)
                if course_clean in expected_core_courses and course_clean not in found_courses:
                    found_courses.append(course_clean)
        
        # Check coverage of expected courses
        missing_courses = [c for c in expected_core_courses if c not in found_courses]
        requirements["core_coverage"] = len(found_courses) / len(expected_core_courses)
        
        if debug_mode:
            logger.debug(f"Found {len(found_courses)}/{len(expected_core_courses)} expected core courses")
            if missing_courses:
                logger.debug(f"Missing core courses: {', '.join(missing_courses)}")
    
    elif program_type == "MS_DATA_SCIENCE":
        # Different validation for MS Data Science
        # (Add expected courses for MS Data Science when available)
        pass
    
    # Log the results
    logger.info(f"Extracted {len(requirements['core_requirements'])} core requirement sections, {len(requirements['electives'])} elective sections")
    
    return requirements

def extract_course_codes(text):
    """Extract course codes from text using regex."""
    # Pattern for course codes like STAT-203, MATH 221, CSC280, etc.
    pattern = r'([A-Z]{2,4})[\s\-]?(\d{3,4}[A-Z]?)'
    matches = re.findall(pattern, text, re.IGNORECASE)
    
    # Format matches into standard course codes
    courses = [f"{dept.upper()}-{num}" for dept, num in matches]
    return courses

def clean_course_code(course_code):
    """Standardize course code format to DEPT-NUM."""
    parts = re.match(r'([A-Z]{2,4})[\s\-]?(\d{3,4}[A-Z]?)', course_code, re.IGNORECASE)
    if parts:
        return f"{parts.group(1).upper()}-{parts.group(2)}"
    return course_code

# Enhanced retrieval function to query for program requirements
def retrieve_validated_program_requirements(chroma_manager, program_name, debug_mode=False):
    """
    Retrieve and validate program requirements from ChromaDB.
    
    Args:
        chroma_manager: ChromaDB manager instance
        program_name (str): Name of the program to retrieve
        debug_mode (bool): Whether to log debug information
    
    Returns:
        dict: Validated program requirements
    """
    # Determine exact program type to avoid confusion between similar programs
    # Normalize program name for comparison
    normalized_program = program_name.lower().strip()
    
    # Identify the specific program
    if normalized_program == "bs data science" or normalized_program == "b.s. data science":
        program_type = "BS_DATA_SCIENCE"
    elif normalized_program == "bs data sciences" or normalized_program == "b.s. data sciences":
        program_type = "BS_DATA_SCIENCES"  # Note the plural
    elif normalized_program == "ms data science" or normalized_program == "m.s. data science":
        program_type = "MS_DATA_SCIENCE"
    elif normalized_program == "ms data sciences" or normalized_program == "m.s. data sciences":
        program_type = "MS_DATA_SCIENCES"  # Note the plural
    else:
        # Generic handling for other programs
        program_type = "OTHER"
    
    if debug_mode:
        logger.debug(f"Retrieving requirements for: {program_name} (Type: {program_type})")
    
    # Query for program requirements with exact program name match
    query = f"requirements for {program_name}"
    
    # First try to find the program summary with exact program_name match
    summary_results = chroma_manager.query(
        query_text=query,
        n_results=5,
        metadata_filter={"program_name": program_name, "type": "program", "section_type": "program_summary"}
    )
    
    if summary_results and len(summary_results['ids']) > 0:
        # We found a program summary, which is most reliable
        if debug_mode:
            logger.debug(f"Found program summary for {program_name}")
        
        # Parse the summary to extract structured requirements
        summary_text = summary_results['documents'][0]
        
        # Extract core requirements, electives, and capstone from summary
        requirements = {
            "program_name": program_name,
            "program_type": program_type,
            "department": summary_results['metadatas'][0].get('department', 'Unknown Department'),
            "core_requirements": [],
            "electives": [],
            "capstone": None
        }
        
        # Extract major requirements from summary
        if "REQUIRED COURSES" in summary_text:
            core_section = summary_text.split("REQUIRED COURSES")[1].split("ELECTIVE COURSES")[0] if "ELECTIVE COURSES" in summary_text else summary_text.split("REQUIRED COURSES")[1]
            requirements["core_requirements"] = [{
                "title": "Major Requirements",
                "content": core_section,
                "courses": extract_course_codes(core_section)
            }]
        
        # Extract electives
        if "ELECTIVE COURSES" in summary_text:
            elective_section = summary_text.split("ELECTIVE COURSES")[1]
            requirements["electives"] = [{
                "title": "Elective Courses",
                "content": elective_section,
                "courses": extract_course_codes(elective_section)
            }]
        
        return requirements
    
    # If we don't find a summary, query for individual requirement sections
    section_results = chroma_manager.query(
        query_text=query,
        n_results=10,
        metadata_filter={"program_name": program_name, "type": "program"}
    )
    
    if not section_results or len(section_results['ids']) == 0:
        logger.warning(f"No results found for {program_name} requirements")
        return None
    
    # Parse the results to extract structured requirements
    requirements = {
        "program_name": program_name,
        "program_type": program_type,
        "department": section_results['metadatas'][0].get('department', 'Unknown Department'),
        "core_requirements": [],
        "electives": [],
        "capstone": None
    }
    
    # Process each result
    for i, doc in enumerate(section_results['documents']):
        metadata = section_results['metadatas'][i]
        section_type = metadata.get('section_type', 'unknown')
        title = metadata.get('title', f"Section {i+1}")
        
        # Determine if this section contains requirements, electives, or capstone
        if section_type in ['required_courses', 'option_group']:
            requirements["core_requirements"].append({
                "title": title,
                "content": doc,
                "courses": extract_course_codes(doc)
            })
        elif section_type == 'elective_courses':
            requirements["electives"].append({
                "title": title,
                "content": doc,
                "courses": extract_course_codes(doc)
            })
        elif "capstone" in title.lower() or "senior" in title.lower():
            requirements["capstone"] = {
                "title": title,
                "content": doc,
                "courses": extract_course_codes(doc)
            }
    
    return requirements

# Function to generate accurate program requirement response
def generate_accurate_requirements_response(requirements, program_name):
    """
    Generate an accurate response about program requirements.
    Enhanced to handle the updated classification where required electives and minors
    are properly included in the required_courses category.
    
    Args:
        requirements (dict): Validated program requirements
        program_name (str): Name of the program
    
    Returns:
        str: Formatted response with accurate requirements
    """
    if not requirements:
        return f"I'm sorry, but I couldn't find specific requirements for the {program_name} program. Please check the department website for the most up-to-date information."
    
    response = [f"# {program_name} Requirements", ""]
    
    # Add department information
    if requirements.get("department"):
        response.append(f"**Department:** {requirements['department']}")
        response.append("")
    
    # Add total credits if available
    if requirements.get("total_credits"):
        response.append(f"**Total Credits Required:** {requirements['total_credits']}")
        response.append("")
    
    # Add core requirements
    if requirements.get("core_requirements"):
        response.append("## Core Requirements")
        
        # Track which sections we've already displayed to avoid duplication
        displayed_sections = set()
        
        for section in requirements["core_requirements"]:
            # Skip if we've already displayed this section (by title)
            if section['title'] in displayed_sections:
                continue
                
            response.append(f"**{section['title']}**")
            displayed_sections.add(section['title'])
            
            # Format course list neatly if we have extracted courses
            if section.get("courses"):
                for course in section["courses"]:
                    # Try to find the course name/title from our database
                    # For now, just list the course code
                    response.append(f"- {course}")
            else:
                # Just add the raw content
                response.append(section["content"])
            
            response.append("")
    
    # Add capstone if available
    if requirements.get("capstone"):
        response.append("## Capstone Experience")
        capstone = requirements["capstone"]
        response.append(f"**{capstone['title']}**")
        
        # Special handling for BS Data Science capstone
        program_type = requirements.get("program_type", "OTHER")
        if program_type == "BS_DATA_SCIENCE" and capstone.get("validated", False):
            response.append("**STAT-427: Statistical Machine Learning (3 credits)**")
            response.append("This course serves as the capstone experience for the Data Science program.")
        elif capstone.get("courses"):
            for course in capstone["courses"]:
                response.append(f"- {course}")
        else:
            response.append(capstone["content"])
        
        response.append("")
    
    # Add minor or second major requirements if available
    # This might now be included in core_requirements, so check if it wasn't displayed yet
    if requirements.get("minor_requirement") and not any(
        "minor" in section['title'].lower() for section in requirements.get("core_requirements", [])
    ):
        response.append("## Minor or Second Major Requirement")
        minor = requirements["minor_requirement"]
        response.append(f"**{minor['title']}**")
        response.append(minor["content"])
        response.append("")
    
    # Add required electives
    # Check for electives that are required (might now be in core_requirements)
    required_electives = []
    elective_titles = set()
    
    # First, find elective sections that might be in core requirements
    if requirements.get("core_requirements"):
        for section in requirements["core_requirements"]:
            if 'elective' in section['title'].lower() and section['title'] not in elective_titles:
                required_electives.append(section)
                elective_titles.add(section['title'])
    
    # Then add any from the explicit electives category
    if requirements.get("electives"):
        for section in requirements["electives"]:
            if section['title'] not in elective_titles:
                required_electives.append(section)
                elective_titles.add(section['title'])
    
    # Display required electives
    if required_electives:
        response.append("## Elective Requirements")
        for section in required_electives:
            response.append(f"**{section['title']}**")
            
            # Format course list neatly if we have extracted courses
            if section.get("courses"):
                for course in section["courses"]:
                    response.append(f"- {course}")
            else:
                response.append(section["content"])
            
            response.append("")
    
    # Add option groups if available
    if requirements.get("option_groups"):
        response.append("## Option Groups")
        for section in requirements["option_groups"]:
            response.append(f"**{section['title']}**")
            
            # Format course list neatly if we have extracted courses
            if section.get("courses"):
                for course in section["courses"]:
                    response.append(f"- {course}")
            else:
                response.append(section["content"])
            
            response.append("")
    
    # Add a note about accuracy
    response.append("*Note: These requirements are subject to change. Please consult with an academic advisor or refer to the official program documentation for the most current information.*")
    
    return "\n".join(response)

# Example usage for BS Data Science
# requirements = retrieve_validated_program_requirements(chroma_manager, "BS Data Science", debug_mode=True)
# response = generate_accurate_requirements_response(requirements, "BS Data Science")
# print(response)

class AcademicChatbot:
    """
    A RAG-based chatbot for answering questions about academic programs and courses
    using Mistral 7B model and ChromaDB for retrieval.
    """
    
    # Update the __init__ method of the AcademicChatbot class
    def __init__(self):
        """Initialize the chatbot with ChromaDB and model configuration."""
        # Reuse the existing instance
        self.chroma_manager = global_chroma_manager
        self.collection = self.chroma_manager.get_collection()
        
        # Use global configuration 
        self.api_url = MISTRAL_API_URL
        self.headers = MISTRAL_HEADERS  # Use the globally defined headers
        self.conversation_history = []
        
        # Add a check to ensure headers are properly initialized
        if not self.headers:
            logger.warning("Mistral API headers not properly configured. Regenerate API credentials.")
            raise ValueError("Failed to initialize Mistral API headers. Check API key configuration.")
        
    def add_message(self, role: str, content: str):
        """Add a message to the conversation history."""
        self.conversation_history.append({"role": role, "content": content})
        
    def clear_history(self):
        """Clear the conversation history."""
        self.conversation_history = []
        
    def get_history(self):
        """Get the conversation history."""
        return self.conversation_history
    
    def get_url_from_metadata(self, metadata):
        """Extract URL from metadata, checking multiple possible field names."""
        # Check various possible field names for URLs
        url_field_names = ['url', 'course_url', 'source_url', 'link', 'href', 'source']
        
        for field in url_field_names:
            if field in metadata and metadata[field]:
                return metadata[field]
        
        # If no URL field found, return empty string
        return ''
    
    def retrieve_context(self, query: str, n_results: int = 8) -> Tuple[List[str], List[Dict[str, Any]]]:
        """
        Retrieve diverse and relevant documents from ChromaDB based on the query.
    
        Args:
            query: The user's question
            n_results: Number of documents to retrieve
        
        Returns:
            Tuple containing (contexts, metadata)
        """
        logger.info(f"Retrieving context for query: {query}")
        
        # Use expanded query with academic terminology
        expanded_query = expand_query_with_academic_terms(query)
        logger.info(f"Expanded query: {expanded_query}")
    
        # Retrieve more documents than needed to improve diversity
        retrieve_count = min(n_results * 3, 25)  # Limit to 25 to avoid excessive retrieval
        results = self.chroma_manager.query(expanded_query, n_results=retrieve_count)
    
        # Extract the documents and their metadata
        contexts = []
        metadata_list = []
    
        if 'documents' in results and results['documents']:
            documents = results['documents'][0]
            metadatas = results['metadatas'][0] if 'metadatas' in results and results['metadatas'] else [{}] * len(documents)
        
            # Track URLs to ensure diversity
            seen_urls = set()
            seen_titles = set()
        
            # First pass: group by URL and title
            doc_groups = {}
            for doc, meta in zip(documents, metadatas):
                url = meta.get('url', '') if meta else ''
                title = meta.get('title', '') if meta else ''
                key = (url, title)
            
                if key not in doc_groups:
                    doc_groups[key] = []
            
                doc_groups[key].append((doc, meta))
        
            # Second pass: select one document from each group until we have enough
            while len(contexts) < n_results and doc_groups:
                for key in list(doc_groups.keys()):
                    if doc_groups[key]:
                        doc, meta = doc_groups[key].pop(0)
                        contexts.append(doc)
                        metadata_list.append(meta)
                    
                        if not doc_groups[key]:  # If group is empty, remove it
                            del doc_groups[key]
                    
                        if len(contexts) >= n_results:
                            break
        
            # If we still need more documents, fill in from the original list
            if len(contexts) < n_results:
                i = 0
                while len(contexts) < n_results and i < len(documents):
                    if documents[i] not in contexts:
                        contexts.append(documents[i])
                        metadata_list.append(metadatas[i])
                    i += 1
    
        logger.info(f"Retrieved {len(contexts)} context documents")
    
        return contexts, metadata_list

    def merge_program_documents(self, docs, metas, max_chars=15000):
        """Merge documents by category to create comprehensive context."""
        # Create category containers for all program information sections
        categories = {
            "comprehensive": {"content": "", "sources": []},
            "core": {"content": "", "sources": []},
            "electives": {"content": "", "sources": []},
            "minor": {"content": "", "sources": []},
            "capstone": {"content": "", "sources": []},
            "ethics": {"content": "", "sources": []},
            "admission": {"content": "", "sources": []},
            "au_core": {"content": "", "sources": []},
            "university_requirements": {"content": "", "sources": []},
            "major_requirements": {"content": "", "sources": []},
            "other": {"content": "", "sources": []}
        }
        
        # Process each document and categorize
        for i, (doc, meta) in enumerate(zip(docs, metas)):
            title = meta.get("title", "").lower() if meta else ""
            
            # Determine the appropriate category
            if "complete" in title and "requirements" in title:
                category = "comprehensive"
            elif "elective" in title:
                category = "electives"
            elif "minor" in title or "second major" in title:
                category = "minor"
            elif "capstone" in title:
                category = "capstone"
            elif "ethics" in title:
                category = "ethics"
            elif "admission" in title or "apply" in title:
                category = "admission"
            elif "au core" in title or "general education" in title:
                category = "au_core"
            elif "university requirement" in title:
                category = "university_requirements"
            elif "major requirement" in title:
                category = "major_requirements"
            elif any(term in title for term in ["statistics", "data science essentials", "intermediate"]):
                category = "core"
            else:
                category = "other"
            
            # Add content to the appropriate category
            categories[category]["content"] += f"\n\n## {meta.get('title', '')}\n{doc}"
            categories[category]["sources"].append(i)
        
        # Create output documents ensuring all major categories are included
        output_docs = []
        output_metas = []
        source_indices = set()
        
        # First add comprehensive document if available
        if categories["comprehensive"]["content"]:
            output_docs.append(categories["comprehensive"]["content"])
            output_metas.append({"title": "Complete Program Requirements"})
            source_indices.update(categories["comprehensive"]["sources"])
        
        # Create a document for university and general requirements
        general_content = "# General Program Requirements\n"
        general_sources = []
        
        # Add sections for university requirements, AU Core, admission
        for cat_name, display_name in [
            ("university_requirements", "University Requirements"),
            ("au_core", "AU Core Requirements"),
            ("admission", "Admission Requirements")
        ]:
            if categories[cat_name]["content"]:
                general_content += f"\n\n# {display_name}\n{categories[cat_name]['content']}"
                general_sources.extend(categories[cat_name]["sources"])
        
        # Add general requirements document if not empty
        if general_content.strip() != "# General Program Requirements":
            output_docs.append(general_content)
            output_metas.append({"title": "General Requirements"})
            source_indices.update(general_sources)
        
        # Create a document for major requirements
        major_content = "# Major Requirements\n"
        
        # Add major requirements section
        if categories["major_requirements"]["content"]:
            major_content += categories["major_requirements"]["content"]
        
        # Add core course sections
        if categories["core"]["content"]:
            major_content += "\n\n# Core Course Requirements\n" + categories["core"]["content"]
        
        # Add the major requirements document
        if major_content.strip() != "# Major Requirements":
            output_docs.append(major_content)
            output_metas.append({"title": "Major Requirements"})
            source_indices.update(categories["major_requirements"]["sources"])
            source_indices.update(categories["core"]["sources"])
        
        # Create a document for additional requirements
        additional_content = "# Additional Program Requirements\n"
        additional_sources = []
        
        # Add sections for electives, minor, capstone, ethics
        for cat_name, display_name in [
            ("electives", "Elective Requirements"),
            ("minor", "Minor or Second Major Requirements"),
            ("capstone", "Capstone Requirements"),
            ("ethics", "Ethics Requirements")
        ]:
            if categories[cat_name]["content"]:
                additional_content += f"\n\n# {display_name}\n{categories[cat_name]['content']}"
                additional_sources.extend(categories[cat_name]["sources"])
        
        # Add additional requirements document
        if additional_content.strip() != "# Additional Program Requirements":
            output_docs.append(additional_content)
            output_metas.append({"title": "Additional Requirements"})
            source_indices.update(additional_sources)
        
        # Check if we're under the character limit
        total_chars = sum(len(doc) for doc in output_docs)
        
        # Add other content if space permits
        if categories["other"]["content"] and total_chars + len(categories["other"]["content"]) <= max_chars:
            other_content = "# Other Program Information\n" + categories["other"]["content"]
            output_docs.append(other_content)
            output_metas.append({"title": "Other Information"})
            source_indices.update(categories["other"]["sources"])
        
        # Make sure we have all metadata for sources
        all_sources = []
        for i in range(len(metas)):
            all_sources.append(metas[i])
        
        logger.info(f"Merged {len(docs)} documents into {len(output_docs)} comprehensive documents (Total chars: {sum(len(d) for d in output_docs)})")
        
        return output_docs, all_sources

    # Find and prioritize required course documents for this specific program
    def trim_documents(self, docs, metas, max_chars=12000):
        """Trim documents to avoid token overload while ensuring all requirements are included."""
        output_docs, output_metas = [], []
        total_chars = 0
        
        # First identify documents for this program that are required_courses
        query = getattr(self, "current_query", None)
        query_info = process_program_query(query) if isinstance(query, str) else None
        if query_info:
            logger.info(f"[trim_documents] query_info: {query_info} | program_name: {query_info.get('program_name')}")
        program_name = (query_info.get("program_name") or "").lower() if query_info else ""
        
        # If this is a program requirement query, prioritize required documents
        if program_name:
            # First add comprehensive document if available
            comprehensive_index = None
            for i, meta in enumerate(metas):
                title = meta.get("title", "").lower() if meta else ""
                if "complete" in title and "requirement" in title and program_name in meta.get("program_name", "").lower():
                    comprehensive_index = i
                    break
                    
            if comprehensive_index is not None and total_chars + len(docs[comprehensive_index]) <= max_chars:
                output_docs.append(docs[comprehensive_index])
                output_metas.append(metas[comprehensive_index])
                total_chars += len(docs[comprehensive_index])
            
            # Then add all required course documents for this program
            for i, meta in enumerate(metas):
                # Skip if already added
                if i == comprehensive_index:
                    continue
                    
                # Check if document is a required course for this program
                is_required = meta.get("section_type", "") == "required_courses"
                is_this_program = program_name in meta.get("program_name", "").lower()
                
                # Add if it's a required document and fits within our limit
                if is_required and is_this_program and total_chars + len(docs[i]) <= max_chars:
                    output_docs.append(docs[i])
                    output_metas.append(metas[i])
                    total_chars += len(docs[i])
            
            # Make sure minor/second major is included
            has_minor = any("minor" in meta.get("title", "").lower() or "second major" in meta.get("title", "").lower() 
                        for meta in output_metas)
            
            if not has_minor:
                for i, meta in enumerate(metas):
                    if "minor" in meta.get("title", "").lower() or "second major" in meta.get("title", "").lower():
                        if total_chars + len(docs[i]) <= max_chars:
                            output_docs.append(docs[i])
                            output_metas.append(metas[i])
                            total_chars += len(docs[i])
                            break
            
            # Make sure capstone is included
            has_capstone = any("capstone" in meta.get("title", "").lower() for meta in output_metas)
            
            if not has_capstone:
                for i, meta in enumerate(metas):
                    if "capstone" in meta.get("title", "").lower():
                        if total_chars + len(docs[i]) <= max_chars:
                            output_docs.append(docs[i])
                            output_metas.append(metas[i])
                            total_chars += len(docs[i])
                            break
            
            # Make sure electives are included
            has_electives = any("elective" in meta.get("title", "").lower() for meta in output_metas)
            
            if not has_electives:
                for i, meta in enumerate(metas):
                    if "elective" in meta.get("title", "").lower():
                        if total_chars + len(docs[i]) <= max_chars:
                            output_docs.append(docs[i])
                            output_metas.append(metas[i])
                            total_chars += len(docs[i])
                            break
        
        # If we haven't added any documents yet (or this isn't a program query),
        # fall back to the original trim behavior
        if not output_docs:
            for doc, meta in zip(docs, metas):
                # Always include at least one document
                if len(output_docs) == 0 or total_chars + len(doc) <= max_chars:
                    output_docs.append(doc)
                    output_metas.append(meta)
                    total_chars += len(doc)
                else:
                    break
        
        logger.info(f"Trimmed documents from {len(docs)} to {len(output_docs)} (Total chars: {total_chars})")
        return output_docs, output_metas

    def generate_response(self, query: str, contexts: List[str], 
                    metadata: List[Dict[str, Any]], temperature: float = 0.7) -> str:
        """
        Generate a response using Mistral 7B with retrieved contexts.
        
        Args:
            query: The user's question
            contexts: Retrieved document contents
            metadata: Metadata for the retrieved documents
            temperature: Controls randomness in generation
            
        Returns:
            Generated response
        """
        logger.info(f"Generating response for query: {query}")

        # Store current query for use in other methods
        self.current_query = query
        if not isinstance(query, str) or not query.strip():
            logger.warning("Query is missing or not a string.")
            return "No query provided."
        
        # First check if this is a program course query that we should handle specially
        query_info = process_program_query(query)
        
        if query_info["is_course_query"] and query_info["program_name"]:
            logger.info(f"Detected course query for program: {query_info['program_name']}, type: {query_info['course_type']}")
            
            # First try to use the validated program requirements approach
            try:
                # Use the validated program requirements retrieval
                requirements = retrieve_validated_program_requirements(
                    self.chroma_manager, 
                    query_info["program_name"], 
                    debug_mode=False
                )
                
                # If we have validated requirements, use them to generate a response
                if requirements:
                    logger.info(f"Using validated requirements for {query_info['program_name']}")
                    response = generate_accurate_requirements_response(
                        requirements, 
                        query_info["program_name"]
                    )
                    
                    # Add sources
                    sources = []
                    for i, meta in enumerate(metadata):
                        if meta:
                            title = meta.get("title", "")
                            url = self.get_url_from_metadata(meta)
                            
                            if url:
                                if title:
                                    citation = f"[{i+1}] {title} - {url}"
                                else:
                                    citation = f"[{i+1}] Program information - {url}"
                            else:
                                if title:
                                    citation = f"[{i+1}] {title}"
                                else:
                                    citation = f"[{i+1}] Program information"
                                    
                            sources.append(citation)
                    
                    if sources:
                        # Identify sources referenced in response
                        used_source_indexes = set()
                        for i in range(len(sources)):
                            # Look for [1], [2], etc. references in the text
                            if f"[{i+1}]" in response:
                                used_source_indexes.add(i)
                        
                        # If we found referenced sources, show them first
                        if used_source_indexes:
                            response += "\n\nSources Referenced in Response:"
                            for i in sorted(used_source_indexes):
                                response += f"\n{sources[i]}"
                        
                        # Add all retrieved sources section
                        response += "\n\nAll Retrieved Sources:"
                        for source in sources:
                            response += f"\n{source}"
                        
                    return response
                
            except Exception as e:
                logger.error(f"Error using validated requirements approach: {str(e)}")
                # Fall back to the regular course information retrieval
            
            # Fall back to the basic course information approach if validation fails
            try:
                program_courses = get_program_course_information(
                    query_info["program_name"], 
                    query_info["course_type"]
                )
                
                # If we got results, return them directly
                if program_courses and "No courses found" not in program_courses:
                    program_name = query_info["program_name"].title()
                    
                    # Create a nicely formatted response with introduction
                    response = f"Here's information about the {program_name} program courses:\n\n{program_courses}"
                    
                    # Add sources from metadata
                    sources = []
                    for i, meta in enumerate(metadata):
                        if meta:
                            title = meta.get("title", "")
                            url = self.get_url_from_metadata(meta)
                            
                            if url:
                                if title:
                                    citation = f"[{i+1}] {title} - {url}"
                                else:
                                    citation = f"[{i+1}] Program information - {url}"
                            else:
                                if title:
                                    citation = f"[{i+1}] {title}"
                                else:
                                    citation = f"[{i+1}] Program information"
                                    
                            sources.append(citation)
                    
                    if sources:
                        # Identify sources referenced in response
                        used_source_indexes = set()
                        for i in range(len(sources)):
                            # Look for [1], [2], etc. references in the text
                            if f"[{i+1}]" in response:
                                used_source_indexes.add(i)
                        
                        # If we found referenced sources, show them in a separate section
                        if used_source_indexes:
                            response += "\n\nSources Referenced in Response:"
                            for i in sorted(used_source_indexes):
                                response += f"\n{sources[i]}"
                        
                        # Add all retrieved sources section
                        response += "\n\nAll Retrieved Sources:"
                        for source in sources:
                            response += f"\n{source}"
                        
                    return response
            except Exception as e:
                logger.error(f"Error handling specialized course query: {str(e)}")
                # Fall back to regular processing if there's an error
        
        # Trim documents to avoid token limits
        # For program requirement queries, use document merging instead of trimming
        if query_info["is_course_query"] and query_info["program_name"]:
            contexts, metadata = self.merge_program_documents(contexts, metadata, max_chars=12000)
        else:
            # For other queries, use regular trimming
            contexts, metadata = self.trim_documents(contexts, metadata, max_chars=10000)
        
        # Create a structured context from retrieved documents with their URLs
        enhanced_contexts = []
        for i, (doc, meta) in enumerate(zip(contexts, metadata)):
            source_type = meta.get("type", "document")
            title = meta.get("title", "")
            url = self.get_url_from_metadata(meta)
            
            # Limit document length to prevent token overflow
            doc_preview = doc[:1500] + ("..." if len(doc) > 1500 else "")
            
            # Format document with metadata
            doc_header = f"Document {i+1} ({source_type.capitalize()}"
            if title:
                doc_header += f": {title}"
            if url:
                doc_header += f" - {url}"
            doc_header += "):"
            
            enhanced_contexts.append(f"{doc_header}\n{doc_preview}")
        
        # Include conversation history in the prompt
        history_text = ""
        if self.conversation_history:
            recent_history = self.conversation_history[-3:]  # Include only the last 3 messages
            if recent_history:
                history_text = "### Recent Conversation:\n"
                for msg in recent_history:
                    role = "User" if msg["role"] == "user" else "Assistant"
                    history_text += f"{role}: {msg['content']}\n\n"
        
        # Format the full prompt with context and query
        context_text = "\n\n".join(enhanced_contexts)
        prompt = f"""You are an AI assistant answering questions about American University's academic programs and courses. 
    Use the following documents as your primary source of information. 

    Important rules:
    - If the answer is not explicitly stated, you may reason from the information provided, but explain your reasoning.
    - Courses marked as "must be completed", "prerequisites", or "required" are mandatory. 
    - When you see "one of the following" or "either X or Y", students must choose exactly one course from the options.
    - When you see "option group", students must select some number of courses from that group.
    - Courses listed as electives form a group from which a certain number must be completed, but not every course.
    - Always mention the source document when including specific information.
    - If you don't know or the information is not in the documents, be honest about it.
    - For Data Science programs, STAT-427 (Statistical Machine Learning) is the 3-credit capstone course.
    - Undergraduate courses have numbers 499 and below, graduate courses open to qualified undergraduates have numbers 500-599, 
    core graduate courses have numbers 600-699, and advanced graduate courses have numbers 700-799.

    {history_text if history_text else ""}

    ### Context:
    {context_text}

    ### Question:
    {query}

    """
        # For program requirement queries, use a more comprehensive format
        # Use different prompts based on query type
        logger.info(f"Processing query in process_program_query instructions: {repr(query)}")
        if isinstance(query, str) and ("course requirement" in query.lower() or "program requirement" in query.lower()):
            prompt += """

    IMPORTANT: Your response should include ALL required components for this degree program.
    Ensure you cover all sections mentioned in the documents, including:
    - All core course requirements with their credit hours
    - Any elective requirements with credit hours
    - Any minor or second major requirements
    - Any capstone or project requirements

    Present requirements in a clear, organized format that makes the degree structure easy to understand.
    DO NOT OMIT any requirements or sections mentioned in the documents.
    """

        prompt += "\n\n### Answer:"
        logger.info(f"Processed query in instructions: {repr(query)}")
        # Call Hugging Face API
        payload = {
            "inputs": prompt,
            "parameters": {
                "max_new_tokens": 4000,
                "temperature": temperature,
                "top_p": 0.85,
                "do_sample": True
            }
        }
        
        try:
            response = requests.post(self.api_url, headers=self.headers, json=payload)
            
            if response.status_code == 200:
                # Extract the answer part from the response
                generated_text = response.json()[0]["generated_text"]
                answer = generated_text.split("### Answer:")[-1].strip()
                
            # IMPORTANT CHANGE: Always replace the sources section
            # Remove any existing sources section
                if "\n\nSources:" in answer:
                    answer = answer.split("\n\nSources:")[0].strip()
            
                # Add our properly formatted sources
                sources = []
                for i, meta in enumerate(metadata):
                    if meta:
                        source_type = meta.get("type", "document")
                        title = meta.get("title", "")
                        url = self.get_url_from_metadata(meta)
                    
                        # Build citation with URL
                        if url:
                            if title:
                                citation = f"[{i+1}] {title} - {url}"
                            else:
                                citation = f"[{i+1}] {source_type.capitalize()} - {url}"
                        else:
                            if title:
                                citation = f"[{i+1}] {title}"
                            else:
                                citation = f"[{i+1}] {source_type.capitalize()}"

                        sources.append(citation)
                
                # Always add our formatted sources with new organization
                if sources:
                    # Identify sources referenced in response
                    used_source_indexes = set()
                    for i in range(len(sources)):
                        # Look for [1], [2], etc. references in the text
                        if f"[{i+1}]" in answer:
                            used_source_indexes.add(i)
                    
                    # If we found referenced sources, show them in a separate section
                    if used_source_indexes:
                        answer += "\n\nSources Referenced in Response:"
                        for i in sorted(used_source_indexes):
                            answer += f"\n{sources[i]}"
                    
                    # Add all retrieved sources section
                    answer += "\n\nAll Retrieved Sources:"
                    for source in sources:
                        answer += f"\n{source}"
                
                return answer
            else:
                error_msg = f"Error: {response.status_code}, {response.text}"
                logger.error(error_msg)
                return error_msg
        
        except Exception as e:
            error_msg = f"Exception during response generation: {str(e)}"
            logger.error(error_msg)
            return error_msg
    
    def add_document(self, text: str, metadata: Dict[str, Any], doc_id: Optional[str] = None) -> str:
        """Add a document to the ChromaDB collection."""
        return self.chroma_manager.add_document(text, metadata, doc_id)
    
    def get_collection_info(self) -> Dict[str, Any]:
        """Get information about the ChromaDB collection."""
        return self.collection.get()
    
    def ask(self, query: str, n_results: int = 8, temperature: float = 0.7) -> Dict[str, Any]:
            """
            Process a query and return a response with relevant context.
            
            Args:
                query: The user's question
                n_results: Number of documents to retrieve
                temperature: Controls randomness in generation
                
            Returns:
                Dictionary with response and context information
            """
            # Add user message to history
            self.add_message("user", query)
            
            # Retrieve context
            contexts, metadata = self.retrieve_context(query, n_results)
            
            # Check if we found relevant documents
            if not contexts:
                response = "I couldn't find any relevant information to answer your question. Could you please rephrase or ask about a different topic related to American University's programs or courses?"
            else:
                # Generate response - NO CHUNKING, get full response
                response = self.generate_response(query, contexts, metadata, temperature)
                
                # Instead of chunking, truncate if absolutely necessary (rarely needed with 4000 token limit)
                if len(response) > 15000:  # Very high limit just as a safeguard
                    response = response[:14800] + "...\n\n[Response truncated due to length. Please ask for specific details if needed.]"
            
            # Add assistant message to history
            self.add_message("assistant", response)
            
            # Return the result with context information
            return {
                "response": response,
                "contexts": contexts,
                "metadata": metadata,
                "history": self.conversation_history
            } 

# Then simplify the standalone function to just call this
def ask_question(query: str, n_results: int = 8, temperature: float = 0.7) -> Dict[str, Any]:
    """Ask a question to the chatbot."""
    return chatbot.ask(query, n_results, temperature)

# Create a singleton instance
chatbot = AcademicChatbot()

# Convenience function for direct usage
def ask_question(query: str, n_results: int = 10, temperature: float = 0.7) -> Dict[str, Any]:
    """Ask a question to the chatbot."""
    return chatbot.ask(query, n_results, temperature)

# Convenience function to clear conversation history
def clear_conversation():
    """Clear the conversation history."""
    chatbot.clear_history()

# Convenience function to add a document
def add_document(text: str, metadata: Dict[str, Any], doc_id: Optional[str] = None) -> str:
    """Add a document to the collection."""
    return chatbot.add_document(text, metadata, doc_id)

# Interactive chat function for CLI usage
def split_long_response(response: str, max_chunk_size: int = 3500) -> List[str]:
    """
    Split a long response into manageable chunks while preserving whole sentences.
    
    Args:
        response (str): The full response text
        max_chunk_size (int): Maximum size of each chunk in characters
    
    Returns:
        List[str]: List of response chunks
    """
    # If response is short enough, return as single chunk
    if len(response) <= max_chunk_size:
        return [response]
    
    # Function to split text into sentences
    def split_sentences(text):
        # Use multiple delimiters to split sentences
        import re
        return re.split(r'(?<=[.!?])\s+', text)
    
    chunks = []
    current_chunk = []
    current_chunk_length = 0
    
    sentences = split_sentences(response)
    
    for sentence in sentences:
        # If adding this sentence would exceed max chunk size, start a new chunk
        if current_chunk_length + len(sentence) > max_chunk_size:
            # Join the current chunk and add to chunks
            chunks.append(' '.join(current_chunk))
            current_chunk = []
            current_chunk_length = 0
        
        # Add sentence to current chunk
        current_chunk.append(sentence)
        current_chunk_length += len(sentence) + 1  # +1 for space
    
    # Add the last chunk if not empty
    if current_chunk:
        chunks.append(' '.join(current_chunk))
    
    # Add continuation markers
    for i in range(len(chunks)):
        if i < len(chunks) - 1:
            chunks[i] += f"\n\n(Continued in next message - Part {i+1}/{len(chunks)})"
        else:
            chunks[i] += f"\n\n(End of response - Part {i+1}/{len(chunks)})"
    
    return chunks

def generate_response_with_mistral(prompt, temperature):
    """
    Generate response using Mistral 7B via Hugging Face API.
    
    Args:
        prompt: Fully formatted prompt for the model
        temperature: Sampling temperature for response generation
    
    Returns:
        Generated response as a string
    """
    if not HF_API_KEY:
        raise ValueError("Hugging Face API key not found. Please configure credentials.")
    
    try:
        # Initialize Hugging Face client
        client = InferenceClient(
            "mistralai/Mistral-7B-Instruct-v0.3", 
            token=HF_API_KEY
        )
        
        # Generate response
        response = client.text_generation(
            prompt, 
            max_new_tokens=4096,  # Increased token limit
            temperature=temperature,
            stop_sequences=["\n\nUser:"],  # Prevent generating additional conversations
        )
        
        return response.strip()
    
    except Exception as e:
        error_msg = f"Error generating response with Mistral: {e}"
        logger.error(error_msg)
        return error_msg
    
    # Generate response
    try:
        response = client.text_generation(
            prompt, 
            max_new_tokens=4096,  # Increased token limit
            temperature=temperature,
            stop_sequences=["\n\nUser:"],  # Prevent generating additional conversations
        )
        
        return response.strip()
    
    except Exception as e:
        logging.error(f"Error generating response with Mistral: {str(e)}")
        return f"I apologize, but I encountered an error generating a response: {str(e)}"

def clear_conversation():
    """
    Clear the conversation history.
    Implement this based on your specific conversation tracking mechanism.
    """
    # Reset any conversation-specific state
    # For example, you might clear a list of previous messages
    pass

# Optional: Add a function to retrieve full response chunks if needed
def get_full_response_chunks(result):
    """
    Retrieve all chunks of a potentially long response.
    
    Args:
        result (Dict): Result from ask_question
    
    Returns:
        List[str]: All response chunks
    """
    return result.get('full_response_chunks', [result.get('response', '')])

def initialize_chatbot():
    """
    Initialize the chatbot with a welcome message and system setup.
    
    Returns:
        Dict[str, str]: Initial chatbot response
    """
    welcome_message = """Welcome to the American University Academic Advisor Chatbot!

I'm here to help you with information about:
- Academic programs
- Course details
- Program requirements
- Academic policies

What would you like to know about American University's academic offerings?

Some example questions you can ask:
- Tell me about the Data Science program
- What are the requirements for a Data Science major?
- What courses are required for a Statistics minor?
- Can you help me understand the AU Core curriculum?

Feel free to ask, and I'll do my best to provide comprehensive and helpful information!"""

    return {
        "response": welcome_message,
        "sources": "AU Academic Advisor Chatbot - Initial Welcome Message"
    }

def get_chatbot_info():
    """
    Provide information about the chatbot's capabilities and sources.
    
    Returns:
        Dict[str, str]: Chatbot information
    """
    info_message = """πŸ€– AU Academic Advisor Chatbot Information

Data Sources:
- American University's official website
- Course catalog
- Program description pages
- Academic department information

Technologies Used:
- Retrieval-Augmented Generation (RAG)
- Mistral 7B Language Model
- ChromaDB Vector Database
- Sentence Transformers for Embedding

Capabilities:
- Retrieve detailed information about academic programs
- Explain course requirements
- Provide insights into academic policies
- Offer guidance on course selection

Limitations:
- Information is based on available web sources
- Might not reflect the most recent updates
- Recommended to verify critical information with official AU sources

Developed as a student research project to assist with academic advising.
"""

    return {
        "response": info_message,
        "sources": "AU Academic Advisor Chatbot - System Information"
    }

def interactive_chat():
    """
    Run an interactive chat session in the command line.
    Updated to handle multi-part responses.
    """
    print("πŸ€– AU Academic Advisor Chatbot - Interactive Mode")
    print("Type 'quit', 'exit', or 'q' to end the conversation.")
    print("Type 'info' to get information about the chatbot.\n")

    # Start with initialization message
    init_response = initialize_chatbot()
    print("πŸ€– ", init_response["response"])
    print("\n--- How can I help you today? ---\n")

    while True:
        try:
            # Get user input
            user_query = input("You: ").strip()

            # Check for exit commands
            if user_query.lower() in ['quit', 'exit', 'q']:
                print("\nπŸ€– Thank you for using the AU Academic Advisor Chatbot. Goodbye!")
                break
            
            # Check for info command
            if user_query.lower() == 'info':
                info_response = get_chatbot_info()
                print("πŸ€– ", info_response["response"])
                continue

            # Process the query
            if user_query:
                print("\nπŸ€– Thinking...\n")
                response = ask_question(user_query)
                
                # Print the response - Use full_response if available
                if "full_response" in response:
                    print("πŸ€– ", response["full_response"])
                else:
                    print("πŸ€– ", response["response"])
                
                # Print sources if available from metadata
                if "metadata" in response and response["metadata"]:
                    print("\n--- Sources ---")
                    for i, meta in enumerate(response["metadata"]):
                        source = meta.get('url', 'Unknown Source')
                        title = meta.get('title', 'Untitled')
                        print(f"{i+1}. {title} - {source}")
                
                print("\n")

        except KeyboardInterrupt:
            print("\n\nπŸ€– Chat interrupted. Type 'quit' to exit.")
        except Exception as e:
            print(f"\nπŸ€– An error occurred: {e}")

# Run the interactive chat when the script is executed directly
if __name__ == "__main__":
    try:
        interactive_chat()
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        import traceback
        traceback.print_exc()

# Ensure these functions are available when the module is imported
__all__ = [
    'ask_question', 
    'initialize_chatbot', 
    'get_chatbot_info', 
    'clear_conversation',
    'split_long_response',
    'interactive_chat'
]