abdibrahem commited on
Commit
af650fa
·
1 Parent(s): a77efdb
Files changed (1) hide show
  1. main.py +163 -163
main.py CHANGED
@@ -241,172 +241,172 @@ class HealthcareChatbot:
241
 
242
 
243
  # API routing prompt (reuse existing router_prompt_template)
244
- self.router_prompt_template = PromptTemplate(
245
- template="""
246
- You are an API routing system that operates with strict adherence to documented specifications. Your sole purpose is to precisely match user requests to API endpoints based on the provided documentation.
247
-
248
- === ABSOLUTE REQUIREMENTS ===
249
- 1. ENDPOINT SELECTION MUST BE GROUNDED IN DOCUMENTATION
250
- 2. ALL REQUIRED PARAMETERS MUST BE ACCOUNTED FOR
251
- 3. RESPONSE STRUCTURE MUST REMAIN UNCHANGED
252
-
253
- === INPUT DATA ===
254
- Current Context:
255
- - DateTime: {current_datetime}
256
- - Timezone: {timezone}
257
- - User Locale: {user_locale}
258
-
259
- API Documentation:
260
- {endpoints_documentation}
261
-
262
- User Request:
263
- - Query: {user_query}
264
- - Language: {detected_language}
265
- - Keywords: {extracted_keywords}
266
- - Sentiment: {sentiment_analysis}
267
-
268
- === PROCESSING INSTRUCTIONS ===
269
- 1. Analyze the user request to determine:
270
- - Core intent (what they want to accomplish)
271
- - Target entity/resource
272
- - Required operation type (CRUD)
273
-
274
- 2. Examine the API documentation to:
275
- - Identify all endpoints related to the target entity
276
- - Verify the endpoint's purpose matches the user intent
277
- - Confirm the HTTP method aligns with the operation type
278
-
279
- 3. For the selected endpoint:
280
- - Extract ALL documented parameters
281
- - Determine parameter sources (query, history, or defaults)
282
- - Validate parameter formats against specifications
283
- - Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
284
-
285
- 4. Validate your selection:
286
- - The endpoint must directly satisfy the user's primary need
287
- - All required parameters must be present or explicitly listed as missing
288
- - No undocumented parameters may be included
289
-
290
- === OUTPUT FORMAT ===
291
- {
292
- "reasoning": {
293
- "user_intent": "[concise description]",
294
- "endpoint_selection": "[exact documentation reference]",
295
- "parameter_analysis": "[source and validation for each parameter]"
296
- },
297
- "endpoint": "/documented/endpoint/path",
298
- "method": "HTTP_METHOD",
299
- "params": {
300
- "param1": "value" // only if properly sourced
301
- },
302
- "missing_required": [
303
- "param1" // if required but unavailable
304
- ],
305
- "confidence": 0.95 // 1.0 when all parameters are validated
306
- }
307
-
308
- === STRICT ENFORCEMENT ===
309
- - Empty params objects are prohibited when documentation requires parameters
310
- - All missing required parameters must be explicitly listed
311
- - Confidence scores must reflect parameter completeness
312
- - Date/time formatting must be exact
313
- - Never assume undocumented functionality
314
-
315
- === ERROR CONDITIONS ===
316
- 1. If no endpoint matches:
317
- - Set "endpoint" to null
318
- - Explain in "reasoning"
319
-
320
- 2. If critical parameters are missing:
321
- - List ALL in "missing_required"
322
- - Reduce confidence score accordingly
323
-
324
- Your response will be validated against these requirements before being accepted.
325
- """,
326
- input_variables=[
327
- "endpoints_documentation", "user_query", "detected_language",
328
- "extracted_keywords", "sentiment_analysis", "conversation_history",
329
- "current_datetime", "timezone", "user_locale"
330
- ]
331
- )
332
  # self.router_prompt_template = PromptTemplate(
333
  # template="""
