File size: 38,005 Bytes
b9333d0
 
 
 
 
 
b8f3012
b9333d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d047b2
 
 
 
 
 
 
 
 
b9333d0
 
b8f3012
b9333d0
 
b8f3012
 
76d036e
b8f3012
76d036e
b9333d0
b8f3012
4d047b2
b8f3012
 
 
4d047b2
b8f3012
 
b9333d0
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9333d0
 
b8f3012
b9333d0
b8f3012
b9333d0
4d047b2
b9333d0
 
 
 
 
 
 
b8f3012
b9333d0
b8f3012
 
b9333d0
 
 
b8f3012
b9333d0
 
 
 
 
 
 
b8f3012
b9333d0
 
 
b8f3012
b9333d0
 
 
 
691b688
b9333d0
b8f3012
b9333d0
b8f3012
b9333d0
 
 
4d047b2
 
 
 
 
a7b7d98
5e0c36d
 
 
 
654739c
5e0c36d
4d047b2
a77efdb
5e0c36d
 
 
 
 
 
 
 
a897a27
 
 
 
 
 
 
5e0c36d
 
 
 
471e87b
a48daee
4d047b2
471e87b
5e0c36d
a897a27
5e0c36d
 
a48daee
 
4d047b2
61ff52b
5e0c36d
654739c
a7b7d98
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b65cd76
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d047b2
 
dda2e5f
 
3214945
dda2e5f
 
 
3214945
dda2e5f
 
3214945
dda2e5f
4d047b2
dda2e5f
 
 
 
 
 
3214945
dda2e5f
4d047b2
dda2e5f
4d047b2
dda2e5f
3214945
dda2e5f
4d047b2
dda2e5f
 
 
3214945
dda2e5f
 
 
3214945
dda2e5f
 
 
 
3214945
dda2e5f
4d047b2
dda2e5f
 
 
 
3214945
4d047b2
dda2e5f
 
 
3214945
dda2e5f
 
 
 
 
 
3214945
dda2e5f
 
 
 
4d047b2
 
b8f3012
 
4d047b2
3214945
b8f3012
 
 
 
 
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
4d047b2
b8f3012
 
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d047b2
b8f3012
 
 
 
4d047b2
b8f3012
 
 
 
 
 
 
 
 
 
4d047b2
b8f3012
 
 
b9333d0
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3214945
4d047b2
 
 
 
 
 
 
 
b9333d0
4d047b2
 
b9333d0
4d047b2
 
b9333d0
4d047b2
 
b9333d0
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f3012
4d047b2
b8f3012
 
 
4d047b2
 
 
 
 
b8f3012
 
 
 
 
 
b9333d0
 
 
b8f3012
b9333d0
4d047b2
b8f3012
4d047b2
b9333d0
b8f3012
 
 
 
 
b9333d0
 
b8f3012
b9333d0
 
b8f3012
b011019
 
4d047b2
b8f3012
 
 
4d047b2
b9333d0
3214945
b9333d0
 
 
 
 
 
 
 
 
b8f3012
 
 
b9333d0
 
 
 
 
4d047b2
b9333d0
4d047b2
b9333d0
4d047b2
b9333d0
 
 
 
 
 
 
 
4d047b2
b9333d0
 
4d047b2
 
b8f3012
4d047b2
 
 
 
72dc521
4d047b2
 
 
 
 
 
 
 
 
 
0657353
b8f3012
4d047b2
 
2177417
4d047b2
b8f3012
 
 
 
 
0657353
b8f3012
2177417
4d047b2
 
b8f3012
 
 
4d047b2
b8f3012
4d047b2
b8f3012
 
 
 
 
 
4d047b2
b8f3012
 
 
 
 
 
 
4d047b2
b8f3012
4d047b2
b8f3012
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f3012
4d047b2
 
 
 
 
b8f3012
 
 
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7a66e6
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f3012
4d047b2
b8f3012
f7a66e6
 
b8f3012
 
4d047b2
b8f3012
4d047b2
 
 
 
 
 
b8f3012
4d047b2
b8f3012
 
 
4d047b2
b8f3012
4d047b2
 
 
 
 
 
 
 
 
b8f3012
4d047b2
b8f3012
4d047b2
b8f3012
4d047b2
b8f3012
4d047b2
 
 
 
