File size: 17,513 Bytes
73bb3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import json
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
from pydantic import BaseModel, Field
# from langsmith import traceable
from textwrap import dedent
from typing import Optional

# Load environment variables
load_dotenv()

# ======================= CONFIGURATION =======================
from crew_ai.tools import MentalHealthTools, QueryVectorStoreTool, DataRetrievalTool, CrisisClassifierTool, MentalConditionClassifierTool
from crew_ai.llm_setup import get_llm
from crew_ai.questionnaire import load_questionnaires, conduct_assessment
from crew_ai.config import get_config

# Load config values
config = get_config()

# LLM Initialization
llm = get_llm()

# Tool Setup
mental_health_tools = MentalHealthTools()
crisis_classifier_tool = CrisisClassifierTool()
query_vector_store = QueryVectorStoreTool()
data_retriever_tool = DataRetrievalTool()
mental_condition_classifier_tool = MentalConditionClassifierTool()

# ======================= ASSESSMENT QUESTIONNAIRES =======================
QUESTIONS = load_questionnaires()

# ======================= OUTPUT SCHEMAS =======================
class CrisisDetectionOutput(BaseModel):
    is_crisis: bool = Field(description="True if the query indicates a mental health crisis.")
    explanation: str = Field(description="Reason for classifying as crisis or not.")

class MentalConditionOutput(BaseModel):
    condition: str = Field(description="The diagnosed mental condition or concern.")
    rationale: str = Field(description="Why the classification was made.")

class DataRetrievalOutput(BaseModel):
    id: int = Field(description="User profile ID.")
    name: str = Field(description="User name")
    age: int = Field(description="User age")
    gender: str = Field(description="Gender of user")
    city_region: str = Field(description="City or region of user")
    profession: str = Field(description="Profession of user")
    marital_status: str = Field(description="Marital status of user")
    previous_mental_diagnosis: str = Field(description="Prviously diagnosed mental health conditions of user")
    ethnicity: str = Field(description="Ethnicity of user")

class RecommendationResult(BaseModel):
    recommendation: Optional[str]

# ======================= AGENT FACTORY =======================
def create_agent(role: str, goal: str, backstory: str, tools=None,**kwargs) -> Agent:
    return Agent(
        role=role,
        goal=goal,
        backstory=backstory,
        tools=tools or [],
        llm=llm,
        verbose=True,
        allow_delegation=False,
        **kwargs
    )

# ======================= AGENTS =======================
crisis_detection_agent = create_agent(
    "Crisis Detection Specialist",
    "Identify immediate crisis situations and escalate if needed.",
    "You are a highly empathetic and vigilant AI assistant trained to detect signs of "\
    "severe distress, suicidal ideation, or other mental health emergencies. "\
    "Your primary responsibility is to classify the query as crisis or no-crisis situation using the tool you have. "\
    "If the tool output indicates 'is_crisis=True', then it is a CRISIS situation otherwise it is NO CRISIS situation.",
    tools=[crisis_classifier_tool]
)

mental_condition_classifier_agent = create_agent(
    "Mental Health Condition Classifier",
    "Classify the user\'s mental health concern or condition, specifically aiming to identify the relevant questionnaire based on the condition detected.",
    "You are a meticulous AI specialized in understanding various mental health "\
    "states. You analyze user input and identify keywords for stress, anxiety, depression, substance abuse, bipolar, alcohol use etc. "\
    "and categorize their current concern, with a preference for matching it to a standard assessment "\
    "like PHQ-9 (depression), GAD-7 (anxiety), DAST-10 (substance abuse), AUDIT (Alcohol use) or Bipolar (bipolar syndrome) to 'General Well-being' or 'Other' using the tool you have."\
    """ **Tool Usage:**

        If the confidence score from the tool's result is greater than 0.5, only then classify the query as the condition returned from the tool. 

        Otherwise, classify the query based on your own knowledge.



        If the tool's classification is deemed unreliable (score <= 0.5), analyze the text manually based on your understanding of mental health conditions and identify relevant questionnaires:

        

        - **Depression:** PHQ-9

        - **Anxiety:** GAD-7

        - **Substance Abuse:** DAST-10

        - **Alcohol Use:** AUDIT

        - **Bipolar Syndrome:** Bipolar Disorder Assessment

        - **General Well-being:** Other mental health concerns  

    """,
    tools=[mental_condition_classifier_tool]
)

data_retriever_agent = create_agent(
    "User Profile Data Retriever",
    "Retrieve user profile details using the provided user profile ID. Use the data retrieval tool to fetch the user profile.",
    "You are responsible for fetching the user profile from the database if the user exits in the session. If the user is anonymous, then use the default user profile.",
    tools=[data_retriever_tool]
)

rag_agent = create_agent(
    "Knowledge Base Manager & Query Refiner",
    "Interpret user queries, formulate specific search terms, and manage/query the mental health knowledge base using RAG.'",
    "You are responsible for intelligently understanding user needs, even from vague inputs. "\
    "You will formulate precise search queries or identify relevant mental health keywords "\
    "before efficiently retrieving relevant information from the vector database. "\
    "You ensure that the knowledge base is always up-to-date and accessible for generating "\
    "informed recommendations, and that relevant information is always found, even for general queries.",
    tools=[query_vector_store]               # This tool performs PostgreSQL vector search
)

