Eric Botti commited on
Commit
c30db82
·
1 Parent(s): a1d14dd

removing old testing

Browse files
Files changed (2) hide show
  1. src/agent_process.py +0 -196
  2. src/reasoning_tools.py +0 -73
src/agent_process.py DELETED
@@ -1,196 +0,0 @@
1
-
2
-
3
-
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
11
- Provide:
12
- - Brief description of the game
13
- - IF Herd:
14
- - The Animal the player is pretending to be
15
- - ELSE IF Chameleon
16
- - Prompt that the player is the chameleon and doesn't know the answer but must pretend to know.
17
- - What each player before the current player has answered
18
- Output:
19
- - A Chain of Thought Style response thinking about what to respond
20
-
21
- ACTION STEP
22
- Provide:
23
- - All of the previous + the THOUGHT output
24
- Output:
25
- - A statement about the animal e.g. Respond("I am hairy")
26
- """
27
-
28
- """Voting Stage:
29
- THOUGHT STEP
30
- Provide:
31
- - Brief description of the game
32
- - IF Herd:
33
- - The Animal the player is pretending to be
34
- - ELSE IF Chameleon
35
- - Prompt that the player is the chameleon they ought not to vote for themselves
36
- - What each player answered during the round
37
- - A prompt instruction the agent to choose
38
- Output:
39
- - A Chain of Thought style response thinking about who would be best to vote for
40
-
41
- ACTION STEP
42
- Provide:
43
- - All of the previous + the THOUGHT output
44
- - Instruction to vote for the player of their choice
45
- Output:
46
- - Another player's name as a choice of who to vote for e.g. Vote("Lisa")
47
- """
48
-
49
- import random
50
- import json
51
- import re
52
- import uuid
53
-
54
- from langchain.prompts import PromptTemplate
55
- from agents import PlayerAgent, llm_parameters
56
- from reasoning_tools import extract_vote
57
-
58
- chameleon_agent = PlayerAgent(role="chameleon")
59
- herd_agent = PlayerAgent(role="herd")
60
- judge_agent = PlayerAgent(role="judge")
61
-
62
- # Game Setup
63
- NUM_PLAYERS = 5
64
- ANIMALS = ["Shark", "Tiger", "Owl", "Gorilla"]
65
-
66
- selected_animal = random.choice(ANIMALS)
67
- selected_chameleon = random.randint(0, NUM_PLAYERS - 1)
68
-
69
- print(f'Animal: {selected_animal}')
70
- print(f'Chameleon: Player {selected_chameleon+1}')
71
-
72
- GAME_RULES = '''\
73
- GAME RULES: You are playing a social deduction game where every player pretends the be the same animal.
74
- During the round players go around the room and make an "I"-statement as if they were the animal.
75
- 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.
76
- 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.
77
- WIN CONDITION:
78
- - If the other players vote for the Chameleon, they win the game.
79
- - If they vote for the wrong player then the Chameleon wins the game.
80
-
81
- '''
82
-
83
- HERD_PROMPT = """\
84
- 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.
85
- Previously Mentioned Descriptions:
86
- {player_responses}
87
- Your Response:
88
- """
89
-
90
- CHAMELEON_PROMPT = """\
91
- 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.
92
- Previously Mentioned Descriptions:
93
- {player_responses}
94
- Your Response:
95
- """
96
-
97
- herd_prompt_template = PromptTemplate(
98
- input_variables=["animal", "player_responses"],
99
- template=GAME_RULES + HERD_PROMPT
100
- )
101
- chameleon_prompt_template = PromptTemplate(
102
- input_variables=["player_responses"],
103
- template=GAME_RULES + CHAMELEON_PROMPT
104
- )
105
-
106
-
107
- # Game Round, all the players go around and describe the animal
108
- player_responses = []
109
- formatted_player_responses = ''
110
- for i in range(0, NUM_PLAYERS):
111
- if i == selected_chameleon:
112
- role = "chameleon"
113
- prompt = chameleon_prompt_template.format_prompt(player_responses=formatted_player_responses)
114
- response = chameleon_agent.invoke({"input": prompt})
115
- else:
116
- role = "herd"
117
- prompt = herd_prompt_template.format_prompt(animal=selected_animal, player_responses=formatted_player_responses)
118
- response = herd_agent.invoke({"input": prompt})
119
-
120
- # record thought process - TODO: make this into seperate func
121
- intermediate_steps = []
122
- if response['intermediate_steps']:
123
- for step in response['intermediate_steps']:
124
- intermediate_steps.append(
125
- {
126
- "agent_action": {
127
- "tool": step[0].tool,
128
- "tool_input": step[0].tool_input,
129
- "log": step[0].log
130
- },
131
- "output": step[1]
132
- }
133
- )
134
-
135
-
136
- # Record the LLM Call
137
- player_response = {
138
- "name": f"Player {i+1}",
139
- "role": role,
140
- "prompt": prompt.to_string(),
141
- "intermediate_steps": intermediate_steps,
142
- "final_answer": response['output']
143
- }
144
-
145
- print(intermediate_steps)
146
-
147
- player_responses.append(player_response)
148
- # Nicely Formatted String of Responses
149
- formatted_player_responses += f"- Player {i + 1}: {response['output']}\n"
150
-
151
-
152
- 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.
153
- Player responses:
154
- {formatted_player_responses}
155
-
156
- Please vote for the player you think is most likely to be the Chameleon.
157
- """
158
-
159
- print(JUDGE_PROMPT)
160
-
161
- # election = [0*NUM_PLAYERS]
162
-
163
- judge_response = judge_agent.invoke({"input": JUDGE_PROMPT})
164
-
165
-
166
-
167
-
168
- # Determine Winner - doesn't work because sometimes the judges final answer will mention multiple players...
169
- player_vote = extract_vote(judge_response['output'])
170
-
171
- print(player_vote)
172
-
173
- herd_win = re.match(f"Player {selected_chameleon+1}".lower(), player_vote.lower())
174
- if herd_win:
175
- winner = "Herd"
176
- else:
177
- winner = "Chameleon"
178
-
179
- print(f"The {winner} has won!")
180
-
181
- # Save the experiment
182
- game_ruleset = 'vote'
183
- experiment_id = f"{game_ruleset}-{uuid.uuid4().hex}"
184
- experiment = {
185
- "experiment_id": experiment_id,
186
- "game_ruleset": game_ruleset,
187
- "chameleon_llm_parameters": llm_parameters['chameleon'],
188
- "herd_llm_parameters": llm_parameters['herd'],
189
- "judge_llm_parameters": llm_parameters['judge'],
190
- "player_responses": player_responses
191
- }
192
-
193
- experiment_path = os.path.join(os.pardir, 'experiments', f"{experiment_id}.json")
194
- with open(experiment_path, "w") as output_file:
195
- output_file.write(json.dumps(experiment, indent=4))
196
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/reasoning_tools.py DELETED
@@ -1,73 +0,0 @@
1
- from typing import Dict, Any
2
-
3
- from langchain.prompts import PromptTemplate
4
- from langchain.tools import Tool
5
- from langchain_openai import ChatOpenAI
6
- from langchain.chains import LLMChain
7
-
8
- tool_llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0)
9
-
10
- # Tools
11
- # Get Animals From Description
12
- LIKELY_ANIMALS_PROMPT = """Given the following description of an animal, discuss what possible animals it could be referring to and reasons why.
13
- Animal Description:
14
- {animal_description}
15
- Possible Animals:
16
- """
17
-
18
- likely_animals_prompt = PromptTemplate(
19
- input_variables=['animal_description'],
20
- template=LIKELY_ANIMALS_PROMPT
21
- )
22
-
23
- likely_animals_chain = LLMChain(llm=tool_llm, prompt=likely_animals_prompt)
24
-
25
- def get_likely_animals(description: str) -> str:
26
- """Provides animals from a description"""
27
- return likely_animals_chain.invoke(input={'animal_description': description})['text']
28
-
29
-
30
- # Animal Match Tool
31
- ANIMAL_MATCH_PROMPT = """\
32
- Consider whether the following animal matches with the description provided. Provide your logic for reaching the conclusion.
33
- ANIMAL:
34
- {animal}
35
- Description:
36
- {description}
37
- Your Thoughts:
38
- """
39
-
40
- animal_match_template = PromptTemplate(
41
- input_variables=['animal', 'description'],
42
- template=ANIMAL_MATCH_PROMPT
43
- )
44
-
45
- animal_match_tool = LLMChain(llm=tool_llm, prompt=likely_animals_prompt)
46
-
47
-
48
- def does_animal_match_description(animal: str, description: str) -> dict[str, Any]:
49
- """Given an animal and a description, consider whether the animal matches that description"""
50
- return animal_match_tool.invoke(input={"animal": animal, "description": description})['text']
51
-
52
-
53
- animal_tools = [
54
- Tool(
55
- name='get_likely_animals',
56
- func=get_likely_animals,
57
- description='used to get a list of potential animals corresponding to a description of an animal'
58
- )
59
- ]
60
-
61
- VOTE_EXTRACTION_PROMPT = """Extract the name of the player being voted for, from the following statement:
62
- {statement}"""
63
-
64
- vote_extraction_template = PromptTemplate(
65
- input_variables=['statement'],
66
- template=VOTE_EXTRACTION_PROMPT
67
- )
68
-
69
- vote_extraction_chain = LLMChain(llm=tool_llm, prompt=vote_extraction_template)
70
-
71
- def extract_vote(statement: str) -> str:
72
- """Extract the name of the player being voted for from the statement"""
73
- return vote_extraction_chain.invoke(input={"statement":statement})['text']