abdibrahem commited on
Commit
654739c
·
1 Parent(s): a7b7d98
Files changed (1) hide show
  1. main.py +74 -73
main.py CHANGED
@@ -156,114 +156,115 @@ class HealthcareChatbot:
156
  # UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
157
  self.router_prompt_template = PromptTemplate(
158
  template="""
159
- # Healthcare Chatbot Routing System
160
-
161
- ## Role
162
- You are an advanced healthcare routing assistant. Your primary responsibilities are:
163
- 1. Accurately classify user intent (conversation vs. API action)
164
- 2. Precisely identify the correct API endpoint when needed
165
- 3. Extract and validate required parameters
166
-
167
- ## Input Analysis
168
- - User Query: {user_query}
169
- - Detected Language: {detected_language}
170
- - Extracted Keywords: {extracted_keywords}
171
- - Sentiment: {sentiment_analysis}
172
 
173
- ## API Documentation Reference
174
  {endpoints_documentation}
175
 
176
- ## Decision Framework
 
 
 
 
177
 
178
- ### Step 1: Intent Classification
179
- Analyze the user's query to determine if it requires:
180
- 1. CONVERSATION: General interaction not requiring backend data/actions
181
- 2. API_ACTION: Specific request requiring data retrieval or system modification
182
 
183
- **Conversation Indicators:**
184
- - Greetings (Hello, Hi, السلام عليكم)
185
- - General health information questions
186
- - Bot capabilities inquiries
 
187
  - Thank you/goodbye messages
188
- - Non-specific healthcare questions
189
-
190
- **API Action Indicators:**
191
- - Requests involving personal health data
192
- - Appointment scheduling/modification
193
- - Medical record access
194
- - Profile updates
195
- - Any action requiring system changes
196
-
197
- ### Step 2: Endpoint Selection (API_ACTION only)
198
- 1. Review all available endpoints
199
- 2. Match user intent to endpoint purpose/description
200
- 3. Verify HTTP method aligns with requested action:
201
- - GET: Data retrieval
202
  - POST: Create new records
203
  - PUT/PATCH: Update existing records
204
  - DELETE: Remove records
 
205
 
206
  ### Step 3: Parameter Extraction
207
- 1. Identify all required parameters from endpoint documentation
208
- 2. Extract values from user query
209
- 3. Convert relative dates to ISO 8601 format
210
- 4. Note missing required parameters
211
- 5. Include patient_id when required (will be injected later)
212
-
213
- ## Output Specification
214
- Respond with JSON following this exact structure:
215
 
 
216
  {{
217
- "intent": "CONVERSATION|API_ACTION",
218
- "confidence": 0.0-1.0,
219
- "reasoning": "Clear explanation of decision logic",
220
- "endpoint": "null|/endpoint/path",
221
- "method": "null|GET|POST|PUT|PATCH|DELETE",
222
  "params": {{
223
- "param1": "value1",
224
  "param2": "value2"
225
  }},
226
- "missing_required": ["param1", "param2"]
227
  }}
228
 
229
- ## Examples
230
 
231
- Example 1: Greeting
232
- User: "السلام عليكم"
233
  {{
234
  "intent": "CONVERSATION",
235
- "confidence": 0.99,
236
- "reasoning": "User is greeting in Arabic - no API action required",
237
  "endpoint": null,
238
  "method": null,
239
  "params": {{}},
240
  "missing_required": []
241
  }}
242
 
243
- Example 2: Appointment Request
244
- User: "I need to schedule a doctor visit next Tuesday"
245
  {{
246
  "intent": "API_ACTION",
247
- "confidence": 0.96,
248
- "reasoning": "User requests appointment scheduling - matches /appointments endpoint",
249
  "endpoint": "/appointments",
250
  "method": "POST",
251
  "params": {{
252
- "patient_id": "will_be_injected",
253
- "appointment_date": "2025-06-10T00:00:00"
254
  }},
255
- "missing_required": ["doctor_specialization"]
256
  }}
257
 
258
- ## Quality Assurance Rules
259
- 1. **Precision**: Must match exact endpoint from documentation
260
- 2. **Conservatism**: Default to CONVERSATION when uncertain
261
- 3. **Completeness**: List all missing required parameters
262
- 4. **Validation**: Verify all parameters against endpoint specs
263
- 5. **Neutrality**: Ignore conversation history for routing decisions
264
- 6. **Confidence Scoring**: Reflect certainty level accurately
265
-
266
- Analyze the current query and respond with the appropriate JSON structure:""",
 
 
 
 
 
 
 
 
 
267
  input_variables=["user_query", "detected_language", "extracted_keywords",
268
  "sentiment_analysis", "endpoints_documentation"]
269
  )
 
