abdibrahem commited on
Commit
c6fe741
·
1 Parent(s): ca22c23
Files changed (1) hide show
  1. main.py +29 -123
main.py CHANGED
@@ -266,20 +266,18 @@ class HealthcareChatbot:
266
  - What type of operation are they requesting? (create, read, update, delete, search, etc.)
267
  - What entity/resource are they working with?
268
 
269
- STEP 2: DATE/TIME PROCESSING (CRITICAL FOR ARABIC QUERIES)
270
- - Carefully identify temporal expressions in Arabic:
271
- * "اليوم" (today) = current date ({current_datetime})
272
- * "غدا" or "بكره" (tomorrow) = current date + 1 day
 
273
  * "أمس" (yesterday) = current date - 1 day
274
  * "الأسبوع القادم" (next week) = current date + 7 days
275
- * "بعد ساعة/ساعتين" (in 1 hour/2 hours) = current time + specified hours
276
- * "صباحًا" (morning) = AM time
277
- * "مساءً" (evening) = PM time (convert "10 مساءً" to 22:00)
278
- - For absolute dates in Arabic format (DD/MM/YYYY), convert to ISO format
279
- - ALWAYS verify the calculated date against {current_datetime} to ensure:
280
- - "tomorrow" is correctly calculated as current date + 1 day
281
- - Time conversions respect the 24-hour format (e.g., 10 مساءً = 22:00)
282
- - Final output MUST be in ISO 8601: YYYY-MM-DDTHH:MM:SS
283
 
284
  STEP 3: ENDPOINT MATCHING
285
  - Review each endpoint in the documentation
@@ -295,10 +293,11 @@ class HealthcareChatbot:
295
  - Numbers to integers
296
  - Set appropriate defaults for optional parameters if beneficial
297
 
 
 
298
  STEP 5: VALIDATION
299
  - Ensure ALL required parameters are provided or identified as missing
300
  - Verify parameter formats match documentation requirements
301
- - Double-check date calculations against {current_datetime}
302
  - Check that the selected endpoint actually solves the user's problem
303
 
304
  === RESPONSE FORMAT ===
@@ -308,8 +307,7 @@ class HealthcareChatbot:
308
  "reasoning": {{
309
  "user_intent": "Brief description of what the user wants to accomplish",
310
  "selected_endpoint": "Why this endpoint was chosen over others",
311
- "parameter_mapping": "How user query maps to endpoint parameters",
312
- "date_calculation": "Detailed explanation of how dates were calculated"
313
  }},
314
  "endpoint": "/exact_endpoint_path_from_documentation",
315
  "method": "HTTP_METHOD",
@@ -323,118 +321,26 @@ class HealthcareChatbot:
323
  }}
324
 
325
  === CRITICAL RULES ===
326
- 1. "غدا" or "بكره" MUST always equal current date + 1 day
327
- 2. Time expressions like "10 مساءً" MUST convert to 22:00
328
- 3. Verify all date calculations against {current_datetime}
329
- 4. Add "date_calculation" field to explain date conversions
330
- 5. All other rules from previous versions remain unchanged
331
-
332
- Think step by step and be precise with your endpoint selection and parameter extraction.""",
 
 
 
 
 
 
 
 
 
333
  input_variables=["endpoints_documentation", "user_query", "detected_language",
334
  "extracted_keywords", "sentiment_analysis", "conversation_history",
335
  "current_datetime", "timezone", "user_locale"]
336
  )
337
- ''' last one '''
338
- # self.router_prompt_template = PromptTemplate(
339
- # template="""
340
- # You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
341
-
342
- # === ENDPOINT DOCUMENTATION ===
343
- # {endpoints_documentation}
344
-
345
- # === USER REQUEST ANALYSIS ===
346
- # User Query: {user_query}
347
- # Language: {detected_language}
348
- # Keywords: {extracted_keywords}
349
- # Sentiment: {sentiment_analysis}
350
- # Current Context:
351
- # - DateTime: {current_datetime}
352
- # - Timezone: {timezone}
353
- # - User Locale: {user_locale}
354
-
355
- # === ROUTING PROCESS ===
356
- # Follow these steps in order:
357
-
358
- # STEP 1: INTENT ANALYSIS
359
- # - What is the user trying to accomplish?
360
- # - What type of operation are they requesting? (create, read, update, delete, search, etc.)
361
- # - What entity/resource are they working with?
362
-
363
- # STEP 2: DATE/TIME PROCESSING
364
- # - Identify any temporal expressions in the user query
365
- # - Convert relative dates/times using the current context:
366
- # * "اليوم" (today) = current date
367
- # * "غدا" (tomorrow) = current date + 1 day
368
- # * "أمس" (yesterday) = current date - 1 day
369
- # * "الأسبوع القادم" (next week) = current date + 7 days
370
- # * "بعد ساعتين" (in 2 hours) = current time + 2 hours
371
- # * "صباحًا" (morning/AM), "مساءً" (evening/PM)
372
- # - Handle different date formats and languages
373
- # - Account for timezone differences
374
- # - Convert to ISO 8601 format: YYYY-MM-DDTHH:MM:SS
375
-
376
- # STEP 3: ENDPOINT MATCHING
377
- # - Review each endpoint in the documentation
378
- # - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
379
- # - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
380
- # - Verify the endpoint can handle the user's specific request
381
-
382
- # STEP 4: PARAMETER EXTRACTION
383
- # - Identify ALL required parameters from the endpoint documentation
384
- # - Extract parameter values from the user query
385
- # - Convert data types as needed:
386
- # - Dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
387
- # - Numbers to integers
388
- # - Set appropriate defaults for optional parameters if beneficial
389
-
390
-
391
-
392
- # STEP 5: VALIDATION
393
- # - Ensure ALL required parameters are provided or identified as missing
394
- # - Verify parameter formats match documentation requirements
395
- # - Check that the selected endpoint actually solves the user's problem
396
-
397
- # === RESPONSE FORMAT ===
398
- # Provide your analysis and decision in this exact JSON structure:
399
-
400
- # {{
401
- # "reasoning": {{
402
- # "user_intent": "Brief description of what the user wants to accomplish",
403
- # "selected_endpoint": "Why this endpoint was chosen over others",
404
- # "parameter_mapping": "How user query maps to endpoint parameters"
405
- # }},
406
- # "endpoint": "/exact_endpoint_path_from_documentation",
407
- # "method": "HTTP_METHOD",
408
- # "params": {{
409
- # "required_param_1": "extracted_or_converted_value",
410
- # "required_param_2": "extracted_or_converted_value",
411
- # "optional_param": "value_if_applicable"
412
- # }},
413
- # "missing_required": ["list", "of", "missing", "required", "parameters"],
414
- # "confidence": 0.95
415
- # }}
416
-
417
- # === CRITICAL RULES ===
418
- # 1. ONLY select endpoints that exist in the provided documentation
419
- # 2. NEVER fabricate or assume endpoint parameters not in documentation
420
- # 3. ALL required parameters MUST be included or listed as missing
421
- # 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
422
- # 5. If patient_id is required and not provided, add it to missing_required
423
- # 6. Match endpoints by PURPOSE, not just keywords in the path
424
- # 7. If multiple endpoints could work, choose the most specific one
425
- # 8. If no endpoint matches, set endpoint to null and explain in reasoning
426
-
427
- # === EXAMPLES OF GOOD MATCHING ===
428
- # - User wants "patient records" → Use patient retrieval endpoint, not general search
429
- # - User wants to "schedule appointment" → Use appointment creation endpoint
430
- # - User asks "what appointments today" → Use appointment listing with date filter
431
- # - User wants to "update medication" → Use medication update endpoint with patient_id
432
-
433
- # Think step by step and be precise with your endpoint selection and parameter extraction.:""",
434
- # input_variables=["endpoints_documentation", "user_query", "detected_language",
435
- # "extracted_keywords", "sentiment_analysis", "conversation_history",
436
- # "current_datetime", "timezone", "user_locale"]
437
- # )
438
  # old one
