abdibrahem commited on
Commit
ccc1a48
·
1 Parent(s): af7e05e

Update prompt

Browse files
Files changed (1) hide show
  1. main.py +151 -21
main.py CHANGED
@@ -158,7 +158,7 @@ class HealthcareChatbot:
158
  template="""
159
  You are a routing system. Your job is simple:
160
  1. Understand what the user wants
161
- 2. Handle any dates/times in their request
162
  3. Check if any endpoint can do what they want
163
  4. If yes = API_ACTION, if no = CONVERSATION
164
 
@@ -171,6 +171,7 @@ class HealthcareChatbot:
171
  Current Context:
172
  - DateTime: {current_datetime}
173
  - Timezone: {timezone}
 
174
 
175
  ## Step-by-Step Analysis
176
 
@@ -179,51 +180,180 @@ class HealthcareChatbot:
179
  - Identify the exact action or information the user is requesting
180
  - Focus on understanding their underlying need, not just the words
181
 
182
- **STEP 2: Handle Date/Time Processing**
183
- - Identify any temporal expressions in the user query
184
- - Convert relative dates/times using the current context:
185
- * "اليوم" (today) = current date
186
- * "غدا" (tomorrow) = current date + 1 day
187
- * "أمس" (yesterday) = current date - 1 day
188
- * "الأسبوع القادم" (next week) = current date + 7 days
189
- * "بعد ساعتين" (in 2 hours) = current time + 2 hours
190
- * "صباحًا" (morning/AM), "مساءً" (evening/PM)
191
- * "الشهر القادم" (next month) = current date + 1 month
192
- * "الأسبوع الماضي" (last week) = current date - 7 days
193
- - Handle different date formats and languages
194
- - Account for timezone differences
195
- - Convert to ISO 8601 format: YYYY-MM-DDTHH:MM:SS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
  **STEP 3: Find matching endpoint**
198
  - Read each endpoint description in the documentation
199
  - Check if any endpoint's purpose can fulfill what the user wants
200
  - Match based on functionality, not keywords
201
 
202
- **STEP 3: Decision**
203
  - Found matching endpoint = "API_ACTION"
204
  - No matching endpoint = "CONVERSATION"
205
 
206
- **STEP 4: Parameter Extraction (only if API_ACTION)**
207
  - Extract parameter values from user query
208
- - Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS) using the context above
 
209
  - List any missing required parameters
210
 
211
  ## Output Format
212
  {{
213
  "intent": "CONVERSATION|API_ACTION",
214
  "confidence": 0.8,
215
- "reasoning": "User wants: [what user actually needs]. Date/time processing: [any date conversions made]. Found endpoint: [endpoint path and why it matches] OR No endpoint matches this need",
216
  "endpoint": "/exact/endpoint/path",
217
  "method": "GET|POST|PUT|DELETE",
218
  "params": {{}},
219
- "missing_required": []
 
220
  }}
221
 
 
 
 
 
 
 
222
  Now analyze the user query step by step and give me the JSON response.
223
  """,
224
  input_variables=["user_query", "detected_language", "extracted_keywords",
225
- "sentiment_analysis", "endpoints_documentation", "current_datetime", "timezone"]
 
226
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  # self.router_prompt_template = PromptTemplate(
228
  # template="""
229
  # You are a routing system. Your job is simple:
 
