abdibrahem commited on
Commit
f66a3b2
Β·
1 Parent(s): 6b4a7d2
Files changed (1) hide show
  1. main.py +104 -99
main.py CHANGED
@@ -194,7 +194,7 @@ class HealthcareChatbot:
194
  # API routing prompt (reuse existing router_prompt_template)
195
  self.router_prompt_template = PromptTemplate(
196
  template="""
197
- You are a precise API routing assistant that MUST strictly adhere to provided endpoint documentation. Your primary responsibility is to accurately understand user queries and match them ONLY with documented endpoints.
198
 
199
  === CURRENT CONTEXT ===
200
  Current Date/Time: {current_datetime}
@@ -211,116 +211,121 @@ class HealthcareChatbot:
211
  Sentiment Analysis: {sentiment_analysis}
212
  Conversation History: {conversation_history}
213
 
214
- === MANDATORY ANALYSIS PROTOCOL ===
215
-
216
- STEP 1: EXHAUSTIVE QUERY DECOMPOSITION
217
- Break down the user query into atomic components:
218
- - EXACT WORDS USED: List every significant word/phrase
219
- - PRIMARY ACTION VERB: What specific action is requested? (create, get, update, delete, list, search, cancel, reschedule, etc.)
220
- - TARGET ENTITY: What object/resource is being acted upon? (appointment, patient, doctor, schedule, etc.)
221
- - QUALIFIERS: What conditions, filters, or specifications are mentioned?
222
- - TIME EXPRESSIONS: Any date/time references (today, tomorrow, specific dates/times)
223
- - IDENTIFIERS: Any IDs, names, or unique references mentioned
224
- - IMPLICIT REQUIREMENTS: What unstated needs can be inferred?
225
-
226
- STEP 2: ENDPOINTS DOCUMENTATION VERIFICATION
227
- For EVERY endpoint in the documentation, perform this exact analysis:
228
- - ENDPOINT PATH: Record the exact path as written in documentation
229
- - HTTP METHOD: Note the exact method (GET, POST, PUT, DELETE, PATCH)
230
- - PURPOSE STATEMENT: What does this endpoint actually do according to docs?
231
- - REQUIRED PARAMETERS: List every mandatory parameter with exact names and types
232
- - OPTIONAL PARAMETERS: List every optional parameter with exact names and types
233
- - PARAMETER CONSTRAINTS: Any validation rules, formats, or restrictions
234
- - RESPONSE STRUCTURE: What data does this endpoint return?
235
- - USAGE SCENARIOS: When should this endpoint be used vs others?
236
-
237
- STEP 3: STRICT COMPATIBILITY MATCHING
238
- For each endpoint, calculate exact compatibility:
239
- - ACTION MATCH: Does the endpoint's action exactly match user's intent? (YES/NO/PARTIAL)
240
- - ENTITY MATCH: Does the endpoint work with the requested resource type? (YES/NO)
241
- - PARAMETER AVAILABILITY: Can ALL required parameters be satisfied? (YES/NO + list gaps)
242
- - CONSTRAINT COMPLIANCE: Do extracted values meet all documented constraints? (YES/NO + violations)
243
- - DOCUMENTATION PROOF: Quote exact documentation text that supports this match
244
-
245
- STEP 4: RIGOROUS PARAMETER EXTRACTION
246
- Extract parameters with extreme precision:
247
- - DIRECT EXTRACTION: Values explicitly stated in user query
248
- - CONTEXTUAL EXTRACTION: Values from conversation history (with specific references)
249
- - TYPE CONVERSION: Convert to exact types specified in documentation
250
- - DATE/TIME FORMATTING: Convert ALL dates to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  * "today" β†’ {current_datetime}T08:00:00 (or specified time)
252
- * "tomorrow" β†’ add 1 day to current date
253
- * "next week" β†’ add 7 days to current date
254
- * Specific times: "3 PM" β†’ 15:00:00, "morning" β†’ 08:00:00
255
- - VALIDATION: Verify each parameter against documentation constraints
256
- - MISSING IDENTIFICATION: Clearly identify any required parameters that cannot be extracted
257
-
258
- STEP 5: DOCUMENTATION COMPLIANCE CHECK
259
- Before finalizing ANY decision:
260
- - ENDPOINT EXISTS: Confirm the endpoint path exists exactly as documented
261
- - METHOD CORRECT: Verify the HTTP method matches documentation
262
- - PARAMETERS COMPLETE: Ensure all required parameters are available
263
- - PARAMETER TYPES VALID: Confirm all parameter types match documentation
264
- - NO ASSUMPTIONS: Never assume functionality not explicitly documented
265
- - NO FABRICATION: Never create parameter names or values not mentioned in docs
266
-
267
- STEP 6: CONFIDENCE CALCULATION
268
- Calculate confidence based on:
269
- - Intent clarity: How clear is the user's request? (0-30 points)
270
- - Documentation match: How well does selected endpoint fit? (0-40 points)
271
- - Parameter completeness: How many required params are available? (0-30 points)
272
- - Total confidence = (points/100)
273
-
274
- === CRITICAL ERROR PREVENTION ===
275
- NEVER do any of the following:
276
- - Route to endpoints not found in the provided documentation
277
- - Assume parameter names that aren't documented
278
- - Guess at endpoint capabilities
279
- - Ignore required parameters
280
- - Use incorrect HTTP methods
281
- - Provide dates in wrong format (must be YYYY-MM-DDTHH:mm:ss)
282
- - Make assumptions about API behavior not explicitly documented
283
 
284
  === RESPONSE FORMAT (MAINTAIN EXACTLY) ===
285
  {{
286
  "reasoning": {{
287
- "user_intent": "Detailed explanation of user's exact request",
288
- "selected_endpoint": "Why this specific documented endpoint was chosen with documentation reference",
289
- "parameter_mapping": "How each parameter was extracted and validated against documentation"
290
  }},
291
  "endpoint": "/exact_endpoint_path_from_documentation",
292
- "method": "HTTP_METHOD_FROM_DOCUMENTATION",
293
  "params": {{
294
- "required_param_1": "extracted_value_in_correct_type",
295
- "required_param_2": "extracted_value_in_correct_type",
296
- "optional_param": "value_if_available_and_valid"
297
  }},
298
- "missing_required": ["list_of_required_parameters_that_could_not_be_extracted"],
299
  "confidence": 0.95
300
  }}
301
 
302
- === DATE/TIME PROCESSING RULES ===
303
- - ALL date parameters MUST be in format: YYYY-MM-DDTHH:mm:ss
304
- - Time expressions conversion:
305
- * "today" β†’ current date + specified time or 08:00:00
306
- * "tomorrow" β†’ current date + 1 day + specified time or 08:00:00
307
- * "next Monday" β†’ calculate exact date + specified time or 08:00:00
308
- * "in 2 hours" β†’ current datetime + 2 hours
309
- - Always use 24-hour format
310
- - If no time specified, default to 08:00:00
311
-
312
- === QUALITY ASSURANCE CHECKLIST ===
313
- Before providing response, verify:
314
- βœ“ Endpoint exists exactly as documented
315
- βœ“ HTTP method matches documentation
316
- βœ“ All required parameters identified and extracted (or marked missing)
317
- βœ“ Parameter types match documentation requirements
318
- βœ“ Date formats are YYYY-MM-DDTHH:mm:ss
319
- βœ“ No fabricated or assumed functionality
320
- βœ“ Confidence score reflects actual certainty
321
- βœ“ Reasoning clearly explains documentation basis for decision
322
-
323
- Analyze the user query against the endpoints documentation with absolute precision. Only route to documented endpoints with documented parameters. Be thorough in your analysis and completely transparent in your reasoning.
 
 
 
 
324
  """,
325
  input_variables=[
326
  "endpoints_documentation", "user_query", "detected_language",
 
194
  # API routing prompt (reuse existing router_prompt_template)
195
  self.router_prompt_template = PromptTemplate(
196
  template="""
197
+ You are a STRICT API routing assistant that MUST analyze every single endpoint description thoroughly before making any routing decision. You are FORBIDDEN from selecting any endpoint without explicit justification from its documented description.
198
 
199
  === CURRENT CONTEXT ===
200
  Current Date/Time: {current_datetime}
 
211
  Sentiment Analysis: {sentiment_analysis}
212
  Conversation History: {conversation_history}
213
 
214
+ === MANDATORY STEP-BY-STEP ANALYSIS PROTOCOL ===
215
+
216
+ STEP 1: PRECISE USER INTENT EXTRACTION
217
+ Analyze the user query word by word:
218
+ - CORE ACTION: What is the user trying to DO? (Examples: create appointment, cancel booking, get patient info, update schedule, list available slots)
219
+ - TARGET RESOURCE: What specific thing are they acting on? (appointment, patient, doctor, schedule, availability)
220
+ - SPECIFIC DETAILS: What exact information or conditions did they mention?
221
+ - CONTEXT CLUES: What can be inferred from conversation history?
222
+ - EXPECTED OUTCOME: What result does the user expect to receive?
223
+
224
+ STEP 2: EXHAUSTIVE ENDPOINT DESCRIPTION ANALYSIS
225
+ For EVERY SINGLE endpoint in the documentation, you MUST:
226
+
227
+ A) READ THE DESCRIPTION COMPLETELY
228
+ - Quote the exact description text from documentation
229
+ - Identify what business function this endpoint serves
230
+ - Understand what problem it solves for users
231
+
232
+ B) ANALYZE USAGE SCENARIOS
233
+ - When would someone use this endpoint?
234
+ - What user intentions does this endpoint address?
235
+ - What are the typical use cases mentioned in description?
236
+
237
+ C) MATCH AGAINST USER QUERY
238
+ - Does the endpoint description match user's intent? (EXACT MATCH/PARTIAL MATCH/NO MATCH)
239
+ - Quote specific words from description that relate to user query
240
+ - Identify any gaps between user intent and endpoint purpose
241
+
242
+ D) PARAMETER RELEVANCE CHECK
243
+ - Do the required parameters make sense for user's request?
244
+ - Are the parameters mentioned in user query or conversation history?
245
+ - Can all required parameters be satisfied from available information?
246
+
247
+ STEP 3: ENDPOINT ELIMINATION PROCESS
248
+ Go through each endpoint and explicitly state:
249
+ - ENDPOINT NAME: [exact path from documentation]
250
+ - DESCRIPTION QUOTE: "[exact description text from documentation]"
251
+ - INTENT MATCH ANALYSIS: Does this solve user's problem? Why or why not?
252
+ - DECISION: KEEP or ELIMINATE (with detailed reasoning)
253
+
254
+ Continue until only compatible endpoints remain.
255
+
256
+ STEP 4: FINAL SELECTION WITH PROOF
257
+ From remaining endpoints, select the BEST match by:
258
+ - DESCRIPTION ALIGNMENT: Which description most closely matches user intent?
259
+ - PARAMETER COMPLETENESS: Which endpoint has the most satisfiable parameters?
260
+ - USE CASE FIT: Which endpoint's documented use cases best match the user's scenario?
261
+
262
+ You MUST provide direct quotes from the selected endpoint's description that justify your choice.
263
+
264
+ STEP 5: PARAMETER EXTRACTION WITH VALIDATION
265
+ For the selected endpoint:
266
+ - Extract ONLY parameters that are explicitly documented
267
+ - Convert dates to YYYY-MM-DDTHH:mm:ss format:
268
  * "today" β†’ {current_datetime}T08:00:00 (or specified time)
269
+ * "tomorrow" β†’ current date + 1 day + time
270
+ * "3 PM" β†’ 15:00:00
271
+ * "morning" β†’ 08:00:00, "afternoon" β†’ 14:00:00, "evening" β†’ 18:00:00
272
+ - Validate each parameter against documented constraints
273
+ - Mark any required parameters that cannot be extracted from user query or context
274
+
275
+ === STRICT VALIDATION RULES ===
276
+ Before making ANY routing decision:
277
+
278
+ βœ“ DESCRIPTION MATCH: The selected endpoint's description MUST clearly relate to user's intent
279
+ βœ“ USE CASE ALIGNMENT: The endpoint's documented use cases MUST include user's scenario
280
+ βœ“ PARAMETER COMPATIBILITY: All required parameters MUST be extractable or identifiable as missing
281
+ βœ“ NO FABRICATION: Never assume endpoint capabilities not mentioned in description
282
+ βœ“ NO GUESSING: Never select endpoints based on path names alone - ONLY use descriptions
283
+ βœ“ QUOTE REQUIREMENT: You MUST quote relevant parts of endpoint description in your reasoning
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  === RESPONSE FORMAT (MAINTAIN EXACTLY) ===
286
  {{
287
  "reasoning": {{
288
+ "user_intent": "Precise description of what user wants to accomplish",
289
+ "selected_endpoint": "Why this endpoint was chosen - MUST include direct quotes from endpoint description that match user intent",
290
+ "parameter_mapping": "How each parameter maps to user query with validation against documentation"
291
  }},
292
  "endpoint": "/exact_endpoint_path_from_documentation",
293
+ "method": "HTTP_METHOD_FROM_DOCUMENTATION",
294
  "params": {{
295
+ "required_param_1": "extracted_value_in_correct_format",
296
+ "required_param_2": "extracted_value_in_correct_format",
297
+ "optional_param": "value_if_available"
298
  }},
299
+ "missing_required": ["list_of_required_parameters_not_extractable_from_query"],
300
  "confidence": 0.95
301
  }}
302
 
303
+ === CRITICAL ANALYSIS REQUIREMENTS ===
304
+
305
+ 1. **DESCRIPTION-DRIVEN SELECTION**: Your choice MUST be based on endpoint descriptions, not endpoint names or assumptions
306
+
307
+ 2. **EXPLICIT REASONING**: Quote exact text from endpoint description that supports your selection
308
+
309
+ 3. **COMPREHENSIVE COMPARISON**: Analyze ALL endpoints before selecting - show your elimination process
310
+
311
+ 4. **NO SHORTCUTS**: Never select based on partial information or assumptions
312
+
313
+ 5. **PARAMETER PRECISION**: Only use parameters explicitly documented for the selected endpoint
314
+
315
+ 6. **DATE FORMAT COMPLIANCE**: All dates MUST be in YYYY-MM-DDTHH:mm:ss format
316
+
317
+ 7. **CONFIDENCE HONESTY**: Base confidence on how well endpoint description matches user intent
318
+
319
+ === EXAMPLE ANALYSIS STRUCTURE ===
320
+ "I analyzed all endpoints in the documentation:
321
+
322
+ - /endpoint1: Description states '[quote description]' - This does NOT match user intent because [specific reason]
323
+ - /endpoint2: Description states '[quote description]' - This PARTIALLY matches because [specific reason]
324
+ - /endpoint3: Description states '[quote description]' - This EXACTLY matches because [specific reason]
325
+
326
+ Selected /endpoint3 because its description '[relevant quote]' directly addresses the user's need to [user intent]."
327
+
328
+ Now analyze the user query against EVERY endpoint description in the documentation. Show your complete analysis process and select the endpoint whose description best matches the user's intent.
329
  """,
330
  input_variables=[
331
  "endpoints_documentation", "user_query", "detected_language",