439
  # self.router_prompt_template = PromptTemplate(
440
  # template="""
 
266
  - What type of operation are they requesting? (create, read, update, delete, search, etc.)
267
  - What entity/resource are they working with?
268
 
269
+ STEP 2: DATE/TIME PROCESSING
270
+ - Identify any temporal expressions in the user query
271
+ - Convert relative dates/times using the current context:
272
+ * "اليوم" (today) = current date
273
+ * "غدا" (tomorrow) = current date + 1 day
274
  * "أمس" (yesterday) = current date - 1 day
275
  * "الأسبوع القادم" (next week) = current date + 7 days
276
+ * "بعد ساعتين" (in 2 hours) = current time + 2 hours
277
+ * "صباحًا" (morning/AM), "مساءً" (evening/PM)
278
+ - Handle different date formats and languages
279
+ - Account for timezone differences
280
+ - Convert to ISO 8601 format: YYYY-MM-DDTHH:MM:SS
 
 
 
281
 
282
  STEP 3: ENDPOINT MATCHING
283
  - Review each endpoint in the documentation
 
293
  - Numbers to integers
294
  - Set appropriate defaults for optional parameters if beneficial
295
 
296
+
297
+
298
  STEP 5: VALIDATION
299
  - Ensure ALL required parameters are provided or identified as missing
300
  - Verify parameter formats match documentation requirements
 
301
  - Check that the selected endpoint actually solves the user's problem
302
 
303
  === RESPONSE FORMAT ===
 
307
  "reasoning": {{
308
  "user_intent": "Brief description of what the user wants to accomplish",
309
  "selected_endpoint": "Why this endpoint was chosen over others",
310
+ "parameter_mapping": "How user query maps to endpoint parameters"
 
311
  }},
312
  "endpoint": "/exact_endpoint_path_from_documentation",
313
  "method": "HTTP_METHOD",
 
321
  }}
322
 
323
  === CRITICAL RULES ===
324
+ 1. ONLY select endpoints that exist in the provided documentation
325
+ 2. NEVER fabricate or assume endpoint parameters not in documentation
326
+ 3. ALL required parameters MUST be included or listed as missing
327
+ 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
328
+ 5. If patient_id is required and not provided, add it to missing_required
329
+ 6. Match endpoints by PURPOSE, not just keywords in the path
330
+ 7. If multiple endpoints could work, choose the most specific one
331
+ 8. If no endpoint matches, set endpoint to null and explain in reasoning
332
+
333
+ === EXAMPLES OF GOOD MATCHING ===
334
+ - User wants "patient records" → Use patient retrieval endpoint, not general search
335
+ - User wants to "schedule appointment" → Use appointment creation endpoint
336
+ - User asks "what appointments today" → Use appointment listing with date filter
337
+ - User wants to "update medication" → Use medication update endpoint with patient_id
338
+
339
+ Think step by step and be precise with your endpoint selection and parameter extraction.:""",
340
  input_variables=["endpoints_documentation", "user_query", "detected_language",
341
  "extracted_keywords", "sentiment_analysis", "conversation_history",
342
  "current_datetime", "timezone", "user_locale"]
343
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  # old one
345
  # self.router_prompt_template = PromptTemplate(
346
  # template="""