158
  template="""
159
  You are a routing system. Your job is simple:
160
  1. Understand what the user wants
161
+ 2. Handle any dates/times in their request with PRECISE calculations
162
  3. Check if any endpoint can do what they want
163
  4. If yes = API_ACTION, if no = CONVERSATION
164
 
 
171
  Current Context:
172
  - DateTime: {current_datetime}
173
  - Timezone: {timezone}
174
+ - Current Day of Week: {current_day_name}
175
 
176
  ## Step-by-Step Analysis
177
 
 
180
  - Identify the exact action or information the user is requesting
181
  - Focus on understanding their underlying need, not just the words
182
 
183
+ **STEP 2: Handle Date/Time Processing with PRECISE Calculations**
184
+ IMPORTANT: Use the current datetime ({current_datetime}) and timezone ({timezone}) for ALL calculations.
185
+
186
+ ### Current Date Reference Points:
187
+ - Today is: {current_datetime}
188
+ - Current day of week: {current_day_name}
189
+ - Current timezone: {timezone}
190
+
191
+ ### Arabic Date/Time Expressions Processing:
192
+ **Basic Relative Dates:**
193
+ - "اليوم" (today) = {current_datetime} date portion
194
+ - "غدا" (tomorrow) = current date + 1 day
195
+ - "أمس" (yesterday) = current date - 1 day
196
+ - "بعد غد" (day after tomorrow) = current date + 2 days
197
+
198
+ **Weekly Expressions - CALCULATE PRECISELY:**
199
+ - "الأسبوع القادم" (next week) = current date + 7 days
200
+ - "الأسبوع الماضي" (last week) = current date - 7 days
201
+
202
+ **Specific Weekday Calculations - MOST IMPORTANT:**
203
+ For expressions like "يوم [weekday] القادم" (next [weekday]):
204
+ 1. Identify the target weekday from Arabic names:
205
+ - الأحد (Sunday) = 0
206
+ - الاثنين (Monday) = 1
207
+ - الثلاثاء (Tuesday) = 2
208
+ - الأربعاء (Wednesday) = 3
209
+ - الخميس (Thursday) = 4
210
+ - الجمعة (Friday) = 5
211
+ - السبت (Saturday) = 6
212
+
213
+ 2. Calculate days to add:
214
+ - Get current weekday number (0=Sunday, 1=Monday, etc.)
215
+ - Target weekday number
216
+ - If target > current: days_to_add = target - current
217
+ - If target <= current: days_to_add = 7 - (current - target)
218
+ - Final date = current_date + days_to_add
219
+
220
+ **Example Calculation:**
221
+ If today is Sunday (June 1, 2025) and user says "يوم الاربع القادم" (next Wednesday):
222
+ - Current weekday: 0 (Sunday)
223
+ - Target weekday: 3 (Wednesday)
224
+ - Days to add: 3 - 0 = 3
225
+ - Result: June 1 + 3 days = June 4, 2025
226
+
227
+ **Monthly/Yearly Expressions:**
228
+ - "الشهر القادم" (next month) = add 1 month to current date
229
+ - "الشهر الماضي" (last month) = subtract 1 month from current date
230
+ - "السنة القادمة" (next year) = add 1 year to current date
231
+
232
+ **Time Expressions:**
233
+ - "صباحًا" (morning/AM) = 09:00 if no specific time given
234
+ - "مساءً" (evening/PM) = 18:00 if no specific time given
235
+ - "ظهرًا" (noon) = 12:00
236
+ - "منتصف الليل" (midnight) = 00:00
237
+ - "بعد ساعتين" (in 2 hours) = current time + 2 hours
238
+ - "قبل ساعة" (1 hour ago) = current time - 1 hour
239
+
240
+ **Date Format Output:**
241
+ - Always convert final calculated date to ISO 8601 format: YYYY-MM-DDTHH:MM:SS
242
+ - Include timezone offset if available
243
+ - For date-only expressions, use 00:00:00 as default time
244
 
245
  **STEP 3: Find matching endpoint**
246
  - Read each endpoint description in the documentation
247
  - Check if any endpoint's purpose can fulfill what the user wants
248
  - Match based on functionality, not keywords
249
 
250
+ **STEP 4: Decision**
251
  - Found matching endpoint = "API_ACTION"
252
  - No matching endpoint = "CONVERSATION"
253
 
254
+ **STEP 5: Parameter Extraction (only if API_ACTION)**
255
  - Extract parameter values from user query
256
+ - Use the CALCULATED dates/times from Step 2
257
+ - Convert all dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
258
  - List any missing required parameters
259
 
260
  ## Output Format
261
  {{
262
  "intent": "CONVERSATION|API_ACTION",
263
  "confidence": 0.8,
264
+ "reasoning": "User wants: [what user actually needs]. Date/time processing: [show exact calculation: current date + X days = final date]. Found endpoint: [endpoint path and why it matches] OR No endpoint matches this need",
265
  "endpoint": "/exact/endpoint/path",
266
  "method": "GET|POST|PUT|DELETE",
267
  "params": {{}},
268
+ "missing_required": [],
269
+ "calculated_datetime": "YYYY-MM-DDTHH:MM:SS (if date/time was processed)"
270
  }}
271
 
272
+ ## CRITICAL REMINDERS:
273
+ 1. ALWAYS use the provided current_datetime ({current_datetime}) as your base for calculations
274
+ 2. For "next weekday" expressions, calculate the exact number of days to add
275
+ 3. Show your calculation work in the reasoning field
276
+ 4. Double-check weekday numbers: Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6
277
+
278
  Now analyze the user query step by step and give me the JSON response.
279
  """,