b8f3012
4d047b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f3012
4d047b2
 
 
3214945
4d047b2
 
 
 
3214945
 
 
4d047b2
 
b8f3012
4d047b2
b8f3012
4d047b2
 
 
 
b8f3012
4d047b2
 
b9333d0
b8f3012
b9333d0
4d047b2
 
 
cdee10f
 
 
b9333d0
cdee10f
 
 
 
 
 
 
 
b8f3012
cdee10f
 
 
 
 
4f580f8
cdee10f
 
 
 
 
4d047b2
cdee10f
 
 
 
 
 
 
 
 
3483e5e
 
 
 
5f8f5f0
 
fd37d3e
 
 
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
import re
import json
import requests
import traceback
import time
import os
from typing import Dict, Any, List, Optional, Tuple
from datetime import datetime, timedelta

# Updated imports for pydantic
from pydantic import BaseModel, Field

# Updated imports for LangChain
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_ollama import OllamaLLM
from langchain.chains import LLMChain
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_huggingface.embeddings import HuggingFaceEmbeddings

# Enhanced HuggingFace imports for improved functionality
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import numpy as np

# Import endpoints documentation
from endpoints_documentation import endpoints_documentation

# Set environment variables for HuggingFace
os.environ["HF_HOME"] = "/tmp/huggingface"
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"


class ChatMessage(BaseModel):
    """Data model for chat messages"""
    message_id: str = Field(..., description="Unique identifier for the message")
    user_id: str = Field(..., description="User identifier")
    message: str = Field(..., description="The user's message")
    timestamp: datetime = Field(default_factory=datetime.now, description="When the message was sent")
    language: str = Field(default="english", description="Detected language of the message")


class ChatResponse(BaseModel):
    """Data model for chatbot responses"""
    response_id: str = Field(..., description="Unique identifier for the response")
    response_type: str = Field(..., description="Type of response: 'conversation' or 'api_action'")
    message: str = Field(..., description="The chatbot's response message")
    api_call_made: bool = Field(default=False, description="Whether an API call was made")
    api_data: Optional[Dict[str, Any]] = Field(default=None, description="API response data if applicable")
    language: str = Field(default="english", description="Language of the response")
    timestamp: datetime = Field(default_factory=datetime.now, description="When the response was generated")


class RouterResponse(BaseModel):
    """Data model for router chain response"""
    intent: str = Field(..., description="Either 'API_ACTION' or 'CONVERSATION'")
    confidence: float = Field(..., description="Confidence score between 0.0 and 1.0")
    reasoning: str = Field(..., description="Explanation of the decision")
    endpoint: Optional[str] = Field(default=None, description="API endpoint if intent is API_ACTION")
    method: Optional[str] = Field(default=None, description="HTTP method if intent is API_ACTION")
    params: Dict[str, Any] = Field(default_factory=dict, description="Parameters for API call")
    missing_required: List[str] = Field(default_factory=list, description="Missing required parameters")