334
- # You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
335
-
336
- # === ENDPOINT DOCUMENTATION ===
337
- # {endpoints_documentation}
338
-
339
- # === USER REQUEST ANALYSIS ===
340
- # User Query: {user_query}
341
- # Language: {detected_language}
342
- # Keywords: {extracted_keywords}
343
- # Sentiment: {sentiment_analysis}
344
-
345
- # === ROUTING PROCESS ===
346
- # Follow these steps in order:
347
-
348
- # STEP 1: INTENT ANALYSIS
349
- # - What is the user trying to accomplish?
350
- # - What type of operation are they requesting? (create, read, update, delete, search, etc.)
351
- # - What entity/resource are they working with?
352
-
353
- # STEP 2: ENDPOINT MATCHING
354
- # - Review each endpoint in the documentation
355
- # - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
356
- # - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
357
- # - Verify the endpoint can handle the user's specific request
358
-
359
- # STEP 3: PARAMETER EXTRACTION
360
- # - Identify ALL required parameters from the endpoint documentation
361
- # - Extract parameter values from the user query
362
- # - Convert data types as needed (dates to ISO 8601, numbers to integers, etc.)
363
- # - Set appropriate defaults for optional parameters if beneficial
364
-
365
- # STEP 4: VALIDATION
366
- # - Ensure ALL required parameters are provided or identified as missing
367
- # - Verify parameter formats match documentation requirements
368
- # - Check that the selected endpoint actually solves the user's problem
369
-
370
- # === RESPONSE FORMAT ===
371
- # Provide your analysis and decision in this exact JSON structure:
372
-
373
- # {{
374
- # "reasoning": {{
375
- # "user_intent": "Brief description of what the user wants to accomplish",
376
- # "selected_endpoint": "Why this endpoint was chosen over others",
377
- # "parameter_mapping": "How user query maps to endpoint parameters"
378
- # }},
379
- # "endpoint": "/exact_endpoint_path_from_documentation",
380
- # "method": "HTTP_METHOD",
381
- # "params": {{
382
- # "required_param_1": "extracted_or_converted_value",
383
- # "required_param_2": "extracted_or_converted_value",
384
- # "optional_param": "value_if_applicable"
385
- # }},
386
- # "missing_required": ["list", "of", "missing", "required", "parameters"],
387
- # "confidence": 0.95
388
- # }}
389
-
390
- # === CRITICAL RULES ===
391
- # 1. ONLY select endpoints that exist in the provided documentation
392
- # 2. NEVER fabricate or assume endpoint parameters not in documentation
393
- # 3. ALL required parameters MUST be included or listed as missing
394
- # 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
395
- # 5. If patient_id is required and not provided, add it to missing_required
396
- # 6. Match endpoints by PURPOSE, not just keywords in the path
397
- # 7. If multiple endpoints could work, choose the most specific one
398
- # 8. If no endpoint matches, set endpoint to null and explain in reasoning
399
-
400
- # === EXAMPLES OF GOOD MATCHING ===
401
- # - User wants "patient records" → Use patient retrieval endpoint, not general search
402
- # - User wants to "schedule appointment" → Use appointment creation endpoint
403
- # - User asks "what appointments today" → Use appointment listing with date filter
404
- # - User wants to "update medication" → Use medication update endpoint with patient_id
405
-
406
- # Think step by step and be precise with your endpoint selection and parameter extraction.:""",
407
- # input_variables=["endpoints_documentation", "user_query", "detected_language",
408
- # "extracted_keywords", "sentiment_analysis", "conversation_history"]
 
 
 
 
 
 
 
 
 
 
409
  # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
  # Conversational response prompt
