Eric Botti commited on
Commit
70dce6e
·
1 Parent(s): ade28aa

added game output collecting

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. src/agent_process.py +77 -40
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ /venv/
2
+ /experiments/
src/agent_process.py CHANGED
@@ -4,9 +4,7 @@
4
  """Beginning of the round:
5
  1. Assigned each player role
6
  2. determine the order in which the players will speak"""
7
-
8
-
9
- import regex
10
 
11
  """Describing Stage
12
  THOUGHT STEP
@@ -60,12 +58,30 @@ from langchain.prompts import PromptTemplate
60
  from reasoning_tools import tools
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- # All Players are the same Agent
65
- model_name = 'gpt-3.5-turbo'
66
- temperature = 1
67
- llm = ChatOpenAI(model=model_name, temperature=temperature)
68
- agent_executor = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
69
 
70
  # Game Setup
71
  NUM_PLAYERS = 5
@@ -111,30 +127,55 @@ chameleon_prompt_template = PromptTemplate(
111
  template=GAME_RULES + CHAMELEON_PROMPT
112
  )
113
 
114
- # Game chains
115
- # Herd
116
- # herd_chain = LLMChain(llm=herd_llm, prompt=herd_prompt_template)
117
- # chameleon_chain = LLMChain(llm=chameleon_llm, prompt=chameleon_prompt_template)
118
- # chain = new_sequential_chain()
119
 
 
120
  player_responses = []
 
121
  for i in range(0, NUM_PLAYERS):
122
  if i == selected_chameleon:
123
- prompt = chameleon_prompt_template.format_prompt(player_responses=player_responses)
124
- player_response = agent_executor.invoke({"input": prompt})['output']
 
125
  else:
126
- prompt = herd_prompt_template.format_prompt(animal=selected_animal, player_responses=player_responses)
127
- player_response = agent_executor.invoke({"input": prompt})['output']
128
- # Nicely Formatted Responses
129
- player_responses.append(f"Player {i + 1}: {player_response}")
130
- print(player_responses[i])
131
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- formatted_player_response = '\n- '.join(player_responses)
134
 
135
  JUDGE_PROMPT = f"""You are playing a game called Chameleon. In this game, there are {NUM_PLAYERS} players. {NUM_PLAYERS - 1} of them are all the same animal. 1 player is a different animal who is pretending to be the same animal as everyone else. You are the judge of this game. Each player has said a sentence describing themselves. Use these descriptions to decide which player is most likely not describing the same animal as everyone else.
136
  Player responses:
137
- - {formatted_player_response}
138
 
139
  Please vote for the player you think is most likely to be the Chameleon.
140
  """
@@ -143,38 +184,34 @@ print(JUDGE_PROMPT)
143
 
144
  # election = [0*NUM_PLAYERS]
145
 
146
- judge_response = agent_executor.invoke({"input": JUDGE_PROMPT})
147
 
148
 
149
 
150
 
151
- # Determine Winner
152
  herd_win = re.match(f"Player {selected_chameleon+1}", judge_response['output'])
153
  if herd_win:
154
- print("The Herd has won!")
155
  else:
156
- print("The Chameleon has won!")
157
 
 
158
 
159
  # Save the experiment
160
- experiment_id = f"game-{uuid.uuid4().hex}"
 
161
  experiment = {
162
  "experiment_id": experiment_id,
163
- "chameleon_llm_parameters": {
164
- "model_name": model_name,
165
- "temperature": temperature
166
- },
167
- "herd_llm_parameters": {
168
- "model_name": model_name,
169
- "temperature": temperature
170
- },
171
- "player_agents": [
172
-
173
- ],
174
  "game_ruleset": game_ruleset,
 
 
 
 
175
  }
176
 
177
- with open(f"{experiment_id}.json", "w") as output_file:
 
178
  output_file.write(json.dumps(experiment))
179
 
180
 
 
4
  """Beginning of the round:
5
  1. Assigned each player role
6
  2. determine the order in which the players will speak"""
7
+ import os
 
 
8
 
9
  """Describing Stage
10
  THOUGHT STEP
 
58
  from reasoning_tools import tools
59
 
60
 
