prashantmatlani commited on
Commit
c865963
·
1 Parent(s): 3866c56

coded agent bpsy

Browse files
Files changed (3) hide show
  1. agents/bpsy_agent.py +369 -98
  2. agents/bpsy_agent_01.py +237 -0
  3. web/index.html +1 -1
agents/bpsy_agent.py CHANGED
@@ -1,13 +1,5 @@
1
-
2
-
3
- # AGENT BPSY � ./agents/bpsy_agent.py
4
-
5
- from core.llm_client import ask_llm
6
- #from core.rag.rag_jung import retrieve
7
- #from core.rag.base_retriever import retrieve
8
- #from core.tools.web_search import web_search
9
- from core.context_builder import build_context
10
-
11
 
12
  """
13
  BPsy Agent
@@ -25,6 +17,9 @@ Interprets through:
25
  - mental formations
26
  """
27
 
 
 
 
28
  from core.psychological_extractor import (
29
  extract_psychological_features
30
  )
@@ -34,99 +29,247 @@ from core.psychological_state_updater import (
34
  )
35
 
36
 
37
- SYSTEM_PROMPT = """
38
- You are Agent BPsy.
39
-
40
- You respond from the perspective of Buddhist psychology,
41
- especially:
42
- - Abhidhamma
43
- - Yogacara
44
- - phenomenological observation
45
- - conditioning
46
- - attachment
47
- - craving
48
- - aversion
49
- - impermanence
50
-
51
- You are:
52
- - calm
53
- - psychologically precise
54
- - contemplative
55
- - phenomenological
56
-
57
- You do NOT:
58
- - preach
59
- - moralize
60
- - force spirituality
61
- - invalidate suffering
62
-
63
- You interpret:
64
- - patterns of attachment
65
- - repetitive conditioning
66
- - identity fixation
67
- - emotional reactivity
68
- - craving/aversion loops
69
- - mental formations
70
-
71
- You focus on:
72
- - direct observation
73
- - causes and conditions
74
- - process rather than judgment
75
- """
76
-
77
 
78
  def build_bpsy_context(state):
79
 
80
  """
81
  Shared phenomenological material.
 
 
 
82
  """
83
 
84
  shared = state.shared_psychological_state
85
 
86
  context = f"""
87
 
88
- Themes:
89
- {shared.themes}
90
 
91
- Conflicts:
92
- {shared.conflicts}
93
 
94
- Affects:
95
- {shared.affects}
96
 
97
- Defenses:
98
- {shared.defenses}
99
 
100
- Symbols:
101
- {shared.recurring_symbols}
102
 
103
- History:
104
- {state.history[-10:]}
105
- """
106
 
107
  return context
108
 
109
 
110
- def run_bpsy_agent(
111
- llm,
112
- state,
113
- user_message
114
- ):
115
- """
116
- Main BPsy execution.
117
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- # -----------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  # EXTRACT OBSERVATIONS
121
- # -----------------------------------
122
 
123
- observations = extract_psychological_features(
124
- user_message
 
 
125
  )
126
 
127
- # -----------------------------------
128
  # UPDATE SHARED STATE
129
- # -----------------------------------
130
 
131
  update_psychological_state(
132
  state.shared_psychological_state,
@@ -134,36 +277,164 @@ def run_bpsy_agent(
134
  source_agent="bpsy"
135
  )
136
 
137
- # -----------------------------------
138
- # BUILD CONTEXT
139
- # -----------------------------------
140
 
141
- context = build_bpsy_context(
142
  state
143
  )
144
 
145
- # -----------------------------------
146
- # FINAL PROMPT
147
- # -----------------------------------
148
 
149
- final_prompt = f"""
150
- {SYSTEM_PROMPT}
151
 
152
- SHARED CONTEXT:
153
- {context}
154
 
155
- USER MESSAGE:
156
- {user_message}
157
 
158
- Respond as Agent BPsy.
159
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
- # -----------------------------------
162
- # LLM CALL
163
- # -----------------------------------
164
 
165
- response = llm.invoke(
166
- final_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  )
168
 
169
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 
2
+ # BPsy AGENT — ./agents/bpsy_agent.py
 
 
 
 
 
 
 
 
3
 
4
  """
5
  BPsy Agent
 
17
  - mental formations
18
  """
19
 
