Spaces:
Runtime error
Runtime error
Ajey95
commited on
Commit
ยท
55fd722
1
Parent(s):
b501c05
Fix: chat_history addition
Browse files- agents/drug_info_agent.py +45 -19
agents/drug_info_agent.py
CHANGED
|
@@ -2,57 +2,83 @@
|
|
| 2 |
"""
|
| 3 |
Drug Information Agent - Handles drug-related queries using Generative AI.
|
| 4 |
"""
|
|
|
|
|
|
|
| 5 |
import re
|
| 6 |
from .agent_helpers import format_history_for_prompt
|
|
|
|
| 7 |
class DrugInfoAgent:
|
| 8 |
def __init__(self, gemini_model=None):
|
| 9 |
"""
|
| 10 |
Initializes the agent with the Gemini model.
|
| 11 |
-
|
| 12 |
-
Args:
|
| 13 |
-
gemini_model: An instance of the Gemini model client.
|
| 14 |
"""
|
| 15 |
self.model = gemini_model
|
| 16 |
|
| 17 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
Processes a query to retrieve information about a specific drug.
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
query (str): The user's full query (e.g., "Tell me about Metformin").
|
| 23 |
-
file_context (str): Optional context from uploaded files.
|
| 24 |
-
chat_history (list): The history of the conversation.
|
| 25 |
-
|
| 26 |
-
Returns:
|
| 27 |
-
dict: A dictionary containing the response message and agent metadata.
|
| 28 |
"""
|
| 29 |
if not self.model:
|
| 30 |
return {'message': "๐ The pharmacy database is offline! The Gemini API key is missing.", 'agent_used': 'drug_info', 'status': 'error_no_api_key'}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
history_for_prompt = format_history_for_prompt(chat_history)
|
| 33 |
|
| 34 |
-
prompt = f"""You are a cautious AI Pharmacist Tutor
|
| 35 |
|
| 36 |
**CRITICAL SAFETY INSTRUCTION:** START EVERY RESPONSE with this disclaimer: "โ ๏ธ **Disclaimer:** This information is for educational purposes ONLY and is not a substitute for professional medical advice."
|
| 37 |
|
| 38 |
-
Your reasoning process
|
| 39 |
-
1. Analyze the CONVERSATION HISTORY and
|
| 40 |
-
2.
|
|
|
|
| 41 |
|
| 42 |
CONVERSATION HISTORY:
|
| 43 |
{history_for_prompt}
|
| 44 |
CURRENT QUESTION:
|
| 45 |
User: {query}
|
| 46 |
|
| 47 |
-
Provide a structured summary including: Therapeutic Class, MOA, Indications, Side Effects, and Warnings. DO NOT provide specific dosages.
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
response = self.model.generate_content(prompt)
|
| 51 |
return {'message': response.text, 'agent_used': 'drug_info', 'status': 'success'}
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Drug Info Agent Error: {e}")
|
| 54 |
-
return {'message': f"Sorry, I couldn't access the drug database.
|
| 55 |
-
|
| 56 |
# def process_query(self, query: str, file_context: str = "", chat_history: list = None):
|
| 57 |
# """
|
| 58 |
# Processes a query to retrieve information about a specific drug.
|
|
|
|
| 2 |
"""
|
| 3 |
Drug Information Agent - Handles drug-related queries using Generative AI.
|
| 4 |
"""
|
| 5 |
+
# agents/drug_info_agent.py
|
| 6 |
+
|
| 7 |
import re
|
| 8 |
from .agent_helpers import format_history_for_prompt
|
| 9 |
+
|
| 10 |
class DrugInfoAgent:
|
| 11 |
def __init__(self, gemini_model=None):
|
| 12 |
"""
|
| 13 |
Initializes the agent with the Gemini model.
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
self.model = gemini_model
|
| 16 |
|
| 17 |
+
def _extract_drug_name(self, query: str) -> str:
|
| 18 |
+
"""
|
| 19 |
+
A simple helper to extract the drug name from the user's query.
|
| 20 |
+
This is a crucial step for this agent's reliability.
|
| 21 |
+
"""
|
| 22 |
+
# Remove common phrases and get the potential drug name
|
| 23 |
+
query_lower = query.lower()
|
| 24 |
+
patterns = [
|
| 25 |
+
"what are the side effects of", "tell me about", "information on",
|
| 26 |
+
"info on", "about the drug", "what is", "about"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
cleaned_query = query_lower
|
| 30 |
+
for p in patterns:
|
| 31 |
+
cleaned_query = cleaned_query.replace(p, "")
|
| 32 |
+
|
| 33 |
+
# A simple assumption: the first significant word is the drug name.
|
| 34 |
+
# This can be improved with more advanced NLP in the future.
|
| 35 |
+
match = re.search(r'\b[a-zA-Z0-9]+\b', cleaned_query)
|
| 36 |
+
if match:
|
| 37 |
+
return match.group(0).title()
|
| 38 |
+
return ""
|
| 39 |
+
|
| 40 |
+
def process_query(self, query: str, file_context: str = "", chat_history: list = None):
|
| 41 |
"""
|
| 42 |
Processes a query to retrieve information about a specific drug.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
if not self.model:
|
| 45 |
return {'message': "๐ The pharmacy database is offline! The Gemini API key is missing.", 'agent_used': 'drug_info', 'status': 'error_no_api_key'}
|
| 46 |
|
| 47 |
+
# --- RESTORED LOGIC ---
|
| 48 |
+
drug_name = self._extract_drug_name(query)
|
| 49 |
+
if not drug_name:
|
| 50 |
+
# If the history also doesn't provide context, ask the user.
|
| 51 |
+
if "drug" not in str(chat_history).lower():
|
| 52 |
+
return {
|
| 53 |
+
'message': "Please tell me which drug you want to know about! For example, try 'info on Paracetamol'.",
|
| 54 |
+
'agent_used': 'drug_info',
|
| 55 |
+
'status': 'error_no_topic'
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
history_for_prompt = format_history_for_prompt(chat_history)
|
| 59 |
|
| 60 |
+
prompt = f"""You are a cautious AI Pharmacist Tutor providing educational information like ancient india's Chanakya.
|
| 61 |
|
| 62 |
**CRITICAL SAFETY INSTRUCTION:** START EVERY RESPONSE with this disclaimer: "โ ๏ธ **Disclaimer:** This information is for educational purposes ONLY and is not a substitute for professional medical advice."
|
| 63 |
|
| 64 |
+
Your reasoning process is:
|
| 65 |
+
1. Analyze the CONVERSATION HISTORY and CURRENT QUESTION to identify the specific drug being discussed. The primary drug to focus on is: **{drug_name}**.
|
| 66 |
+
2. If the user asks a follow-up (e.g., "what about its dosage forms?"), answer that specific question in the context of the drug.
|
| 67 |
+
3. Provide a structured summary.
|
| 68 |
|
| 69 |
CONVERSATION HISTORY:
|
| 70 |
{history_for_prompt}
|
| 71 |
CURRENT QUESTION:
|
| 72 |
User: {query}
|
| 73 |
|
| 74 |
+
Provide a structured summary for the drug **{drug_name}** including: Therapeutic Class, Mechanism of Action (MOA), Indications, Side Effects, and Warnings. DO NOT provide specific dosages.
|
| 75 |
"""
|
| 76 |
try:
|
| 77 |
response = self.model.generate_content(prompt)
|
| 78 |
return {'message': response.text, 'agent_used': 'drug_info', 'status': 'success'}
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Drug Info Agent Error: {e}")
|
| 81 |
+
return {'message': f"Sorry, I couldn't access the drug database. An error occurred.", 'agent_used': 'drug_info', 'status': 'error_api_call'}
|
|
|
|
| 82 |
# def process_query(self, query: str, file_context: str = "", chat_history: list = None):
|
| 83 |
# """
|
| 84 |
# Processes a query to retrieve information about a specific drug.
|