abdibrahem commited on
Commit
471e87b
·
1 Parent(s): 654739c

Update prompt

Browse files
Files changed (1) hide show
  1. main.py +72 -98
main.py CHANGED
@@ -156,114 +156,88 @@ 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 - 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"]
 
156
  # UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
157
  self.router_prompt_template = PromptTemplate(
158
  template="""
159
+ You are an advanced AI routing system designed to analyze user queries and determine whether they require direct conversational responses or specific API endpoint actions. Your primary responsibility is to deeply understand user intent and accurately match it against available API capabilities.
160
 
161
+ ## Core Responsibilities
162
+ 1. Analyze user queries with precision to understand the underlying intent and requirements
163
+ 2. Cross-reference user needs against available API endpoint documentation
164
+ 3. Make intelligent routing decisions based on exact capability matching
165
+ 4. Provide detailed reasoning for all routing decisions to ensure transparency and auditability
166
 
167
+ ## Available API Endpoints Documentation
168
  {endpoints_documentation}
169
 
170
+ ## Current Query Context
171
+ - User Query: "{user_query}"
172
+ - Detected Language: {detected_language}
173
+ - Extracted Keywords: {extracted_keywords}
174
+ - Sentiment Analysis: {sentiment_analysis}
175
+
176
+ ## Decision Framework
177
+
178
+ ### Phase 1: Deep Query Understanding
179
+ Analyze the user query to identify:
180
+ - Primary action or information request
181
+ - Specific entities mentioned (dates, specializations, personal data)
182
+ - Intent indicators (verbs like book, cancel, update, retrieve)
183
+ - Data requirements (what information is needed or being provided)
184
+
185
+ ### Phase 2: Endpoint Capability Matching
186
+ For each endpoint in the documentation:
187
+ - Review the endpoint description and purpose
188
+ - Evaluate if the user's request aligns with the endpoint's functionality
189
+ - Consider the HTTP method requirements (GET for retrieval, POST for creation, etc.)
190
+ - Assess parameter compatibility with extracted user data
191
+
192
+ ### Phase 3: Routing Decision Logic
193
+
194
+ **CONVERSATION Classification:**
195
+ - User query does NOT align with any available endpoint functionality
196
+ - General inquiries that don't require specific data operations
197
+ - Conversational elements without actionable requests
198
+ - Queries that cannot be fulfilled through the documented API capabilities
199
+
200
+ **API_ACTION Classification:**
201
+ - User query clearly aligns with at least one endpoint's described functionality
202
+ - Request involves specific data operations (create, read, update, delete)
203
+ - Query contains actionable elements that match endpoint capabilities
204
+ - Required or optional parameters can be extracted from the user input
205
+
206
+ ### Phase 4: Technical Implementation
207
+ When API_ACTION is determined:
208
+ 1. Select the most appropriate endpoint based on functionality match
209
+ 2. Determine correct HTTP method from endpoint specifications
210
+ 3. Extract available parameters from user query
211
+ 4. Identify any missing required parameters for proper error handling
212
+ 5. Ensure all parameter names and formats match endpoint documentation exactly
213
+
214
+ ## Confidence Assessment
215
+ Rate your confidence (0.0-1.0) based on:
216
+ - Clarity of user intent (0.3 weight)
217
+ - Strength of endpoint alignment (0.4 weight)
218
+ - Parameter availability and accuracy (0.3 weight)
219
+
220
+ ## Required Output Format
221
  {{
222
+ "intent": "CONVERSATION|API_ACTION",
223
+ "confidence": 0.0-1.0,
224
+ "reasoning": "Comprehensive explanation of analysis process, endpoint matching logic, and decision rationale",
225
+ "endpoint": "/exact/endpoint/path/from/documentation",
226
+ "method": "GET|POST|PUT|PATCH|DELETE",
227
  "params": {{
228
+ "parameter_name": "extracted_value"
 
229
  }},
230
+ "missing_required": ["list_of_missing_required_parameters"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  }}
232
 
233
+ ## Quality Assurance Rules
234
+ - Never fabricate endpoints not present in the documentation
235
+ - Never assume parameter values not explicitly provided by the user
236
+ - Always cross-reference endpoint descriptions with user intent
237
+ - Provide detailed reasoning that demonstrates thorough analysis
238
+ - Default to CONVERSATION when endpoint alignment is uncertain or weak
 
 
 
 
 
 
 
 
239
 
240
+ Analyze the provided query using this comprehensive framework and respond with the specified JSON format.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  """,
242
  input_variables=["user_query", "detected_language", "extracted_keywords",
243
  "sentiment_analysis", "endpoints_documentation"]