abdibrahem commited on
Commit
61ff52b
·
1 Parent(s): ab198d6

Update the api generate promot

Browse files
Files changed (1) hide show
  1. main.py +207 -77
main.py CHANGED
@@ -68,9 +68,9 @@ class HealthcareChatbot:
68
  self.endpoints_documentation = endpoints_documentation
69
  self.ollama_base_url = "http://localhost:11434"
70
  self.model_name = "gemma3"
71
- self.BASE_URL = 'https://a21e-197-54-54-66.ngrok-free.app'
72
  self.headers = {'Content-type': 'application/json'}
73
- self.user_id = '8eb58851-f276-4854-8548-5abd40ed383d'
74
  self.max_retries = 3
75
  self.retry_delay = 2
76
 
@@ -194,82 +194,212 @@ class HealthcareChatbot:
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
- === ENDPOINT DOCUMENTATION ===
200
- {endpoints_documentation}
201
-
202
- === USER REQUEST ANALYSIS ===
203
- User Query: {user_query}
204
- Language: {detected_language}
205
- Keywords: {extracted_keywords}
206
- Sentiment: {sentiment_analysis}
207
-
208
- === ROUTING PROCESS ===
209
- Follow these steps in order:
210
-
211
- STEP 1: INTENT ANALYSIS
212
- - What is the user trying to accomplish?
213
- - What type of operation are they requesting? (create, read, update, delete, search, etc.)
214
- - What entity/resource are they working with?
215
-
216
- STEP 2: ENDPOINT MATCHING
217
- - Review each endpoint in the documentation
218
- - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
219
- - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
220
- - Verify the endpoint can handle the user's specific request
221
-
222
- STEP 3: PARAMETER EXTRACTION
223
- - Identify ALL required parameters from the endpoint documentation
224
- - Extract parameter values from the user query
225
- - Convert data types as needed (dates to ISO 8601, numbers to integers, etc.)
226
- - Set appropriate defaults for optional parameters if beneficial
227
-
228
- STEP 4: VALIDATION
229
- - Ensure ALL required parameters are provided or identified as missing
230
- - Verify parameter formats match documentation requirements
231
- - Check that the selected endpoint actually solves the user's problem
232
-
233
- === RESPONSE FORMAT ===
234
- Provide your analysis and decision in this exact JSON structure:
235
-
236
- {{
237
- "reasoning": {{
238
- "user_intent": "Brief description of what the user wants to accomplish",
239
- "selected_endpoint": "Why this endpoint was chosen over others",
240
- "parameter_mapping": "How user query maps to endpoint parameters"
241
- }},
242
- "endpoint": "/exact_endpoint_path_from_documentation",
243
- "method": "HTTP_METHOD",
244
- "params": {{
245
- "required_param_1": "extracted_or_converted_value",
246
- "required_param_2": "extracted_or_converted_value",
247
- "optional_param": "value_if_applicable"
248
- }},
249
- "missing_required": ["list", "of", "missing", "required", "parameters"],
250
- "confidence": 0.95
251
- }}
252
-
253
- === CRITICAL RULES ===
254
- 1. ONLY select endpoints that exist in the provided documentation
255
- 2. NEVER fabricate or assume endpoint parameters not in documentation
256
- 3. ALL required parameters MUST be included or listed as missing
257
- 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
258
- 5. If patient_id is required and not provided, add it to missing_required
259
- 6. Match endpoints by PURPOSE, not just keywords in the path
260
- 7. If multiple endpoints could work, choose the most specific one
261
- 8. If no endpoint matches, set endpoint to null and explain in reasoning
262
-
263
- === EXAMPLES OF GOOD MATCHING ===
264
- - User wants "patient records" Use patient retrieval endpoint, not general search
265
- - User wants to "schedule appointment" Use appointment creation endpoint
266
- - User asks "what appointments today" → Use appointment listing with date filter
267
- - User wants to "update medication" → Use medication update endpoint with patient_id
268
-
269
- Think step by step and be precise with your endpoint selection and parameter extraction.:""",
270
- input_variables=["endpoints_documentation", "user_query", "detected_language",
271
- "extracted_keywords", "sentiment_analysis", "conversation_history"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
 
274
  # Conversational response prompt
275
  self.conversation_template = PromptTemplate(
 
68
  self.endpoints_documentation = endpoints_documentation
69
  self.ollama_base_url = "http://localhost:11434"
70
  self.model_name = "gemma3"
71
+ self.BASE_URL = 'https://9f87-197-54-54-66.ngrok-free.app'
72
  self.headers = {'Content-type': 'application/json'}
73
+ self.user_id = 'e2fe5655-d6ac-447d-8237-850413288561'
74
  self.max_retries = 3
75
  self.retry_delay = 2
76
 
 
194
  # API routing prompt (reuse existing router_prompt_template)
195
  self.router_prompt_template = PromptTemplate(
196
  template="""
