Spaces:
Running
Running
Commit
·
a48daee
1
Parent(s):
daa6699
Update
Browse files
main.py
CHANGED
@@ -156,99 +156,44 @@ class HealthcareChatbot:
|
|
156 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
157 |
self.router_prompt_template = PromptTemplate(
|
158 |
template="""
|
159 |
-
You are
|
160 |
|
161 |
-
##
|
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 |
-
##
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
-
|
183 |
-
-
|
184 |
-
|
185 |
-
|
186 |
-
|
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 |
-
**IMPORTANT: Default to API_ACTION when there's ANY potential endpoint match**
|
195 |
-
|
196 |
-
**API_ACTION Classification (PRIMARY):**
|
197 |
-
- User mentions ANY action that could relate to healthcare operations
|
198 |
-
- ANY request for information that might exist in the system
|
199 |
-
- Keywords indicating medical services, appointments, records, or healthcare data
|
200 |
-
- Even vague requests that could be interpreted as needing API data
|
201 |
-
- When user asks about anything related to their health, medical history, or healthcare services
|
202 |
-
- Any mention of booking, scheduling, checking, updating, or retrieving information
|
203 |
-
|
204 |
-
**CONVERSATION Classification (SECONDARY - Use ONLY when certain):**
|
205 |
-
- Pure greetings with no follow-up request (just "hello", "hi")
|
206 |
-
- Thank you messages with no additional requests
|
207 |
-
- Questions ONLY about the bot's capabilities with no data needs
|
208 |
-
- Completely unrelated topics (weather, sports, general knowledge)
|
209 |
-
- Clear goodbye messages with no pending requests
|
210 |
-
|
211 |
-
### Phase 4: Technical Implementation
|
212 |
-
When API_ACTION is determined:
|
213 |
-
1. Select the most appropriate endpoint based on functionality match
|
214 |
-
2. Determine correct HTTP method from endpoint specifications
|
215 |
-
3. Extract available parameters from user query
|
216 |
-
4. Identify any missing required parameters for proper error handling
|
217 |
-
5. Ensure all parameter names and formats match endpoint documentation exactly
|
218 |
-
|
219 |
-
## Confidence Assessment
|
220 |
-
Rate your confidence (0.0-1.0) based on:
|
221 |
-
- BIAS TOWARD API_ACTION: Start with 0.7 confidence for any healthcare-related query
|
222 |
-
- Increase confidence when specific actions or data requests are mentioned
|
223 |
-
- Only reduce confidence below 0.5 for pure conversational elements
|
224 |
-
- Healthcare context should maintain high confidence for API_ACTION
|
225 |
-
|
226 |
-
## Required Output Format
|
227 |
{{
|
228 |
"intent": "CONVERSATION|API_ACTION",
|
229 |
"confidence": 0.0-1.0,
|
230 |
-
"reasoning": "
|
231 |
-
"endpoint": "/
|
232 |
-
"method": "
|
233 |
-
"params": {{
|
234 |
-
|
235 |
-
}},
|
236 |
-
"missing_required": ["list_of_missing_required_parameters"]
|
237 |
}}
|
238 |
|
239 |
-
|
240 |
-
- **CRITICAL**: Favor API_ACTION over CONVERSATION when in doubt
|
241 |
-
- Never fabricate endpoints not present in the documentation
|
242 |
-
- When multiple endpoints could match, select the most appropriate one
|
243 |
-
- If unsure about parameters, still route to API_ACTION and list missing parameters
|
244 |
-
- Only use CONVERSATION for clearly non-actionable queries
|
245 |
-
- Healthcare-related queries should almost always be API_ACTION
|
246 |
-
|
247 |
-
Analyze the provided query using this comprehensive framework and respond with the specified JSON format.
|
248 |
""",
|
249 |
input_variables=["user_query", "detected_language", "extracted_keywords",
|
250 |
"sentiment_analysis", "endpoints_documentation"]
|
251 |
)
|
|
|
252 |
# CONVERSATION CHAIN - Handles conversational responses
|
253 |
self.conversation_template = PromptTemplate(
|
254 |
template="""
|
|
|
156 |
# UNIFIED ROUTER CHAIN - Handles both intent classification AND API routing
|
157 |
self.router_prompt_template = PromptTemplate(
|
158 |
template="""
|
159 |
+
You are a routing system that determines if a user query matches any available API endpoint.
|
160 |
|
161 |
+
## Available API Endpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
{endpoints_documentation}
|
163 |
|
164 |
+
## User Query
|
165 |
+
"{user_query}"
|
166 |
+
|
167 |
+
## Simple Decision Process
|
168 |
+
|
169 |
+
**Step 1: Check Endpoint Match**
|
170 |
+
- Read each endpoint description in the documentation
|
171 |
+
- Check if the user query aligns with ANY endpoint's purpose/description
|
172 |
+
- If YES → intent = "API_ACTION"
|
173 |
+
- If NO → intent = "CONVERSATION"
|
174 |
+
|
175 |
+
**Step 2: Extract Details (only if API_ACTION)**
|
176 |
+
- Select the matching endpoint
|
177 |
+
- Extract parameters from user query
|
178 |
+
- List missing required parameters
|
179 |
+
|
180 |
+
## Output Format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
{{
|
182 |
"intent": "CONVERSATION|API_ACTION",
|
183 |
"confidence": 0.0-1.0,
|
184 |
+
"reasoning": "Brief explanation of whether query matches any endpoint description",
|
185 |
+
"endpoint": "/endpoint/path/or/null",
|
186 |
+
"method": "HTTP_METHOD/or/null",
|
187 |
+
"params": {{}},
|
188 |
+
"missing_required": []
|
|
|
|
|
189 |
}}
|
190 |
|
191 |
+
Analyze the query against the endpoint descriptions and respond with the JSON format.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
""",
|
193 |
input_variables=["user_query", "detected_language", "extracted_keywords",
|
194 |
"sentiment_analysis", "endpoints_documentation"]
|
195 |
)
|
196 |
+
|
197 |
# CONVERSATION CHAIN - Handles conversational responses
|
198 |
self.conversation_template = PromptTemplate(
|
199 |
template="""
|