Dirk Haupt commited on
Commit
3f2a1dc
·
1 Parent(s): 90afc49

AI decision phase but AI doesn't lay out its reasoning

Browse files
Files changed (1) hide show
  1. app.py +60 -26
app.py CHANGED
@@ -10,6 +10,7 @@ from timer import AsyncTimer
10
  import httpx
11
  import glob
12
  from string import Template
 
13
 
14
  # DEFAULT_PROMPT = """You are participating in a Prisoner's Dilemma game. You will have a conversation with the human player before making your decision to either cooperate (C) or defect (D).
15
 
@@ -87,8 +88,8 @@ def build_decision_prompt(game_name, coplayer, currency, total_sum=None):
87
  template_vars = {
88
  "coplayer": coplayer,
89
  "currency": currency,
90
- "move1": "C",
91
- "move2": "D"
92
  }
93
 
94
  # Add total_sum if needed
@@ -109,6 +110,13 @@ class GameState:
109
  phase: str = "INIT" # INIT, CONVERSATION, USER_DECISION, AI_DECISION, RESULTS, NEXT_ROUND
110
  timer_start: float = None
111
  ai_reflection: str = None # Store AI's reflection
 
 
 
 
 
 
 
112
 
113
  class StreamlitWebSocketHandler(WebSocketHandler):
114
  async def on_message(self, message: SubscribeEvent):
@@ -138,6 +146,31 @@ class StreamlitWebSocketHandler(WebSocketHandler):
138
  # Force streamlit to rerun and update the UI
139
  # st.rerun()
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  async def create_hume_config(system_prompt: str) -> str:
142
  """Create a new Hume.ai config with custom system prompt"""
143
  url = "https://api.hume.ai/v0/evi/configs"
@@ -168,7 +201,9 @@ async def create_hume_config(system_prompt: str) -> str:
168
  async with httpx.AsyncClient() as client:
169
  response = await client.post(url, headers=headers, json=data)
170
  if 200 <= response.status_code < 300:
171
- return response.json()["id"]
 
 
172
  else:
173
  st.error(f"{response.status_code} Failed to create config: {response.text}")
174
  return os.getenv("HUME_CONFIG_ID") # Fallback to default config
@@ -177,10 +212,17 @@ async def run_chat():
177
  # Initialize client and handlers
178
  client = AsyncHumeClient(api_key=os.getenv("HUME_API_KEY"))
179
 
180
- # Check if system prompt was modified
181
- config_id = os.getenv("HUME_CONFIG_ID")
182
- # if st.session_state.system_prompt != DEFAULT_PROMPT:
183
- config_id = await create_hume_config(st.session_state.system_prompt)
 
 
 
 
 
 
 
184
 
185
  # Add resumed_chat_group_id to options if we have one
