Spaces:
Runtime error
Runtime error
Upload 15 files
Browse files- chat_sessions.db +2 -2
- chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/data_level0.bin +3 -0
- chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/header.bin +3 -0
- chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/index_metadata.pickle +3 -0
- chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/length.bin +3 -0
- chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/link_lists.bin +3 -0
- chroma_db/chroma.sqlite3 +2 -2
- config.py +4 -4
- rag_system.py +291 -150
chat_sessions.db
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2fdb10530d430ae2008feae769e1c6b580b1d7aef113e7a58b40c50002d55dbc
|
| 3 |
+
size 520192
|
chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a8513e9a00214034d1311a61fdb2074b349fb99d25939eebabd9904fa5c7e94
|
| 3 |
+
size 16760000
|
chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7268d4f199f12b83f0f7d7c16866619e34bddc0d2539c3ba13ed29881182d127
|
| 3 |
+
size 100
|
chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/index_metadata.pickle
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bbc7539f3e47e69ce48b558b36b8900e61ec8412e7878e90b092bd5c1617113c
|
| 3 |
+
size 121562
|
chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:90b1c4f6ebf674ed09cada19da2cc417fa05733b24ffd46a6991ae3c8c72a725
|
| 3 |
+
size 40000
|
chroma_db/6a785c03-cb3a-4ae8-871e-27aba60c6344/link_lists.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:54e0bbf6c068cc9ed7bff91f84710464dfbf1455d5668bf2c8b96a8a341e68d2
|
| 3 |
+
size 26376
|
chroma_db/chroma.sqlite3
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e29ff0df6adc4b85c6b955c9beff7f5bb1bd32342e7b6c6f7ffb07b501ba5ca
|
| 3 |
+
size 59514880
|
config.py
CHANGED
|
@@ -24,7 +24,7 @@ load_dotenv()
|
|
| 24 |
|
| 25 |
# Hugging Face Configuration
|
| 26 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 27 |
-
HF_DATASET_NAME = "
|
| 28 |
|
| 29 |
# Groq Configuration
|
| 30 |
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
|
@@ -36,12 +36,12 @@ EMBEDDING_DIMENSION = 384
|
|
| 36 |
|
| 37 |
# ChromaDB Configuration
|
| 38 |
CHROMA_PERSIST_DIR = "./chroma_db"
|
| 39 |
-
CHROMA_COLLECTION_NAME = "
|
| 40 |
|
| 41 |
# FastAPI Configuration
|
| 42 |
-
API_TITLE = "
|
| 43 |
API_VERSION = "1.0.0"
|
| 44 |
-
API_DESCRIPTION = "RAG-based
|
| 45 |
HOST = "0.0.0.0"
|
| 46 |
PORT = 8000
|
| 47 |
|
|
|
|
| 24 |
|
| 25 |
# Hugging Face Configuration
|
| 26 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 27 |
+
HF_DATASET_NAME = "Amod/mental_health_counseling_conversations"
|
| 28 |
|
| 29 |
# Groq Configuration
|
| 30 |
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
|
|
|
| 36 |
|
| 37 |
# ChromaDB Configuration
|
| 38 |
CHROMA_PERSIST_DIR = "./chroma_db"
|
| 39 |
+
CHROMA_COLLECTION_NAME = "mental_health_counseling"
|
| 40 |
|
| 41 |
# FastAPI Configuration
|
| 42 |
+
API_TITLE = "Mental Health Counseling Chatbot API"
|
| 43 |
API_VERSION = "1.0.0"
|
| 44 |
+
API_DESCRIPTION = "RAG-based mental health counseling chatbot using Amod/mental_health_counseling_conversations data"
|
| 45 |
HOST = "0.0.0.0"
|
| 46 |
PORT = 8000
|
| 47 |
|
rag_system.py
CHANGED
|
@@ -194,7 +194,7 @@ class RAGSystem:
|
|
| 194 |
|
| 195 |
documents.append(chunk)
|
| 196 |
metadatas.append({
|
| 197 |
-
"source": "
|
| 198 |
"original_index": start_idx + idx,
|
| 199 |
"chunk_index": chunk_idx,
|
| 200 |
"dataset": HF_DATASET_NAME,
|
|
@@ -221,15 +221,11 @@ class RAGSystem:
|
|
| 221 |
# Try to extract question and answer content
|
| 222 |
content_parts = []
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
if "question_body" in item and item["question_body"]:
|
| 229 |
-
content_parts.append(f"Question Body: {item['question_body']}")
|
| 230 |
-
|
| 231 |
# Extract answers (multiple answers possible)
|
| 232 |
-
if "
|
| 233 |
for i, answer in enumerate(item["answers"]):
|
| 234 |
if isinstance(answer, dict) and "body" in answer:
|
| 235 |
content_parts.append(f"Answer {i+1}: {answer['body']}")
|
|
@@ -306,7 +302,7 @@ class RAGSystem:
|
|
| 306 |
|
| 307 |
if not search_results:
|
| 308 |
return {
|
| 309 |
-
"answer": "I couldn't
|
| 310 |
"sources": [],
|
| 311 |
"confidence": 0.0
|
| 312 |
}
|
|
@@ -418,24 +414,32 @@ class RAGSystem:
|
|
| 418 |
try:
|
| 419 |
# Count tokens for the entire request
|
| 420 |
prompt_template = """
|
| 421 |
-
You are a
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
"""
|
| 440 |
|
| 441 |
# Estimate total tokens
|
|
@@ -666,121 +670,202 @@ If you have a specific legal question, please try rephrasing it or contact a loc
|
|
| 666 |
return []
|
| 667 |
|
| 668 |
def _simplify_search_terms(self, question: str) -> List[str]:
|
| 669 |
-
"""Simplify search terms for broader search"""
|
| 670 |
-
# Remove common legal terms that might be too specific
|
| 671 |
question_lower = question.lower()
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
if "
|
| 677 |
-
|
| 678 |
-
if "
|
| 679 |
-
|
| 680 |
-
if "
|
| 681 |
-
|
| 682 |
-
if "
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
|
| 691 |
def _generate_search_variations(self, question: str) -> List[str]:
|
| 692 |
-
"""Generate multiple search query variations"""
|
| 693 |
variations = [question]
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
|
|
|
| 697 |
variations.extend([
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
])
|
| 706 |
-
|
| 707 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
variations.extend([
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
|
|
|
|
|
|
|
|
|
| 713 |
])
|
| 714 |
-
|
| 715 |
-
return variations[:8] #
|
|
|
|
| 716 |
|
| 717 |
def _extract_legal_concepts(self, question: str) -> List[str]:
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
]
|
| 727 |
-
|
| 728 |
question_lower = question.lower()
|
| 729 |
-
for term in
|
| 730 |
if term in question_lower:
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
def _is_legal_query(self, question: str) -> bool:
|
| 736 |
-
"""Check if the query is asking for legal information"""
|
| 737 |
-
question_lower = question.lower().strip()
|
| 738 |
-
|
| 739 |
-
# Legal keywords that indicate legal questions
|
| 740 |
-
legal_keywords = [
|
| 741 |
-
"law", "legal", "rights", "liability", "sue", "sued", "court", "judge",
|
| 742 |
-
"attorney", "lawyer", "criminal", "civil", "penalty", "punishment", "fine",
|
| 743 |
-
"jail", "prison", "arrest", "charge", "conviction", "sentence", "damages",
|
| 744 |
-
"compensation", "contract", "agreement", "lease", "rent", "eviction",
|
| 745 |
-
"divorce", "custody", "inheritance", "will", "trust", "property", "real estate",
|
| 746 |
-
"employment", "workplace", "discrimination", "harassment", "injury", "accident",
|
| 747 |
-
"insurance", "claim", "settlement", "mediation", "arbitration", "appeal",
|
| 748 |
-
"drunk driving", "dui", "dwi", "traffic", "speeding", "reckless", "negligence"
|
| 749 |
-
]
|
| 750 |
|
| 751 |
-
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
if keyword in question_lower:
|
| 754 |
return True
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
question_words = ["
|
| 758 |
-
has_question_word = any(word
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 766 |
]
|
| 767 |
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
# More permissive: if it has a question word and seems like it could be legal
|
| 771 |
-
if has_question_word:
|
| 772 |
-
# Check for words that suggest legal topics
|
| 773 |
-
topic_indicators = [
|
| 774 |
-
"penalties", "consequences", "punishment", "fine", "jail", "prison",
|
| 775 |
-
"arrest", "charge", "conviction", "sentence", "damages", "compensation",
|
| 776 |
-
"rights", "obligations", "responsibilities", "liability", "fault",
|
| 777 |
-
"accident", "injury", "damage", "property", "money", "cost", "time"
|
| 778 |
-
]
|
| 779 |
-
|
| 780 |
-
if any(indicator in question_lower for indicator in topic_indicators):
|
| 781 |
return True
|
| 782 |
-
|
| 783 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
|
| 785 |
def _is_conversational_query(self, question: str) -> bool:
|
| 786 |
"""Detect if the query is conversational and doesn't need legal document search"""
|
|
@@ -817,43 +902,98 @@ If you have a specific legal question, please try rephrasing it or contact a loc
|
|
| 817 |
question_lower = question.lower().strip()
|
| 818 |
|
| 819 |
if question_lower in ["hi", "hello", "hey"]:
|
| 820 |
-
return """Hello! I'm your
|
| 821 |
|
| 822 |
-
•
|
| 823 |
-
•
|
| 824 |
-
•
|
| 825 |
-
•
|
| 826 |
-
•
|
| 827 |
-
•
|
|
|
|
|
|
|
| 828 |
|
| 829 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 830 |
|
| 831 |
elif "how can you help" in question_lower or "what can you do" in question_lower:
|
| 832 |
-
return """I'm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 833 |
|
| 834 |
-
|
| 835 |
-
• Providing information about legal principles and procedures
|
| 836 |
-
• Explaining legal concepts in understandable terms
|
| 837 |
-
• Citing relevant legal sources and precedents
|
| 838 |
-
• Offering general legal guidance (though not specific legal advice)
|
| 839 |
|
| 840 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
|
| 842 |
elif "who are you" in question_lower or "what are you" in question_lower:
|
| 843 |
-
return """I'm an AI-powered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
• Provide information about laws and regulations
|
| 848 |
-
• Help you understand legal procedures
|
| 849 |
-
• Cite relevant legal sources
|
| 850 |
|
| 851 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 852 |
|
| 853 |
else:
|
| 854 |
-
return """Hello! I
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
|
| 856 |
-
What
|
| 857 |
|
| 858 |
def _filter_relevant_results(self, search_results: List[Dict[str, Any]], question: str) -> List[Dict[str, Any]]:
|
| 859 |
"""Filter search results for relevance to the question"""
|
|
@@ -882,10 +1022,11 @@ What legal question would you like me to help you with?"""
|
|
| 882 |
|
| 883 |
# Check if content contains relevant legal terms
|
| 884 |
legal_terms = [
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
|
|
|
| 889 |
|
| 890 |
has_legal_content = any(term in content for term in legal_terms)
|
| 891 |
|
|
|
|
| 194 |
|
| 195 |
documents.append(chunk)
|
| 196 |
metadatas.append({
|
| 197 |
+
"source": "mental_health_counseling_conversations",
|
| 198 |
"original_index": start_idx + idx,
|
| 199 |
"chunk_index": chunk_idx,
|
| 200 |
"dataset": HF_DATASET_NAME,
|
|
|
|
| 221 |
# Try to extract question and answer content
|
| 222 |
content_parts = []
|
| 223 |
|
| 224 |
+
if "Context" in item and item["Context"]:
|
| 225 |
+
content_parts.append(f"Question Body: {item['Context']}")
|
| 226 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
# Extract answers (multiple answers possible)
|
| 228 |
+
if "Response" in item and isinstance(item["Response"], list):
|
| 229 |
for i, answer in enumerate(item["answers"]):
|
| 230 |
if isinstance(answer, dict) and "body" in answer:
|
| 231 |
content_parts.append(f"Answer {i+1}: {answer['body']}")
|
|
|
|
| 302 |
|
| 303 |
if not search_results:
|
| 304 |
return {
|
| 305 |
+
"answer": "I couldn't help in this case, please consult a mental health professional.",
|
| 306 |
"sources": [],
|
| 307 |
"confidence": 0.0
|
| 308 |
}
|
|
|
|
| 414 |
try:
|
| 415 |
# Count tokens for the entire request
|
| 416 |
prompt_template = """
|
| 417 |
+
You are a compassionate mental health supporter with training in anxiety, depression, trauma, and coping strategies.
|
| 418 |
+
Use the following evidence-based psychological information to address the user’s concerns with care and accuracy.
|
| 419 |
+
|
| 420 |
+
Therapeutic Context:
|
| 421 |
+
{context}
|
| 422 |
+
|
| 423 |
+
User’s Concern: {question}
|
| 424 |
+
|
| 425 |
+
Guidelines for Response:
|
| 426 |
+
|
| 427 |
+
Provide empathetic, evidence-based support rooted in the context (e.g., CBT, DBT, or mindfulness principles).
|
| 428 |
+
|
| 429 |
+
If context is insufficient, acknowledge limits and offer general wellness strategies (e.g., grounding techniques, self-care tips).
|
| 430 |
+
|
| 431 |
+
Cite sources when referencing specific therapies or studies (e.g., "APA guidelines suggest...").
|
| 432 |
+
|
| 433 |
+
For symptom-related questions, differentiate between mild, moderate, and severe cases (e.g., situational stress vs. clinical anxiety).
|
| 434 |
+
|
| 435 |
+
Use clear, stigma-free language while maintaining clinical accuracy.
|
| 436 |
+
|
| 437 |
+
When discussing crises, emphasize jurisdictional resources (e.g., "Laws/programs vary by location, but here’s how to find local help...").
|
| 438 |
+
|
| 439 |
+
Prioritize validation and education—not just information.
|
| 440 |
+
|
| 441 |
+
Example Response:
|
| 442 |
+
"I hear you’re feeling overwhelmed. Based on [Context Source], deep breathing exercises can help calm acute anxiety. However, if these feelings persist for weeks, it might reflect generalized anxiety disorder (GAD). Always consult a licensed therapist for personalized care. Would you like crisis hotline numbers or a step-by-step grounding technique?
|
| 443 |
"""
|
| 444 |
|
| 445 |
# Estimate total tokens
|
|
|
|
| 670 |
return []
|
| 671 |
|
| 672 |
def _simplify_search_terms(self, question: str) -> List[str]:
|
|
|
|
|
|
|
| 673 |
question_lower = question.lower()
|
| 674 |
+
|
| 675 |
+
# Extract key mental health concepts
|
| 676 |
+
mental_health_keywords = []
|
| 677 |
+
|
| 678 |
+
if "anxiety" in question_lower or "panic" in question_lower:
|
| 679 |
+
mental_health_keywords.extend(["anxiety", "panic", "stress", "mental health"])
|
| 680 |
+
if "depression" in question_lower or "sad" in question_lower:
|
| 681 |
+
mental_health_keywords.extend(["depression", "mood", "mental health"])
|
| 682 |
+
if "trauma" in question_lower or "ptsd" in question_lower:
|
| 683 |
+
mental_health_keywords.extend(["trauma", "PTSD", "coping", "mental health"])
|
| 684 |
+
if "therapy" in question_lower or "counseling" in question_lower:
|
| 685 |
+
mental_health_keywords.extend(["therapy", "counseling", "treatment"])
|
| 686 |
+
if "stress" in question_lower or "overwhelmed" in question_lower:
|
| 687 |
+
mental_health_keywords.extend(["stress", "coping", "mental health"])
|
| 688 |
+
|
| 689 |
+
# Emotional state indicators
|
| 690 |
+
emotional_terms = ["feel", "feeling", "experience", "struggling"]
|
| 691 |
+
if any(term in question_lower for term in emotional_terms):
|
| 692 |
+
mental_health_keywords.extend(["emotions", "feelings", "mental health"])
|
| 693 |
+
|
| 694 |
+
# If no specific keywords found, use general terms
|
| 695 |
+
if not mental_health_keywords:
|
| 696 |
+
mental_health_keywords = ["mental health", "well-being", "emotional support"]
|
| 697 |
+
|
| 698 |
+
return list(set(mental_health_keywords)) # Remove duplicates
|
| 699 |
|
| 700 |
def _generate_search_variations(self, question: str) -> List[str]:
|
|
|
|
| 701 |
variations = [question]
|
| 702 |
+
question_lower = question.lower()
|
| 703 |
+
|
| 704 |
+
# Anxiety-specific variations
|
| 705 |
+
if "anxiety" in question_lower or "panic" in question_lower:
|
| 706 |
variations.extend([
|
| 707 |
+
"coping strategies for anxiety",
|
| 708 |
+
"how to calm anxiety attacks",
|
| 709 |
+
"difference between anxiety and panic attacks",
|
| 710 |
+
"best therapy approaches for anxiety",
|
| 711 |
+
"natural remedies for anxiety relief",
|
| 712 |
+
"when to seek help for anxiety",
|
| 713 |
+
"anxiety self-help techniques"
|
| 714 |
+
])
|
| 715 |
+
|
| 716 |
+
# Depression-specific variations
|
| 717 |
+
elif "depression" in question_lower or "sad" in question_lower:
|
| 718 |
+
variations.extend([
|
| 719 |
+
"signs of clinical depression",
|
| 720 |
+
"self-care for depression",
|
| 721 |
+
"therapy options for depression",
|
| 722 |
+
"how to support someone with depression",
|
| 723 |
+
"difference between sadness and depression",
|
| 724 |
+
"depression coping skills",
|
| 725 |
+
"when depression requires medication"
|
| 726 |
])
|
| 727 |
+
|
| 728 |
+
# Trauma-specific variations
|
| 729 |
+
elif "trauma" in question_lower or "ptsd" in question_lower:
|
| 730 |
+
variations.extend([
|
| 731 |
+
"healing from trauma strategies",
|
| 732 |
+
"PTSD symptoms and treatment",
|
| 733 |
+
"trauma-focused therapy approaches",
|
| 734 |
+
"coping with flashbacks",
|
| 735 |
+
"how trauma affects the brain",
|
| 736 |
+
"self-help for PTSD",
|
| 737 |
+
"when to seek trauma therapy"
|
| 738 |
+
])
|
| 739 |
+
|
| 740 |
+
# General mental health variations
|
| 741 |
variations.extend([
|
| 742 |
+
f"mental health resources for {question}",
|
| 743 |
+
f"coping strategies {question}",
|
| 744 |
+
f"therapy approaches {question}",
|
| 745 |
+
question.replace("?", "").strip() + " psychological support",
|
| 746 |
+
question.replace("?", "").strip() + " emotional help",
|
| 747 |
+
"how to deal with " + question.replace("?", "").strip(),
|
| 748 |
+
"best ways to manage " + question.replace("?", "").strip()
|
| 749 |
])
|
| 750 |
+
|
| 751 |
+
return list(set(variations))[:8] # Remove duplicates and limit to 8
|
| 752 |
+
|
| 753 |
|
| 754 |
def _extract_legal_concepts(self, question: str) -> List[str]:
|
| 755 |
+
mental_health_concepts = []
|
| 756 |
+
|
| 757 |
+
# Common mental health terms organized by category
|
| 758 |
+
mental_health_terms = [
|
| 759 |
+
# Conditions
|
| 760 |
+
"anxiety", "depression", "ptsd", "trauma", "ocd",
|
| 761 |
+
"bipolar", "adhd", "autism", "eating disorder",
|
| 762 |
+
# Symptoms
|
| 763 |
+
"panic", "sadness", "flashback", "trigger",
|
| 764 |
+
"mood swing", "dissociation", "suicidal",
|
| 765 |
+
# Treatments
|
| 766 |
+
"therapy", "counseling", "medication", "ssri",
|
| 767 |
+
"cbt", "dbt", "exposure therapy",
|
| 768 |
+
# Emotional states
|
| 769 |
+
"stress", "overwhelmed", "burnout", "grief",
|
| 770 |
+
"loneliness", "anger", "fear",
|
| 771 |
+
# Coping/help
|
| 772 |
+
"coping", "self-care", "support group",
|
| 773 |
+
"hotline", "crisis", "intervention"
|
| 774 |
]
|
| 775 |
+
|
| 776 |
question_lower = question.lower()
|
| 777 |
+
for term in mental_health_terms:
|
| 778 |
if term in question_lower:
|
| 779 |
+
mental_health_concepts.append(term)
|
| 780 |
+
|
| 781 |
+
# Handle common synonyms and related phrases
|
| 782 |
+
synonyms = {
|
| 783 |
+
"sad": "depression",
|
| 784 |
+
"nervous": "anxiety",
|
| 785 |
+
"scared": "anxiety",
|
| 786 |
+
"triggered": "trigger",
|
| 787 |
+
"ptsd": "trauma",
|
| 788 |
+
"mental illness": "mental health",
|
| 789 |
+
"shrinks": "therapy",
|
| 790 |
+
"mental breakdown": "crisis"
|
| 791 |
+
}
|
| 792 |
|
| 793 |
+
# Check for synonyms
|
| 794 |
+
for term, concept in synonyms.items():
|
| 795 |
+
if term in question_lower and concept not in mental_health_concepts:
|
| 796 |
+
mental_health_concepts.append(concept)
|
| 797 |
+
|
| 798 |
+
return mental_health_concepts
|
| 799 |
+
|
| 800 |
+
# def _is_legal_query(self, question: str) -> bool:
|
| 801 |
+
|
| 802 |
def _is_legal_query(self, question: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
|
| 804 |
+
question_lower = question.lower().strip()
|
| 805 |
+
|
| 806 |
+
# Mental health keywords
|
| 807 |
+
mental_health_keywords = [
|
| 808 |
+
# Conditions
|
| 809 |
+
"anxiety", "depression", "ptsd", "trauma", "ocd", "bipolar", "adhd",
|
| 810 |
+
"autism", "eating disorder", "panic", "stress", "burnout", "grief",
|
| 811 |
+
# Symptoms
|
| 812 |
+
"sad", "hopeless", "overwhelmed", "triggered", "flashback",
|
| 813 |
+
"dissociation", "suicidal", "self-harm", "numb", "irritable",
|
| 814 |
+
# Treatments
|
| 815 |
+
"therapy", "counseling", "cbt", "dbt", "medication", "ssri",
|
| 816 |
+
"antidepressant", "treatment", "intervention",
|
| 817 |
+
# Emotional states
|
| 818 |
+
"feel", "feeling", "emotion", "mental state", "mood",
|
| 819 |
+
"emotional", "psychology",
|
| 820 |
+
# Coping/help
|
| 821 |
+
"cope", "coping", "self-care", "support", "help", "resources",
|
| 822 |
+
"hotline", "crisis", "well-being", "mental health", "mental illness",
|
| 823 |
+
"therapist", "psychologist", "psychiatrist", "counselor"
|
| 824 |
+
]
|
| 825 |
+
|
| 826 |
+
# Check for mental health keywords
|
| 827 |
+
for keyword in mental_health_keywords:
|
| 828 |
if keyword in question_lower:
|
| 829 |
return True
|
| 830 |
+
|
| 831 |
+
# Check for question words that often indicate mental health queries
|
| 832 |
+
question_words = ["how", "why", "what", "when", "should", "can", "does"]
|
| 833 |
+
has_question_word = any(question_lower.startswith(word) for word in question_words)
|
| 834 |
+
|
| 835 |
+
# Check for mental health context indicators
|
| 836 |
+
mental_health_context = [
|
| 837 |
+
"i feel", "i'm feeling", "i am feeling", "struggling with", "dealing with",
|
| 838 |
+
"coping with", "mental state", "emotional state", "my mood", "my anxiety",
|
| 839 |
+
"my depression", "my trauma", "my stress", "help me with", "support for",
|
| 840 |
+
"resources for", "ways to manage", "how to handle", "should i seek",
|
| 841 |
+
"do i need", "am i", "is this normal", "signs of", "symptoms of",
|
| 842 |
+
"crisis", "urgent", "emergency", "can't cope", "can't handle"
|
| 843 |
+
]
|
| 844 |
+
|
| 845 |
+
has_mental_health_context = any(context in question_lower for context in mental_health_context)
|
| 846 |
+
|
| 847 |
+
# More permissive check for emotional distress indicators
|
| 848 |
+
if has_question_word:
|
| 849 |
+
# Emotional distress indicators
|
| 850 |
+
distress_indicators = [
|
| 851 |
+
"overwhelmed", "hopeless", "alone", "stuck", "lost", "empty",
|
| 852 |
+
"numb", "crying", "scared", "fear", "worry", "panic", "stress",
|
| 853 |
+
"can't sleep", "appetite", "energy", "motivation", "concentrate",
|
| 854 |
+
"suicidal", "self-harm", "harm myself", "end it all"
|
| 855 |
]
|
| 856 |
|
| 857 |
+
if any(indicator in question_lower for indicator in distress_indicators):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
return True
|
| 859 |
+
|
| 860 |
+
# Check for emotional expression patterns
|
| 861 |
+
emotion_words = ["sad", "anxious", "depressed", "angry", "stressed", "nervous"]
|
| 862 |
+
has_emotion_word = any(word in question_lower for word in emotion_words)
|
| 863 |
+
|
| 864 |
+
# Final decision logic
|
| 865 |
+
return (
|
| 866 |
+
has_question_word and
|
| 867 |
+
(has_mental_health_context or has_emotion_word or any(keyword in question_lower for keyword in mental_health_keywords))
|
| 868 |
+
)
|
| 869 |
|
| 870 |
def _is_conversational_query(self, question: str) -> bool:
|
| 871 |
"""Detect if the query is conversational and doesn't need legal document search"""
|
|
|
|
| 902 |
question_lower = question.lower().strip()
|
| 903 |
|
| 904 |
if question_lower in ["hi", "hello", "hey"]:
|
| 905 |
+
return """Hello! I'm your compassionate mental health companion. I'm here to offer support and guidance for various emotional well-being topics including:
|
| 906 |
|
| 907 |
+
• Anxiety and stress management
|
| 908 |
+
• Depression and mood challenges
|
| 909 |
+
• Trauma healing and PTSD recovery
|
| 910 |
+
• Relationship and family dynamics
|
| 911 |
+
• Workplace stress and burnout prevention
|
| 912 |
+
• Self-esteem and personal growth journeys
|
| 913 |
+
• Grief processing and life transitions
|
| 914 |
+
• And many other emotional wellness concerns
|
| 915 |
|
| 916 |
+
This is a safe space where you can:
|
| 917 |
+
|
| 918 |
+
Share what's on your mind without judgment
|
| 919 |
+
|
| 920 |
+
Explore healthy coping strategies
|
| 921 |
+
|
| 922 |
+
Understand your emotional experiences
|
| 923 |
+
|
| 924 |
+
Find resources for professional support
|
| 925 |
+
|
| 926 |
+
How would you like to begin today?
|
| 927 |
+
You could tell me how you're feeling, ask about coping techniques, or explore resources for specific challenges."""
|
| 928 |
|
| 929 |
elif "how can you help" in question_lower or "what can you do" in question_lower:
|
| 930 |
+
return """"Hello! I'm your compassionate mental health companion. I'm here to provide emotional support and guidance for various psychological well-being topics including:
|
| 931 |
+
|
| 932 |
+
• Anxiety and stress management
|
| 933 |
+
• Depression and mood disorders
|
| 934 |
+
• Trauma recovery and PTSD
|
| 935 |
+
• Relationship and family challenges
|
| 936 |
+
• Workplace burnout and career stress
|
| 937 |
+
• Grief and loss processing
|
| 938 |
+
• Self-esteem and personal growth
|
| 939 |
+
• Coping skills and resilience building
|
| 940 |
+
• And many other emotional wellness concerns
|
| 941 |
|
| 942 |
+
I offer a safe space to explore your feelings, develop coping strategies, and find resources. Remember, while I'm here to support you, I'm not a replacement for professional care in crisis situations.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 943 |
|
| 944 |
+
How would you like to begin today?
|
| 945 |
+
You could share what's on your mind, how you're feeling, or ask about:
|
| 946 |
+
|
| 947 |
+
Coping techniques for [specific emotion]
|
| 948 |
+
|
| 949 |
+
Understanding [mental health term]
|
| 950 |
+
|
| 951 |
+
Local therapist resources
|
| 952 |
+
|
| 953 |
+
Self-care strategies"""
|
| 954 |
|
| 955 |
elif "who are you" in question_lower or "what are you" in question_lower:
|
| 956 |
+
return """I'm an AI-powered mental health companion here to offer emotional support and wellness guidance. I can:
|
| 957 |
+
|
| 958 |
+
• Search through therapeutic resources and evidence-based practices
|
| 959 |
+
• Explain mental health concepts and coping strategies
|
| 960 |
+
• Provide information on conditions, symptoms, and treatments
|
| 961 |
+
• Help you navigate therapy options and self-care techniques
|
| 962 |
+
• Share reputable mental health sources and crisis resources
|
| 963 |
+
|
| 964 |
+
I'm not a licensed therapist, and I can't diagnose or treat conditions, but I can offer general information, emotional support, and tools to help you better understand your well-being.
|
| 965 |
|
| 966 |
+
What would you like to explore today?
|
| 967 |
+
You might ask about:
|
|
|
|
|
|
|
|
|
|
| 968 |
|
| 969 |
+
Understanding anxiety/depression symptoms
|
| 970 |
+
|
| 971 |
+
Grounding techniques for stress
|
| 972 |
+
|
| 973 |
+
How cognitive behavioral therapy (CBT) works
|
| 974 |
+
|
| 975 |
+
Finding a therapist near you
|
| 976 |
+
|
| 977 |
+
Managing [specific emotion or situation]"""
|
| 978 |
|
| 979 |
else:
|
| 980 |
+
return """Hello! I’m here to offer emotional support and mental health resources. I can help you explore coping strategies, explain therapeutic concepts, and provide evidence-based information to support your well-being.
|
| 981 |
+
|
| 982 |
+
How can I assist you today? You might ask about:**
|
| 983 |
+
|
| 984 |
+
Relaxation techniques for anxiety
|
| 985 |
+
|
| 986 |
+
Understanding depression symptoms
|
| 987 |
+
|
| 988 |
+
How to find a therapist
|
| 989 |
+
|
| 990 |
+
Coping with [specific stressor]
|
| 991 |
+
|
| 992 |
+
Self-care for tough emotions
|
| 993 |
+
|
| 994 |
+
(Note: I’m not a substitute for professional care, but I’m here to listen and guide.)
|
| 995 |
|
| 996 |
+
What’s on your mind?"""
|
| 997 |
|
| 998 |
def _filter_relevant_results(self, search_results: List[Dict[str, Any]], question: str) -> List[Dict[str, Any]]:
|
| 999 |
"""Filter search results for relevance to the question"""
|
|
|
|
| 1022 |
|
| 1023 |
# Check if content contains relevant legal terms
|
| 1024 |
legal_terms = [
|
| 1025 |
+
"therapy", "counseling", "psychology", "depression", "anxiety",
|
| 1026 |
+
"trauma", "stress", "diagnosis", "treatment", "intervention",
|
| 1027 |
+
"client", "therapist", "counselor", "session", "assessment",
|
| 1028 |
+
"diagnostic", "recovery", "wellness", "coping", "disorder"
|
| 1029 |
+
]
|
| 1030 |
|
| 1031 |
has_legal_content = any(term in content for term in legal_terms)
|
| 1032 |
|