Eric Botti
commited on
Commit
·
2340286
1
Parent(s):
6998476
get_likely_animals tool added, now using GPT-4 for some responses
Browse files- src/agent_process.py +65 -59
src/agent_process.py
CHANGED
@@ -60,8 +60,8 @@ from langchain.chains import SimpleSequentialChain
|
|
60 |
import random
|
61 |
|
62 |
herd_llm = ChatOpenAI(temperature=0.7)
|
63 |
-
chameleon_llm = ChatOpenAI(temperature=0.4)
|
64 |
-
decision_llm = ChatOpenAI(temperature=0)
|
65 |
|
66 |
# Game Setup
|
67 |
NUM_PLAYERS = 5
|
@@ -71,7 +71,7 @@ selected_animal = random.choice(ANIMALS)
|
|
71 |
selected_chameleon = random.randint(0, NUM_PLAYERS - 1)
|
72 |
|
73 |
print(f'Animal: {selected_animal}')
|
74 |
-
print(f'Chameleon: Player {selected_chameleon}')
|
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.
|
@@ -107,18 +107,19 @@ chameleon_chain = LLMChain(llm=chameleon_llm, prompt=chameleon_prompt_template)
|
|
107 |
player_responses = []
|
108 |
for i in range(0, NUM_PLAYERS):
|
109 |
if i == selected_chameleon:
|
110 |
-
|
111 |
else:
|
112 |
-
|
113 |
-
|
114 |
-
player_responses
|
|
|
115 |
|
116 |
|
117 |
-
formatted_player_response = '\n-'.join(player_responses)
|
118 |
|
119 |
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.
|
120 |
Player responses:
|
121 |
-
-{formatted_player_response}
|
122 |
|
123 |
Please vote for the player you think is most likely to be the Chameleon.
|
124 |
"""
|
@@ -128,28 +129,33 @@ print(VOTING_PROMPT)
|
|
128 |
# election = [0*NUM_PLAYERS]
|
129 |
|
130 |
# Tools
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
-
|
136 |
-
# try:
|
137 |
-
# election[player] += 1
|
138 |
-
# return f"You successfully voted for {player}"
|
139 |
-
# except KeyError:
|
140 |
-
# return f"{player} is not a valid option on this ballot, please try again"
|
141 |
|
|
|
|
|
|
|
142 |
|
143 |
tools = [
|
144 |
Tool(
|
145 |
-
name='
|
146 |
-
func=
|
147 |
-
description='used to
|
148 |
)
|
149 |
]
|
150 |
|
151 |
-
|
152 |
-
|
153 |
|
154 |
|
155 |
# # This is an LLMChain to write a synopsis given a title of a play and the era it is set in.
|
@@ -181,39 +187,39 @@ tools = [
|
|
181 |
# output_variables=["synopsis", "review"],
|
182 |
# verbose=True)
|
183 |
|
184 |
-
|
185 |
-
#BABYAGI
|
186 |
-
import os
|
187 |
-
from collections import deque
|
188 |
-
from typing import Dict, List, Optional, Any
|
189 |
-
|
190 |
-
from langchain.chains import LLMChain
|
191 |
-
from langchain.prompts import PromptTemplate
|
192 |
-
from langchain.embeddings import OpenAIEmbeddings
|
193 |
-
# from langchain.llms import BaseLLM
|
194 |
-
# from langchain.schema.vectorstore import VectorStore
|
195 |
-
# from pydantic import BaseModel, Field
|
196 |
-
# from langchain.chains.base import Chain
|
197 |
-
from langchain_experimental.autonomous_agents import BabyAGI
|
198 |
-
|
199 |
-
from langchain.vectorstores import FAISS
|
200 |
-
from langchain.docstore import InMemoryDocstore
|
201 |
-
# Define your embedding model
|
202 |
-
embeddings_model = OpenAIEmbeddings()
|
203 |
-
# Initialize the vectorstore as empty
|
204 |
-
import faiss
|
205 |
-
|
206 |
-
embedding_size = 1536
|
207 |
-
index = faiss.IndexFlatL2(embedding_size)
|
208 |
-
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
|
209 |
-
llm = ChatOpenAI(model='gpt-4', temperature=0)
|
210 |
-
|
211 |
-
# Logging of LLMChains
|
212 |
-
verbose = False
|
213 |
-
# If None, will keep going on forever
|
214 |
-
max_iterations = 10
|
215 |
-
baby_agi = BabyAGI.from_llm(
|
216 |
-
|
217 |
-
)
|
218 |
-
|
219 |
-
baby_agi({"objective": VOTING_PROMPT})
|
|
|
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
|
|
|
71 |
selected_chameleon = random.randint(0, NUM_PLAYERS - 1)
|
72 |
|
73 |
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.
|
|
|
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
|
114 |
+
player_responses.append(f"Player {i + 1}: {player_response}")
|
115 |
+
print(player_responses[i])
|
116 |
|
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 |
"""
|
|
|
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.
|
|
|
187 |
# output_variables=["synopsis", "review"],
|
188 |
# verbose=True)
|
189 |
|
190 |
+
#
|
191 |
+
# #BABYAGI
|
192 |
+
# import os
|
193 |
+
# from collections import deque
|
194 |
+
# from typing import Dict, List, Optional, Any
|
195 |
+
#
|
196 |
+
# from langchain.chains import LLMChain
|
197 |
+
# from langchain.prompts import PromptTemplate
|
198 |
+
# from langchain.embeddings import OpenAIEmbeddings
|
199 |
+
# # from langchain.llms import BaseLLM
|
200 |
+
# # from langchain.schema.vectorstore import VectorStore
|
201 |
+
# # from pydantic import BaseModel, Field
|
202 |
+
# # from langchain.chains.base import Chain
|
203 |
+
# from langchain_experimental.autonomous_agents import BabyAGI
|
204 |
+
#
|
205 |
+
# from langchain.vectorstores import FAISS
|
206 |
+
# from langchain.docstore import InMemoryDocstore
|
207 |
+
# # Define your embedding model
|
208 |
+
# embeddings_model = OpenAIEmbeddings()
|
209 |
+
# # Initialize the vectorstore as empty
|
210 |
+
# import faiss
|
211 |
+
#
|
212 |
+
# embedding_size = 1536
|
213 |
+
# index = faiss.IndexFlatL2(embedding_size)
|
214 |
+
# vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
|
215 |
+
# llm = ChatOpenAI(model='gpt-4', temperature=0)
|
216 |
+
#
|
217 |
+
# # Logging of LLMChains
|
218 |
+
# verbose = False
|
219 |
+
# # If None, will keep going on forever
|
220 |
+
# max_iterations = 10
|
221 |
+
# baby_agi = BabyAGI.from_llm(
|
222 |
+
# llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations
|
223 |
+
# )
|
224 |
+
#
|
225 |
+
# baby_agi({"objective": VOTING_PROMPT})
|