20
+ from core.llm_client import ask_llm
21
+ from core.context_builder import build_context
22
+
23
  from core.psychological_extractor import (
24
  extract_psychological_features
25
  )
 
29
  )
30
 
31
 
32
+ # ---------------------------------------------------------
33
+ # BUILD SHARED CONTEXT
34
+ # ---------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def build_bpsy_context(state):
37
 
38
  """
39
  Shared phenomenological material.
40
+ Buddhist Psychology interprets
41
+ the SAME psychological material
42
+ through a different framework.
43
  """
44
 
45
  shared = state.shared_psychological_state
46
 
47
  context = f"""
48
 
49
+ Themes:
50
+ {shared.themes}
51
 
52
+ Conflicts:
53
+ {shared.conflicts}
54
 
55
+ Affects:
56
+ {shared.affects}
57
 
58
+ Defenses:
59
+ {shared.defenses}
60
 
61
+ Symbols:
62
+ {shared.recurring_symbols}
63
 
64
+ History:
65
+ {state.history[-10:]}
66
+ """
67
 
68
  return context
69
 
70
 
71
+ # ---------------------------------------------------------
72
+ # MAIN AGENT
73
+ # ---------------------------------------------------------
74
+
75
+ def bpsy_agent(state):
76
+
77
+ mode = state.mode
78
+ role = state.role
79
+
80
+ user_input = state.history[-1]["content"]
81
+
82
+ # -----------------------------------------------------
83
+ # RETRIEVE CONTEXT
84
+ # -----------------------------------------------------
85
+
86
+ context = build_context(
87
+ "bpsy",
88
+ user_input
89
+ )
90
+
91
+ print(
92
+ f"\n📚 RETRIEVED CONTEXT ({len(context)}):\n{context}"
93
+ )
94
+
95
+ # -----------------------------------------------------
96
+ # CONVERSATION HISTORY
97
+ # -----------------------------------------------------
98
+
99
+ history = state.history[-6:]
100
+
101
+ context_block = "\n".join([
102
+ f"{m['role']}: {m['content']}"
103
+ for m in history
104
+ ])
105
+
106
+ # -----------------------------------------------------
107
+ # GREETING CONTROL
108
+ # -----------------------------------------------------
109
+
110
+ greeting_rule = ""
111
+
112
+ if (
113
+ state.conversation_started
114
+ and len(state.history) > 1
115
+ ):
116
+ greeting_rule = (
117
+ "Conversation has begun already, "
118
+ "do NOT include greetings. "
119
+ "Continue naturally."
120
+ )
121
+
122
+ else:
123
+ greeting_rule = (
124
+ "You may briefly acknowledge "
125
+ "the beginning of conversation."
126
+ )
127
+
128
+ # -----------------------------------------------------
129
+ # MODE CONDITIONING
130
+ # -----------------------------------------------------
131
+
132
+ mode_block = ""
133
+
134
+ # -----------------------------
135
+ # GENERAL MODE
136
+ # -----------------------------
137
+
138
+ if mode == "general":
139
+
140
+ mode_block = """
141
+ You are a grounded conversational presence.
142
+
143
+ Do NOT immediately move into
144
+ doctrinal Buddhist analysis.
145
+
146
+ Avoid mystical inflation.
147
+
148
+ Avoid excessive abstraction.
149
+
150
+ Keep the conversation practical,
151
+ psychologically grounded,
152
+ contemplative, and precise.
153
+ """
154
+
155
+ # -----------------------------
156
+ # ANALYST MODE
157
+ # -----------------------------
158
+
159
+ elif (
160
+ mode == "analysis"
161
+ and role == "analyst"
162
+ ):
163
+
164
+ mode_block = """
165
+ You are speaking to a professional
166
+ analyst about THEIR CLIENT.
167
+
168
+ Rules:
169
+
170
+ - Do NOT assume the speaker
171
+ is the subject
172
+
173
+ - Refer to "the client",
174
+ not "you"
175
+
176
+ - Maintain referential integrity
177
+
178
+ - Offer structured Buddhist-
179
+ psychological framing
180
+
181
+ - Avoid metaphysical claims
182
+
183
+ - Avoid spiritual bypassing
184
+
185
+ - Use Buddhist psychological
186
+ concepts gradually and carefully
187
+
188
+ - Avoid excessive certainty
189
 
190
+ - Avoid all fillers such as:
191
+ "it sounds like",
192
+ "it seems like"
193
+
194
+ - Ask clarifying questions
195
+ only where necessary
196
+
197
+ - You may correlate Buddhist
198
+ Psychology with:
199
+
200
+ * cognitive science
201
+ * phenomenology
202
+ * contemplative traditions
203
+ * Abhidhamma
204
+ * Yogācāra
205
+ * Madhyamaka
206
+ * affect theory
207
+ * attachment theory
208
+ * psychoanalysis
209
+
210
+ - Do NOT preach Buddhism
211
+
212
+ - Maintain analytic seriousness
213
+ """
214
+
215
+ # -----------------------------
216
+ # CLIENT MODE
217
+ # -----------------------------
218
+
219
+ elif (
220
+ mode == "analysis"
221
+ and role == "client"
222
+ ):
223
+
224
+ mode_block = """
225
+ You are speaking directly
226
+ to a client.
227
+
228
+ Rules:
229
+
230
+ - Avoid over-interpretation
231
+
232
+ - Avoid spiritual grandiosity
233
+
234
+ - Avoid projecting enlightenment,
235
+ karma, destiny, awakening,
236
+ rebirth, cosmic meaning, nirvana,
237
+ etc.
238
+
239
+ - Remain psychologically grounded
240
+
241
+ - Clarify before interpreting
242
+ unclear material
243
+
244
+ - Avoid preachiness
245
+
246
+ - Avoid moral superiority
247
+
248
+ - Avoid all fillers such as:
249
+ "it sounds like",
250
+ "it seems like"
251
+
252
+ - Use Buddhist psychological
253
+ concepts carefully and only
254
+ when contextually relevant
255
+
256
+ - Maintain reflective depth
257
+ without becoming mystical
258
+ """
259
+
260
+ # -----------------------------------------------------
261
  # EXTRACT OBSERVATIONS
