abdibrahem commited on
Commit
a7b7d98
ยท
1 Parent(s): 76d036e

Update promptclear

Browse files
Files changed (1) hide show
  1. main.py +85 -93
main.py CHANGED
@@ -155,126 +155,118 @@ class HealthcareChatbot:
155
 
156
  # UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
157
  self.router_prompt_template = PromptTemplate(
158
- template="""
159
- You are an intelligent healthcare chatbot router. Your job is to:
160
- 1. Understand what the user wants (intent classification)
161
- 2. If they need API action, determine the exact endpoint and parameters
162
-
163
- === CONTEXT ===
164
- User Query: {user_query}
165
- Language: {detected_language}
166
- Keywords: {extracted_keywords}
167
- Sentiment: {sentiment_analysis}
168
- Conversation History: {conversation_history}
169
-
170
- === API ENDPOINTS DOCUMENTATION ===
 
 
 
171
  {endpoints_documentation}
172
 
173
- === DECISION PROCESS ===
174
-
175
- STEP 1: INTENT ANALYSIS
176
- - What is the user trying to accomplish?
177
- - Is this a casual conversation/greeting/general question? โ†’ CONVERSATION
178
- - Is this a specific request that requires backend data/action? โ†’ API_ACTION
179
-
180
- Common CONVERSATION intents:
181
- - Greetings, thank you, goodbye
182
- - General health questions (not personal data requests)
183
- - Casual chat, complaints, compliments
184
- - Questions about the bot's capabilities
185
-
186
- Common API_ACTION intents:
187
- - "Book an appointment", "Schedule appointment"
188
- - "Show my appointments", "What are my appointments"
189
- - "Cancel my appointment", "Reschedule appointment"
190
- - "Show my medical records", "What's in my file"
191
- - "Update my information"
192
- - Any request for personal healthcare data or actions
193
-
194
- STEP 2: IF API_ACTION - ENDPOINT SELECTION
195
- - Review each endpoint in the documentation
196
- - Match user intent to endpoint PURPOSE/DESCRIPTION
197
- - Consider HTTP method (GET for retrieval, POST for creation)
198
- - Extract and validate parameters
199
-
200
- STEP 3: PARAMETER EXTRACTION (API_ACTION only)
201
- - Identify ALL required parameters from endpoint documentation
202
- - Extract values from user query
203
- - Convert dates to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
204
- - List any missing required parameters
205
- - Add patient_id to params if endpoint requires it
206
-
207
- === RESPONSE FORMAT ===
208
- You MUST respond with valid JSON in this exact structure:
 
 
 
209
 
210
  {{
211
- "intent": "CONVERSATION" or "API_ACTION",
212
- "confidence": 0.95,
213
- "reasoning": "Brief explanation of why this intent was chosen and what the user wants",
214
- "endpoint": "/endpoint/path" or null,
215
- "method": "GET/POST/PUT/DELETE" or null,
216
  "params": {{
217
  "param1": "value1",
218
  "param2": "value2"
219
  }},
220
- "missing_required": ["list", "of", "missing", "required", "params"]
221
  }}
222
 
223
- === EXAMPLES ===
224
 
225
- User: "Hello, how are you?"
 
226
  {{
227
  "intent": "CONVERSATION",
228
- "confidence": 0.98,
229
- "reasoning": "User is greeting - casual conversation",
230
  "endpoint": null,
231
  "method": null,
232
  "params": {{}},
233
  "missing_required": []
234
  }}
235
 
236
- User: "I want to book an appointment for tomorrow"
 
237
  {{
238
  "intent": "API_ACTION",
239
- "confidence": 0.95,
240
- "reasoning": "User wants to schedule appointment - requires API call",
241
  "endpoint": "/appointments",
242
- "method": "POST",
243
  "params": {{
244
  "patient_id": "will_be_injected",
245
- "appointment_date": "2025-06-02T10:00:00"
246
- }},
247
- "missing_required": []
248
- }}
249
-
250
- User: "What are my appointments?"
251
- {{
252
- "intent": "API_ACTION",
253
- "confidence": 0.97,
254
- "reasoning": "User wants to view their appointments - requires data retrieval",
255
- "endpoint": "/appointments",
256
- "method": "GET",
257
- "params": {{
258
- "patient_id": "will_be_injected"
259
  }},
260
- "missing_required": []
261
  }}
262
 
263
- === CRITICAL RULES ===
264
- 1. ONLY use endpoints that exist in the provided documentation
265
- 2. Focus on user's PRIMARY intent - what do they actually want?
266
- 3. When in doubt between CONVERSATION and API_ACTION, lean toward CONVERSATION
267
- 4. Always include confidence score based on clarity of intent
268
- 5. For API_ACTION, endpoint and method must be specified
269
- 6. Convert relative dates (tomorrow, next week) to specific ISO dates
270
- 7. If multiple endpoints could work, choose the most specific one
271
- 8. Missing required parameters should be listed even if you can't extract them
272
-
273
- Analyze the user query and respond with the appropriate JSON:""",
274
- input_variables=["user_query", "detected_language", "extracted_keywords",
275
- "sentiment_analysis", "conversation_history", "endpoints_documentation"]
276
- )
277
-
278
  # CONVERSATION CHAIN - Handles conversational responses
279
  self.conversation_template = PromptTemplate(
280
  template="""
 
155
 
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
+ )
 
 
 
270
  # CONVERSATION CHAIN - Handles conversational responses
271
  self.conversation_template = PromptTemplate(
272
  template="""