File size: 6,550 Bytes
74b76f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from data_loader import DataLoader
from config import Config
from doctor_evaluator import DoctorEvaluator
from vector_store import VectorStoreManager
from rag_chain import RAGChain

def main():
    # print("LOADING TẤT CẢ CHUNKS...")
    
    # # 1. Load TẤT CẢ 3 JSON → Gộp 1 lần
    # docs = DataLoader.load_all_chunks()
    
    # 2. Build FAISS chung
    vs_manager = VectorStoreManager()
    # vs_manager.build_from_docs(docs)
    if not vs_manager.vector_store:
        print("Chạy: python build_faise.py trước")
    # 3. Tạo RAG chain
    rag = RAGChain(vs_manager)
     
    print("\n CHATBOT Y TẾ READY!")
    print(f"API KEY: {Config.GOOGLE_API_KEY}")
    print(f"Embedding: {Config.EMBEDDING_MODEL}")  
    print(" Gõ câu hỏi (quit để thoát)")
    print("-" * 50)
    
    print("\n CHỈNH THỨC: 1=Chatbot | 2=Đánh giá bác sĩ")
    mode = input("Chọn mode (1/2): ").strip()
    
    if mode == "2":
        """
        # evaluator = DoctorEvaluator(rag)
        # print("\n ĐÁNH GIÁ BÁC SĨ")
        # print("Format: Bệnh + Triệu chứng + Trả lời bác sĩ")
        
        # disease = input("Bệnh: ").strip()
        # symptoms = input("Triệu chứng: ").strip()
        # doctor_answer = input("Trả lời bác sĩ: ").strip()
        """
        # standard, evaluation = evaluator.evaluate_doctor(disease, symptoms, doctor_answer)
        
        # print("\n ĐÁP ÁN CHUẨN:")
        # print(standard)
        # print("\n  NHẬN XÉT:")
        # print(evaluation) 

        """
        1. Nhận case: gồm bệnh + triệu chứng 
        2. Nhận câu trả lời của bác sĩ cho case
        2. RAG truy tìm đáp án chuẩn (CHẨN ĐOÁN + ĐIỀU TRỊ) của case đó
        3. So sánh câu trả lời của bác sĩ vs key answer qua gemini theo các tiêu chí (điem manh, diem yeu, da co, thieu, nhan xet tong quan)
        """ 

        """
        CÓ THỂ THAY ĐỔI LUỒNG TRÊN THÀNH: 
        1. Nhận mỗi triệu chứng --> RAG ra được tên bệnh --> RAG tiếp tên bệnh + CHẨN ĐOÁN + ĐIỀU TRỊ (có thể thêm nếu cần thiết)
        """

        """
        NHỮNG THỨ CẦN RAG ĐƯỢC: 
        1. Nhận một câu bất kỳ nằm trong pdf --> RAG đúng mục chứa câu đó 
        VD: Liều:
        +Trẻ ≤ 5 tuổi: 4 mg
        +Trẻ ≥ 5 tuổi và ≤ 13 (15) tuổi: 5 mg
        +Trẻ ≥ 13 (15) tuổi: 10 mg 
        ==> SUYỄN TRẺ EM (VI. PHÒNG NGỪA)
        2. Nhận triêu chứng --> RAG đúng bệnh có triệu chứng đó 
        3. Nhận mục --> RAG đúng mục đó trả về 

        """

        # result = evaluator.evaluate_doctor(disease, symptoms, doctor_answer)

         
        # print("\n ĐÁP ÁN CHUẨN:")
        # print(result['standard'])  


        disease = input("\nBỆNH: ").strip()
        
        evaluator = DoctorEvaluator(rag)
        result = evaluator.evaluate_doctor(disease)
        
        print("=" * 80)
        print("\nTHU THẬP ĐẦY ĐỦ - TIẾN HÀNH LUỒNG")
        print("=" * 80)
        print("\nCASE BỆNH NHÂN:")
        print(result['case'])
        print("=" * 80)
        print("\nĐÁP ÁN CHUẨN:")
        print(result['standard'])
        print("=" * 80)
        print("\nPHÂN TÍCH CHI TIẾT:")
        print(result['evaluation'])

         #  TOP 1 + TÀI LIỆU
        print("\n TOP 1 CHẮC CHẮN:")
        if result['sources'] and result['sources'][0]:
            top_doc = result['sources'][0]
            print(f"[{top_doc.metadata['source_file']}]")
            print(f" {top_doc.metadata['chunk_title']}")
            print(f" {top_doc.metadata['section_title']}")
            print(f" {top_doc.page_content.strip()[:300]}")
            print()
        
        print(" TÀI LIỆU THAM KHẢO:")
        if result['sources']:
            for i, doc in enumerate(result['sources'][:3], 1):
                file = doc.metadata.get("source_file", "N/A")
                chunk_id = doc.metadata.get("chunk_id", "N/A")
                chunk_title = doc.metadata.get("chunk_title", "N/A")
                section_id = doc.metadata.get("section_id", "N/A")
                section_title = doc.metadata.get("section_title", "N/A")
                preview = doc.page_content[:80] + "..." 
                print(f" {i}. [{file}] {chunk_id} | {chunk_title} | {section_id} | {section_title}")
                print(f"     {preview}")
            print()
    
    else:
        print("\n CHATBOT Y TẾ READY!")
        print(f"API KEY: {Config.GOOGLE_API_KEY}")
        print(f"Embedding: {Config.EMBEDDING_MODEL}")
        print("Gõ câu hỏi (quit để thoát)")
        print("-" * 50)
        
        # 4. CHAT LOOP - CHỈ HỎI → TRẢ LỜI
        while True:
            query = input(" Bạn: ").strip()
            if query.lower() in ['quit', 'exit', 'bye']:
                print(" Tạm biệt!")
                break
            
            if not query:
                continue
                
            print(" Đang trả lời...")
            answer, sources = rag.query(query)  # LẤY CẢ sources
            
            print(f"\n Trả lời: {answer}\n")

            #  TOP 1 CHẮC CHẮN - FULL CONTENT
            if sources and sources[0]:
                top_doc = sources[0]
                print(" TOP 1 CHẮC CHẮN:")
                print(f" [{top_doc.metadata['source_file']}]")
                print(f"  {top_doc.metadata['chunk_title']}")
                print(f"  {top_doc.metadata['section_title']}")
                print(f"  {top_doc.page_content.strip()[:300]}")  # FULL content
                print()
            
            #  IN 3 RETRIEVED DOCS
            if sources:
                print(" TÀI LIỆU THAM KHẢO:")
                for i, doc in enumerate(sources[:3], 1):  # Top 3
                    file = doc.metadata.get("source_file", "N/A")
                    chunk_id = doc.metadata.get("chunk_id", "N/A")
                    chunk_title = doc.metadata.get("chunk_title", "N/A")
                    section_id = doc.metadata.get("section_id", "N/A")
                    section_title = doc.metadata.get("section_title", "N/A")
                    preview = doc.page_content[:80] + "..." 
                    print(f"  {i}. [{file}] {chunk_id} | {chunk_title} | {section_id} | {section_title}")
                    print(f"     {preview}")
                print()

if __name__ == "__main__":
    main()