412
  self.conversation_template = PromptTemplate(
 
241
 
242
 
243
  # API routing prompt (reuse existing router_prompt_template)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  # self.router_prompt_template = PromptTemplate(
245
  # template="""
246
+ # You are an API routing system that operates with strict adherence to documented specifications. Your sole purpose is to precisely match user requests to API endpoints based on the provided documentation.
247
+
248
+ # === ABSOLUTE REQUIREMENTS ===
249
+ # 1. ENDPOINT SELECTION MUST BE GROUNDED IN DOCUMENTATION
250
+ # 2. ALL REQUIRED PARAMETERS MUST BE ACCOUNTED FOR
251
+ # 3. RESPONSE STRUCTURE MUST REMAIN UNCHANGED
252
+
253
+ # === INPUT DATA ===
254
+ # Current Context:
255
+ # - DateTime: {current_datetime}
256
+ # - Timezone: {timezone}
257
+ # - User Locale: {user_locale}
258
+
259
+ # API Documentation:
260
+ # {endpoints_documentation}
261
+
262
+ # User Request:
263
+ # - Query: {user_query}
264
+ # - Language: {detected_language}
265
+ # - Keywords: {extracted_keywords}
266
+ # - Sentiment: {sentiment_analysis}
267
+
268
+ # === PROCESSING INSTRUCTIONS ===
269
+ # 1. Analyze the user request to determine:
270
+ # - Core intent (what they want to accomplish)
271
+ # - Target entity/resource
272
+ # - Required operation type (CRUD)
273
+
274
+ # 2. Examine the API documentation to:
275
+ # - Identify all endpoints related to the target entity
276
+ # - Verify the endpoint's purpose matches the user intent
277
+ # - Confirm the HTTP method aligns with the operation type
278
+
279
+ # 3. For the selected endpoint:
280
+ # - Extract ALL documented parameters
281
+ # - Determine parameter sources (query, history, or defaults)
282
+ # - Validate parameter formats against specifications
283
+ # - Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
284
+
285
+ # 4. Validate your selection:
286
+ # - The endpoint must directly satisfy the user's primary need
287
+ # - All required parameters must be present or explicitly listed as missing
288
+ # - No undocumented parameters may be included
289
+
290
+ # === OUTPUT FORMAT ===
291
+ # {
292
+ # "reasoning": {
293
+ # "user_intent": "[concise description]",
294
+ # "endpoint_selection": "[exact documentation reference]",
295
+ # "parameter_analysis": "[source and validation for each parameter]"
296
+ # },
297
+ # "endpoint": "/documented/endpoint/path",
298
+ # "method": "HTTP_METHOD",
299
+ # "params": {
300
+ # "param1": "value" // only if properly sourced
301
+ # },
302
+ # "missing_required": [
303
+ # "param1" // if required but unavailable
304
+ # ],
305
+ # "confidence": 0.95 // 1.0 when all parameters are validated
306
+ # }
307
+
308
+ # === STRICT ENFORCEMENT ===
309
+ # - Empty params objects are prohibited when documentation requires parameters
310
+ # - All missing required parameters must be explicitly listed
311
+ # - Confidence scores must reflect parameter completeness
312
+ # - Date/time formatting must be exact
313
+ # - Never assume undocumented functionality
314
+
315
+ # === ERROR CONDITIONS ===
316
+ # 1. If no endpoint matches:
317
+ # - Set "endpoint" to null
318
+ # - Explain in "reasoning"
319
+
320
+ # 2. If critical parameters are missing:
321
+ # - List ALL in "missing_required"
322
+ # - Reduce confidence score accordingly
323
+
324
+ # Your response will be validated against these requirements before being accepted.
325
+ # """,
326
+ # input_variables=[
327
+ # "endpoints_documentation", "user_query", "detected_language",
328
+ # "extracted_keywords", "sentiment_analysis", "conversation_history",
329
+ # "current_datetime", "timezone", "user_locale"
330
+ # ]
331
  # )
332
+ self.router_prompt_template = PromptTemplate(
333
+ template="""
334
+ You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
335
+
336
+ === ENDPOINT DOCUMENTATION ===
337
+ {endpoints_documentation}
338
+
339
+ === USER REQUEST ANALYSIS ===
340
+ User Query: {user_query}
341
+ Language: {detected_language}
342
+ Keywords: {extracted_keywords}
343
+ Sentiment: {sentiment_analysis}
344
+
345
+ === ROUTING PROCESS ===
346
+ Follow these steps in order:
347
+
348
+ STEP 1: INTENT ANALYSIS
349
+ - What is the user trying to accomplish?
350
+ - What type of operation are they requesting? (create, read, update, delete, search, etc.)
351
+ - What entity/resource are they working with?
352
+
353
+ STEP 2: ENDPOINT MATCHING
354
+ - Review each endpoint in the documentation
355
+ - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
356
+ - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
357
+ - Verify the endpoint can handle the user's specific request
358
+
359
+ STEP 3: PARAMETER EXTRACTION
360
+ - Identify ALL required parameters from the endpoint documentation
361
+ - Extract parameter values from the user query
362
+ - Convert data types as needed (dates to ISO 8601, numbers to integers, etc.)
363
+ - Set appropriate defaults for optional parameters if beneficial
364
+
365
+ STEP 4: VALIDATION
366
+ - Ensure ALL required parameters are provided or identified as missing
367
+ - Verify parameter formats match documentation requirements
368
+ - Check that the selected endpoint actually solves the user's problem
369
+
370
+ === RESPONSE FORMAT ===
371
+ Provide your analysis and decision in this exact JSON structure:
372
+
373
+ {{
374
+ "reasoning": {{
375
+ "user_intent": "Brief description of what the user wants to accomplish",
376
+ "selected_endpoint": "Why this endpoint was chosen over others",
377
+ "parameter_mapping": "How user query maps to endpoint parameters"
378
+ }},
379
+ "endpoint": "/exact_endpoint_path_from_documentation",
380
+ "method": "HTTP_METHOD",
381
+ "params": {{
382
+ "required_param_1": "extracted_or_converted_value",
383
+ "required_param_2": "extracted_or_converted_value",
384
+ "optional_param": "value_if_applicable"
385
+ }},
386
+ "missing_required": ["list", "of", "missing", "required", "parameters"],
387
+ "confidence": 0.95
388
+ }}
389
+
390
+ === CRITICAL RULES ===
391
+ 1. ONLY select endpoints that exist in the provided documentation
392
+ 2. NEVER fabricate or assume endpoint parameters not in documentation
393
+ 3. ALL required parameters MUST be included or listed as missing
394
+ 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
395
+ 5. If patient_id is required and not provided, add it to missing_required
396
+ 6. Match endpoints by PURPOSE, not just keywords in the path
397
+ 7. If multiple endpoints could work, choose the most specific one
398
+ 8. If no endpoint matches, set endpoint to null and explain in reasoning
399
+
400
+ === EXAMPLES OF GOOD MATCHING ===
401
+ - User wants "patient records" → Use patient retrieval endpoint, not general search
402
+ - User wants to "schedule appointment" → Use appointment creation endpoint
403
+ - User asks "what appointments today" → Use appointment listing with date filter
404
+ - User wants to "update medication" → Use medication update endpoint with patient_id
405
+
406
+ Think step by step and be precise with your endpoint selection and parameter extraction.:""",
407
+ input_variables=["endpoints_documentation", "user_query", "detected_language",
408
+ "extracted_keywords", "sentiment_analysis", "conversation_history"]
409
+ )
410
 
411
  # Conversational response prompt
412
  self.conversation_template = PromptTemplate(