-
-
Notifications
You must be signed in to change notification settings - Fork 122
feat: implement medicine and BP handlers with clean history and intent fix #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||
| import re | ||||||
| import sqlite3 | ||||||
| from db.db import open_db | ||||||
|
|
||||||
| def handle(query, user_context): | ||||||
| """ | ||||||
| Parses blood pressure readings (systolic/diastolic) from the user query, | ||||||
| validates the numeric ranges, and records the data into the database logs. | ||||||
| """ | ||||||
| db = open_db() | ||||||
| try: | ||||||
|
|
||||||
| match = re.search(r'(\d{1,3})\s*(?:/|over|-)\s*(\d{1,3})', query, re.IGNORECASE) | ||||||
| if not match: | ||||||
| return "Please provide BP in format like '120/80' or '120 over 80'." | ||||||
|
|
||||||
| systolic = int(match.group(1)) | ||||||
| diastolic = int(match.group(2)) | ||||||
|
|
||||||
| if not (70 <= systolic <= 250 and 40 <= diastolic <= 150): | ||||||
| return "BP values out of valid range. Please check." | ||||||
|
|
||||||
| week = user_context.get('week_number', 1) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Inconsistent user context key across handlers. Line 24 uses 🔧 Proposed fix for context key consistency- week = user_context.get('week_number', 1)
+ week = user_context.get('current_week', 1)Apply the same fix to Backend/agent/handlers/medicine.py line 19 and Backend/agent/handlers/insights.py line 12. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
|
|
||||||
| db.execute( | ||||||
| 'INSERT INTO blood_pressure_logs (week_number, systolic, diastolic, time) VALUES (?, ?, ?, datetime("now"))', | ||||||
| (week, systolic, diastolic) | ||||||
| ) | ||||||
| db.commit() | ||||||
| return f"Recorded BP: {systolic}/{diastolic} for week {week}." | ||||||
| except sqlite3.Error as e: | ||||||
| return f"Database error: {str(e)}" | ||||||
| except Exception as e: | ||||||
| return f"Unexpected error: {str(e)}" | ||||||
| finally: | ||||||
| db.close() | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import re | ||
| import sqlite3 | ||
| from db.db import open_db | ||
|
|
||
| def handle(query, user_context): | ||
| """ | ||
| Extracts medicine name, dosage, and time from user query using regex | ||
| and persists the record into the weekly_medicine database table. | ||
| """ | ||
| db = open_db() | ||
| try: | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| name_match = re.search(r'(?:took|taking|take)\s+([\w\s\-()]+?)(?:\s+\d|\s+at|$)', query, re.IGNORECASE) | ||
| name = name_match.group(1).strip() if name_match else "Medicine" | ||
|
|
||
| dose_match = re.search(r'(\d+\s*(?:tablet|pill|capsule|mg|ml|unit)s?)', query, re.IGNORECASE) | ||
| dose = dose_match.group(1).strip() if dose_match else "1 unit" | ||
|
|
||
| time_match = re.search(r'at\s+(\d{1,2}(?::\d{2})?\s*(?:am|pm)?)', query, re.IGNORECASE) | ||
| time = time_match.group(1) if time_match else "00:00" | ||
|
|
||
| week = user_context.get('week_number', 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Same context key inconsistency. This line has the same issue as Backend/agent/handlers/blood_pressure.py line 24. Use 🤖 Prompt for AI Agents |
||
|
|
||
| db.execute( | ||
| 'INSERT INTO weekly_medicine (week_number, name, dose, time) VALUES (?, ?, ?, ?)', | ||
| (week, name, dose, time) | ||
| ) | ||
| db.commit() | ||
| return f"Logged {name} ({dose}) at {time} for week {week}." | ||
| except sqlite3.Error as e: | ||
| return f"Database error: {str(e)}" | ||
| except Exception as e: | ||
| return f"Unexpected error: {str(e)}" | ||
| finally: | ||
| db.close() | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||
| # agent/intent.py | ||||||||||
|
|
||||||||||
| def classify_intent(query: str) -> str: | ||||||||||
| if not query or not isinstance(query, str): | ||||||||||
| return "general" | ||||||||||
|
|
@@ -10,6 +10,10 @@ def classify_intent(query: str) -> str: | |||||||||
| return "weight" | ||||||||||
| elif "symptom" in query: | ||||||||||
| return "symptoms" | ||||||||||
| elif "medicine" in query or "took" in query or "tablet" in query or "pill" in query: | ||||||||||
| return "medicine" | ||||||||||
|
Comment on lines
+16
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The keyword "took" is too generic and may cause false positives. The word "took" appears in many non-medicine contexts (e.g., "I took a walk", "I took notes"). Consider requiring a combination of keywords or using a regex pattern that looks for medicine-related context. Suggested improvement- elif "medicine" in query or "took" in query or "tablet" in query or "pill" in query:
+ elif "medicine" in query or "tablet" in query or "pill" in query or ("took" in query and any(kw in query for kw in ["mg", "tablet", "pill", "capsule", "dose"])):
return "medicine"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| elif "bp" in query or "blood pressure" in query or "/" in query: | ||||||||||
| return "blood_pressure" | ||||||||||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Overly broad "/" pattern will cause false positives. The "/" character alone as a trigger for Additionally, the ordering creates an issue: a query like "I took my BP 120/80" would be classified as "medicine" (due to "took") rather than "blood_pressure". Proposed fix: Use a more specific BP pattern- elif "bp" in query or "blood pressure" in query or "/" in query:
+ elif "bp" in query or "blood pressure" in query or re.search(r'\d{2,3}\s*/\s*\d{2,3}', query):
return "blood_pressure"This requires adding
🤖 Prompt for AI Agents |
||||||||||
| elif "vaccine" in query or "guideline" in query or "what tests" in query or "recommend" in query: | ||||||||||
| return "guidelines" | ||||||||||
| return "general" | ||||||||||
| return "general" | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.