recommendation_agent = create_agent(
    "Personalized Recommendation Generator",
    "Provide tailored mental health recommendations based on all gathered information, including questionnaire interpretation.",
    "You are a compassionate and knowledgeable AI dedicated to offering "\
    "actionable and personalized advice. You synthesize user queries, "\
    "profile data, assessment answers, and the interpretation from assessments "\
    "to deliver helpful recommendations, including suggesting professional help when appropriate.",
    tools=[mental_health_tools.get_bhutanese_helplines],
    reasoning=True
)

# ======================= TASKS =======================
crisis_detection_task = Task(
    description="Analyze the user's current query: '{user_query}' to determine if it indicates a mental health crisis or emergency."\
        "Use the crisis detection tool to analyze the text. "\
        "Look for indicators such as: suicidal ideation, self-harm, immediate danger, severe distress, emergency situations. ",
    expected_output="Strict JSON with keys is_crisis (bool) and explanation (string). No markdown, no commentary.",
    output_json=CrisisDetectionOutput,
    input_variables=["user_query"],
    agent=crisis_detection_agent
)

mental_condition_classification_task = Task(
    description="Analyze the user's query '{user_query}' and user profile '{user_profile}' to classify their mental health condition.",
    expected_output="Strict JSON with keys condition (string) and rationale fields (string).",
    output_json=MentalConditionOutput,
    input_variables=["user_query", "user_profile"],
    agent=mental_condition_classifier_agent
)

data_retriever_task = Task(
    description="Fetch user profile data in structured JSON."\
        "Use Data Retrieval tool to retrieve the user profile information with input as '{user_profile_id}'.",
    expected_output="User demographic and background profile as Strict JSON.",
    input_variables=["user_query", "user_profile_id"],
    output_json = DataRetrievalOutput,    
    agent=data_retriever_agent
)

rag_task = Task(
    description=dedent("""

        Retrieve top 3 relevant documents for the query: '{user_query}' and condition: '{classified_condition}'

        

        Guidelines:

        1. Analyze the '{user_query}'. If it is general or vague (e.g., 'I'm feeling down', 'I need some advice'), 

            use your intelligence to formulate a more specific query or identify potential mental health keywords 

            (e.g., 'stress', 'anxiety', 'depression', 'general well-being') that reflect the user's potential 

            underlying condition. Prioritize keywords present in the vector database's . If the query is already specific, use it directly.

        2. The output should be relevant information blocks from the knowledge base.

            based on the refined query.

    """),
    expected_output="A JSON object with a list of relevant texts under key 'docs'",
    input_variables=["user_query", "classified_condition"],
    agent=rag_agent
)

recommendation_task = Task(
    description=dedent("""

        Generate comprehensive, personalized mental health recommendations based on all available information:

        - User query: '{user_query}'

        - User profile: '{user_profile}'

        - Identified condition: '{classified_condition}'

        - Assessment results: '{assessment_answers}' with interpretation: '{interpretation}'

        - Crisis status: '{is_crisis}'

    

    

        Guidelines:

        1. Provide culturally sensitive recommendations aligned with Bhutanese values and Gross National Happiness principles.

        2. Include actionable, practical steps the user can take.

        3. Consider the user's specific profile (age, background, history).

        4. Summarize the retrieved documents in your context.

        4. Use this summary to support your recommendation. Reference specific sources used from your context'.

        6. If assessment was completed, incorporate the interpretation. DO NOT reveal the assessment results to the user.

        7. For crisis situations, prioritize immediate safety and professional help.

        8. Use compassionate, encouraging language.

        9. Suggest community resources, traditional practices, and professional help as appropriate.

        10. Only provide helplines when specifically needed for crisis situations.

        

        Structure your response as a comprehensive recommendation that addresses the user's specific needs.

    """),
    expected_output="A comprehensive, personalized, and empathetic mental health recommendation tailored to the user's specific situation.",
    agent=recommendation_agent,
    context=[rag_task],
    output_json=RecommendationResult,
    input_variables=["user_query", "user_profile","classified_condition", "assessment_answers", "interpretation", "is_crisis"],
)

# ======================= CREWS =======================
crisis_management_crew = Crew(agents=[crisis_detection_agent], tasks=[crisis_detection_task], verbose=True)
mental_condition_crew = Crew(agents=[mental_condition_classifier_agent], tasks=[mental_condition_classification_task], verbose=True)
data_retrieval_crew = Crew(agents=[data_retriever_agent], tasks=[data_retriever_task], verbose=True)
recommendation_crew = Crew(agents=[rag_agent, recommendation_agent], tasks=[rag_task, recommendation_task], full_output=True, output_log_file=True, verbose=True)

# ======================= EXPORTABLE API =======================
def run_crisis_check(user_query: str) -> dict:
    result = crisis_management_crew.kickoff({"user_query": user_query})
    return result.json_dict