186
  options_dict = {
@@ -206,19 +248,7 @@ async def run_chat():
206
  # Q: Which Option do you choose, "Option C" or "Option D"?
207
  # A: Option """
208
  # }
209
- if st.session_state.game.phase == "AI_DECISION":
210
- # Build decision prompt using same variables as system prompt
211
- decision_prompt = build_decision_prompt(
212
- game_name=st.session_state.game_name,
213
- emotion_type=st.session_state.emotion_type,
214
- coplayer=st.session_state.coplayer,
215
- currency=st.session_state.currency,
216
- total_sum=getattr(st.session_state, 'total_sum', None)
217
- )
218
- options_dict["initial_message"] = {
219
- "role": "user",
220
- "content": decision_prompt
221
- }
222
 
223
  options = ChatConnectOptions(**options_dict)
224
  websocket_handler = StreamlitWebSocketHandler()
@@ -243,7 +273,6 @@ async def run_chat():
243
  await microphone_task
244
 
245
  async def handle_conversation_phase():
246
- print("handle_conversation_phase")
247
  """Handle the CONVERSATION phase with timer and chat"""
248
  timer_placeholder = st.empty()
249
 
@@ -294,12 +323,11 @@ if 'game' not in st.session_state:
294
  # currency=currency,
295
  # total_sum=total_sum
296
  # )
297
-
298
  # Show system prompt in all phases
299
  if st.session_state.game.phase != "INIT":
300
  st.expander("System Prompt", expanded=False).text_area(
301
  "System Prompt",
302
- value=st.session_state.system_prompt,
303
  height=400,
304
  disabled=True,
305
  label_visibility="collapsed"
@@ -355,7 +383,7 @@ if st.session_state.game.phase == "INIT":
355
  )
356
 
357
  # Build and display system prompt
358
- st.session_state.system_prompt = build_system_prompt(
359
  game_name=game_name,
360
  emotion_type=emotion_type,
361
  coplayer=coplayer,
@@ -365,16 +393,22 @@ if st.session_state.game.phase == "INIT":
365
 
366
  st.text_area(
367
  "System Prompt",
368
- value=st.session_state.system_prompt,
369
  height=400,
370
  key="system_prompt",
371
  help="The generated system prompt based on your selections"
372
  )
373
 
374
  if st.button("Start Game"):
375
- print("START GAME")
376
  st.session_state.game.phase = "CONVERSATION"
377
  st.session_state.game.timer_start = time.time()
 
 
 
 
 
 
 
378
  st.rerun()
379
 
380
  elif st.session_state.game.phase == "CONVERSATION":
 
10
  import httpx
11
  import glob
12
  from string import Template
13
+ import atexit
14
 
15
  # DEFAULT_PROMPT = """You are participating in a Prisoner's Dilemma game. You will have a conversation with the human player before making your decision to either cooperate (C) or defect (D).
16
 
 
88
  template_vars = {
89
  "coplayer": coplayer,
90
  "currency": currency,
91
+ "move1": "J",
92
+ "move2": "F"
93
  }
94
 
95
  # Add total_sum if needed
 
110
  phase: str = "INIT" # INIT, CONVERSATION, USER_DECISION, AI_DECISION, RESULTS, NEXT_ROUND
111
  timer_start: float = None
112
  ai_reflection: str = None # Store AI's reflection
113
+ system_prompt: str = None
114
+ # Add game configuration
115
+ game_name: str = None
116
+ emotion_type: str = None
117
+ coplayer: str = None
118
+ currency: str = None
119
+ total_sum: int = None
120
 
121
  class StreamlitWebSocketHandler(WebSocketHandler):
122
  async def on_message(self, message: SubscribeEvent):
 
146
  # Force streamlit to rerun and update the UI
147
  # st.rerun()
148
 
149
+ # Keep track of created configs
150
+ if 'created_configs' not in st.session_state:
151
+ st.session_state.created_configs = set()
152
+
153
+ async def delete_config(config_id: str):
154
+ """Delete a Hume.ai config"""
155
+ url = f"https://api.hume.ai/v0/evi/configs/{config_id}"
156
+ headers = {
157
+ "X-Hume-Api-Key": os.getenv("HUME_API_KEY")
158
+ }
159
+
160
+ async with httpx.AsyncClient() as client:
161
+ response = await client.delete(url, headers=headers)
162
+ if response.status_code != 204:
163
+ print(f"Failed to delete config {config_id}: {response.text}")
164
+
165
+ def cleanup_configs():
166
+ """Delete all created configs on app shutdown"""
167
+ for config_id in st.session_state.created_configs:
168
+ asyncio.run(delete_config(config_id))
169
+ st.session_state.created_configs.clear()
170
+
171
+ # Register cleanup function
172
+ atexit.register(cleanup_configs)
173
+
174
  async def create_hume_config(system_prompt: str) -> str:
175
  """Create a new Hume.ai config with custom system prompt"""
176
  url = "https://api.hume.ai/v0/evi/configs"
 
201
  async with httpx.AsyncClient() as client:
202
  response = await client.post(url, headers=headers, json=data)
203
  if 200 <= response.status_code < 300:
204
+ config_id = response.json()["id"]
205
+ st.session_state.created_configs.add(config_id)
206
+ return config_id
207
  else:
208
  st.error(f"{response.status_code} Failed to create config: {response.text}")
209
  return os.getenv("HUME_CONFIG_ID") # Fallback to default config
 
212
  # Initialize client and handlers
213
  client = AsyncHumeClient(api_key=os.getenv("HUME_API_KEY"))
214
 
215
+ if st.session_state.game.phase == "AI_DECISION":
216
+ # Build decision prompt using same variables as system prompt
217
+ decision_prompt = build_decision_prompt(
218
+ game_name=st.session_state.game.game_name,
219
+ coplayer=st.session_state.game.coplayer,
220
+ currency=st.session_state.game.currency,
221
+ total_sum=st.session_state.game.total_sum
222
+ )
223
+ config_id = await create_hume_config(decision_prompt)
224
+ else:
225
+ config_id = await create_hume_config(st.session_state.game.system_prompt)
226
 
227
  # Add resumed_chat_group_id to options if we have one
228
  options_dict = {
 
248
  # Q: Which Option do you choose, "Option C" or "Option D"?
249
  # A: Option """
250
  # }
251
+
 
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  options = ChatConnectOptions(**options_dict)
254
  websocket_handler = StreamlitWebSocketHandler()
 
273
  await microphone_task
274
 
275
  async def handle_conversation_phase():
 
276
  """Handle the CONVERSATION phase with timer and chat"""
277
  timer_placeholder = st.empty()
278
 
 
323
  # currency=currency,
324
  # total_sum=total_sum
325
  # )
 
326
  # Show system prompt in all phases
327
  if st.session_state.game.phase != "INIT":
328
  st.expander("System Prompt", expanded=False).text_area(
329
  "System Prompt",
330
+ value=st.session_state.game.system_prompt,
331
  height=400,
332
  disabled=True,
333
  label_visibility="collapsed"
 
383
  )
384
 
385
  # Build and display system prompt
386
+ st.session_state.game.system_prompt = build_system_prompt(
387
  game_name=game_name,
388
  emotion_type=emotion_type,
389
  coplayer=coplayer,
 
393
 
394
  st.text_area(
395
  "System Prompt",
396
+ value=st.session_state.game.system_prompt,
397
  height=400,
398
  key="system_prompt",
399
  help="The generated system prompt based on your selections"
400
  )
401
 
402
  if st.button("Start Game"):
 
403
  st.session_state.game.phase = "CONVERSATION"
404
  st.session_state.game.timer_start = time.time()
405
+ # Store configuration in game state
406
+ st.session_state.game.game_name = game_name
407
+ st.session_state.game.emotion_type = emotion_type
408
+ st.session_state.game.coplayer = coplayer
409
+ st.session_state.game.currency = currency
410
+ st.session_state.game.total_sum = total_sum
411
+ st.session_state.game.system_prompt = st.session_state.system_prompt
412
  st.rerun()
413
 
414
  elif st.session_state.game.phase == "CONVERSATION":