Skip to content

Commit 1985e7d

Browse files
author
Satvik Golechha
committed
command line is best :(
1 parent 453424d commit 1985e7d

4 files changed

Lines changed: 92 additions & 42 deletions

File tree

among-agents/amongagents/agent/agent.py

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -265,68 +265,115 @@ def choose_observation_location(self, map):
265265

266266

267267
class HumanAgent(Agent):
268-
def __init__(self, player):
268+
def __init__(self, player, tools=None, game_index=0, agent_config=None, list_of_impostors=None):
269269
super().__init__(player)
270-
271-
def choose_action(self):
272-
print(f"{str(self.player)}")
273-
270+
self.model = "homosapiens/brain-1.0"
271+
self.tools = tools
272+
self.game_index = game_index
273+
self.summarization = "No thought process has been made."
274+
self.processed_memory = "No memory has been processed."
275+
self.log_path = os.getenv("EXPERIMENT_PATH") + "/agent-logs.json"
276+
self.compact_log_path = os.getenv("EXPERIMENT_PATH") + "/agent-logs-compact.json"
277+
278+
# Initialize global session state if it doesn't exist
279+
if os.getenv("FLASK") == "True":
280+
# Global session state for actions across page refreshes
281+
if "human_actions" not in st.session_state:
282+
st.session_state.human_actions = {}
283+
if "action_history" not in st.session_state:
284+
st.session_state.action_history = {}
285+
if "game_started" not in st.session_state:
286+
st.session_state.game_started = False
287+
288+
async def choose_action(self, timestep):
289+
all_info = self.player.all_info_prompt()
274290
available_actions = self.player.get_available_actions()
275-
print(self.player.all_info_prompt())
291+
292+
# Log the start of action selection
293+
action_prompt = f"Available actions:\n" + "\n".join([f"{i+1}: {action}" for i, action in enumerate(available_actions)])
294+
full_prompt = {
295+
"All Info": all_info,
296+
"Available Actions": action_prompt
297+
}
298+
# Command line interface
299+
print(f"{str(self.player)}")
300+
print(all_info)
301+
print("Choose an action:")
302+
for i, action in enumerate(available_actions):
303+
print(f"{i+1}: {action}")
304+
276305
stop_triggered = False
277306
valid_input = False
278307
while (not stop_triggered) and (not valid_input):
279-
print("Choose an action:")
280308
try:
281309
action_idx = int(input())
282310
if action_idx == 0:
283311
stop_triggered = True
284312
elif action_idx < 1 or action_idx > len(available_actions):
285-
raise ValueError(
286-
f"Invalid input. Please enter a number between 1 and {len(available_actions)}."
287-
)
313+
raise ValueError(f"Invalid input. Please enter a number between 1 and {len(available_actions)}.")
288314
else:
289315
valid_input = True
290-
291316
except:
292317
print("Invalid input. Please enter a number.")
293318
continue
319+
294320
if stop_triggered:
295321
raise ValueError("Game stopped by user.")
296-
action = available_actions[action_idx - 1]
297-
if action.name == "SPEAK":
298-
message = self.speak()
299-
action.provide_message(message)
300-
if action.name == "SPEAK":
301-
action.provide_message(message)
302-
return action
322+
323+
selected_action = available_actions[action_idx - 1]
324+
325+
if selected_action.name == "SPEAK":
326+
print("Enter your response:")
327+
action_message = input()
328+
selected_action.provide_message(action_message)
329+
self.log_interaction(sysprompt="Human Agent", prompt=full_prompt,
330+
original_response=f"[Action] {selected_action} with message: {action_message}",
331+
step=timestep)
332+
else:
333+
self.log_interaction(sysprompt="Human Agent", prompt=full_prompt,
334+
original_response=f"[Action] {selected_action}",
335+
step=timestep)
336+
337+
return selected_action
303338

304339
def respond(self, message):
305340
print(message)
306341
response = input()
307342
return response
308343

309-
def speak(self):
310-
print("Enter your response:")
311-
message = input()
312-
return message
313-
314344
def choose_observation_location(self, map):
315-
map = list(map)
316-
print("Please select the room you wish to observe:")
317-
for i, room in enumerate(map):
318-
print(f"{i}: " + room)
345+
map_list = list(map)
319346
while True:
320-
index = int(input())
321-
if index < 0 or index >= len(map):
322-
print(
323-
f"Invalid input. Please enter a number between 0 and {len(map) - 1}."
324-
)
325-
else:
326-
print(map)
327-
print("index", index)
328-
print("map[index]", map[index])
329-
return map[index]
347+
try:
348+
index = int(input())
349+
if index < 0 or index >= len(map_list):
350+
print(f"Invalid input. Please enter a number between 0 and {len(map_list) - 1}.")
351+
else:
352+
return map_list[index]
353+
except:
354+
print("Invalid input. Please enter a number.")
355+
356+
def log_interaction(self, sysprompt, prompt, original_response, step):
357+
"""Log human player interactions similar to LLMAgent"""
358+
interaction = {
359+
'game_index': 'Game ' + str(self.game_index),
360+
'step': step,
361+
"timestamp": str(datetime.now()),
362+
"player": {"name": self.player.name, "identity": self.player.identity, "personality": self.player.personality, "model": self.model, "location": self.player.location},
363+
"interaction": {"system_prompt": sysprompt, "prompt": prompt, "response": original_response, "full_response": original_response},
364+
}
365+
366+
# Write to file
367+
with open(self.log_path, "a") as f:
368+
json.dump(interaction, f, indent=2, separators=(",", ": "))
369+
f.write("\n")
370+
f.flush()
371+
with open(self.compact_log_path, "a") as f:
372+
json.dump(interaction, f, separators=(",", ": "))
373+
f.write("\n")
374+
f.flush()
375+
376+
print(".", end="", flush=True)
330377

331378

332379
class LLMHumanAgent(HumanAgent, LLMAgent):

among-agents/amongagents/envs/game.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def initialize_agents(self):
149149
for i, player in enumerate(self.players):
150150
if self.include_human and i == random_idx:
151151
self.agents.append(HumanAgent(player))
152+
print(f"{i} Initializing player {player.name} with identity {player.identity} and LLM choice {self.agents[-1].model}")
152153
else:
153154
self.agents.append(agent_dict[self.agent_config[player.identity]](player))
154155
print(f"{i} Initializing player {player.name} with identity {player.identity} and LLM choice {self.agents[-1].model}")

human_trials/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44

55
# Generate unique session ID
6-
SESSION_ID = '1'
6+
SESSION_ID = str(uuid.uuid4())[:8]
77

88
# Get experiment date and Git commit hash
99
DATE = datetime.datetime.now().strftime("%Y-%m-%d")
@@ -29,7 +29,7 @@
2929
},
3030
"UI": False,
3131
},
32-
"experiment_name": f"streamlit_session_{SESSION_ID}",
32+
"experiment_name": f"session_{SESSION_ID}",
3333
"logs_path": "expt-logs",
3434
"assets_path": "assets",
3535
}

human_trials/game.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
"agent_config": {
5252
"Impostor": "LLM",
5353
"Crewmate": "LLM",
54-
"IMPOSTOR_LLM_CHOICES": BIG_LIST_OF_MODELS,
55-
"CREWMATE_LLM_CHOICES": BIG_LIST_OF_MODELS,
54+
# "IMPOSTOR_LLM_CHOICES": BIG_LIST_OF_MODELS,
55+
# "CREWMATE_LLM_CHOICES": BIG_LIST_OF_MODELS,
56+
"IMPOSTOR_LLM_CHOICES": ["meta-llama/llama-3.3-70b-instruct"],
57+
"CREWMATE_LLM_CHOICES": ["meta-llama/llama-3.3-70b-instruct"],
5658
},
5759
"UI": False,
5860
"Streamlit": False, # Set to False for command line
@@ -146,4 +148,4 @@ def main():
146148
print("Game results:", game_results)
147149

148150
if __name__ == "__main__":
149-
main()
151+
main()

0 commit comments

Comments
 (0)