dpaul93 commited on
Commit
b5d2d69
·
verified ·
1 Parent(s): ce71e3a

Update agents.py

Browse files
Files changed (1) hide show
  1. agents.py +70 -33
agents.py CHANGED
@@ -1,5 +1,4 @@
1
- import requests
2
- import os
3
 
4
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
5
  LLAMA_MODEL = "llama3-8b-8192"
@@ -15,49 +14,87 @@ def call_groq(prompt, model=LLAMA_MODEL):
15
  "temperature": 0.7
16
  }
17
  )
18
- print(response)
19
- return response.json()["choices"][0]["message"]["content"]
 
 
20
 
21
- def get_prompt(entry, mood, mode):
22
- if "Workplace" in mode:
23
- return f"""
24
- You are a workplace CBT/NLP counselor. Analyze this journal:
25
-
26
- "{entry}"
27
 
28
  Mood: {mood}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- Return:
31
- - Summary
32
- - Workplace Stressors
33
- - Cognitive Distortions
34
- - Reframe
35
- - Affirmation
 
 
 
36
  """
37
- else:
38
- return f"""
39
- You are a CBT/NLP journaling agent. Analyze the following:
40
 
41
- "{entry}"
 
 
 
 
 
42
 
 
 
 
 
 
 
 
 
 
43
  Mood: {mood}
 
 
 
 
 
 
44
 
45
- Return:
46
- - Summary
47
- - Emotional Triggers
48
- - Distortions
49
- - CBT Reframe
50
- - Daily Affirmation
51
  """
52
 
53
- def run_journaling_pipeline(mood, entry, mode):
54
- if not entry.strip():
55
- return {"error": "Empty entry."}
56
- prompt = get_prompt(entry, mood, mode)
57
- output = call_groq(prompt)
58
  return {
 
59
  "mood": mood,
60
  "mode": mode,
61
- "entry": entry,
62
- "response": output
63
  }
 
1
+ import os, json, requests
 
2
 
3
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
4
  LLAMA_MODEL = "llama3-8b-8192"
 
14
  "temperature": 0.7
15
  }
16
  )
17
+ if response.status_code != 200:
18
+ return f"[Groq API Error {response.status_code}]: {response.text}"
19
+ data = response.json()
20
+ return data.get("choices", [{}])[0].get("message", {}).get("content", "[No valid response]")
21
 
22
+ # --- Empathetic Chat Agent ---
23
+ def run_first_aider(message, mood):
24
+ prompt = f"""
25
+ You're a warm and respectful AI listener. Respond to this user's message kindly and briefly, in 1-2 sentences.
26
+ Do not give advice. Use supportive, non-clinical tone.
 
27
 
28
  Mood: {mood}
29
+ Message: "{message}"
30
+ """
31
+ return call_groq(prompt, model=SLM_MODEL)
32
+
33
+ # --- Introspect Agent (with chat/journal memory) ---
34
+ def get_user_context():
35
+ context = ""
36
+ if os.path.exists("chat_log.json"):
37
+ with open("chat_log.json") as f:
38
+ chats = json.load(f)[-3:]
39
+ context += "\nRecent Conversations:\n"
40
+ for c in chats:
41
+ context += f"User: {c['user']}\nAI: {c['ai']}\n"
42
+ if os.path.exists("journal_log.json"):
43
+ with open("journal_log.json") as f:
44
+ logs = json.load(f)[-2:]
45
+ context += "\nJournal Entries:\n"
46
+ for j in logs:
47
+ context += f"{j['entry']} → {j['response']}\n"
48
+ return context if context.strip() else None
49
+
50
+ def run_introspect(message, mood):
51
+ context = get_user_context()
52
+ if not context:
53
+ return "Let's first talk a bit or write a few journal entries."
54
 
55
+ prompt = f"""
56
+ Based on this context:
57
+ {context}
58
+
59
+ The user says: "{message}"
60
+ Their mood is: {mood}
61
+
62
+ Offer a kind and short reflection. Suggest something gentle like a shift in perspective, or a small action.
63
+ Never use words like therapy, CBT, mental health, or psychology.
64
  """
65
+ return call_groq(prompt, model=LLAMA_MODEL)
 
 
66
 
67
+ # --- Journal Agent with mood context ---
68
+ def get_mood_context():
69
+ if not os.path.exists("journal_log.json"): return []
70
+ with open("journal_log.json") as f:
71
+ logs = json.load(f)
72
+ return [x["mood"] for x in logs[-3:]]
73
 
74
+ def run_journaling_pipeline(mood, entry, mode):
75
+ recent_moods = get_mood_context()
76
+ context_line = f"Recent moods: {', '.join(recent_moods)}." if recent_moods else "No recent mood history."
77
+ question = "What emotional patterns do you notice in your life recently?" if recent_moods else "What stood out to you emotionally today?"
78
+
79
+ prompt = f"""
80
+ You are a calm, non-judgmental journaling assistant.
81
+
82
+ User mode: {mode}
83
  Mood: {mood}
84
+ User wrote: "{entry}"
85
+ {context_line}
86
+ Question: {question}
87
+
88
+ Reflect back to the user with kind interpretation, help them notice something they may have missed.
89
+ End with a gentle affirmation or reflective nudge.
90
 
91
+ Avoid therapy talk.
 
 
 
 
 
92
  """
93
 
94
+ response = call_groq(prompt, model=LLAMA_MODEL)
 
 
 
 
95
  return {
96
+ "entry": entry,
97
  "mood": mood,
98
  "mode": mode,
99
+ "response": response
 
100
  }