Spaces:
Running
Running
Commit
ยท
a7b7d98
1
Parent(s):
76d036e
Update promptclear
Browse files
main.py
CHANGED
@@ -155,126 +155,118 @@ class HealthcareChatbot:
|
|
155 |
|
156 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
157 |
self.router_prompt_template = PromptTemplate(
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
171 |
{endpoints_documentation}
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
- Greetings,
|
182 |
-
- General health questions
|
183 |
-
-
|
184 |
-
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
-
|
189 |
-
-
|
190 |
-
-
|
191 |
-
-
|
192 |
-
- Any
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
-
|
199 |
-
|
200 |
-
|
201 |
-
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
|
|
|
|
|
|
209 |
|
210 |
{{
|
211 |
-
"intent": "CONVERSATION
|
212 |
-
"confidence": 0.
|
213 |
-
"reasoning": "
|
214 |
-
"endpoint": "
|
215 |
-
"method": "GET
|
216 |
"params": {{
|
217 |
"param1": "value1",
|
218 |
"param2": "value2"
|
219 |
}},
|
220 |
-
"missing_required": ["
|
221 |
}}
|
222 |
|
223 |
-
|
224 |
|
225 |
-
|
|
|
226 |
{{
|
227 |
"intent": "CONVERSATION",
|
228 |
-
"confidence": 0.
|
229 |
-
"reasoning": "User is greeting -
|
230 |
"endpoint": null,
|
231 |
"method": null,
|
232 |
"params": {{}},
|
233 |
"missing_required": []
|
234 |
}}
|
235 |
|
236 |
-
|
|
|
237 |
{{
|
238 |
"intent": "API_ACTION",
|
239 |
-
"confidence": 0.
|
240 |
-
"reasoning": "User
|
241 |
"endpoint": "/appointments",
|
242 |
-
"method": "POST",
|
243 |
"params": {{
|
244 |
"patient_id": "will_be_injected",
|
245 |
-
"appointment_date": "2025-06-
|
246 |
-
}},
|
247 |
-
"missing_required": []
|
248 |
-
}}
|
249 |
-
|
250 |
-
User: "What are my appointments?"
|
251 |
-
{{
|
252 |
-
"intent": "API_ACTION",
|
253 |
-
"confidence": 0.97,
|
254 |
-
"reasoning": "User wants to view their appointments - requires data retrieval",
|
255 |
-
"endpoint": "/appointments",
|
256 |
-
"method": "GET",
|
257 |
-
"params": {{
|
258 |
-
"patient_id": "will_be_injected"
|
259 |
}},
|
260 |
-
"missing_required": []
|
261 |
}}
|
262 |
|
263 |
-
|
264 |
-
1.
|
265 |
-
2.
|
266 |
-
3.
|
267 |
-
4.
|
268 |
-
5.
|
269 |
-
6.
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
"sentiment_analysis", "conversation_history", "endpoints_documentation"]
|
276 |
-
)
|
277 |
-
|
278 |
# CONVERSATION CHAIN - Handles conversational responses
|
279 |
self.conversation_template = PromptTemplate(
|
280 |
template="""
|
|
|
155 |
|
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 |
+
## Role
|
162 |
+
You are an advanced healthcare routing assistant. Your primary responsibilities are:
|
163 |
+
1. Accurately classify user intent (conversation vs. API action)
|
164 |
+
2. Precisely identify the correct API endpoint when needed
|
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 Reference
|
174 |
{endpoints_documentation}
|
175 |
|
176 |
+
## Decision Framework
|
177 |
+
|
178 |
+
### Step 1: Intent Classification
|
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 |
+
**Conversation Indicators:**
|
184 |
+
- Greetings (Hello, Hi, ุงูุณูุงู
ุนูููู
)
|
185 |
+
- General health information questions
|
186 |
+
- Bot capabilities inquiries
|
187 |
+
- Thank you/goodbye messages
|
188 |
+
- Non-specific healthcare questions
|
189 |
+
|
190 |
+
**API Action Indicators:**
|
191 |
+
- Requests involving personal health data
|
192 |
+
- Appointment scheduling/modification
|
193 |
+
- Medical record access
|
194 |
+
- Profile updates
|
195 |
+
- Any action requiring system changes
|
196 |
+
|
197 |
+
### Step 2: Endpoint Selection (API_ACTION only)
|
198 |
+
1. Review all available endpoints
|
199 |
+
2. Match user intent to endpoint purpose/description
|
200 |
+
3. Verify HTTP method aligns with requested action:
|
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. Identify all required parameters from endpoint documentation
|
208 |
+
2. Extract values from user query
|
209 |
+
3. Convert relative dates to ISO 8601 format
|
210 |
+
4. Note missing required parameters
|
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": "Clear explanation of decision logic",
|
220 |
+
"endpoint": "null|/endpoint/path",
|
221 |
+
"method": "null|GET|POST|PUT|PATCH|DELETE",
|
222 |
"params": {{
|
223 |
"param1": "value1",
|
224 |
"param2": "value2"
|
225 |
}},
|
226 |
+
"missing_required": ["param1", "param2"]
|
227 |
}}
|
228 |
|
229 |
+
## Examples
|
230 |
|
231 |
+
Example 1: Greeting
|
232 |
+
User: "ุงูุณูุงู
ุนูููู
"
|
233 |
{{
|
234 |
"intent": "CONVERSATION",
|
235 |
+
"confidence": 0.99,
|
236 |
+
"reasoning": "User is greeting in Arabic - no API action required",
|
237 |
"endpoint": null,
|
238 |
"method": null,
|
239 |
"params": {{}},
|
240 |
"missing_required": []
|
241 |
}}
|
242 |
|
243 |
+
Example 2: Appointment Request
|
244 |
+
User: "I need to schedule a doctor visit next Tuesday"
|
245 |
{{
|
246 |
"intent": "API_ACTION",
|
247 |
+
"confidence": 0.96,
|
248 |
+
"reasoning": "User requests appointment scheduling - matches /appointments endpoint",
|
249 |
"endpoint": "/appointments",
|
250 |
+
"method": "POST",
|
251 |
"params": {{
|
252 |
"patient_id": "will_be_injected",
|
253 |
+
"appointment_date": "2025-06-10T00:00:00"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
}},
|
255 |
+
"missing_required": ["doctor_specialization"]
|
256 |
}}
|
257 |
|
258 |
+
## Quality Assurance Rules
|
259 |
+
1. **Precision**: Must match exact endpoint from documentation
|
260 |
+
2. **Conservatism**: Default to CONVERSATION when uncertain
|
261 |
+
3. **Completeness**: List all missing required parameters
|
262 |
+
4. **Validation**: Verify all parameters against endpoint specs
|
263 |
+
5. **Neutrality**: Ignore conversation history for routing decisions
|
264 |
+
6. **Confidence Scoring**: Reflect certainty level accurately
|
265 |
+
|
266 |
+
Analyze the current query and respond with the appropriate JSON structure:""",
|
267 |
+
input_variables=["user_query", "detected_language", "extracted_keywords",
|
268 |
+
"sentiment_analysis", "endpoints_documentation"]
|
269 |
+
)
|
|
|
|
|
|
|
270 |
# CONVERSATION CHAIN - Handles conversational responses
|
271 |
self.conversation_template = PromptTemplate(
|
272 |
template="""
|