280
  input_variables=["user_query", "detected_language", "extracted_keywords",
281
+ "sentiment_analysis", "endpoints_documentation", "current_datetime",
282
+ "timezone", "current_day_name"]
283
  )
284
+ ''' Second Approach '''
285
+ # self.router_prompt_template = PromptTemplate(
286
+ # template="""
287
+ # You are a routing system. Your job is simple:
288
+ # 1. Understand what the user wants
289
+ # 2. Handle any dates/times in their request
290
+ # 3. Check if any endpoint can do what they want
291
+ # 4. If yes = API_ACTION, if no = CONVERSATION
292
+
293
+ # ## Available API Endpoints Documentation
294
+ # {endpoints_documentation}
295
+
296
+ # ## User Query to Analyze
297
+ # Query: "{user_query}"
298
+ # Language: {detected_language}
299
+ # Current Context:
300
+ # - DateTime: {current_datetime}
301
+ # - Timezone: {timezone}
302
+
303
+ # ## Step-by-Step Analysis
304
+
305
+ # **STEP 1: What does the user want?**
306
+ # - If query is in Arabic, translate it to English first
307
+ # - Identify the exact action or information the user is requesting
308
+ # - Focus on understanding their underlying need, not just the words
309
+
310
+ # **STEP 2: Handle Date/Time Processing**
311
+ # - Identify any temporal expressions in the user query
312
+ # - Convert relative dates/times using the current context:
313
+ # * "اليوم" (today) = current date
314
+ # * "غدا" (tomorrow) = current date + 1 day
315
+ # * "أمس" (yesterday) = current date - 1 day
316
+ # * "الأسبوع القادم" (next week) = current date + 7 days
317
+ # * "بعد ساعتين" (in 2 hours) = current time + 2 hours
318
+ # * "صباحًا" (morning/AM), "مساءً" (evening/PM)
319
+ # * "الشهر القادم" (next month) = current date + 1 month
320
+ # * "الأسبوع الماضي" (last week) = current date - 7 days
321
+ # - Handle different date formats and languages
322
+ # - Account for timezone differences
323
+ # - Convert to ISO 8601 format: YYYY-MM-DDTHH:MM:SS
324
+
325
+ # **STEP 3: Find matching endpoint**
326
+ # - Read each endpoint description in the documentation
327
+ # - Check if any endpoint's purpose can fulfill what the user wants
328
+ # - Match based on functionality, not keywords
329
+
330
+ # **STEP 3: Decision**
331
+ # - Found matching endpoint = "API_ACTION"
332
+ # - No matching endpoint = "CONVERSATION"
333
+
334
+ # **STEP 4: Parameter Extraction (only if API_ACTION)**
335
+ # - Extract parameter values from user query
336
+ # - Convert dates/times to ISO 8601 format (YYYY-MM-DDTHH:MM:SS) using the context above
337
+ # - List any missing required parameters
338
+
339
+ # ## Output Format
340
+ # {{
341
+ # "intent": "CONVERSATION|API_ACTION",
342
+ # "confidence": 0.8,
343
+ # "reasoning": "User wants: [what user actually needs]. Date/time processing: [any date conversions made]. Found endpoint: [endpoint path and why it matches] OR No endpoint matches this need",
344
+ # "endpoint": "/exact/endpoint/path",
345
+ # "method": "GET|POST|PUT|DELETE",
346
+ # "params": {{}},
347
+ # "missing_required": []
348
+ # }}
349
+
350
+ # Now analyze the user query step by step and give me the JSON response.
351
+ # """,
352
+ # input_variables=["user_query", "detected_language", "extracted_keywords",
353
+ # "sentiment_analysis", "endpoints_documentation", "current_datetime", "timezone"]
354
+ # )
355
+
356
+ ''' First Approach '''
357
  # self.router_prompt_template = PromptTemplate(
358
  # template="""
359
  # You are a routing system. Your job is simple: