Spaces:
Sleeping
Sleeping
Commit
Β·
c5454c2
1
Parent(s):
845e045
fix
Browse files
main.py
CHANGED
@@ -192,9 +192,10 @@ class HealthcareChatbot:
|
|
192 |
|
193 |
|
194 |
# API routing prompt (reuse existing router_prompt_template)
|
|
|
195 |
self.router_prompt_template = PromptTemplate(
|
196 |
template="""
|
197 |
-
You are a precise API routing assistant.
|
198 |
|
199 |
=== CURRENT CONTEXT ===
|
200 |
Current Date/Time: {current_datetime}
|
@@ -211,101 +212,96 @@ class HealthcareChatbot:
|
|
211 |
Sentiment: {sentiment_analysis}
|
212 |
Conversation History: {conversation_history}
|
213 |
|
214 |
-
===
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
- What
|
219 |
-
-
|
220 |
-
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
- Handle date/time expressions with precision:
|
232 |
-
*
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
*
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
- "3 PM" β 15:00:00
|
244 |
-
- "10:30 AM" β 10:30:00
|
245 |
-
* ARABIC TIME EXPRESSIONS:
|
246 |
-
- "Ψ§ΩΩΩΩ
" (today) β current date + time
|
247 |
-
- "ΨΊΨ―Ψ§" (tomorrow) β current date + 1 day + time
|
248 |
-
- "Ψ£Ω
Ψ³" (yesterday) β current date - 1 day + time
|
249 |
-
- "Ψ§ΩΨ΅Ψ¨Ψ§Ψ" (morning) β 08:00:00
|
250 |
-
- "Ψ¨ΨΉΨ― Ψ§ΩΨΈΩΨ±" (afternoon) β 14:00:00
|
251 |
-
- "Ψ§ΩΩ
Ψ³Ψ§Ψ‘" (evening) β 18:00:00
|
252 |
-
- Convert ALL dates to ISO 8601 format: YYYY-MM-DDTHH:mm:ss
|
253 |
-
- Convert data types as needed (numbers to integers, booleans, etc.)
|
254 |
-
- Set appropriate defaults for optional parameters if beneficial
|
255 |
-
|
256 |
-
STEP 4: VALIDATION
|
257 |
-
- Ensure ALL required parameters are provided or identified as missing
|
258 |
-
- Verify parameter formats match documentation requirements
|
259 |
-
- Check that the selected endpoint actually solves the user's problem
|
260 |
-
- Validate date formats are exactly YYYY-MM-DDTHH:mm:ss
|
261 |
|
262 |
=== RESPONSE FORMAT ===
|
263 |
-
|
|
|
|
|
|
|
264 |
|
|
|
265 |
{{
|
266 |
"reasoning": {{
|
267 |
-
"user_intent": "
|
268 |
-
"selected_endpoint": "Why this endpoint was chosen
|
269 |
-
"parameter_mapping": "How
|
270 |
}},
|
271 |
"endpoint": "/exact_endpoint_path_from_documentation",
|
272 |
"method": "HTTP_METHOD",
|
273 |
"params": {{
|
274 |
-
"
|
275 |
-
"
|
276 |
-
"optional_param": "value_if_applicable"
|
277 |
}},
|
278 |
-
"missing_required": ["
|
279 |
"confidence": 0.95
|
280 |
}}
|
281 |
|
282 |
=== CRITICAL RULES ===
|
283 |
-
1.
|
284 |
-
2.
|
285 |
-
3.
|
286 |
-
4.
|
287 |
-
5.
|
288 |
-
6.
|
289 |
-
7.
|
290 |
-
8.
|
291 |
-
9. Use conversation history to
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
-
|
296 |
-
- "
|
297 |
-
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
-
|
303 |
-
|
304 |
-
|
305 |
-
- User wants to "update medication" β Use medication update endpoint with patient_id
|
306 |
-
- User says "cancel my appointment tomorrow" β Use appointment cancellation endpoint with tomorrow's date
|
307 |
-
|
308 |
-
Think step by step and be precise with your endpoint selection and parameter extraction. Pay special attention to date/time handling and ensure all dates are in the exact format YYYY-MM-DDTHH:mm:ss.
|
309 |
""",
|
310 |
input_variables=[
|
311 |
"endpoints_documentation", "user_query", "detected_language",
|
|
|
192 |
|
193 |
|
194 |
# API routing prompt (reuse existing router_prompt_template)
|
195 |
+
|
196 |
self.router_prompt_template = PromptTemplate(
|
197 |
template="""
|
198 |
+
You are a precise API routing assistant. You MUST analyze EVERY SINGLE endpoint in the documentation before making any decision.
|
199 |
|
200 |
=== CURRENT CONTEXT ===
|
201 |
Current Date/Time: {current_datetime}
|
|
|
212 |
Sentiment: {sentiment_analysis}
|
213 |
Conversation History: {conversation_history}
|
214 |
|
215 |
+
=== MANDATORY ANALYSIS PROCESS ===
|
216 |
+
|
217 |
+
STEP 1: USER INTENT EXTRACTION
|
218 |
+
Analyze the user query to identify:
|
219 |
+
- PRIMARY ACTION: What exactly does the user want to do? (create, get, update, delete, list, search, cancel, etc.)
|
220 |
+
- TARGET ENTITY: What resource are they working with? (appointment, patient, doctor, schedule, etc.)
|
221 |
+
- SPECIFIC CONDITIONS: Any filters, dates, IDs, or constraints mentioned?
|
222 |
+
- CONTEXT FROM HISTORY: Any relevant information from previous conversation?
|
223 |
+
|
224 |
+
STEP 2: COMPREHENSIVE ENDPOINT ANALYSIS
|
225 |
+
For EACH AND EVERY endpoint in the documentation, you MUST perform this analysis:
|
226 |
+
|
227 |
+
ENDPOINT: [Write the exact endpoint path]
|
228 |
+
METHOD: [Write the HTTP method]
|
229 |
+
DESCRIPTION: [Quote the exact description/purpose from documentation]
|
230 |
+
INTENT MATCH: [Does this endpoint's purpose match the user's intent? YES/NO/PARTIAL]
|
231 |
+
REASONING: [Explain why this endpoint matches or doesn't match]
|
232 |
+
PARAMETERS REQUIRED: [List all required parameters]
|
233 |
+
PARAMETERS AVAILABLE: [Which required parameters can be extracted from user query/history?]
|
234 |
+
DECISION: [SUITABLE/NOT_SUITABLE]
|
235 |
+
|
236 |
+
Repeat this analysis for EVERY endpoint before proceeding.
|
237 |
+
|
238 |
+
STEP 3: ENDPOINT SELECTION
|
239 |
+
From your analysis above, select the endpoint where:
|
240 |
+
- The description/purpose EXACTLY matches the user's intent
|
241 |
+
- All or most required parameters are available
|
242 |
+
- The HTTP method makes sense for the operation
|
243 |
+
|
244 |
+
STEP 4: PARAMETER EXTRACTION WITH DATE HANDLING
|
245 |
+
Extract parameters with these rules:
|
246 |
- Handle date/time expressions with precision:
|
247 |
+
* "today" β current date + T08:00:00 (or specified time)
|
248 |
+
* "tomorrow" β current date + 1 day + T08:00:00 (or specified time)
|
249 |
+
* "yesterday" β current date - 1 day + T08:00:00 (or specified time)
|
250 |
+
* "next week" β current date + 7 days + T08:00:00 (or specified time)
|
251 |
+
* "in X hours" β current datetime + X hours
|
252 |
+
* "morning" β 08:00:00, "afternoon" β 14:00:00, "evening" β 18:00:00
|
253 |
+
* "3 PM" β 15:00:00, "10:30 AM" β 10:30:00
|
254 |
+
* Arabic: "Ψ§ΩΩΩΩ
" β today, "ΨΊΨ―Ψ§" β tomorrow, "Ψ§ΩΨ΅Ψ¨Ψ§Ψ" β 08:00:00, etc.
|
255 |
+
- Convert ALL dates to format: YYYY-MM-DDTHH:mm:ss
|
256 |
+
- Extract values from user query and conversation history
|
257 |
+
- Identify any missing required parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
|
259 |
=== RESPONSE FORMAT ===
|
260 |
+
You MUST show your complete endpoint analysis, then provide the JSON response:
|
261 |
+
|
262 |
+
ENDPOINT ANALYSIS:
|
263 |
+
[Show your analysis for each endpoint as described in Step 2]
|
264 |
|
265 |
+
FINAL DECISION:
|
266 |
{{
|
267 |
"reasoning": {{
|
268 |
+
"user_intent": "What the user wants to accomplish",
|
269 |
+
"selected_endpoint": "Why this specific endpoint was chosen - reference its description",
|
270 |
+
"parameter_mapping": "How parameters were extracted and converted"
|
271 |
}},
|
272 |
"endpoint": "/exact_endpoint_path_from_documentation",
|
273 |
"method": "HTTP_METHOD",
|
274 |
"params": {{
|
275 |
+
"param1": "value",
|
276 |
+
"param2": "value"
|
|
|
277 |
}},
|
278 |
+
"missing_required": ["missing_param1", "missing_param2"],
|
279 |
"confidence": 0.95
|
280 |
}}
|
281 |
|
282 |
=== CRITICAL RULES ===
|
283 |
+
1. You MUST analyze EVERY endpoint in the documentation - no exceptions
|
284 |
+
2. ONLY select endpoints that exist in the provided documentation
|
285 |
+
3. NEVER skip the endpoint-by-endpoint analysis
|
286 |
+
4. Match by endpoint DESCRIPTION/PURPOSE, not just endpoint name
|
287 |
+
5. ALL dates MUST be in format YYYY-MM-DDTHH:mm:ss
|
288 |
+
6. If no endpoint matches user intent, set endpoint to null
|
289 |
+
7. Show your complete analysis before giving the final JSON response
|
290 |
+
8. Required parameters not available must be listed in missing_required
|
291 |
+
9. Use conversation history to fill in missing context
|
292 |
+
|
293 |
+
=== EXAMPLES ===
|
294 |
+
If user says "I want to schedule an appointment for tomorrow at 2 PM":
|
295 |
+
- Look for endpoint with description mentioning appointment creation/scheduling
|
296 |
+
- Convert "tomorrow at 2 PM" to proper date format (e.g., 2025-06-01T14:00:00)
|
297 |
+
- Extract any other required parameters or mark as missing
|
298 |
+
|
299 |
+
If user says "show me my appointments today":
|
300 |
+
- Look for endpoint with description mentioning appointment retrieval/listing
|
301 |
+
- Convert "today" to proper date format for filtering
|
302 |
+
- Check if patient_id or similar identifier is required
|
303 |
+
|
304 |
+
Now analyze EVERY endpoint in the documentation systematically before making your decision.
|
|
|
|
|
|
|
|
|
305 |
""",
|
306 |
input_variables=[
|
307 |
"endpoints_documentation", "user_query", "detected_language",
|