Eric Botti commited on
Commit
8c4b624
·
1 Parent(s): 2340286

using agent for chameleon responses, reworked get_likely_animals tool to "discuss"

Browse files
Files changed (2) hide show
  1. src/agent_process.py +27 -36
  2. src/reasoning_tools.py +33 -0
src/agent_process.py CHANGED
@@ -4,7 +4,10 @@
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
  """Describing Stage
10
  THOUGHT STEP
@@ -45,23 +48,20 @@ Provide:
45
  Output:
46
  - Another player's name as a choice of who to vote for e.g. Vote("Lisa")
47
  """
 
48
 
49
-
50
- from langchain.agents import load_tools
51
  from langchain.agents import initialize_agent
52
  from langchain.agents import AgentType
53
- from langchain.llms import OpenAI
54
  from langchain.chat_models import ChatOpenAI
55
  from langchain.prompts import PromptTemplate
56
- from langchain.tools import Tool
57
  from langchain.chains import LLMChain
58
- from langchain.chains import SimpleSequentialChain
59
 
60
- import random
 
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=0)
65
 
66
  # Game Setup
67
  NUM_PLAYERS = 5
@@ -100,14 +100,20 @@ chameleon_prompt_template = PromptTemplate(
100
  )
101
 
102
  # Game chains
 
103
  herd_chain = LLMChain(llm=herd_llm, prompt=herd_prompt_template)
104
- chameleon_chain = LLMChain(llm=chameleon_llm, prompt=chameleon_prompt_template)
 
 
 
 
105
  # chain = new_sequential_chain()
106
 
107
  player_responses = []
108
  for i in range(0, NUM_PLAYERS):
109
  if i == selected_chameleon:
110
- player_response = chameleon_chain.run(player_responses=player_responses)
 
111
  else:
112
  player_response = herd_chain.run(animal=selected_animal, player_responses=player_responses)
113
  # Nicely Formatted Responses
@@ -117,45 +123,30 @@ for i in range(0, NUM_PLAYERS):
117
 
118
  formatted_player_response = '\n- '.join(player_responses)
119
 
120
- VOTING_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.
121
  Player responses:
122
  - {formatted_player_response}
123
 
124
  Please vote for the player you think is most likely to be the Chameleon.
125
  """
126
 
127
- print(VOTING_PROMPT)
128
 
129
  # election = [0*NUM_PLAYERS]
130
 
131
- # Tools
132
- LIKELY_ANIMALS_PROMPT = """Given the following description of an animal, return a list of animals that may correspond to it and the approximate probalities of each.
133
- Animal Description:
134
- {animal_description}
135
- Likely Animals:
136
- """
137
-
138
- likely_animals_prompt = PromptTemplate(
139
- input_variables=['animal_description'],
140
- template=LIKELY_ANIMALS_PROMPT
141
- )
142
-
143
- likely_animals_chain = LLMChain(llm=decision_llm, prompt=likely_animals_prompt)
144
 
145
- def get_likely_animals(description: str) -> str:
146
- """Provides animals from a description"""
147
- return likely_animals_chain.run(animal_description=description)
148
 
149
- tools = [
150
- Tool(
151
- name='get_likely_animals',
152
- func=get_likely_animals,
153
- description='used to get a list of potential animals corresponding to a description of an animal'
154
- )
155
- ]
156
 
157
- voting_agent_executor = initialize_agent(tools, decision_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
158
- voting_agent_executor.invoke({"input": VOTING_PROMPT})
 
 
 
 
159
 
160
 
161
  # # This is an LLMChain to write a synopsis given a title of a play and the era it is set in.
 
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
 
12
  """Describing Stage
13
  THOUGHT STEP
 
48
  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
 
100
  )
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
 
123
 
124
  formatted_player_response = '\n- '.join(player_responses)
125
 
126
+ 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.
127
  Player responses:
128
  - {formatted_player_response}
129
 
130
  Please vote for the player you think is most likely to be the Chameleon.
131
  """
132
 
133
+ print(JUDGE_PROMPT)
134
 
135
  # election = [0*NUM_PLAYERS]
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
145
+ herd_win = re.match(f"Player {selected_chameleon+1}", judge_response['output'])
146
+ if herd_win:
147
+ print("The Herd has won!")
148
+ 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.
src/reasoning_tools.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts import PromptTemplate
2
+ from langchain.tools import Tool
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.chains import LLMChain
5
+
6
+ tool_llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0)
7
+
8
+ # Tools
9
+ # Get Animals From Description
10
+ LIKELY_ANIMALS_PROMPT = """Given the following description of an animal, discuss what possible animals it could be referring to and reasons why.
11
+ Animal Description:
12
+ {animal_description}
13
+ Possible Animals:
14
+ """
15
+
16
+ likely_animals_prompt = PromptTemplate(
17
+ input_variables=['animal_description'],
18
+ template=LIKELY_ANIMALS_PROMPT
19
+ )
20
+
21
+ likely_animals_chain = LLMChain(llm=tool_llm, prompt=likely_animals_prompt)
22
+
23
+ def get_likely_animals(description: str) -> str:
24
+ """Provides animals from a description"""
25
+ return likely_animals_chain.run(animal_description=description)
26
+
27
+ tools = [
28
+ Tool(
29
+ name='get_likely_animals',
30
+ func=get_likely_animals,
31
+ description='used to get a list of potential animals corresponding to a description of an animal'
32
+ )
33
+ ]