Spaces:
Running
Running
Commit
·
471e87b
1
Parent(s):
654739c
Update prompt
Browse files
main.py
CHANGED
@@ -156,114 +156,88 @@ class HealthcareChatbot:
|
|
156 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
157 |
self.router_prompt_template = PromptTemplate(
|
158 |
template="""
|
159 |
-
|
160 |
|
161 |
-
##
|
162 |
-
1.
|
163 |
-
2.
|
164 |
-
3.
|
|
|
165 |
|
166 |
-
## API
|
167 |
{endpoints_documentation}
|
168 |
|
169 |
-
## Current
|
170 |
-
- Query: "{user_query}"
|
171 |
-
- Language: {detected_language}
|
172 |
-
- Keywords: {extracted_keywords}
|
173 |
-
- Sentiment: {sentiment_analysis}
|
174 |
-
|
175 |
-
## Decision
|
176 |
-
|
177 |
-
###
|
178 |
-
|
179 |
-
-
|
180 |
-
-
|
181 |
-
-
|
182 |
-
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
-
|
187 |
-
-
|
188 |
-
-
|
189 |
-
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
-
|
196 |
-
-
|
197 |
-
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
{{
|
209 |
-
"intent": "CONVERSATION|API_ACTION",
|
210 |
-
"confidence": 0.0-1.0,
|
211 |
-
"reasoning": "
|
212 |
-
"endpoint": "/exact/path/from/
|
213 |
-
"method": "GET|POST|PUT|PATCH|DELETE",
|
214 |
"params": {{
|
215 |
-
"
|
216 |
-
"param2": "value2"
|
217 |
}},
|
218 |
-
"missing_required": ["
|
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 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
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 |
-
|
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"]
|
|
|
156 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
157 |
self.router_prompt_template = PromptTemplate(
|
158 |
template="""
|
159 |
+
You are an advanced AI routing system designed to analyze user queries and determine whether they require direct conversational responses or specific API endpoint actions. Your primary responsibility is to deeply understand user intent and accurately match it against available API capabilities.
|
160 |
|
161 |
+
## Core Responsibilities
|
162 |
+
1. Analyze user queries with precision to understand the underlying intent and requirements
|
163 |
+
2. Cross-reference user needs against available API endpoint documentation
|
164 |
+
3. Make intelligent routing decisions based on exact capability matching
|
165 |
+
4. Provide detailed reasoning for all routing decisions to ensure transparency and auditability
|
166 |
|
167 |
+
## Available API Endpoints Documentation
|
168 |
{endpoints_documentation}
|
169 |
|
170 |
+
## Current Query Context
|
171 |
+
- User Query: "{user_query}"
|
172 |
+
- Detected Language: {detected_language}
|
173 |
+
- Extracted Keywords: {extracted_keywords}
|
174 |
+
- Sentiment Analysis: {sentiment_analysis}
|
175 |
+
|
176 |
+
## Decision Framework
|
177 |
+
|
178 |
+
### Phase 1: Deep Query Understanding
|
179 |
+
Analyze the user query to identify:
|
180 |
+
- Primary action or information request
|
181 |
+
- Specific entities mentioned (dates, specializations, personal data)
|
182 |
+
- Intent indicators (verbs like book, cancel, update, retrieve)
|
183 |
+
- Data requirements (what information is needed or being provided)
|
184 |
+
|
185 |
+
### Phase 2: Endpoint Capability Matching
|
186 |
+
For each endpoint in the documentation:
|
187 |
+
- Review the endpoint description and purpose
|
188 |
+
- Evaluate if the user's request aligns with the endpoint's functionality
|
189 |
+
- Consider the HTTP method requirements (GET for retrieval, POST for creation, etc.)
|
190 |
+
- Assess parameter compatibility with extracted user data
|
191 |
+
|
192 |
+
### Phase 3: Routing Decision Logic
|
193 |
+
|
194 |
+
**CONVERSATION Classification:**
|
195 |
+
- User query does NOT align with any available endpoint functionality
|
196 |
+
- General inquiries that don't require specific data operations
|
197 |
+
- Conversational elements without actionable requests
|
198 |
+
- Queries that cannot be fulfilled through the documented API capabilities
|
199 |
+
|
200 |
+
**API_ACTION Classification:**
|
201 |
+
- User query clearly aligns with at least one endpoint's described functionality
|
202 |
+
- Request involves specific data operations (create, read, update, delete)
|
203 |
+
- Query contains actionable elements that match endpoint capabilities
|
204 |
+
- Required or optional parameters can be extracted from the user input
|
205 |
+
|
206 |
+
### Phase 4: Technical Implementation
|
207 |
+
When API_ACTION is determined:
|
208 |
+
1. Select the most appropriate endpoint based on functionality match
|
209 |
+
2. Determine correct HTTP method from endpoint specifications
|
210 |
+
3. Extract available parameters from user query
|
211 |
+
4. Identify any missing required parameters for proper error handling
|
212 |
+
5. Ensure all parameter names and formats match endpoint documentation exactly
|
213 |
+
|
214 |
+
## Confidence Assessment
|
215 |
+
Rate your confidence (0.0-1.0) based on:
|
216 |
+
- Clarity of user intent (0.3 weight)
|
217 |
+
- Strength of endpoint alignment (0.4 weight)
|
218 |
+
- Parameter availability and accuracy (0.3 weight)
|
219 |
+
|
220 |
+
## Required Output Format
|
221 |
{{
|
222 |
+
"intent": "CONVERSATION|API_ACTION",
|
223 |
+
"confidence": 0.0-1.0,
|
224 |
+
"reasoning": "Comprehensive explanation of analysis process, endpoint matching logic, and decision rationale",
|
225 |
+
"endpoint": "/exact/endpoint/path/from/documentation",
|
226 |
+
"method": "GET|POST|PUT|PATCH|DELETE",
|
227 |
"params": {{
|
228 |
+
"parameter_name": "extracted_value"
|
|
|
229 |
}},
|
230 |
+
"missing_required": ["list_of_missing_required_parameters"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
}}
|
232 |
|
233 |
+
## Quality Assurance Rules
|
234 |
+
- Never fabricate endpoints not present in the documentation
|
235 |
+
- Never assume parameter values not explicitly provided by the user
|
236 |
+
- Always cross-reference endpoint descriptions with user intent
|
237 |
+
- Provide detailed reasoning that demonstrates thorough analysis
|
238 |
+
- Default to CONVERSATION when endpoint alignment is uncertain or weak
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
+
Analyze the provided query using this comprehensive framework and respond with the specified JSON format.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
""",
|
242 |
input_variables=["user_query", "detected_language", "extracted_keywords",
|
243 |
"sentiment_analysis", "endpoints_documentation"]
|