def run_condition_classification(user_query: str, user_profile: str) -> dict:
    condition_result = mental_condition_crew.kickoff({
        "user_query": user_query,
        "user_profile": user_profile
    })
    return condition_result.json_dict

def run_user_profile_retrieval(user_query: str, user_profile_id: str) -> dict:
    data_result = data_retrieval_crew.kickoff({
        "user_query": user_query,
        "user_profile_id": user_profile_id
    })
    return data_result.json_dict

def run_recommendations(user_query: str, user_profile: str, condition: str, answers: str, interpretation: str, is_crisis: str):
    recommendation_result = recommendation_crew.kickoff({
        "user_query": user_query,
        "user_profile": user_profile,
        "classified_condition": condition,
        "assessment_answers": answers,
        "interpretation": interpretation,
        "is_crisis": is_crisis
    })
    return recommendation_result.json_dict

# ======================= FULL CHAT FLOW =======================
# @traceable(name= "Druckare Chatbot full flow")
def full_chat_flow(user_query: str, user_id: str = "anon_user"):
    print("πŸ“„ Fetching user profile...")
    dummy_profile = {
        "id": user_id,
        "age": "",
        "location": "",
        "history": "",
        "preferences": "Prefers meditation"
    }

    print("πŸ” Checking for crisis...")
    crisis_result = run_crisis_check(user_query)
    print("πŸ“¦ Crew result:", crisis_result)

    is_crisis = crisis_result.get("is_crisis", False)
    explanation = crisis_result.get("explanation", "")

    if is_crisis:
        print(f"🚨 Crisis Detected: {explanation}")
        rec = run_recommendations(
            user_query, 
            user_profile=json.dumps(dummy_profile), 
            condition="Crisis", 
            answers="{}", 
            interpretation="N/A", 
            is_crisis="true"
        )
        # task_outputs = rec.tasks_output
        # retrieved_docs_crisis = task_outputs[0]

        print("\nπŸ†˜ Crisis Support Recommendation:\n", rec)
        return {
            "recommendation": rec["recommendation"],
            "score_interpretation": interpretation,
            "condition": condition,
            "is_crisis": is_crisis,
            "crisis_explanation": explanation,
            # "retrieved_docs": retrieved_docs_crisis
        }
    else:
        print("No crisis detected")

    print("πŸ”Ž Classifying condition...")
    condition_result = run_condition_classification(user_query, json.dumps(dummy_profile))
    condition = condition_result.get("condition").lower()
    questionnaire_name = config["CONDITION_TO_QUESTIONNAIRE"].get(condition)

    print(f"🧠 Detected condition: {condition}")
    if questionnaire_name not in QUESTIONS:
        print("Skipping assessment as condition is general or unknown.")
        score = "N/A"
        answers = {}
        interpretation = "Not applicable"
    else:
        # Ask for user confirmation 
        confirm = input(f"πŸ‘‰ We recommend a brief '{questionnaire_name}' assessment (e.g., {condition.upper()} questionnaire). Do you want to proceed? (yes/no): ").strip().lower()

        if confirm != 'yes':
            print("🚫 Assessment skipped by user.")
            score = 'N/A'
            answers = {}
            interpretation = "User chose not to proceed with the assessment."
        else:
            # Show instructions
            print("\nπŸ“ Instructions:")
            print("You will now be presented with a few questions related to your mental health condition.")
            if questionnaire_name in ["PHQ-9", "GAD-7"]:
                print("Please answer each question honestly, based on how you've felt over the **last 2 weeks**.")
                print("Use the following scale to respond:")
                print("  0 - Not at all")
                print("  1 - Several days")
                print("  2 - More than half the days")
                print("  3 - Nearly every day")
            elif questionnaire_name == "DAST-10":
                print("Answer each question with Yes or No based on your past year's experience.")
            elif questionnaire_name == "AUDIT":
                print("Answer using options like: Never, 1-2, 3-4, Weekly, Yes, No, etc., as applicable.")
            elif questionnaire_name == "Bipolar":
                print("Answer each question with Yes or No based on your past mood and energy patterns.")
            
            input("\nPress Enter to begin the questionnaire...")

            # Proceed with assessment
            assessment = conduct_assessment(questionnaire_name)
            answers = assessment["answers"]
            score = assessment["score"]
            interpretation = assessment["interpretation"]
    
    print("πŸ’‘ Generating recommendations...")
    final_rec = run_recommendations(
        user_query,
        json.dumps(dummy_profile),
        condition,
        json.dumps(answers),
        interpretation,
        is_crisis="false"
    )

    # task_outputs = final_rec.tasks_output
    # retrieved_docs = task_outputs[0]
    
    return {
        "recommendation": final_rec["recommendation"],
        "score_interpretation": interpretation,
        "condition": condition,
        "is_crisis": is_crisis,
        "crisis_explanation": explanation,
    #     "retrieved_docs": retrieved_docs
    }


if __name__ == "__main__":
    query = input("πŸ‘€ Enter your mental health query: ")
    final_output = full_chat_flow(query)
    print("Final Output:\n", final_output)