197
+ You are an expert API routing assistant with deep analytical capabilities. Your role is to thoroughly understand user intentions and precisely match them with the most appropriate API endpoint after comprehensive analysis.
198
+
199
+ === CURRENT CONTEXT ===
200
+ Current Date/Time: {current_datetime}
201
+ Current Timezone: {timezone}
202
+ User Location/Locale: {user_locale}
203
+
204
+ === AVAILABLE ENDPOINTS ===
205
+ {endpoints_documentation}
206
+
207
+ === USER REQUEST DETAILS ===
208
+ User Query: {user_query}
209
+ Detected Language: {detected_language}
210
+ Extracted Keywords: {extracted_keywords}
211
+ Sentiment Analysis: {sentiment_analysis}
212
+ Conversation History: {conversation_history}
213
+
214
+ === COMPREHENSIVE ANALYSIS PROCESS ===
215
+
216
+ PHASE 1: DEEP QUERY UNDERSTANDING
217
+ Thoroughly analyze the user's request by examining:
218
+ - PRIMARY INTENT: What is the core action the user wants to perform?
219
+ - SECONDARY INTENTS: Are there multiple actions or follow-up implications?
220
+ - ENTITY IDENTIFICATION: What specific resources/objects are mentioned?
221
+ - OPERATION TYPE: Create, Read, Update, Delete, Search, List, Filter, Aggregate?
222
+ - IMPLICIT CONTEXT: What assumptions or unstated requirements exist?
223
+ - URGENCY/PRIORITY: Is this time-sensitive or routine?
224
+ - SCOPE: Single item, multiple items, or system-wide operation?
225
+
226
+ PHASE 2: ENDPOINTS DOCUMENTATION COMPREHENSION
227
+ For EACH endpoint in the documentation, analyze:
228
+ - PURPOSE AND FUNCTIONALITY: What business problem does this endpoint solve?
229
+ - REQUIRED VS OPTIONAL PARAMETERS: What data is absolutely necessary?
230
+ - INPUT DATA TYPES AND FORMATS: How should data be structured?
231
+ - EXPECTED OUTPUT: What will this endpoint return?
232
+ - USE CASES: When would this endpoint be the best choice?
233
+ - CONSTRAINTS AND LIMITATIONS: What are the boundaries of this endpoint?
234
+ - RELATIONSHIP TO OTHER ENDPOINTS: How does it fit in the overall API ecosystem?
235
+
236
+ PHASE 3: SEMANTIC MATCHING AND SCORING
237
+ For each potential endpoint match:
238
+ - INTENT ALIGNMENT SCORE (0-100): How well does the endpoint's purpose match user intent?
239
+ - PARAMETER AVAILABILITY SCORE (0-100): How much required data can be extracted/inferred?
240
+ - CONTEXT RELEVANCE SCORE (0-100): How well does this fit the conversation context?
241
+ - OPERATIONAL COMPLEXITY SCORE (0-100): How straightforward is this operation?
242
+ - TOTAL MATCH SCORE: Weighted average of above scores
243
+
244
+ PHASE 4: TEMPORAL AND CONTEXTUAL PROCESSING
245
+ Handle time-related expressions with precision:
246
+ - RELATIVE TIME CONVERSION:
247
+ * Arabic: اليوم/today, غدا/tomorrow, أمس/yesterday, بعد/after, قبل/before
248
+ * English: today, tomorrow, yesterday, next week, in 2 hours, etc.
249
+ * Context-aware: "next appointment", "usual time", "same day"
250
+ - ABSOLUTE TIME PARSING:
251
+ * Date formats: DD/MM/YYYY, MM-DD-YYYY, YYYY-MM-DD
252
+ * Time formats: 12-hour (AM/PM), 24-hour, relative (morning/afternoon)
253
+ - TIMEZONE CONSIDERATIONS:
254
+ * Convert all times to user's timezone
255
+ * Account for daylight saving time if applicable
256
+ * Provide ISO 8601 formatted output
257
+
258
+ PHASE 5: PARAMETER EXTRACTION AND VALIDATION
259
+ Extract and validate ALL parameters:
260
+ - DIRECT EXTRACTION: Explicitly mentioned values in user query
261
+ - CONTEXTUAL INFERENCE: Values derivable from conversation history
262
+ - TYPE CONVERSION: Ensure proper data types (string, int, float, datetime, boolean)
263
+ - FORMAT VALIDATION: Check against endpoint requirements
264
+ - DEFAULT VALUES: Apply sensible defaults for optional parameters
265
+ - MISSING PARAMETER IDENTIFICATION: Clearly identify what's missing and why it's needed
266
+
267
+ PHASE 6: DECISION VALIDATION AND CONFIDENCE ASSESSMENT
268
+ Before finalizing the decision:
269
+ - SANITY CHECK: Does this endpoint actually solve the user's problem?
270
+ - ALTERNATIVE CONSIDERATION: Are there better endpoint options?
271
+ - COMPLETENESS VERIFICATION: Are all required parameters available or obtainable?
272
+ - EDGE CASE EVALUATION: What could go wrong with this choice?
273
+ - CONFIDENCE CALCULATION: How certain are you about this routing decision?
274
+
275
+ === RESPONSE FORMAT ===
276
+ Provide your comprehensive analysis and decision in this exact JSON structure:
277
+
278
+ {{
279
+ "reasoning": {{
280
+ "user_intent": "Detailed description of what the user wants to accomplish, including primary and secondary goals",
281
+ "temporal_analysis": "How dates/times were interpreted and converted with step-by-step calculations",
282
+ "context_used": "Any context from conversation history that was applied, including entity persistence and implicit references",
283
+ "endpoint_analysis": "Thorough evaluation of why each potential endpoint was considered or rejected, with scoring rationale",
284
+ "selected_endpoint": "Detailed explanation of why this specific endpoint was chosen over all others",
285
+ "parameter_mapping": "Comprehensive explanation of how user query maps to endpoint parameters, including extraction methods and confidence levels"
286
+ }},
287
+ "endpoint": "/exact_endpoint_path_from_documentation",
288
+ "method": "HTTP_METHOD",
289
+ "params": {{
290
+ "required_param_1": "extracted_or_converted_value",
291
+ "required_param_2": "extracted_or_converted_value",
292
+ "optional_param": "value_if_applicable"
293
+ }},
294
+ "missing_required": ["list", "of", "missing", "required", "parameters"],
295
+ "confidence": 0.95
296
+ }}
297
+
298
+ === CRITICAL EXCELLENCE STANDARDS ===
299
+ 1. **ACCURACY FIRST**: Only route to endpoints that exist in the documentation
300
+ 2. **NO FABRICATION**: Never assume parameters or capabilities not explicitly documented
301
+ 3. **COMPLETE UNDERSTANDING**: Demonstrate deep comprehension of both query and endpoints
302
+ 4. **TEMPORAL PRECISION**: Handle all time expressions with mathematical accuracy
303
+ 5. **CONTEXT MASTERY**: Leverage conversation history intelligently and completely
304
+ 6. **MULTILINGUAL COMPETENCY**: Handle Arabic, English, and mixed-language queries flawlessly
305
+ 7. **PARAMETER COMPLETENESS**: Account for every required parameter or clearly identify gaps
306
+ 8. **CONFIDENCE HONESTY**: Provide realistic confidence scores with clear reasoning
307
+ 9. **EDGE CASE AWARENESS**: Consider what could go wrong and prepare for it
308
+ 10. **DECISION TRANSPARENCY**: Make your reasoning process completely clear and auditable
309
+
310
+ === ADVANCED CONTEXT HANDLING ===
311
+ - **Pronoun Resolution**: "Cancel it" → identify what "it" refers to from history
312
+ - **Implicit References**: "Same time tomorrow" → extract time from previous context
313
+ - **Entity Persistence**: Remember patient IDs, appointment IDs across conversation turns
314
+ - **Intent Continuation**: Understand when user is continuing a previous request
315
+ - **Correction Handling**: Detect when user is correcting previous information
316
+
317
+ Think methodically through each phase. Demonstrate deep understanding before making decisions. Be thorough in your analysis and transparent in your reasoning.
318
+ """,
319
+ input_variables=[
320
+ "endpoints_documentation", "user_query", "detected_language",
321
+ "extracted_keywords", "sentiment_analysis", "conversation_history",
322
+ "current_datetime", "timezone", "user_locale"
323
+ ]
324
  )
325
+ # self.router_prompt_template = PromptTemplate(
326
+ # template="""
327
+ # You are a precise API routing assistant. Your job is to analyze user queries and select the correct API endpoint with proper parameters.
328
+
329
+ # === ENDPOINT DOCUMENTATION ===
330
+ # {endpoints_documentation}
331
+
332
+ # === USER REQUEST ANALYSIS ===
333
+ # User Query: {user_query}
334
+ # Language: {detected_language}
335
+ # Keywords: {extracted_keywords}
336
+ # Sentiment: {sentiment_analysis}
337
+
338
+ # === ROUTING PROCESS ===
339
+ # Follow these steps in order:
340
+
341
+ # STEP 1: INTENT ANALYSIS
342
+ # - What is the user trying to accomplish?
343
+ # - What type of operation are they requesting? (create, read, update, delete, search, etc.)
344
+ # - What entity/resource are they working with?
345
+
346
+ # STEP 2: ENDPOINT MATCHING
347
+ # - Review each endpoint in the documentation
348
+ # - Match the user's intent to the endpoint's PURPOSE/DESCRIPTION
349
+ # - Consider the HTTP method (GET for retrieval, POST for creation, etc.)
350
+ # - Verify the endpoint can handle the user's specific request
351
+
352
+ # STEP 3: PARAMETER EXTRACTION
353
+ # - Identify ALL required parameters from the endpoint documentation
354
+ # - Extract parameter values from the user query
355
+ # - Convert data types as needed (dates to ISO 8601, numbers to integers, etc.)
356
+ # - Set appropriate defaults for optional parameters if beneficial
357
+
358
+ # STEP 4: VALIDATION
359
+ # - Ensure ALL required parameters are provided or identified as missing
360
+ # - Verify parameter formats match documentation requirements
361
+ # - Check that the selected endpoint actually solves the user's problem
362
+
363
+ # === RESPONSE FORMAT ===
364
+ # Provide your analysis and decision in this exact JSON structure:
365
+
366
+ # {{
367
+ # "reasoning": {{
368
+ # "user_intent": "Brief description of what the user wants to accomplish",
369
+ # "selected_endpoint": "Why this endpoint was chosen over others",
370
+ # "parameter_mapping": "How user query maps to endpoint parameters"
371
+ # }},
372
+ # "endpoint": "/exact_endpoint_path_from_documentation",
373
+ # "method": "HTTP_METHOD",
374
+ # "params": {{
375
+ # "required_param_1": "extracted_or_converted_value",
376
+ # "required_param_2": "extracted_or_converted_value",
377
+ # "optional_param": "value_if_applicable"
378
+ # }},
379
+ # "missing_required": ["list", "of", "missing", "required", "parameters"],
380
+ # "confidence": 0.95
381
+ # }}
382
+
383
+ # === CRITICAL RULES ===
384
+ # 1. ONLY select endpoints that exist in the provided documentation
385
+ # 2. NEVER fabricate or assume endpoint parameters not in documentation
386
+ # 3. ALL required parameters MUST be included or listed as missing
387
+ # 4. Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
388
+ # 5. If patient_id is required and not provided, add it to missing_required
389
+ # 6. Match endpoints by PURPOSE, not just keywords in the path
390
+ # 7. If multiple endpoints could work, choose the most specific one
391
+ # 8. If no endpoint matches, set endpoint to null and explain in reasoning
392
+
393
+ # === EXAMPLES OF GOOD MATCHING ===
394
+ # - User wants "patient records" → Use patient retrieval endpoint, not general search
395
+ # - User wants to "schedule appointment" → Use appointment creation endpoint
396
+ # - User asks "what appointments today" → Use appointment listing with date filter
397
+ # - User wants to "update medication" → Use medication update endpoint with patient_id
398
+
399
+ # Think step by step and be precise with your endpoint selection and parameter extraction.:""",
400
+ # input_variables=["endpoints_documentation", "user_query", "detected_language",
401
+ # "extracted_keywords", "sentiment_analysis", "conversation_history"]
402
+ # )
403
 
404
  # Conversational response prompt
405
  self.conversation_template = PromptTemplate(