Spaces:
Sleeping
Sleeping
Commit
Β·
25dd69a
1
Parent(s):
c5454c2
Fix
Browse files
main.py
CHANGED
@@ -192,10 +192,9 @@ class HealthcareChatbot:
|
|
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.
|
199 |
|
200 |
=== CURRENT CONTEXT ===
|
201 |
Current Date/Time: {current_datetime}
|
@@ -210,98 +209,101 @@ class HealthcareChatbot:
|
|
210 |
Language: {detected_language}
|
211 |
Keywords: {extracted_keywords}
|
212 |
Sentiment: {sentiment_analysis}
|
213 |
-
Conversation History: {conversation_history}
|
214 |
|
215 |
-
===
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
-
|
220 |
-
-
|
221 |
-
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
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 |
-
*
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
*
|
255 |
-
|
256 |
-
|
257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
|
259 |
=== RESPONSE FORMAT ===
|
260 |
-
|
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": "
|
269 |
-
"selected_endpoint": "Why this
|
270 |
-
"parameter_mapping": "How
|
271 |
}},
|
272 |
"endpoint": "/exact_endpoint_path_from_documentation",
|
273 |
"method": "HTTP_METHOD",
|
274 |
"params": {{
|
275 |
-
"
|
276 |
-
"
|
|
|
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.
|
292 |
-
|
293 |
-
=== EXAMPLES ===
|
294 |
-
|
295 |
-
-
|
296 |
-
-
|
297 |
-
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
-
|
302 |
-
-
|
303 |
-
|
304 |
-
|
|
|
|
|
|
|
305 |
""",
|
306 |
input_variables=[
|
307 |
"endpoints_documentation", "user_query", "detected_language",
|
|
|
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. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
|
198 |
|
199 |
=== CURRENT CONTEXT ===
|
200 |
Current Date/Time: {current_datetime}
|
|
|
209 |
Language: {detected_language}
|
210 |
Keywords: {extracted_keywords}
|
211 |
Sentiment: {sentiment_analysis}
|
|
|
212 |
|
213 |
+
=== ROUTING PROCESS ===
|
214 |
+
Follow these steps in order:
|
215 |
+
|
216 |
+
STEP 1: INTENT ANALYSIS
|
217 |
+
- What is the user trying to accomplish?
|
218 |
+
- What type of operation are they requesting? (create, read, update, delete, search, etc.)
|
219 |
+
- What entity/resource are they working with?
|
220 |
+
|
221 |
+
STEP 2: ENDPOINT MATCHING
|
222 |
+
- Review each endpoint in the documentation
|
223 |
+
- Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
|
224 |
+
- Consider the HTTP method (GET for retrieval, POST for creation, etc.)
|
225 |
+
- Verify the endpoint can handle the user's specific request
|
226 |
+
|
227 |
+
STEP 3: PARAMETER EXTRACTION WITH DATE HANDLING
|
228 |
+
- Identify ALL required parameters from the endpoint documentation
|
229 |
+
- Extract parameter values from the user query and conversation history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
- Handle date/time expressions with precision:
|
231 |
+
* RELATIVE TIME CONVERSION:
|
232 |
+
- "today" β current date (from current_datetime) + specified time or T08:00:00
|
233 |
+
- "tomorrow" β current date + 1 day + specified time or T08:00:00
|
234 |
+
- "yesterday" β current date - 1 day + specified time or T08:00:00
|
235 |
+
- "next week" β current date + 7 days + specified time or T08:00:00
|
236 |
+
- "in 2 hours" β current datetime + 2 hours
|
237 |
+
- "next Monday/Tuesday/etc." β calculate exact date + specified time or T08:00:00
|
238 |
+
* TIME EXPRESSIONS:
|
239 |
+
- "morning" β 08:00:00
|
240 |
+
- "afternoon" β 14:00:00
|
241 |
+
- "evening" β 18:00:00
|
242 |
+
- "3 PM" β 15:00:00
|
243 |
+
- "10:30 AM" β 10:30:00
|
244 |
+
* ARABIC TIME EXPRESSIONS:
|
245 |
+
- "Ψ§ΩΩΩΩ
" (today) β current date + time
|
246 |
+
- "ΨΊΨ―Ψ§" (tomorrow) β current date + 1 day + time
|
247 |
+
- "Ψ£Ω
Ψ³" (yesterday) β current date - 1 day + time
|
248 |
+
- "Ψ§ΩΨ΅Ψ¨Ψ§Ψ" (morning) β 08:00:00
|
249 |
+
- "Ψ¨ΨΉΨ― Ψ§ΩΨΈΩΨ±" (afternoon) β 14:00:00
|
250 |
+
- "Ψ§ΩΩ
Ψ³Ψ§Ψ‘" (evening) β 18:00:00
|
251 |
+
- Convert ALL dates to ISO 8601 format: YYYY-MM-DDTHH:mm:ss
|
252 |
+
- Convert data types as needed (numbers to integers, booleans, etc.)
|
253 |
+
- Set appropriate defaults for optional parameters if beneficial
|
254 |
+
|
255 |
+
STEP 4: VALIDATION
|
256 |
+
- Ensure ALL required parameters are provided or identified as missing
|
257 |
+
- Verify parameter formats match documentation requirements
|
258 |
+
- Check that the selected endpoint actually solves the user's problem
|
259 |
+
- Validate date formats are exactly YYYY-MM-DDTHH:mm:ss
|
260 |
|
261 |
=== RESPONSE FORMAT ===
|
262 |
+
Provide your analysis and decision in this exact JSON structure:
|
|
|
|
|
|
|
263 |
|
|
|
264 |
{{
|
265 |
"reasoning": {{
|
266 |
+
"user_intent": "Brief description of what the user wants to accomplish",
|
267 |
+
"selected_endpoint": "Why this endpoint was chosen over others",
|
268 |
+
"parameter_mapping": "How user query maps to endpoint parameters"
|
269 |
}},
|
270 |
"endpoint": "/exact_endpoint_path_from_documentation",
|
271 |
"method": "HTTP_METHOD",
|
272 |
"params": {{
|
273 |
+
"required_param_1": "extracted_or_converted_value",
|
274 |
+
"required_param_2": "extracted_or_converted_value",
|
275 |
+
"optional_param": "value_if_applicable"
|
276 |
}},
|
277 |
+
"missing_required": ["list", "of", "missing", "required", "parameters"],
|
278 |
"confidence": 0.95
|
279 |
}}
|
280 |
|
281 |
=== CRITICAL RULES ===
|
282 |
+
1. ONLY select endpoints that exist in the provided documentation
|
283 |
+
2. NEVER fabricate or assume endpoint parameters not in documentation
|
284 |
+
3. ALL required parameters MUST be included or listed as missing
|
285 |
+
4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss) - this is MANDATORY
|
286 |
+
5. If patient_id is required and not provided, add it to missing_required
|
287 |
+
6. Match endpoints by PURPOSE/DESCRIPTION, not just keywords in the path
|
288 |
+
7. If multiple endpoints could work, choose the most specific one
|
289 |
+
8. If no endpoint matches, set endpoint to null and explain in reasoning
|
290 |
+
9. Handle timezone considerations using the provided timezone context
|
291 |
+
|
292 |
+
=== DATE FORMATTING EXAMPLES ===
|
293 |
+
- Current datetime: 2025-05-31T10:30:00
|
294 |
+
- "today at 3 PM" β 2025-05-31T15:00:00
|
295 |
+
- "tomorrow morning" β 2025-06-01T08:00:00
|
296 |
+
- "next Tuesday at 2:30 PM" β calculate exact date + T14:30:00
|
297 |
+
- "in 3 hours" β 2025-05-31T13:30:00
|
298 |
+
|
299 |
+
=== EXAMPLES OF GOOD MATCHING ===
|
300 |
+
- User wants "patient records" β Use patient retrieval endpoint, not general search
|
301 |
+
- User wants to "schedule appointment" β Use appointment creation endpoint
|
302 |
+
- User asks "what appointments today" β Use appointment listing with date filter (today's date in YYYY-MM-DDTHH:mm:ss format)
|
303 |
+
- User wants to "update medication" β Use medication update endpoint with patient_id
|
304 |
+
- User says "cancel my appointment tomorrow" β Use appointment cancellation endpoint with tomorrow's date
|
305 |
+
|
306 |
+
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.
|
307 |
""",
|
308 |
input_variables=[
|
309 |
"endpoints_documentation", "user_query", "detected_language",
|