From 53e408b07767419c2b10c9d5ec08f97b2317bcc2 Mon Sep 17 00:00:00 2001 From: chowington Date: Fri, 10 Nov 2023 20:41:58 -0600 Subject: [PATCH 1/5] Exploration --- .../temp_storage/curr_sim_code.json | 4 ++++ .../templates/home/main_script.html | 1 + environment/frontend_server/translator/views.py | 2 ++ .../persona/prompt_template/gpt_structure.py | 15 ++++++++------- reverie/backend_server/reverie.py | 4 ++++ 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/environment/frontend_server/temp_storage/curr_sim_code.json b/environment/frontend_server/temp_storage/curr_sim_code.json index ec1ffce466..23bd5106d7 100644 --- a/environment/frontend_server/temp_storage/curr_sim_code.json +++ b/environment/frontend_server/temp_storage/curr_sim_code.json @@ -1,3 +1,7 @@ { +<<<<<<< Updated upstream "sim_code": "testJuly31" +======= + "sim_code": "test-12" +>>>>>>> Stashed changes } \ No newline at end of file diff --git a/environment/frontend_server/templates/home/main_script.html b/environment/frontend_server/templates/home/main_script.html index 19c1dcf9f0..560e0bac2d 100644 --- a/environment/frontend_server/templates/home/main_script.html +++ b/environment/frontend_server/templates/home/main_script.html @@ -365,6 +365,7 @@ player.body.setVelocityY(camera_speed); } + // Here // console.log("phase: " + phase + ", step: " + step); // *** MOVE PERSONAS *** // Moving personas take place in three distinct phases: "process," "update," diff --git a/environment/frontend_server/translator/views.py b/environment/frontend_server/translator/views.py index 20b8a29bba..23d7fecfce 100644 --- a/environment/frontend_server/translator/views.py +++ b/environment/frontend_server/translator/views.py @@ -238,6 +238,7 @@ def path_tester(request): return render(request, template, context) +# Here def process_environment(request): """ @@ -265,6 +266,7 @@ def process_environment(request): return HttpResponse("received") +# Here def update_environment(request): """ diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index f9c4718949..0df8b9f810 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -18,13 +18,14 @@ def temp_sleep(seconds=0.1): def ChatGPT_single_request(prompt): temp_sleep() - - completion = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=[{"role": "user", "content": prompt}] - ) - return completion["choices"][0]["message"]["content"] - + try: + response = llm(prompt) + # response = llm('[INST]\n' + prompt + '\n[/INST]') + except ValueError: + print("Requested tokens exceed context window") + ### TODO: Add map-reduce or splitter to handle this error. + return "LLM ERROR" + return response # ============================================================================ # #####################[SECTION 1: CHATGPT-3 STRUCTURE] ###################### diff --git a/reverie/backend_server/reverie.py b/reverie/backend_server/reverie.py index 2d753d1029..aded7c7fd1 100644 --- a/reverie/backend_server/reverie.py +++ b/reverie/backend_server/reverie.py @@ -137,6 +137,7 @@ def __init__(self, # cycle; this is to not kill our machine. self.server_sleep = 0.1 + # Here # SIGNALING THE FRONTEND SERVER: # curr_sim_code.json contains the current simulation code, and # curr_step.json contains the current step of the simulation. These are @@ -308,6 +309,7 @@ def start_server(self, int_counter): if int_counter == 0: break + # Here # file is the file that our frontend outputs. When the # frontend has done its job and moved the personas, then it will put a # new environment file that matches our step count. That's when we run @@ -391,6 +393,7 @@ def start_server(self, int_counter): movements["meta"]["curr_time"] = (self.curr_time .strftime("%B %d, %Y, %H:%M:%S")) + # Here # We then write the personas' movements to a file that will be sent # to the frontend server. # Example json output: @@ -608,6 +611,7 @@ def open_server(self): origin = input("Enter the name of the forked simulation: ").strip() target = input("Enter the name of the new simulation: ").strip() + # Here rs = ReverieServer(origin, target) rs.open_server() From 328345132166d14fbf4be8d75b68220c084be003 Mon Sep 17 00:00:00 2001 From: chowington Date: Wed, 20 Dec 2023 19:43:26 -0600 Subject: [PATCH 2/5] Add headless simulation functionality --- reverie/backend_server/reverie.py | 81 ++++++++++--------------------- 1 file changed, 26 insertions(+), 55 deletions(-) diff --git a/reverie/backend_server/reverie.py b/reverie/backend_server/reverie.py index bb730811ea..34022b0cfd 100644 --- a/reverie/backend_server/reverie.py +++ b/reverie/backend_server/reverie.py @@ -283,7 +283,7 @@ def _print_tree(tree, depth): time.sleep(self.server_sleep * 10) - def start_server(self, int_counter): + def start_server(self, int_counter, headless=False): """ The main backend server of Reverie. This function retrieves the environment file from the frontend to @@ -315,7 +315,7 @@ def start_server(self, int_counter): if int_counter == 0: break - # Here + # Here here # file is the file that our frontend outputs. When the # frontend has done its job and moved the personas, then it will put a # new environment file that matches our step count. That's when we run @@ -341,6 +341,7 @@ def start_server(self, int_counter): # Then we initialize game_obj_cleanup for this cycle. game_obj_cleanup = dict() + # Here here # We first move our personas in the backend environment to match # the frontend environment. for persona_name, persona in self.personas.items(): @@ -378,6 +379,9 @@ def start_server(self, int_counter): # This is where the core brains of the personas are invoked. movements = {"persona": dict(), "meta": dict()} + # This will only be used in headless mode + next_env = {} + for persona_name, persona in self.personas.items(): # is a x,y coordinate. e.g., (58, 9) # is an emoji. e.g., "\ud83d\udca4" @@ -393,13 +397,19 @@ def start_server(self, int_counter): movements["persona"][persona_name]["description"] = description movements["persona"][persona_name]["chat"] = (persona .scratch.chat) + if headless: + next_env[persona_name] = { + "x": next_tile[0], + "y": next_tile[1], + "maze": self.maze.maze_name, + } # Include the meta information about the current stage in the # movements dictionary. movements["meta"]["curr_time"] = (self.curr_time .strftime("%B %d, %Y, %H:%M:%S")) - # Here + # Here here # We then write the personas' movements to a file that will be sent # to the frontend server. # Example json output: @@ -413,8 +423,14 @@ def start_server(self, int_counter): with open(curr_move_file, "w") as outfile: outfile.write(json.dumps(movements, indent=2)) + # If we're running in headless mode, also create the environment file + if headless: + with open(f"{sim_folder}/environment/{self.step + 1}.json", "w") as outfile: + outfile.write(json.dumps(next_env, indent=2)) + # After this cycle, the world takes one step forward, and the # current time moves by amount. + # Here self.step += 1 self.curr_time += datetime.timedelta(seconds=self.sec_per_step) @@ -474,6 +490,13 @@ def open_server(self): # Example: save self.save() + elif sim_command[:8].lower() == "headless": + # Runs the simulation in headless mode, which means that it will + # run without the frontend server. + # Example: headless 1000 + int_count = int(sim_command.split()[-1]) + self.start_server(int_count, headless=True) + elif sim_command[:3].lower() == "run": # Runs the number of steps specified in the prompt. # Example: run 1000 @@ -643,55 +666,3 @@ def open_server(self): outfile.write(f"{origin_prompt}{origin}\n{target_prompt}{target}\n") rs.open_server() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 2a700daa6f6e43a6c3831a5011a01342be2a9b40 Mon Sep 17 00:00:00 2001 From: chowington Date: Wed, 20 Dec 2023 19:43:46 -0600 Subject: [PATCH 3/5] Add script to watch headless simulation --- monitor_simulation.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 monitor_simulation.py diff --git a/monitor_simulation.py b/monitor_simulation.py new file mode 100644 index 0000000000..83226682c7 --- /dev/null +++ b/monitor_simulation.py @@ -0,0 +1,29 @@ +import os +import time +import json +import glob + +frontend_path = "environment/frontend_server" + +# Extract curr_sim_code from JSON file +with open(f"{frontend_path}/temp_storage/curr_sim_code.json", "r") as f: + data = json.load(f) + curr_sim_code = data["sim_code"] + +movement_path = f"{frontend_path}/storage/{curr_sim_code}/movement" +length = len(movement_path) + 1 +last_file = None + +while True: + list_of_files = glob.glob(f"{movement_path}/*.json") + latest_file = sorted(list_of_files, key=lambda s: int(s[length:-5]))[-1] + + if latest_file != last_file: + os.system("clear") + with open(latest_file, "r") as f: + print(f.read()) + print(latest_file) + + last_file = latest_file + + time.sleep(0.2) From ca45a7bc26e415d31afde3d18139a03ae43b6bb9 Mon Sep 17 00:00:00 2001 From: chowington Date: Wed, 20 Dec 2023 20:15:42 -0600 Subject: [PATCH 4/5] Clean up --- .../temp_storage/curr_sim_code.json | 4 ---- .../templates/home/main_script.html | 1 - .../frontend_server/translator/views.py | 2 -- .../persona/prompt_template/gpt_structure.py | 15 +++++++------ reverie/backend_server/reverie.py | 21 +++++++------------ 5 files changed, 15 insertions(+), 28 deletions(-) diff --git a/environment/frontend_server/temp_storage/curr_sim_code.json b/environment/frontend_server/temp_storage/curr_sim_code.json index 23bd5106d7..ec1ffce466 100644 --- a/environment/frontend_server/temp_storage/curr_sim_code.json +++ b/environment/frontend_server/temp_storage/curr_sim_code.json @@ -1,7 +1,3 @@ { -<<<<<<< Updated upstream "sim_code": "testJuly31" -======= - "sim_code": "test-12" ->>>>>>> Stashed changes } \ No newline at end of file diff --git a/environment/frontend_server/templates/home/main_script.html b/environment/frontend_server/templates/home/main_script.html index 560e0bac2d..19c1dcf9f0 100644 --- a/environment/frontend_server/templates/home/main_script.html +++ b/environment/frontend_server/templates/home/main_script.html @@ -365,7 +365,6 @@ player.body.setVelocityY(camera_speed); } - // Here // console.log("phase: " + phase + ", step: " + step); // *** MOVE PERSONAS *** // Moving personas take place in three distinct phases: "process," "update," diff --git a/environment/frontend_server/translator/views.py b/environment/frontend_server/translator/views.py index 23d7fecfce..20b8a29bba 100644 --- a/environment/frontend_server/translator/views.py +++ b/environment/frontend_server/translator/views.py @@ -238,7 +238,6 @@ def path_tester(request): return render(request, template, context) -# Here def process_environment(request): """ @@ -266,7 +265,6 @@ def process_environment(request): return HttpResponse("received") -# Here def update_environment(request): """ diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index a419935ec2..5a57ee09a5 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -18,14 +18,13 @@ def temp_sleep(seconds=0.1): def ChatGPT_single_request(prompt): temp_sleep() - try: - response = llm(prompt) - # response = llm('[INST]\n' + prompt + '\n[/INST]') - except ValueError: - print("Requested tokens exceed context window") - ### TODO: Add map-reduce or splitter to handle this error. - return "LLM ERROR" - return response + + completion = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": prompt}] + ) + return completion["choices"][0]["message"]["content"] + # ============================================================================ # #####################[SECTION 1: CHATGPT-3 STRUCTURE] ###################### diff --git a/reverie/backend_server/reverie.py b/reverie/backend_server/reverie.py index 34022b0cfd..d363826da9 100644 --- a/reverie/backend_server/reverie.py +++ b/reverie/backend_server/reverie.py @@ -143,7 +143,6 @@ def __init__(self, # cycle; this is to not kill our machine. self.server_sleep = 0.1 - # Here # SIGNALING THE FRONTEND SERVER: # curr_sim_code.json contains the current simulation code, and # curr_step.json contains the current step of the simulation. These are @@ -315,7 +314,6 @@ def start_server(self, int_counter, headless=False): if int_counter == 0: break - # Here here # file is the file that our frontend outputs. When the # frontend has done its job and moved the personas, then it will put a # new environment file that matches our step count. That's when we run @@ -341,7 +339,6 @@ def start_server(self, int_counter, headless=False): # Then we initialize game_obj_cleanup for this cycle. game_obj_cleanup = dict() - # Here here # We first move our personas in the backend environment to match # the frontend environment. for persona_name, persona in self.personas.items(): @@ -397,6 +394,7 @@ def start_server(self, int_counter, headless=False): movements["persona"][persona_name]["description"] = description movements["persona"][persona_name]["chat"] = (persona .scratch.chat) + if headless: next_env[persona_name] = { "x": next_tile[0], @@ -409,7 +407,6 @@ def start_server(self, int_counter, headless=False): movements["meta"]["curr_time"] = (self.curr_time .strftime("%B %d, %Y, %H:%M:%S")) - # Here here # We then write the personas' movements to a file that will be sent # to the frontend server. # Example json output: @@ -423,14 +420,13 @@ def start_server(self, int_counter, headless=False): with open(curr_move_file, "w") as outfile: outfile.write(json.dumps(movements, indent=2)) - # If we're running in headless mode, also create the environment file + # If we're running in headless mode, also create the environment file to immediately trigger the next simulation step if headless: with open(f"{sim_folder}/environment/{self.step + 1}.json", "w") as outfile: outfile.write(json.dumps(next_env, indent=2)) # After this cycle, the world takes one step forward, and the # current time moves by amount. - # Here self.step += 1 self.curr_time += datetime.timedelta(seconds=self.sec_per_step) @@ -490,6 +486,12 @@ def open_server(self): # Example: save self.save() + elif sim_command[:3].lower() == "run": + # Runs the number of steps specified in the prompt. + # Example: run 1000 + int_count = int(sim_command.split()[-1]) + rs.start_server(int_count) + elif sim_command[:8].lower() == "headless": # Runs the simulation in headless mode, which means that it will # run without the frontend server. @@ -497,12 +499,6 @@ def open_server(self): int_count = int(sim_command.split()[-1]) self.start_server(int_count, headless=True) - elif sim_command[:3].lower() == "run": - # Runs the number of steps specified in the prompt. - # Example: run 1000 - int_count = int(sim_command.split()[-1]) - rs.start_server(int_count) - elif ("print persona schedule" in sim_command[:22].lower()): # Print the decomposed schedule of the persona specified in the @@ -657,7 +653,6 @@ def open_server(self): target_prompt = f"Enter the name of the new simulation (last was {last_sim_code}): " target = input(target_prompt).strip() - # Here rs = ReverieServer(origin, target) # Allow the server to create this folder above before writing to the logfile From 3cfd9047e24b5143fb205f9c3e8f669e0a30d4b0 Mon Sep 17 00:00:00 2001 From: chowington Date: Wed, 20 Dec 2023 20:20:25 -0600 Subject: [PATCH 5/5] Minor changes --- .gitignore | 1 + reverie/backend_server/reverie.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9b9ef8e864..dd06ab69b3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ environment/frontend_server/storage/* !environment/frontend_server/storage/base_the_ville_n25/ !environment/frontend_server/storage/July1_the_ville_isabella_maria_klaus-step-3-*/ !environment/frontend_server/storage/test-22/ +!environment/frontend_server/storage/test-23/ bin/ include/ diff --git a/reverie/backend_server/reverie.py b/reverie/backend_server/reverie.py index d363826da9..6ca51cc582 100644 --- a/reverie/backend_server/reverie.py +++ b/reverie/backend_server/reverie.py @@ -420,7 +420,8 @@ def start_server(self, int_counter, headless=False): with open(curr_move_file, "w") as outfile: outfile.write(json.dumps(movements, indent=2)) - # If we're running in headless mode, also create the environment file to immediately trigger the next simulation step + # If we're running in headless mode, also create the environment file + # to immediately trigger the next simulation step if headless: with open(f"{sim_folder}/environment/{self.step + 1}.json", "w") as outfile: outfile.write(json.dumps(next_env, indent=2))