Eric Botti commited on
Commit
ade28aa
·
1 Parent(s): 06aab8b

upgrade Herd to use ReAct reasoning

Browse files
Files changed (1) hide show
  1. src/agent_process.py +49 -20
src/agent_process.py CHANGED
@@ -4,8 +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
- import json
8
- import re
9
 
10
  import regex
11
 
@@ -49,19 +48,24 @@ Output:
49
  - Another player's name as a choice of who to vote for e.g. Vote("Lisa")
50
  """
51
  import random
 
 
 
52
 
53
  from langchain.agents import initialize_agent
54
  from langchain.agents import AgentType
55
  from langchain.chat_models import ChatOpenAI
56
  from langchain.prompts import PromptTemplate
57
- from langchain.chains import LLMChain
58
 
59
  from reasoning_tools import tools
60
 
61
 
62
- herd_llm = ChatOpenAI(temperature=0.7)
63
- chameleon_llm = ChatOpenAI(model='gpt-4', temperature=0.4)
64
- decision_llm = ChatOpenAI(model='gpt-4', temperature=1)
 
 
 
65
 
66
  # Game Setup
67
  NUM_PLAYERS = 5
@@ -74,17 +78,25 @@ print(f'Animal: {selected_animal}')
74
  print(f'Chameleon: Player {selected_chameleon+1}')
75
 
76
  GAME_RULES = '''\
77
- GAME RULES: You are playing a social deduction game where every player pretends the be the same animal. The players go around the room and make an "I"-statement as if they were the animal. All players know what animal they are pretending to be, except one who is known as the Chameleon. The Chameleon and must blend in by providing details about the animal using context from other players. The other players must be careful not to give away too much information with their responses so that Chameleon cannot guess the animal. After all players have spoken, they vote on who they think the Chameleon is. If the other players vote for the Chameleon, they win the game. If they vote for the wrong player then the Chameleon wins the game.
 
 
 
 
 
 
78
 
79
  '''
80
 
81
- HERD_PROMPT = """You are a {animal}. In 10 words or less give a description of yourself starting with "I". Do not repeat responses from other players.
 
82
  Previously Mentioned Descriptions:
83
  {player_responses}
84
  Your Response:
85
  """
86
 
87
- CHAMELEON_PROMPT = """You are the Chameleon. You don't know what animal the other players are. Using the context that they have provided, In 10 words or less give a description of yourself starting with "I". If no one else has said anything try to say something generic that could be true of any animals.
 
88
  Previously Mentioned Descriptions:
89
  {player_responses}
90
  Your Response:
@@ -101,21 +113,18 @@ chameleon_prompt_template = PromptTemplate(
101
 
102
  # Game chains
103
  # Herd
104
- herd_chain = LLMChain(llm=herd_llm, prompt=herd_prompt_template)
105
- # Chameleon
106
- agent_executor = initialize_agent(tools, decision_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
107
-
108
-
109
  # chameleon_chain = LLMChain(llm=chameleon_llm, prompt=chameleon_prompt_template)
110
  # chain = new_sequential_chain()
111
 
112
  player_responses = []
113
  for i in range(0, NUM_PLAYERS):
114
  if i == selected_chameleon:
115
- # player_response = chameleon_chain.run(player_responses=player_responses)
116
- player_response = agent_executor.invoke({"input": chameleon_prompt_template.format_prompt(player_responses=player_responses)})['output']
117
  else:
118
- player_response = herd_chain.run(animal=selected_animal, player_responses=player_responses)
 
119
  # Nicely Formatted Responses
120
  player_responses.append(f"Player {i + 1}: {player_response}")
121
  print(player_responses[i])
@@ -136,9 +145,7 @@ print(JUDGE_PROMPT)
136
 
137
  judge_response = agent_executor.invoke({"input": JUDGE_PROMPT})
138
 
139
- with open("response.json", "w") as output_file:
140
- judge = json.dumps(judge_response)
141
- output_file.write(judge)
142
 
143
 
144
  # Determine Winner
@@ -149,6 +156,28 @@ else:
149
  print("The Chameleon has won!")
150
 
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  # # This is an LLMChain to write a synopsis given a title of a play and the era it is set in.
153
  # llm = OpenAI(temperature=.7)
154
  # synopsis_template = """You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
 
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
 
 
48
  - Another player's name as a choice of who to vote for e.g. Vote("Lisa")
49
  """
50
  import random
51
+ import json
52
+ import re
53
+ import uuid
54
 
55
  from langchain.agents import initialize_agent
56
  from langchain.agents import AgentType
57
  from langchain.chat_models import ChatOpenAI
58
  from langchain.prompts import PromptTemplate
 
59
 
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
 
78
  print(f'Chameleon: Player {selected_chameleon+1}')
79
 
80
  GAME_RULES = '''\
81
+ GAME RULES: You are playing a social deduction game where every player pretends the be the same animal.
82
+ During the round players go around the room and make an "I"-statement as if they were the animal.
83
+ All players know what animal they are pretending to be, except one who is known as the Chameleon. The Chameleon and must blend in by providing details about the animal using context from other players.
84
+ The other players must be careful not to give away too much information with their responses so that Chameleon cannot guess the animal. After all players have spoken, they vote on who they think the Chameleon is.
85
+ WIN CONDITION:
86
+ - If the other players vote for the Chameleon, they win the game.
87
+ - If they vote for the wrong player then the Chameleon wins the game.
88
 
89
  '''
90
 
91
+ HERD_PROMPT = """\
92
+ You are a {animal}. In 10 words or less give a description of yourself starting with "I". The description should not give away too much information about the {animal} as you do not want the Chameleon to be able to guess what animal you are. Do not repeat responses from other players.
93
  Previously Mentioned Descriptions:
94
  {player_responses}
95
  Your Response:
96
  """
97
 
98
+ CHAMELEON_PROMPT = """\
99
+ You are the Chameleon. Initially, you don't know what animal the other players are, your goal is to deduce . Using the context that they have provided, In 10 words or less give a description of yourself starting with "I". If no one else has said anything try to say something generic that could be true of any animals.
100
  Previously Mentioned Descriptions:
101
  {player_responses}
102
  Your Response:
 
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])
 
145
 
146
  judge_response = agent_executor.invoke({"input": JUDGE_PROMPT})
147
 
148
+
 
 
149
 
150
 
151
  # Determine Winner
 
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
+
181
  # # This is an LLMChain to write a synopsis given a title of a play and the era it is set in.
182
  # llm = OpenAI(temperature=.7)
183
  # synopsis_template = """You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.