import os import json import requests # Set your API Key securely GROQ_API_KEY = os.getenv("GROQ_API_KEY") LLAMA_MODEL = "llama3-8b-8192" SLM_MODEL = "llama3-8b-8192" # --------------------------- # Groq API Call Wrapper # --------------------------- def call_groq(prompt, model=LLAMA_MODEL): response = requests.post( "https://api.groq.com/openai/v1/chat/completions", headers={"Authorization": f"Bearer {GROQ_API_KEY}"}, json={ "model": model, "messages": [{"role": "user", "content": prompt}], "temperature": 0.7 } ) if response.status_code != 200: return f"[Groq API Error {response.status_code}]: {response.text}" try: data = response.json() return data.get("choices", [{}])[0].get("message", {}).get("content", "[No valid response]") except Exception as e: return f"[Groq Parsing Error]: {str(e)}" # --------------------------- # Empathetic First-Aider Chat Agent # --------------------------- def run_first_aider(message, mood): prompt = f""" You're a warm and respectful AI listener. Respond to this user's message kindly and briefly, in 1-2 sentences. - Never give advice. - Be supportive, clear, emotionally kind, and safe. - Maintain boundaries. User mood: {mood} Message: "{message}" """ return call_groq(prompt, model=SLM_MODEL) # --------------------------- # Context-Aware Introspect Agent # --------------------------- def get_user_context(): context = "" if os.path.exists("chat_log.json"): with open("chat_log.json") as f: chats = json.load(f)[-3:] # last 3 chat messages context += "\nRecent Conversations:\n" for c in chats: context += f"User: {c['user']}\nAI: {c['ai']}\n" if os.path.exists("journal_log.json"): with open("journal_log.json") as f: logs = json.load(f)[-2:] context += "\nJournal Entries:\n" for j in logs: context += f"Mood: {j['mood']}\nEntry: {j['entry']}\nAI Response: {j['response']}\n" return context.strip() if context.strip() else None def run_introspect(message, mood): context = get_user_context() if not context: return "Let's talk a bit or journal first so I can help you reflect better." prompt = f""" You're a calm, thoughtful AI helping a user gently reflect on their emotional patterns. Context from past chats and journals: {context} User's new message: "{message}" Mood: {mood} Instructions: - Highlight any potential recurring emotional themes (gently). - Suggest a new way to think about the situation (without naming therapy techniques). - End with a kind affirmation or journaling suggestion. Be kind, short, helpful, and never mention psychology, CBT, or NLP. """ return call_groq(prompt, model=LLAMA_MODEL) # --------------------------- # Journaling Agent (Context-Aware, CBT/NLP Masked) # --------------------------- def get_mood_context(): if not os.path.exists("journal_log.json"): return [] with open("journal_log.json") as f: logs = json.load(f) return [x["mood"] for x in logs[-3:]] def run_journaling_pipeline(mood, entry, mode): recent_moods = get_mood_context() mood_summary = f"Recent moods: {', '.join(recent_moods)}." if recent_moods else "No mood history." reflective_prompt = "What patterns have you noticed in how you've been feeling lately?" if recent_moods else "What stood out to you emotionally today?" prompt = f""" You're a reflective journaling assistant. Help the user explore their thoughts kindly and safely. User wrote: "{entry}" Journaling Mode: {mode} Mood: {mood} {mood_summary} Prompt: {reflective_prompt} Instructions: - Reflect what the user might be feeling or thinking. - Suggest gentle rephrasing or ways to understand the situation better. - End with a kind affirmation or open-ended journaling question. - Do not mention therapy, CBT, NLP, psychology, or diagnosis. Be gentle and warm. """ response = call_groq(prompt, model=LLAMA_MODEL) return { "entry": entry, "mood": mood, "mode": mode, "response": response }