Spaces:
Sleeping
Sleeping
File size: 4,213 Bytes
d1a71ba 958773c d1a71ba 1ab41d1 d1a71ba 713c265 d1a71ba 958773c |
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 |
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
}
|