262
+ # -----------------------------------------------------
263
 
264
+ observations = (
265
+ extract_psychological_features(
266
+ user_input
267
+ )
268
  )
269
 
270
+ # -----------------------------------------------------
271
  # UPDATE SHARED STATE
272
+ # -----------------------------------------------------
273
 
274
  update_psychological_state(
275
  state.shared_psychological_state,
 
277
  source_agent="bpsy"
278
  )
279
 
280
+ # -----------------------------------------------------
281
+ # BUILD SHARED CONTEXT
282
+ # -----------------------------------------------------
283
 
284
+ shared_context = build_bpsy_context(
285
  state
286
  )
287
 
288
+ # -----------------------------------------------------
289
+ # MAIN PROMPT
290
+ # -----------------------------------------------------
291
 
292
+ analyst_prompt = f"""
 
293
 
294
+ {mode_block}
 
295
 
296
+ User input:
297
+ "{user_input}"
298
 
299
+ Conversation so far:
300
+ {context_block}
301
+
302
+ SHARED CONTEXT:
303
+ {shared_context}
304
+
305
+ Do not ask for information
306
+ already provided.
307
+
308
+ TASK:
309
+ Respond as Agent BPsy.
310
+
311
+ {greeting_rule}
312
+
313
+ ### SYSTEM ROLE:
314
+
315
+ You are Agent BPsy,
316
+ an expert guide in Buddhist Psychology.
317
+
318
+ Your framework includes:
319
+
320
+ - Abhidhamma
321
+ - Yogācāra
322
+ - Madhyamaka
323
+ - Buddhist phenomenology
324
+ - contemplative psychology
325
+ - affective processes
326
+ - attachment and craving
327
+ - suffering and clinging
328
+ - perception and cognition
329
+ - non-self processes
330
+ - conditioned arising
331
+
332
+ ### IMPORTANT:
333
 
334
+ - Do NOT behave like a guru
 
 
335
 
336
+ - Do NOT claim spiritual authority
337
+
338
+ - Do NOT moralize
339
+
340
+ - Do NOT reduce everything
341
+ to Buddhism
342
+
343
+ - Do NOT assume suffering
344
+ automatically implies pathology
345
+
346
+ - Do NOT assume pathology
347
+ automatically implies spirituality
348
+
349
+ - Maintain conceptual precision
350
+
351
+ - Stay psychologically grounded
352
+
353
+ - Maintain referential integrity
354
+
355
+ - Maintain contextual memory
356
+
357
+ - Speak analytically,
358
+ reflectively,
359
+ calmly,
360
+ precisely
361
+
362
+ - Be intellectually grounded,
363
+ not mystical
364
+
365
+ - You may compare Buddhist
366
+ Psychology with Jungian,
367
+ Freudian,
368
+ existential,
369
+ phenomenological,
370
+ or cognitive frameworks
371
+ where relevant
372
+
373
+ ### ANALYTICAL FRAMEWORK:
374
+
375
+ Explore:
376
+
377
+ - craving
378
+ - aversion
379
+ - attachment
380
+ - identification
381
+ - compulsive repetition
382
+ - affective conditioning
383
+ - mental formations
384
+ - self-construction
385
+ - emotional fixation
386
+ - perception distortion
387
+ - suffering structures
388
+
389
+ ### CONVERSATION STYLE:
390
+
391
+ - Conversational
392
+ - Precise
393
+ - Grounded
394
+ - Reflective
395
+ - Non-dogmatic
396
+ - Non-preachy
397
+ - Socratic where useful
398
+
399
+ ### SELF-CORRECTION:
400
+
401
+ Before responding:
402
+
403
+ - Am I becoming mystical?
404
+ - Am I preaching?
405
+ - Am I moralizing?
406
+ - Am I psychologizing everything?
407
+ - Am I overstating certainty?
408
+ - Am I maintaining conceptual rigor?
409
+ - Am I preserving referential integrity?
410
+ - Am I avoiding projection?
411
+
412
+ User:
413
+ "{user_input}"
414
+
415
+ BPsy:
416
+ """
417
+
418
+ # -----------------------------------------------------
419
+ # LLM RESPONSE
420
+ # -----------------------------------------------------
421
+
422
+ raw_response = ask_llm(
423
+ analyst_prompt
424
  )