61
+ # LLM Configuration for each role
62
+ # TODO: Agents keep throwing OutputParserExceptions can't parse output, need to look into this
63
+ # Chameleon
64
+ chameleon_llm_params = {
65
+ 'model': 'gpt-3.5-turbo',
66
+ 'temperature': 1
67
+ }
68
+ chameleon_llm = ChatOpenAI(**chameleon_llm_params)
69
+ chameleon_agent = initialize_agent(tools, chameleon_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
70
+ # Herd
71
+ herd_llm_params = {
72
+ 'model': 'gpt-3.5-turbo',
73
+ 'temperature': 1
74
+ }
75
+ herd_llm = ChatOpenAI(**herd_llm_params)
76
+ herd_agent = initialize_agent(tools, chameleon_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
77
+ # Judge
78
+ judge_llm_params = {
79
+ 'model': 'gpt-3.5-turbo',
80
+ 'temperature': 1
81
+ }
82
+ judge_llm = ChatOpenAI(**judge_llm_params)
83
+ judge_agent = initialize_agent(tools, chameleon_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
84
 
 
 
 
 
 
85
 
86
  # Game Setup
87
  NUM_PLAYERS = 5
 
127
  template=GAME_RULES + CHAMELEON_PROMPT
128
  )
129
 
 
 
 
 
 
130
 
131
+ # Game Round, all the players go around and describe the animal
132
  player_responses = []
133
+ formatted_player_responses = ''
134
  for i in range(0, NUM_PLAYERS):
135
  if i == selected_chameleon:
136
+ role = "chameleon"
137
+ prompt = chameleon_prompt_template.format_prompt(player_responses=formatted_player_responses)
138
+ response = chameleon_agent.invoke({"input": prompt})
139
  else:
140
+ role = "herd"
141
+ prompt = herd_prompt_template.format_prompt(animal=selected_animal, player_responses=formatted_player_responses)
142
+ response = herd_agent.invoke({"input": prompt})
143
+
144
+ # record thought process - TODO: make this into seperate func
145
+ intermediate_steps = []
146
+ if response['intermediate_steps']:
147
+ for step in response['intermediate_steps']:
148
+ intermediate_steps.append(
149
+ {
150
+ "agent_action": {
151
+ "tool": step[0].tool,
152
+ "tool_input": step[0].tool_input,
153
+ "log": step[0].log
154
+ },
155
+ "output": step[1]
156
+ }
157
+ )
158
+
159
+
160
+ # Record the LLM Call
161
+ player_response = {
162
+ "name": f"Player {i+1}",
163
+ "role": role,
164
+ "prompt": prompt.to_string(),
165
+ "intermediate_steps": intermediate_steps,
166
+ "final_answer": response['output']
167
+ }
168
+
169
+ print(intermediate_steps)
170
+
171
+ player_responses.append(player_response)
172
+ # Nicely Formatted String of Responses
173
+ formatted_player_responses += f"- Player {i + 1}: {response['output']}\n"
174
 
 
175
 
176
  JUDGE_PROMPT = f"""You are playing a game called Chameleon. In this game, there are {NUM_PLAYERS} players. {NUM_PLAYERS - 1} of them are all the same animal. 1 player is a different animal who is pretending to be the same animal as everyone else. You are the judge of this game. Each player has said a sentence describing themselves. Use these descriptions to decide which player is most likely not describing the same animal as everyone else.
177
  Player responses:
178
+ {formatted_player_responses}
179
 
180
  Please vote for the player you think is most likely to be the Chameleon.
181
  """
 
184
 
185
  # election = [0*NUM_PLAYERS]
186
 
187
+ judge_response = judge_agent.invoke({"input": JUDGE_PROMPT})
188
 
189
 
190
 
191
 
192
+ # Determine Winner - doesn't work because sometimes the judges final answer will mention multiple players...
193
  herd_win = re.match(f"Player {selected_chameleon+1}", judge_response['output'])
194
  if herd_win:
195
+ winner = "Herd"
196
  else:
197
+ winner = "Chameleon"
198
 
199
+ print(f"The {winner} has won!")
200
 
201
  # Save the experiment
202
+ game_ruleset = 'judge'
203
+ experiment_id = f"{game_ruleset}-{uuid.uuid4().hex}"
204
  experiment = {
205
  "experiment_id": experiment_id,
 
 
 
 
 
 
 
 
 
 
 
206
  "game_ruleset": game_ruleset,
207
+ "chameleon_llm_parameters": chameleon_llm_params,
208
+ "herd_llm_parameters": herd_llm_params,
209
+ "judge_llm_parameters": judge_llm_params,
210
+ "player_responses": player_responses
211
  }
212
 
213
+ experiment_path = os.path.join(os.pardir, 'experiments', f"{experiment_id}.json")
214
+ with open(experiment_path, "w") as output_file:
215
  output_file.write(json.dumps(experiment))
216
 
217