class HealthcareChatbot:
    def __init__(self):
        self.endpoints_documentation = endpoints_documentation
        self.ollama_base_url = "http://localhost:11434"
        self.model_name = "gemma3"
        self.BASE_URL = 'https://ae84-197-54-54-66.ngrok-free.app'
        self.headers = {'Content-type': 'application/json'}
        self.user_id = 'c5e7e4f0-63c0-4fe6-801e-4a0880e155bc'
        self.max_retries = 3
        self.retry_delay = 2
        
        # Store conversation history
        self.conversation_history = []
        self.max_history_length = 10  # Keep last 10 exchanges
        
        # Initialize components
        self._initialize_language_tools()
        self._initialize_llm()
        self._initialize_parsers_and_chains()
        self._initialize_date_parser()
        
        print("Healthcare Chatbot initialized successfully!")
        self._print_welcome_message()

    def _print_welcome_message(self):
        """Print welcome message in both languages"""
        print("\n" + "="*60)
        print("🏥 HEALTHCARE CHATBOT READY")
        print("="*60)
        print("English: Hello! I'm your healthcare assistant. I can help you with:")
        print("• Booking and managing appointments")
        print("• Finding hospital information")
        print("• Viewing your medical records")
        print("• General healthcare questions")
        print()
        print("Arabic: مرحباً! أنا مساعدك الطبي. يمكنني مساعدتك في:")
        print("• حجز وإدارة المواعيد")
        print("• العثور على معلومات المستشفى")
        print("• عرض سجلاتك الطبية")
        print("• الأسئلة الطبية العامة")
        print("="*60)
        print("Type 'quit' or 'خروج' to exit\n")

    def _initialize_language_tools(self):
        """Initialize language processing tools"""
        try:
            self.embeddings = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-large")
            self.language_classifier = pipeline(
                "text-classification", 
                model="papluca/xlm-roberta-base-language-detection",
                top_k=1
            )
            self.sentiment_analyzer = pipeline(
                "sentiment-analysis",
                model="cardiffnlp/twitter-xlm-roberta-base-sentiment"
            )
            print("✓ Language processing models loaded successfully")
        except Exception as e:
            print(f"⚠ Warning: Some language models failed to load: {e}")
            self.language_classifier = None
            self.sentiment_analyzer = None

    def _initialize_date_parser(self):
        """Initialize date parsing model"""
        try:
            self.date_parser = pipeline(
                "token-classification",
                model="Jean-Baptiste/roberta-large-ner-english",
                aggregation_strategy="simple"
            )
        except Exception as e:
            print(f"⚠ Warning: Date parsing model failed to load: {e}")
            self.date_parser = None

    def _initialize_llm(self):
        """Initialize the LLM"""
        callbacks = [StreamingStdOutCallbackHandler()]
        self.llm = OllamaLLM(
            model=self.model_name,
            base_url=self.ollama_base_url,
            callbacks=callbacks,
            temperature=0.7,
            num_ctx=8192,
            top_p=0.9,
            request_timeout=60,
        )

    def _initialize_parsers_and_chains(self):
        """Initialize all prompt templates and chains - REVAMPED to 3 chains only"""
        self.json_parser = JsonOutputParser(pydantic_object=RouterResponse)

        # UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
        self.router_prompt_template = PromptTemplate(
                template="""
            You are a routing system. Your job is simple:
            1. Understand what the user wants
            2. Check if any endpoint can do what they want
            3. If yes = API_ACTION, if no = CONVERSATION

            ## Available API Endpoints Documentation
            {endpoints_documentation}

            ## User Query to Analyze
            Query: "{user_query}"
            Language: {detected_language}

            ## Step-by-Step Analysis

            **STEP 1: What does the user want?**
            - If query is in Arabic, translate it to English first
            - Identify the exact action or information the user is requesting
            - Focus on understanding their underlying need, not just the words

            **STEP 2: Find matching endpoint**
            - Read each endpoint description in the documentation
            - Check if any endpoint's purpose can fulfill what the user wants
            - Match based on functionality, not keywords

            **STEP 3: Decision**
            - Found matching endpoint = "API_ACTION"
            - No matching endpoint = "CONVERSATION"

            ## Output Format
            {{
                "intent": "CONVERSATION|API_ACTION",
                "confidence": 0.8,
                "reasoning": "User wants: [what user actually needs]. Found endpoint: [endpoint path and why it matches] OR No endpoint matches this need",
                "endpoint": "/exact/endpoint/path",
                "method": "GET|POST|PUT|DELETE",
                "params": {{}},
                "missing_required": []
            }}

            Now analyze the user query step by step and give me the JSON response.
            """,
                input_variables=["user_query", "detected_language", "extracted_keywords", 
                                "sentiment_analysis", "endpoints_documentation"]
            )
        # CONVERSATION CHAIN - Handles conversational responses
        self.conversation_template = PromptTemplate(
            template="""
            You are a friendly and professional healthcare chatbot assistant.

            === RESPONSE GUIDELINES ===
            - Respond ONLY in {detected_language}
            - Be helpful, empathetic, and professional
            - Keep responses concise but informative
            - Use appropriate medical terminology when needed
            - Maintain a caring and supportive tone

            === CONTEXT ===
            User Message: {user_query}
            Language: {detected_language}
            Sentiment: {sentiment_analysis}
            Conversation History: {conversation_history}

            === LANGUAGE-SPECIFIC INSTRUCTIONS ===

            FOR ARABIC RESPONSES:
            - Use Modern Standard Arabic (الفصحى)
            - Be respectful and formal as appropriate in Arabic culture
            - Use proper Arabic medical terminology
            - Keep sentences clear and grammatically correct

            FOR ENGLISH RESPONSES:
            - Use clear, professional English
            - Be warm and approachable
            - Use appropriate medical terminology

            === RESPONSE RULES ===
            1. Address the user's question or comment directly
            2. Provide helpful information when possible
            3. If you cannot help with something specific, explain what you CAN help with
            4. Never provide specific medical advice - always recommend consulting healthcare professionals
            5. Be encouraging and supportive
            6. Do NOT mix languages in your response
            7. End responses naturally without asking multiple questions

            Generate a helpful conversational response:""",
            input_variables=["user_query", "detected_language", "sentiment_analysis", "conversation_history"]
        )

        # API RESPONSE CHAIN - Formats API responses for users
        self.api_response_template = PromptTemplate(
            template="""
            You are a professional healthcare assistant. Answer the user's question using the provided API data.

            User Query: {user_query}
            User Sentiment: {sentiment_analysis}
            Response Language: {detected_language}

            API Response Data:
            {api_response}

            === INSTRUCTIONS ===
            
            1. Read and understand the API response data above
            2. Use ONLY the actual data from the API response - never make up information
            3. Respond in {detected_language} language only
            4. Write like you're talking to a friend or family member - warm, friendly, and caring
            5. Make it sound natural and conversational, not like a system message
            6. Convert technical data to simple, everyday language

            === DATE AND TIME FORMATTING ===
            
            When you see date_time fields like '2025-05-30T10:28:10':
            - For English: Convert to "May 30, 2025 at 10:28 AM" 
            - For Arabic: Convert to "٣٠ مايو ٢٠٢٥ في الساعة ١٠:٢٨ صباحاً"

            === RESPONSE EXAMPLES ===
            
            For appointment confirmations:
            - English: "Great! I've got your appointment set up for May 30, 2025 at 10:28 AM. Everything looks good!"
            - Arabic: "ممتاز! موعدك محجوز يوم ٣٠ مايو ٢٠٢٥ الساعة ١٠:٢٨ صباحاً. كل شيء جاهز!"

            For appointment info:
            - English: "Your next appointment is on May 30, 2025 at 10:28 AM. See you then!"
            - Arabic: "موعدك القادم يوم ٣٠ مايو ٢٠٢٥ الساعة ١٠:٢٨ صباحاً. نراك قريباً!"

            === TONE GUIDELINES ===
            - Use friendly words like: "Great!", "Perfect!", "All set!", "ممتاز!", "رائع!", "تمام!"
            - Add reassuring phrases: "Everything looks good", "You're all set", "كل شيء جاهز", "تم بنجاح"
            - Sound helpful and caring, not robotic or formal

            === LANGUAGE FORMATTING ===
            
            For Arabic responses:
            - Use Arabic numerals: ٠١٢٣٤٥٦٧٨٩
            - Use Arabic month names: يناير، فبراير، مارس، أبريل، مايو، يونيو، يوليو، أغسطس، سبتمبر، أكتوبر، نوفمبر، ديسمبر
            - Friendly, warm Arabic tone

            For English responses:  
            - Use standard English numerals
            - 12-hour time format with AM/PM
            - Friendly, conversational English tone

            === CRITICAL RULES ===
            - Extract dates and times exactly as they appear in the API response
            - Never use example dates or placeholder information
            - Respond only in the specified language
            - Make your response sound like a helpful friend, not a computer
            - Focus on answering the user's specific question with warmth and care

            Generate a friendly, helpful response using the API data provided above.
            """,
            input_variables=["user_query", "api_response", "detected_language", "sentiment_analysis"]
        )

        # Create the 3 chains
        self.router_chain = LLMChain(llm=self.llm, prompt=self.router_prompt_template)
        self.conversation_chain = LLMChain(llm=self.llm, prompt=self.conversation_template)
        self.api_response_chain = LLMChain(llm=self.llm, prompt=self.api_response_template)

    def detect_language(self, text):
        """Detect language of the input text"""
        if self.language_classifier and len(text.strip()) > 3:
            try:
                result = self.language_classifier(text)
                detected_lang = result[0][0]['label']
                confidence = result[0][0]['score']
                
                if detected_lang in ['ar', 'arabic']:
                    return "arabic"
                elif detected_lang in ['en', 'english']:
                    return "english"
                elif confidence > 0.8:
                    return "english"  # Default to English for unsupported languages
            except:
                pass
        
        # Fallback: Basic Arabic detection
        arabic_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+')
        if arabic_pattern.search(text):
            return "arabic"
        
        return "english"

    def analyze_sentiment(self, text):
        """Analyze sentiment of the text"""
        if self.sentiment_analyzer and len(text.strip()) > 3:
            try:
                result = self.sentiment_analyzer(text)
                return {
                    "sentiment": result[0]['label'],
                    "score": result[0]['score']
                }
            except:
                pass
        
        return {"sentiment": "NEUTRAL", "score": 0.5}

    def extract_keywords(self, text):
        """Extract keywords from text"""
        # Simple keyword extraction
        words = re.findall(r'\b\w+\b', text.lower())
        # Filter out common words and keep meaningful ones
        stopwords = {'the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were'}
        keywords = [w for w in words if len(w) > 3 and w not in stopwords]
        return list(set(keywords))[:5]  # Return top 5 unique keywords

    def get_conversation_context(self):
        """Get recent conversation history as context"""
        if not self.conversation_history:
            return "No previous conversation"
        
        context = []
        for item in self.conversation_history[-3:]:  # Last 3 exchanges
            context.append(f"User: {item['user_message']}")
            context.append(f"Bot: {item['bot_response'][:100]}...")  # Truncate long responses
        
        return " | ".join(context)

    def add_to_history(self, user_message, bot_response, response_type):
        """Add exchange to conversation history"""
        self.conversation_history.append({
            'timestamp': datetime.now(),
            'user_message': user_message,
            'bot_response': bot_response,
            'response_type': response_type
        })
        
        # Keep only recent history
        if len(self.conversation_history) > self.max_history_length:
            self.conversation_history = self.conversation_history[-self.max_history_length:]

    def parse_relative_date(self, text, detected_language):
        """Parse relative dates from text using a combination of methods"""
        today = datetime.now()
        
        # Handle common relative date patterns in English and Arabic
        tomorrow_patterns = {
            'english': [r'\btomorrow\b', r'\bnext day\b'],
            'arabic': [r'\bغدا\b', r'\bبكرة\b', r'\bغدًا\b', r'\bالغد\b']
        }
        
        next_week_patterns = {
            'english': [r'\bnext week\b'],
            'arabic': [r'\bالأسبوع القادم\b', r'\bالأسبوع المقبل\b', r'\bالاسبوع الجاي\b']
        }
        
        # Check for "tomorrow" patterns
        for pattern in tomorrow_patterns.get(detected_language, []) + tomorrow_patterns.get('english', []):
            if re.search(pattern, text, re.IGNORECASE):
                return (today + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%S')
        
        # Check for "next week" patterns
        for pattern in next_week_patterns.get(detected_language, []) + next_week_patterns.get('english', []):
            if re.search(pattern, text, re.IGNORECASE):
                return (today + timedelta(days=7)).strftime('%Y-%m-%dT%H:%M:%S')
        
        # If NER model is available, use it to extract date entities
        if self.date_parser and detected_language == 'english':
            try:
                date_entities = self.date_parser(text)
                for entity in date_entities:
                    if entity['entity_group'] == 'DATE':
                        print(f"Found date entity: {entity['word']}")
                        # Default to tomorrow if we detect any date
                        return (today + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%S')
            except Exception as e:
                print(f"Error in date parsing: {e}")
        
        # Default return None if no date pattern is recognized
        return None

    def parse_router_response(self, router_text):
        """Parse the router chain response into structured data"""
        try:
            # Clean the response text
            cleaned_response = router_text
            
            # Remove any comments (both single-line and multi-line)
            cleaned_response = re.sub(r'//.*?$', '', cleaned_response, flags=re.MULTILINE)
            cleaned_response = re.sub(r'/\*.*?\*/', '', cleaned_response, flags=re.DOTALL)
            
            # Remove any trailing commas
            cleaned_response = re.sub(r',(\s*[}\]])', r'\1', cleaned_response)
            
            # Try different methods to parse the JSON response
            try:
                # First attempt: direct JSON parsing of cleaned response
                parsed_response = json.loads(cleaned_response)
            except json.JSONDecodeError:
                try:
                    # Second attempt: extract JSON from markdown code block
                    json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', cleaned_response, re.DOTALL)
                    if json_match:
                        parsed_response = json.loads(json_match.group(1))
                    else:
                        raise ValueError("No JSON found in code block")
                except (json.JSONDecodeError, ValueError):
                    try:
                        # Third attempt: find JSON-like content using regex
                        json_pattern = r'\{\s*"intent"\s*:.*?\}'
                        json_match = re.search(json_pattern, cleaned_response, re.DOTALL)
                        if json_match:
                            json_str = json_match.group(0)
                            # Additional cleaning for the extracted JSON
                            json_str = re.sub(r'//.*?$', '', json_str, flags=re.MULTILINE)
                            json_str = re.sub(r',(\s*[}\]])', r'\1', json_str)
                            parsed_response = json.loads(json_str)
                        else:
                            raise ValueError("Could not extract JSON using regex")
                    except (json.JSONDecodeError, ValueError):
                        print(f"Failed to parse JSON. Raw response: {router_text}")
                        print(f"Cleaned response: {cleaned_response}")
                        # Return default conversation response on parse failure
                        return {
                            "intent": "CONVERSATION",
                            "confidence": 0.5,
                            "reasoning": "Failed to parse router response - defaulting to conversation",
                            "endpoint": None,
                            "method": None,
                            "params": {},
                            "missing_required": []
                        }
            
            # Validate required fields and set defaults
            validated_response = {
                "intent": parsed_response.get("intent", "CONVERSATION"),
                "confidence": parsed_response.get("confidence", 0.5),
                "reasoning": parsed_response.get("reasoning", "Router decision"),
                "endpoint": parsed_response.get("endpoint"),
                "method": parsed_response.get("method"),
                "params": parsed_response.get("params", {}),
                "missing_required": parsed_response.get("missing_required", [])
            }
            
            return validated_response
            
        except Exception as e:
            print(f"Error parsing router response: {e}")
            return {
                "intent": "CONVERSATION",
                "confidence": 0.5,
                "reasoning": f"Parse error: {str(e)}",
                "endpoint": None,
                "method": None,
                "params": {},
                "missing_required": []
            }

    def handle_conversation(self, user_query, detected_language, sentiment_result):
        """Handle conversational responses"""
        try:
            result = self.conversation_chain.invoke({
                "user_query": user_query,
                "detected_language": detected_language,
                "sentiment_analysis": json.dumps(sentiment_result),
                "conversation_history": self.get_conversation_context()
            })
            
            return result["text"].strip()
            
        except Exception as e:
            # Fallback response
            if detected_language == "arabic":
                return "أعتذر، واجهت مشكلة في المعالجة. كيف يمكنني مساعدتك؟"
            else:
                return "I apologize, I encountered a processing issue. How can I help you?"

    def backend_call(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Make API call to backend with retry logic"""
        endpoint_url = data.get('endpoint')
        endpoint_method = data.get('method')
        endpoint_params = data.get('params', {}).copy()

        print(f"🔗 Making API call to {endpoint_method} {self.BASE_URL + endpoint_url} with params: {endpoint_params}")
        
        # Inject patient_id if needed
        if 'patient_id' in endpoint_params:
            endpoint_params['patient_id'] = self.user_id
        
        retries = 0
        response = None
        while retries < self.max_retries:
            try:
                if endpoint_method.upper() == 'GET':
                    response = requests.get(
                        self.BASE_URL + endpoint_url,
                        params=endpoint_params,
                        headers=self.headers,
                        timeout=10
                    )
                elif endpoint_method.upper() in ['POST', 'PUT', 'DELETE']:
                    response = requests.request(
                        endpoint_method.upper(),
                        self.BASE_URL + endpoint_url,
                        json=endpoint_params,
                        headers=self.headers,
                        timeout=10
                    )
                
                response.raise_for_status()
                print('Backend Response:', response.json())
                return response.json()
                
            except requests.exceptions.RequestException as e:
                retries += 1
                if retries >= self.max_retries:
                    return {
                        "error": "Backend API call failed after multiple retries",
                        "details": str(e),
                        "status_code": getattr(e.response, 'status_code', None) if hasattr(e, 'response') else None
                    }
                
                time.sleep(self.retry_delay)

    def handle_api_action(self, user_query, detected_language, sentiment_result, keywords, router_data):
        """Handle API-based actions using router data"""
        try:
            # Parse relative dates and inject into parameters
            parsed_date = self.parse_relative_date(user_query, detected_language)
            if parsed_date:
                print(f"Parsed relative date: {parsed_date}")
                # Inject parsed date if available and a date parameter exists
                date_params = ['appointment_date', 'date', 'schedule_date', 'date_time', 'new_date_time']
                for param in date_params:
                    if param in router_data['params']:
                        router_data['params'][param] = parsed_date
            
            # Inject patient_id if needed
            if 'patient_id' in router_data['params']:
                router_data['params']['patient_id'] = self.user_id
            
            print(f"🔍 Final API call data: {router_data}")

            # Make backend API call
            api_response = self.backend_call(router_data)
            
            print("🔗 API response received:", api_response)
            
            # Generate user-friendly response
            user_response_result = self.api_response_chain.invoke({
                "user_query": user_query,
                "api_response": json.dumps(api_response, indent=2),
                "detected_language": detected_language,
                "sentiment_analysis": json.dumps(sentiment_result),
            })

            print("🔗 Final user response:", user_response_result["text"].strip())
            
            return {
                "response": user_response_result["text"].strip(),
                "api_data": api_response,
                "routing_info": router_data
            }
            
        except Exception as e:
            # Fallback error response
            if detected_language == "arabic":
                error_msg = "أعتذر، لم أتمكن من معالجة طلبك. يرجى المحاولة مرة أخرى أو صياغة السؤال بطريقة مختلفة."
            else:
                error_msg = "I apologize, I couldn't process your request. Please try again or rephrase your question."
            
            return {
                "response": error_msg,
                "api_data": {"error": str(e)},
                "routing_info": None
            }

    def chat(self, user_message: str) -> ChatResponse:
        """Main chat method that handles user messages - REVAMPED to use 3 chains"""
        start_time = time.time()
        
        # Check for exit commands
        if user_message.lower().strip() in ['quit', 'exit', 'خروج', 'bye', 'goodbye']:
            if self.detect_language(user_message) == "arabic":
                return ChatResponse(
                    response_id=str(time.time()),
                    response_type="conversation",
                    message="مع السلامة! أتمنى لك يوماً سعيداً. 👋",
                    language="arabic"
                )
            else:
                return ChatResponse(
                    response_id=str(time.time()),
                    response_type="conversation",
                    message="Goodbye! Have a great day! 👋",
                    language="english"
                )
        
        try:
            print(f"\n{'='*50}")
            print(f"🔍 Processing: '{user_message}'")
            print(f"{'='*50}")
            
            # Step 1: Language and sentiment analysis
            detected_language = self.detect_language(user_message)
            sentiment_result = self.analyze_sentiment(user_message)
            keywords = self.extract_keywords(user_message)
            
            print(f"🌐 Detected Language: {detected_language}")
            print(f"😊 Sentiment: {sentiment_result}")
            print(f"🔑 Keywords: {keywords}")
            
            # Step 2: Router Chain - Determine intent and route appropriately
            print(f"\n🤖 Running Router Chain...")
            router_result = self.router_chain.invoke({
                "user_query": user_message,
                "detected_language": detected_language,
                "extracted_keywords": json.dumps(keywords),
                "sentiment_analysis": json.dumps(sentiment_result),
                "conversation_history": self.get_conversation_context(),
                "endpoints_documentation": json.dumps(self.endpoints_documentation, indent=2)
            })
            
            # Parse router response
            router_data = self.parse_router_response(router_result["text"])
            print(f"🎯 Router Decision: {router_data}")
            
            # Step 3: Handle based on intent
            if router_data["intent"] == "CONVERSATION" and router_data['endpoint'] is None:
                print(f"\n💬 Handling as CONVERSATION")
                response_text = self.handle_conversation(user_message, detected_language, sentiment_result)
                
                # Add to conversation history
                self.add_to_history(user_message, response_text, "conversation")
                
                return ChatResponse(
                    response_id=str(time.time()),
                    response_type="conversation",
                    message=response_text,
                    api_call_made=False,
                    language=detected_language,
                    api_data=None
                )
                
            elif router_data["intent"] == "API_ACTION":
                print(f"\n🔗 Handling as API_ACTION")
                
                # Check for missing required parameters
                # if router_data.get("missing_required"):
                #     missing_params = router_data["missing_required"]
                #     if detected_language == "arabic":
                #         response_text = f"أحتاج إلى مزيد من المعلومات: {', '.join(missing_params)}"
                #     else:
                #         response_text = f"I need more information: {', '.join(missing_params)}"
                    
                #     return ChatResponse(
                #         response_id=str(time.time()),
                #         response_type="conversation",
                #         message=response_text,
                #         api_call_made=False,
                #         language=detected_language
                #     )
                
                # Handle API action
                api_result = self.handle_api_action(
                    user_message, detected_language, sentiment_result, keywords, router_data
                )
                
                # Add to conversation history
                self.add_to_history(user_message, api_result["response"], "api_action")
                
                return ChatResponse(
                    response_id=str(time.time()),
                    response_type="api_action",
                    message=api_result["response"],
                    api_call_made=True,
                    # api_data=api_result["api_data"] 
                    # api_data=json.dumps(api_result["api_data"]) if 'action_result' in api_result else None,
                    language=detected_language
                )
            
            else:
                # Fallback for unknown intent
                print(f"⚠️ Unknown intent: {router_data['intent']}")
                fallback_response = self.handle_conversation(user_message, detected_language, sentiment_result)
                
                return ChatResponse(
                    response_id=str(time.time()),
                    response_type="conversation",
                    message=fallback_response,
                    api_call_made=False,
                    language=detected_language
                )
                
        except Exception as e:
            print(f"❌ Error in chat method: {str(e)}")
            print(f"❌ Traceback: {traceback.format_exc()}")
            
            # Fallback error response
            if self.detect_language(user_message) == "arabic":
                error_message = "أعتذر، حدث خطأ في معالجة رسالتك. يرجى المحاولة مرة أخرى."
            else:
                error_message = "I apologize, there was an error processing your message. Please try again."
            
            return ChatResponse(
                response_id=str(time.time()),
                response_type="conversation",
                message=error_message,
                api_call_made=False,
                language=self.detect_language(user_message)
            )
        
        finally:
            end_time = time.time()
            print(f"⏱️ Processing time: {end_time - start_time:.2f} seconds")

    def run_interactive_chat(self):
        """Run the interactive chat interface"""
        try:
            while True:
                try:
                    # Get user input
                    user_input = input("\n👤 You: ").strip()
                    
                    if not user_input:
                        continue
                    
                    # Process the message
                    response = self.chat(user_input)
                    
                    # Display the response
                    print(f"\n🤖 Bot: {response.message}")
                    
                    # Check for exit
                    if user_input.lower() in ['quit', 'exit', 'خروج', 'bye', 'goodbye']:
                        break
                        
                except KeyboardInterrupt:
                    print("\n\n👋 Chat interrupted. Goodbye!")
                    break
                except EOFError:
                    print("\n\n👋 Chat ended. Goodbye!")
                    break
                except Exception as e:
                    print(f"\n❌ Error: {e}")
                    continue
                    
        except Exception as e:
            print(f"❌ Fatal error in chat interface: {e}")

    def clear_history(self):
        """Clear conversation history"""
        self.conversation_history = []
        print("🗑️ Conversation history cleared.")



# def main():
#     """Main function to run the healthcare chatbot"""
#     try:
#         print("🚀 Starting Healthcare Chatbot...")
#         chatbot = HealthcareChatbot()
#         chatbot.run_interactive_chat()
        
#     except KeyboardInterrupt:
#         print("\n\n👋 Shutting down gracefully...")
#     except Exception as e:
#         print(f"❌ Fatal error: {e}")
#         print(f"❌ Traceback: {traceback.format_exc()}")


# if __name__ == "__main__":
#     main()


from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, Any, Optional


app = FastAPI(
    title="Healthcare AI Assistant",
    description="An AI-powered healthcare assistant that handles appointment booking and queries",
    version="1.0.0"
)

# Initialize the AI agent
agent = HealthcareChatbot()

class QueryRequest(BaseModel):
    query: str


@app.post("/query")
async def process_query(request: QueryRequest):
    """
    Process a user query and return a response
    """
    try:
        response = agent.chat(request.query).message
        return response
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/health")
async def health_check():
    """
    Health check endpoint
    """
    return {"status": "healthy", "service": "healthcare-ai-assistant"}

@app.get("/")
async def root():
    return {"message": "Hello World"}

# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)