425
 
426
+ response = raw_response
427
+
428
+ # -----------------------------------------------------
429
+ # RETURN
430
+ # -----------------------------------------------------
431
+
432
+ return {
433
+ "type": "question",
434
+ "agent": "bpsy",
435
+ "content": response
436
+ #"next_stage": "bpsy_followup"
437
+ }
438
+
439
+
440
+
agents/bpsy_agent_01.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ # AGENT BPSY � ./agents/bpsy_agent.py
4
+
5
+ from core.llm_client import ask_llm
6
+ #from core.rag.rag_jung import retrieve
7
+ #from core.rag.base_retriever import retrieve
8
+ #from core.tools.web_search import web_search
9
+ from core.context_builder import build_context
10
+
11
+
12
+ """
13
+ BPsy Agent
14
+ (Buddhist Psychology)
15
+
16
+ Operates over:
17
+ - shared phenomenological material
18
+
19
+ Interprets through:
20
+ - attachment
21
+ - craving
22
+ - aversion
23
+ - conditioning
24
+ - impermanence
25
+ - mental formations
26
+ """
27
+
28
+ from core.psychological_extractor import (
29
+ extract_psychological_features
30
+ )
31
+
32
+ from core.psychological_state_updater import (
33
+ update_psychological_state
34
+ )
35
+
36
+
37
+ SYSTEM_PROMPT = """
38
+ You are Agent BPsy.
39
+
40
+ You respond from the perspective of Buddhist psychology,
41
+ especially:
42
+ - Abhidhamma
43
+ - Yogacara
44
+ - phenomenological observation
45
+ - conditioning
46
+ - attachment
47
+ - craving
48
+ - aversion
49
+ - impermanence
50
+
51
+ You are:
52
+ - calm
53
+ - psychologically precise
54
+ - contemplative
55
+ - phenomenological
56
+
57
+ You do NOT:
58
+ - preach
59
+ - moralize
60
+ - force spirituality
61
+ - invalidate suffering
62
+
63
+ You interpret:
64
+ - patterns of attachment
65
+ - repetitive conditioning
66
+ - identity fixation
67
+ - emotional reactivity
68
+ - craving/aversion loops
69
+ - mental formations
70
+
71
+ You focus on:
72
+ - direct observation
73
+ - causes and conditions
74
+ - process rather than judgment
75
+ """
76
+
77
+
78
+ def build_bpsy_context(state):
79
+
80
+ """
81
+ Shared phenomenological material.
82
+ """
83
+
84
+ shared = state.shared_psychological_state
85
+
86
+ context = f"""
87
+
88
+ Themes:
89
+ {shared.themes}
90
+
91
+ Conflicts:
92
+ {shared.conflicts}
93
+
94
+ Affects:
95
+ {shared.affects}
96
+
97
+ Defenses:
98
+ {shared.defenses}
99
+
100
+ Symbols:
101
+ {shared.recurring_symbols}
102
+
103
+ History:
104
+ {state.history[-10:]}
105
+ """
106
+
107
+ return context
108
+
109
+
110
+ def run_bpsy_agent(
111
+ llm,
112
+ state,
113
+ user_message
114
+ ):
115
+ """
116
+ Main BPsy execution.
117
+ """
118
+
119
+ # -----------------------------------
120
+ # EXTRACT OBSERVATIONS
121
+ # -----------------------------------
122
+
123
+ observations = extract_psychological_features(
124
+ user_message
125
+ )
126
+
127
+ # -----------------------------------
128
+ # UPDATE SHARED STATE
129
+ # -----------------------------------
130
+
131
+ update_psychological_state(
132
+ state.shared_psychological_state,
133
+ observations,
134
+ source_agent="bpsy"
135
+ )
136
+
137
+ # -----------------------------------
138
+ # BUILD CONTEXT
139
+ # -----------------------------------
140
+
141
+ context = build_bpsy_context(
142
+ state
143
+ )
144
+
145
+ # -----------------------------------
146
+ # FINAL PROMPT
147
+ # -----------------------------------
148
+
149
+ final_prompt = f"""
150
+ {SYSTEM_PROMPT}
151
+
152
+ SHARED CONTEXT:
153
+ {context}
154
+
155
+ USER MESSAGE:
156
+ {user_message}
157
+
158
+ Respond as Agent BPsy.
159
+ """
160
+
161
+ # -----------------------------------
162
+ # LLM CALL
163
+ # -----------------------------------
164
+
165
+ response = llm.invoke(
166
+ final_prompt
167
+ )
168
+
169
+ return response
170
+
171
+
172
+ # ---------------------------------------------------------
173
+ # FOLLOWUP AGENT
174
+ # ---------------------------------------------------------
175
+
176
+ def bpsy_followup_agent(state):
177
+
178
+ last_user_input = (
179
+ state.history[-1]["content"]
180
+ )
181
+
182
+ prompt = f"""
183
+ Based on the user's further input:
184
+
185
+ "{last_user_input}"
186
+
187
+ Decide whether deeper exploration
188
+ is needed.
189
+
190
+ If yes:
191
+ continue Buddhist-psychological inquiry.
192
+
193
+ Otherwise:
194
+ suggest alternative perspectives.
195
+
196
+ Respond either with:
197
+ - a reflective continuation
198
+ - OR a list of options
199
+ """
200
+
201
+ response = ask_llm(prompt)
202
+
203
+ return {
204
+ "type": "choice",
205
+ "agent": "bpsy",
206
+ "content": (
207
+ response
208
+ + "\n\n"
209
+ + "Which perspective would you like to explore next?"
210
+ ),
211
+ "choices": [
212
+ {
213
+ "id": "jung",
214
+ "label": "1. Jungian"
215
+ },
216
+ {
217
+ "id": "dream",
218
+ "label": "2. Dream Analysis"
219
+ },
220
+ {
221
+ "id": "shadow",
222
+ "label": "3. Shadow"
223
+ },
224
+ {
225
+ "id": "myth",
226
+ "label": "4. Mythological"
227
+ },
228
+ {
229
+ "id": "epistemic",
230
+ "label": "5. Epistemic"
231
+ },
232
+ {
233
+ "id": "jred",
234
+ "label": "6. Comparative Philosophy"
235
+ }
236
+ ]
237
+ }
web/index.html CHANGED
@@ -24,7 +24,7 @@
24
  <button onclick="loadSession()">Load</button>
25
 
26
  <!-- COMMAND BOX -->
27
- <input id="command_input" type="text" placeholder="/analysis -client -jung" style="width:200px;">
28
  <button onclick="submitCommand()">Command</button>
29
 
30
 
 
24
  <button onclick="loadSession()">Load</button>
25
 
26
  <!-- COMMAND BOX -->
27
+ <input id="command_input" type="text" placeholder="/analysis client jung" style="width:200px;">
28
  <button onclick="submitCommand()">Command</button>
29
 
30