Spaces:
Sleeping
Sleeping
Commit
·
d7c596c
1
Parent(s):
af650fa
ix
Browse files
main.py
CHANGED
@@ -241,94 +241,6 @@ class HealthcareChatbot:
|
|
241 |
|
242 |
|
243 |
# API routing prompt (reuse existing router_prompt_template)
|
244 |
-
# self.router_prompt_template = PromptTemplate(
|
245 |
-
# template="""
|
246 |
-
# You are an API routing system that operates with strict adherence to documented specifications. Your sole purpose is to precisely match user requests to API endpoints based on the provided documentation.
|
247 |
-
|
248 |
-
# === ABSOLUTE REQUIREMENTS ===
|
249 |
-
# 1. ENDPOINT SELECTION MUST BE GROUNDED IN DOCUMENTATION
|
250 |
-
# 2. ALL REQUIRED PARAMETERS MUST BE ACCOUNTED FOR
|
251 |
-
# 3. RESPONSE STRUCTURE MUST REMAIN UNCHANGED
|
252 |
-
|
253 |
-
# === INPUT DATA ===
|
254 |
-
# Current Context:
|
255 |
-
# - DateTime: {current_datetime}
|
256 |
-
# - Timezone: {timezone}
|
257 |
-
# - User Locale: {user_locale}
|
258 |
-
|
259 |
-
# API Documentation:
|
260 |
-
# {endpoints_documentation}
|
261 |
-
|
262 |
-
# User Request:
|
263 |
-
# - Query: {user_query}
|
264 |
-
# - Language: {detected_language}
|
265 |
-
# - Keywords: {extracted_keywords}
|
266 |
-
# - Sentiment: {sentiment_analysis}
|
267 |
-
|
268 |
-
# === PROCESSING INSTRUCTIONS ===
|
269 |
-
# 1. Analyze the user request to determine:
|
270 |
-
# - Core intent (what they want to accomplish)
|
271 |
-
# - Target entity/resource
|
272 |
-
# - Required operation type (CRUD)
|
273 |
-
|
274 |
-
# 2. Examine the API documentation to:
|
275 |
-
# - Identify all endpoints related to the target entity
|
276 |
-
# - Verify the endpoint's purpose matches the user intent
|
277 |
-
# - Confirm the HTTP method aligns with the operation type
|
278 |
-
|
279 |
-
# 3. For the selected endpoint:
|
280 |
-
# - Extract ALL documented parameters
|
281 |
-
# - Determine parameter sources (query, history, or defaults)
|
282 |
-
# - Validate parameter formats against specifications
|
283 |
-
# - Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
|
284 |
-
|
285 |
-
# 4. Validate your selection:
|
286 |
-
# - The endpoint must directly satisfy the user's primary need
|
287 |
-
# - All required parameters must be present or explicitly listed as missing
|
288 |
-
# - No undocumented parameters may be included
|
289 |
-
|
290 |
-
# === OUTPUT FORMAT ===
|
291 |
-
# {
|
292 |
-
# "reasoning": {
|
293 |
-
# "user_intent": "[concise description]",
|
294 |
-
# "endpoint_selection": "[exact documentation reference]",
|
295 |
-
# "parameter_analysis": "[source and validation for each parameter]"
|
296 |
-
# },
|
297 |
-
# "endpoint": "/documented/endpoint/path",
|
298 |
-
# "method": "HTTP_METHOD",
|
299 |
-
# "params": {
|
300 |
-
# "param1": "value" // only if properly sourced
|
301 |
-
# },
|
302 |
-
# "missing_required": [
|
303 |
-
# "param1" // if required but unavailable
|
304 |
-
# ],
|
305 |
-
# "confidence": 0.95 // 1.0 when all parameters are validated
|
306 |
-
# }
|
307 |
-
|
308 |
-
# === STRICT ENFORCEMENT ===
|
309 |
-
# - Empty params objects are prohibited when documentation requires parameters
|
310 |
-
# - All missing required parameters must be explicitly listed
|
311 |
-
# - Confidence scores must reflect parameter completeness
|
312 |
-
# - Date/time formatting must be exact
|
313 |
-
# - Never assume undocumented functionality
|
314 |
-
|
315 |
-
# === ERROR CONDITIONS ===
|
316 |
-
# 1. If no endpoint matches:
|
317 |
-
# - Set "endpoint" to null
|
318 |
-
# - Explain in "reasoning"
|
319 |
-
|
320 |
-
# 2. If critical parameters are missing:
|
321 |
-
# - List ALL in "missing_required"
|
322 |
-
# - Reduce confidence score accordingly
|
323 |
-
|
324 |
-
# Your response will be validated against these requirements before being accepted.
|
325 |
-
# """,
|
326 |
-
# input_variables=[
|
327 |
-
# "endpoints_documentation", "user_query", "detected_language",
|
328 |
-
# "extracted_keywords", "sentiment_analysis", "conversation_history",
|
329 |
-
# "current_datetime", "timezone", "user_locale"
|
330 |
-
# ]
|
331 |
-
# )
|
332 |
self.router_prompt_template = PromptTemplate(
|
333 |
template="""
|
334 |
You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
|
@@ -341,6 +253,10 @@ class HealthcareChatbot:
|
|
341 |
Language: {detected_language}
|
342 |
Keywords: {extracted_keywords}
|
343 |
Sentiment: {sentiment_analysis}
|
|
|
|
|
|
|
|
|
344 |
|
345 |
=== ROUTING PROCESS ===
|
346 |
Follow these steps in order:
|
@@ -359,7 +275,9 @@ class HealthcareChatbot:
|
|
359 |
STEP 3: PARAMETER EXTRACTION
|
360 |
- Identify ALL required parameters from the endpoint documentation
|
361 |
- Extract parameter values from the user query
|
362 |
-
- Convert data types as needed
|
|
|
|
|
363 |
- Set appropriate defaults for optional parameters if beneficial
|
364 |
|
365 |
STEP 4: VALIDATION
|
@@ -391,7 +309,7 @@ class HealthcareChatbot:
|
|
391 |
1. ONLY select endpoints that exist in the provided documentation
|
392 |
2. NEVER fabricate or assume endpoint parameters not in documentation
|
393 |
3. ALL required parameters MUST be included or listed as missing
|
394 |
-
4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:
|
395 |
5. If patient_id is required and not provided, add it to missing_required
|
396 |
6. Match endpoints by PURPOSE, not just keywords in the path
|
397 |
7. If multiple endpoints could work, choose the most specific one
|
@@ -405,8 +323,87 @@ class HealthcareChatbot:
|
|
405 |
|
406 |
Think step by step and be precise with your endpoint selection and parameter extraction.:""",
|
407 |
input_variables=["endpoints_documentation", "user_query", "detected_language",
|
408 |
-
"extracted_keywords", "sentiment_analysis", "conversation_history"
|
|
|
409 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
|
411 |
# Conversational response prompt
|
412 |
self.conversation_template = PromptTemplate(
|
|
|
241 |
|
242 |
|
243 |
# API routing prompt (reuse existing router_prompt_template)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
self.router_prompt_template = PromptTemplate(
|
245 |
template="""
|
246 |
You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
|
|
|
253 |
Language: {detected_language}
|
254 |
Keywords: {extracted_keywords}
|
255 |
Sentiment: {sentiment_analysis}
|
256 |
+
Current Context:
|
257 |
+
- DateTime: {current_datetime}
|
258 |
+
- Timezone: {timezone}
|
259 |
+
- User Locale: {user_locale}
|
260 |
|
261 |
=== ROUTING PROCESS ===
|
262 |
Follow these steps in order:
|
|
|
275 |
STEP 3: PARAMETER EXTRACTION
|
276 |
- Identify ALL required parameters from the endpoint documentation
|
277 |
- Extract parameter values from the user query
|
278 |
+
- Convert data types as needed:
|
279 |
+
- Dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
|
280 |
+
- Numbers to integers
|
281 |
- Set appropriate defaults for optional parameters if beneficial
|
282 |
|
283 |
STEP 4: VALIDATION
|
|
|
309 |
1. ONLY select endpoints that exist in the provided documentation
|
310 |
2. NEVER fabricate or assume endpoint parameters not in documentation
|
311 |
3. ALL required parameters MUST be included or listed as missing
|
312 |
+
4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:mm:ss)
|
313 |
5. If patient_id is required and not provided, add it to missing_required
|
314 |
6. Match endpoints by PURPOSE, not just keywords in the path
|
315 |
7. If multiple endpoints could work, choose the most specific one
|
|
|
323 |
|
324 |
Think step by step and be precise with your endpoint selection and parameter extraction.:""",
|
325 |
input_variables=["endpoints_documentation", "user_query", "detected_language",
|
326 |
+
"extracted_keywords", "sentiment_analysis", "conversation_history",
|
327 |
+
"current_datetime", "timezone", "user_locale"]
|
328 |
)
|
329 |
+
# self.router_prompt_template = PromptTemplate(
|
330 |
+
# template="""
|
331 |
+
# You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
|
332 |
+
|
333 |
+
# === ENDPOINT DOCUMENTATION ===
|
334 |
+
# {endpoints_documentation}
|
335 |
+
|
336 |
+
# === USER REQUEST ANALYSIS ===
|
337 |
+
# User Query: {user_query}
|
338 |
+
# Language: {detected_language}
|
339 |
+
# Keywords: {extracted_keywords}
|
340 |
+
# Sentiment: {sentiment_analysis}
|
341 |
+
|
342 |
+
# === ROUTING PROCESS ===
|
343 |
+
# Follow these steps in order:
|
344 |
+
|
345 |
+
# STEP 1: INTENT ANALYSIS
|
346 |
+
# - What is the user trying to accomplish?
|
347 |
+
# - What type of operation are they requesting? (create, read, update, delete, search, etc.)
|
348 |
+
# - What entity/resource are they working with?
|
349 |
+
|
350 |
+
# STEP 2: ENDPOINT MATCHING
|
351 |
+
# - Review each endpoint in the documentation
|
352 |
+
# - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
|
353 |
+
# - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
|
354 |
+
# - Verify the endpoint can handle the user's specific request
|
355 |
+
|
356 |
+
# STEP 3: PARAMETER EXTRACTION
|
357 |
+
# - Identify ALL required parameters from the endpoint documentation
|
358 |
+
# - Extract parameter values from the user query
|
359 |
+
# - Convert data types as needed (dates to ISO 8601, numbers to integers, etc.)
|
360 |
+
# - Set appropriate defaults for optional parameters if beneficial
|
361 |
+
|
362 |
+
# STEP 4: VALIDATION
|
363 |
+
# - Ensure ALL required parameters are provided or identified as missing
|
364 |
+
# - Verify parameter formats match documentation requirements
|
365 |
+
# - Check that the selected endpoint actually solves the user's problem
|
366 |
+
|
367 |
+
# === RESPONSE FORMAT ===
|
368 |
+
# Provide your analysis and decision in this exact JSON structure:
|
369 |
+
|
370 |
+
# {{
|
371 |
+
# "reasoning": {{
|
372 |
+
# "user_intent": "Brief description of what the user wants to accomplish",
|
373 |
+
# "selected_endpoint": "Why this endpoint was chosen over others",
|
374 |
+
# "parameter_mapping": "How user query maps to endpoint parameters"
|
375 |
+
# }},
|
376 |
+
# "endpoint": "/exact_endpoint_path_from_documentation",
|
377 |
+
# "method": "HTTP_METHOD",
|
378 |
+
# "params": {{
|
379 |
+
# "required_param_1": "extracted_or_converted_value",
|
380 |
+
# "required_param_2": "extracted_or_converted_value",
|
381 |
+
# "optional_param": "value_if_applicable"
|
382 |
+
# }},
|
383 |
+
# "missing_required": ["list", "of", "missing", "required", "parameters"],
|
384 |
+
# "confidence": 0.95
|
385 |
+
# }}
|
386 |
+
|
387 |
+
# === CRITICAL RULES ===
|
388 |
+
# 1. ONLY select endpoints that exist in the provided documentation
|
389 |
+
# 2. NEVER fabricate or assume endpoint parameters not in documentation
|
390 |
+
# 3. ALL required parameters MUST be included or listed as missing
|
391 |
+
# 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
|
392 |
+
# 5. If patient_id is required and not provided, add it to missing_required
|
393 |
+
# 6. Match endpoints by PURPOSE, not just keywords in the path
|
394 |
+
# 7. If multiple endpoints could work, choose the most specific one
|
395 |
+
# 8. If no endpoint matches, set endpoint to null and explain in reasoning
|
396 |
+
|
397 |
+
# === EXAMPLES OF GOOD MATCHING ===
|
398 |
+
# - User wants "patient records" → Use patient retrieval endpoint, not general search
|
399 |
+
# - User wants to "schedule appointment" → Use appointment creation endpoint
|
400 |
+
# - User asks "what appointments today" → Use appointment listing with date filter
|
401 |
+
# - User wants to "update medication" → Use medication update endpoint with patient_id
|
402 |
+
|
403 |
+
# Think step by step and be precise with your endpoint selection and parameter extraction.:""",
|
404 |
+
# input_variables=["endpoints_documentation", "user_query", "detected_language",
|
405 |
+
# "extracted_keywords", "sentiment_analysis", "conversation_history"]
|
406 |
+
# )
|
407 |
|
408 |
# Conversational response prompt
|
409 |
self.conversation_template = PromptTemplate(
|