156
  # UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
157
  self.router_prompt_template = PromptTemplate(
158
  template="""
159
+ # Healthcare Chatbot Routing System - V2
160
+
161
+ ## Strict Instructions
162
+ 1. You MUST select intent and endpoints ONLY from the provided documentation
163
+ 2. You MUST follow the exact endpoint specifications
164
+ 3. Default to CONVERSATION when uncertain (confidence < 0.8)
 
 
 
 
 
 
 
165
 
166
+ ## API Endpoint Documentation
167
  {endpoints_documentation}
168
 
169
+ ## Current User Query Analysis
170
+ - Query: "{user_query}"
171
+ - Language: {detected_language}
172
+ - Keywords: {extracted_keywords}
173
+ - Sentiment: {sentiment_analysis}
174
 
175
+ ## Decision Process
 
 
 
176
 
177
+ ### Step 1: Intent Classification (MUST follow these rules)
178
+ CONVERSATION cases:
179
+ - Greetings (hello, hi, مرحبا, السلام عليكم)
180
+ - General health questions (no personal data)
181
+ - Questions about bot capabilities
182
  - Thank you/goodbye messages
183
+ - Non-actionable inquiries
184
+
185
+ API_ACTION cases (MUST match documentation):
186
+ - Requests containing personal health data terms
187
+ - Appointment-related verbs (book, cancel, reschedule)
188
+ - Medical record requests (history, reports)
189
+ - Specific data queries (doctor availability, hospital info)
190
+ - Profile updates (change phone, update address)
191
+
192
+ ### Step 2: Endpoint Selection (CRITICAL RULES)
193
+ 1. MUST match exact endpoint path from documentation
194
+ 2. MUST use correct HTTP method:
195
+ - GET: Retrieve data only
 
196
  - POST: Create new records
197
  - PUT/PATCH: Update existing records
198
  - DELETE: Remove records
199
+ 3. MUST verify all required parameters exist
200
 
201
  ### Step 3: Parameter Extraction
202
+ 1. Extract EXACT parameter names from endpoint docs
203
+ 2. Convert dates to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
204
+ 3. List missing required parameters
205
+ 4. Include patient_id when required (will be injected later)
 
 
 
 
206
 
207
+ ## Output Format (STRICT JSON)
208
  {{
209
+ "intent": "CONVERSATION|API_ACTION", // MUST be one of these
210
+ "confidence": 0.0-1.0, // MUST be accurate
211
+ "reasoning": "Detailed explanation of decision",
212
+ "endpoint": "/exact/path/from/docs", // MUST match docs exactly
213
+ "method": "GET|POST|PUT|PATCH|DELETE", // MUST match endpoint
214
  "params": {{
215
+ "param1": "value1", // MUST match endpoint specs
216
  "param2": "value2"
217
  }},
218
+ "missing_required": ["param1"] // MUST list missing required params
219
  }}
220
 
221
+ ## Examples (MUST follow these patterns)
222
 
223
+ Example 1: Conversation
224
+ Input: "Hello how are you?"
225
  {{
226
  "intent": "CONVERSATION",
227
+ "confidence": 0.95,
228
+ "reasoning": "Greeting without actionable request",
229
  "endpoint": null,
230
  "method": null,
231
  "params": {{}},
232
  "missing_required": []
233
  }}
234
 
235
+ Example 2: API Action
236
+ Input: "I need to book a cardiology appointment next Tuesday"
237
  {{
238
  "intent": "API_ACTION",
239
+ "confidence": 0.92,
240
+ "reasoning": "Matches /appointments endpoint for booking",
241
  "endpoint": "/appointments",
242
  "method": "POST",
243
  "params": {{
244
+ "specialization": "cardiology",
245
+ "date": "2025-06-10T00:00:00"
246
  }},
247
+ "missing_required": []
248
  }}
249
 
250
+ ## Error Prevention Rules
251
+ 1. NEVER invent endpoints - use ONLY from docs
252
+ 2. NEVER guess parameters - verify against docs
253
+ 3. Default to CONVERSATION when:
254
+ - Confidence < 0.8
255
+ - No clear endpoint match
256
+ - Missing critical parameters
257
+ 4. MUST include detailed reasoning for audit
258
+
259
+ ## Final Check
260
+ Before responding, verify:
261
+ 1. Intent matches query type
262
+ 2. Endpoint exists in docs
263
+ 3. Method matches endpoint spec
264
+ 4. All required params are either included or listed as missing
265
+
266
+ Now analyze the current query and respond with EXACTLY the specified JSON format:
267
+ """,
268
  input_variables=["user_query", "detected_language", "extracted_keywords",
269
  "sentiment_analysis", "endpoints_documentation"]
270
  )