Spaces:
Sleeping
Sleeping
Commit
·
654739c
1
Parent(s):
a7b7d98
prompt
Browse files
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 |
-
##
|
162 |
-
You
|
163 |
-
|
164 |
-
|
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
|
174 |
{endpoints_documentation}
|
175 |
|
176 |
-
##
|
|
|
|
|
|
|
|
|
177 |
|
178 |
-
|
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 |
-
|
184 |
-
|
185 |
-
-
|
186 |
-
-
|
|
|
187 |
- Thank you/goodbye messages
|
188 |
-
- Non-
|
189 |
-
|
190 |
-
|
191 |
-
- Requests
|
192 |
-
- Appointment
|
193 |
-
- Medical record
|
194 |
-
-
|
195 |
-
-
|
196 |
-
|
197 |
-
### Step 2: Endpoint Selection (
|
198 |
-
1.
|
199 |
-
2.
|
200 |
-
|
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.
|
208 |
-
2.
|
209 |
-
3.
|
210 |
-
4.
|
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": "
|
220 |
-
"endpoint": "
|
221 |
-
"method": "
|
222 |
"params": {{
|
223 |
-
"param1": "value1",
|
224 |
"param2": "value2"
|
225 |
}},
|
226 |
-
"missing_required": ["param1"
|
227 |
}}
|
228 |
|
229 |
-
## Examples
|
230 |
|
231 |
-
Example 1:
|
232 |
-
|
233 |
{{
|
234 |
"intent": "CONVERSATION",
|
235 |
-
"confidence": 0.
|
236 |
-
"reasoning": "
|
237 |
"endpoint": null,
|
238 |
"method": null,
|
239 |
"params": {{}},
|
240 |
"missing_required": []
|
241 |
}}
|
242 |
|
243 |
-
Example 2:
|
244 |
-
|
245 |
{{
|
246 |
"intent": "API_ACTION",
|
247 |
-
"confidence": 0.
|
248 |
-
"reasoning": "
|
249 |
"endpoint": "/appointments",
|
250 |
"method": "POST",
|
251 |
"params": {{
|
252 |
-
"
|
253 |
-
"
|
254 |
}},
|
255 |
-
"missing_required": [
|
256 |
}}
|
257 |
|
258 |
-
##
|
259 |
-
1.
|
260 |
-
2.
|
261 |
-
3.
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
)
|