Spaces:
Sleeping
Sleeping
Commit
·
423d658
1
Parent(s):
037ab87
Fix the prompts
Browse files
main.py
CHANGED
@@ -68,9 +68,9 @@ class HealthcareChatbot:
|
|
68 |
self.endpoints_documentation = endpoints_documentation
|
69 |
self.ollama_base_url = "http://localhost:11434"
|
70 |
self.model_name = "gemma3"
|
71 |
-
self.BASE_URL = 'https://
|
72 |
self.headers = {'Content-type': 'application/json'}
|
73 |
-
self.user_id = '
|
74 |
self.max_retries = 3
|
75 |
self.retry_delay = 2
|
76 |
|
@@ -155,56 +155,102 @@ class HealthcareChatbot:
|
|
155 |
self.json_parser = JsonOutputParser(pydantic_object=RouterResponse)
|
156 |
|
157 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
self.router_prompt_template = PromptTemplate(
|
159 |
template="""
|
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 |
-
CRITICAL RULES:
|
202 |
-
• Use {current_datetime} for ALL date calculations
|
203 |
-
• Show calculation work in reasoning
|
204 |
-
• Translate ALL Arabic parameters to English
|
205 |
-
• Match endpoints by functionality, not keywords
|
206 |
-
|
207 |
-
Analyze and respond with JSON:""",
|
208 |
input_variables=["user_query", "detected_language", "extracted_keywords",
|
209 |
"sentiment_analysis", "endpoints_documentation", "current_datetime",
|
210 |
"timezone", "current_day_name"]
|
@@ -317,42 +363,73 @@ class HealthcareChatbot:
|
|
317 |
|
318 |
self.api_response_template = PromptTemplate(
|
319 |
template="""
|
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 |
-
English: 12-hour format with AM/PM
|
346 |
-
|
347 |
-
EXAMPLES:
|
348 |
-
Appointment confirmed:
|
349 |
-
• English: "Great! Your appointment is set for May 30, 2025 at 10:28 AM!"
|
350 |
-
• Arabic: "ممتاز! موعدك محجوز يوم ٣٠ مايو ٢٠٢٥ الساعة ١٠:٢٨ صباحاً!"
|
351 |
-
|
352 |
-
Generate a friendly response using the API data:""",
|
353 |
input_variables=["user_query", "api_response", "detected_language", "sentiment_analysis"]
|
354 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
|
|
|
356 |
# self.api_response_template = PromptTemplate(
|
357 |
# template="""
|
358 |
# You are a professional healthcare assistant. Generate a natural language response to the user's query using ONLY the provided API data.
|
@@ -686,6 +763,7 @@ class HealthcareChatbot:
|
|
686 |
else:
|
687 |
router_data['params']['patient_id'] = self.user_id
|
688 |
|
|
|
689 |
print(f"🔍 Final API call data: {router_data}")
|
690 |
|
691 |
# Make backend API call
|
@@ -776,7 +854,7 @@ class HealthcareChatbot:
|
|
776 |
print(f"🎯 Router Decision: {router_data}")
|
777 |
|
778 |
# Step 3: Handle based on intent
|
779 |
-
if router_data["intent"] == "CONVERSATION"
|
780 |
print(f"\n💬 Handling as CONVERSATION")
|
781 |
response_text = self.handle_conversation(user_message, detected_language, sentiment_result)
|
782 |
|
|
|
68 |
self.endpoints_documentation = endpoints_documentation
|
69 |
self.ollama_base_url = "http://localhost:11434"
|
70 |
self.model_name = "gemma3"
|
71 |
+
self.BASE_URL = 'https://0bb3-197-54-60-164.ngrok-free.app'
|
72 |
self.headers = {'Content-type': 'application/json'}
|
73 |
+
self.user_id = '8881d156-8bc4-4090-a79d-35f8c03db7a2'
|
74 |
self.max_retries = 3
|
75 |
self.retry_delay = 2
|
76 |
|
|
|
155 |
self.json_parser = JsonOutputParser(pydantic_object=RouterResponse)
|
156 |
|
157 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
158 |
+
# self.router_prompt_template = PromptTemplate(
|
159 |
+
# template="""
|
160 |
+
# You are a routing system. Analyze user intent and handle dates precisely.
|
161 |
+
|
162 |
+
# CONTEXT:
|
163 |
+
# Query: "{user_query}"
|
164 |
+
# Language: {detected_language}
|
165 |
+
# Current: {current_datetime} ({current_day_name})
|
166 |
+
# Timezone: {timezone}
|
167 |
+
|
168 |
+
# ENDPOINTS:
|
169 |
+
# {endpoints_documentation}
|
170 |
+
|
171 |
+
# ANALYSIS STEPS:
|
172 |
+
# 1. **Intent**: What does the user want? (translate Arabic to English first)
|
173 |
+
# 2. **Date/Time**: Calculate precisely using current datetime as base
|
174 |
+
# 3. **Match**: Find endpoint that can fulfill the request
|
175 |
+
# 4. **Decision**: Matching endpoint = API_ACTION, else CONVERSATION
|
176 |
+
|
177 |
+
# DATE CALCULATIONS (use {current_datetime} as base):
|
178 |
+
# • Today/اليوم = current date
|
179 |
+
# • Tomorrow/غدا = +1 day
|
180 |
+
# • Next week/الأسبوع القادم = +7 days
|
181 |
+
# • Next [weekday]/يوم [weekday] القادم: Calculate days to target weekday
|
182 |
+
# - Weekdays: الأحد=0, الاثنين=1, الثلاثاء=2, الأربعاء=3, الخميس=4, الجمعة=5, السبت=6
|
183 |
+
# - Formula: If target > current: target-current, else: 7-(current-target)
|
184 |
+
# • Times: صباحًا=09:00, مساءً=18:00, ظهرًا=12:00
|
185 |
+
# • Format: YYYY-MM-DDTHH:MM:SS
|
186 |
+
|
187 |
+
# OUTPUT FORMAT:
|
188 |
+
# {{
|
189 |
+
# "intent": "CONVERSATION|API_ACTION",
|
190 |
+
# "confidence": 0.8,
|
191 |
+
# "reasoning": "User wants: [need]. Date calc: [show work]. Endpoint: [path/reason]",
|
192 |
+
# "endpoint": "/exact/path" or null,
|
193 |
+
# "method": "GET|POST|PUT|DELETE" or null,
|
194 |
+
# "params": {{
|
195 |
+
# // ALL VALUES IN ENGLISH - translate Arabic names/terms
|
196 |
+
# }},
|
197 |
+
# "missing_required": [],
|
198 |
+
# "calculated_datetime": "YYYY-MM-DDTHH:MM:SS" or null
|
199 |
+
# }}
|
200 |
+
|
201 |
+
# CRITICAL RULES:
|
202 |
+
# • Use {current_datetime} for ALL date calculations
|
203 |
+
# • Show calculation work in reasoning
|
204 |
+
# • Translate ALL Arabic parameters to English
|
205 |
+
# • Match endpoints by functionality, not keywords
|
206 |
+
|
207 |
+
# Analyze and respond with JSON:""",
|
208 |
+
# input_variables=["user_query", "detected_language", "extracted_keywords",
|
209 |
+
# "sentiment_analysis", "endpoints_documentation", "current_datetime",
|
210 |
+
# "timezone", "current_day_name"]
|
211 |
+
# )
|
212 |
self.router_prompt_template = PromptTemplate(
|
213 |
template="""
|
214 |
+
You are a medical appointment router. Determine if user needs API call or conversation.
|
215 |
+
|
216 |
+
CONTEXT: Query: "{user_query}" | Language: {detected_language} | Current: {current_datetime}
|
217 |
+
|
218 |
+
ENDPOINTS: {endpoints_documentation}
|
219 |
+
|
220 |
+
DECISION RULES:
|
221 |
+
API_ACTION only for these medical requests:
|
222 |
+
- View appointments/reservations (my/all)
|
223 |
+
- Book/cancel/reschedule appointments
|
224 |
+
- Show doctors/hospitals/patients
|
225 |
+
- Get doctor details
|
226 |
+
|
227 |
+
CONVERSATION for everything else:
|
228 |
+
- Greetings: "hello", "hi", "مرحبا"
|
229 |
+
- General questions, help requests, small talk
|
230 |
+
- Unclear/non-medical requests
|
231 |
+
|
232 |
+
DATE PARSING (if needed):
|
233 |
+
- Today/اليوم = current date
|
234 |
+
- Tomorrow/غدا = +1 day
|
235 |
+
- Times: صباحًا=09:00, مساءً=18:00, ظهرًا=12:00
|
236 |
+
- Format: YYYY-MM-DDTHH:MM:SS
|
237 |
+
|
238 |
+
OUTPUT JSON:
|
239 |
+
{{
|
240 |
+
"intent": "CONVERSATION|API_ACTION",
|
241 |
+
"confidence": 0.9,
|
242 |
+
"reasoning": "Brief explanation of decision",
|
243 |
+
"endpoint": "/path" or null,
|
244 |
+
"method": "GET|POST|PUT" or null,
|
245 |
+
"params": {{
|
246 |
+
// ALL VALUES IN ENGLISH - translate Arabic names/terms
|
247 |
+
}},
|
248 |
+
"calculated_datetime": "YYYY-MM-DDTHH:MM:SS" or null
|
249 |
+
}}
|
250 |
+
|
251 |
+
RULE: When uncertain, choose CONVERSATION. Translate Arabic terms to English in params.
|
252 |
+
|
253 |
+
Analyze:""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
input_variables=["user_query", "detected_language", "extracted_keywords",
|
255 |
"sentiment_analysis", "endpoints_documentation", "current_datetime",
|
256 |
"timezone", "current_day_name"]
|
|
|
363 |
|
364 |
self.api_response_template = PromptTemplate(
|
365 |
template="""
|
366 |
+
You are a friendly healthcare assistant. Answer using API data in {detected_language}.
|
367 |
+
|
368 |
+
CONTEXT:
|
369 |
+
Query: {user_query}
|
370 |
+
API Data: {api_response}
|
371 |
+
|
372 |
+
DATE/TIME PARSING:
|
373 |
+
Extract from API format "YYYY-MM-DDTHH:MM:SS":
|
374 |
+
- Parse: Year(0-3), Month(5-6), Day(8-9), Hour(11-12), Minute(14-15)
|
375 |
+
- Months: 01=Jan/يناير, 02=Feb/فبراير, 03=Mar/مارس, 04=Apr/أبريل, 05=May/مايو, 06=Jun/يونيو, 07=Jul/يوليو, 08=Aug/أغسطس, 09=Sep/سبتمبر, 10=Oct/أكتوبر, 11=Nov/نوفمبر, 12=Dec/ديسمبر
|
376 |
+
- Time: 00-11=AM/صباحاً, 12=12PM/١٢ظهراً, 13-23=subtract 12+PM/مساءً
|
377 |
+
|
378 |
+
STEP-BY-STEP PROCESS:
|
379 |
+
1. Find date field in API data
|
380 |
+
2. Extract the datetime string (YYYY-MM-DDTHH:MM:SS format)
|
381 |
+
3. Parse each component using the rules above
|
382 |
+
4. Format according to language requirements
|
383 |
+
|
384 |
+
RULES:
|
385 |
+
- CRITICAL: Read date field from API data, ignore any example dates
|
386 |
+
- Parse the ACTUAL datetime string from the API response
|
387 |
+
- Use ONLY API data, never make up information
|
388 |
+
- Friendly tone with starters
|
389 |
+
|
390 |
+
Generate response:""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
input_variables=["user_query", "api_response", "detected_language", "sentiment_analysis"]
|
392 |
)
|
393 |
+
# last one
|
394 |
+
# self.api_response_template = PromptTemplate(
|
395 |
+
# template="""
|
396 |
+
# You are a friendly healthcare assistant. Answer using the API data provided.
|
397 |
+
|
398 |
+
# CONTEXT:
|
399 |
+
# Query: {user_query}
|
400 |
+
# Language: {detected_language}
|
401 |
+
# API Data: {api_response}
|
402 |
+
|
403 |
+
# INSTRUCTIONS:
|
404 |
+
# • Use ONLY actual API data - never make up information
|
405 |
+
# • Respond in {detected_language} only
|
406 |
+
# • Sound warm and conversational, like talking to a friend
|
407 |
+
# • Convert technical data to simple language
|
408 |
+
|
409 |
+
# DATE/TIME FORMAT:
|
410 |
+
# '2025-05-30T10:28:10' →
|
411 |
+
# • English: "May 30, 2025 at 10:28 AM"
|
412 |
+
# • Arabic: "٣٠ مايو ٢٠٢٥ في الساعة ١٠:٢٨ صباحاً"
|
413 |
+
|
414 |
+
# TONE:
|
415 |
+
# • Friendly starters: "Great!", "Perfect!", "ممتاز!", "رائع!"
|
416 |
+
# • Reassuring: "Everything looks good", "كل شيء جاهز"
|
417 |
+
# • Natural conversation, not robotic
|
418 |
+
|
419 |
+
# LANGUAGE SPECIFICS:
|
420 |
+
# Arabic: Use Arabic numerals (٠١٢٣٤٥٦٧٨٩) and month names
|
421 |
+
# English: 12-hour format with AM/PM
|
422 |
+
|
423 |
+
# EXAMPLES:
|
424 |
+
# Appointment confirmed:
|
425 |
+
# • English: "Great! Your appointment is set for May 30, 2025 at 10:28 AM!"
|
426 |
+
# • Arabic: "ممتاز! موعدك محجوز يوم ٣٠ مايو ٢٠٢٥ الساعة ١٠:٢٨ صباحاً!"
|
427 |
+
|
428 |
+
# Generate a friendly response using the API data:""",
|
429 |
+
# input_variables=["user_query", "api_response", "detected_language", "sentiment_analysis"]
|
430 |
+
# )
|
431 |
|
432 |
+
# first one to be removed
|
433 |
# self.api_response_template = PromptTemplate(
|
434 |
# template="""
|
435 |
# You are a professional healthcare assistant. Generate a natural language response to the user's query using ONLY the provided API data.
|
|
|
763 |
else:
|
764 |
router_data['params']['patient_id'] = self.user_id
|
765 |
|
766 |
+
|
767 |
print(f"🔍 Final API call data: {router_data}")
|
768 |
|
769 |
# Make backend API call
|
|
|
854 |
print(f"🎯 Router Decision: {router_data}")
|
855 |
|
856 |
# Step 3: Handle based on intent
|
857 |
+
if router_data["intent"] == "CONVERSATION":
|
858 |
print(f"\n💬 Handling as CONVERSATION")
|
859 |
response_text = self.handle_conversation(user_message, detected_language, sentiment_result)
|
860 |
|