joonspk-research / generative_agents

Generative Agents: Interactive Simulacra of Human Behavior
Apache License 2.0
16.26k stars 2.09k forks source link

Refreshing "http://localhost:8000/simulator_home" error, it shows "Please start the backend first." #15

Open Ancrilin opened 1 year ago

Ancrilin commented 1 year ago

Hi, Thank you very much for open-sourcing this repository. I think I successfully set up the environment following your tutorial, and I get a response when I input "run 1".

~~~ prompt    ----------------------------------------------------
Name: Isabella Rodriguez
Age: 34
Innate traits: friendly, outgoing, hospitable
Learned traits: Isabella Rodriguez is a cafe owner of Hobbs Cafe who loves to make people feel welcome. She is always looking for ways to make the cafe a place where people can come to relax and enjoy themselves.
Currently: Isabella Rodriguez is planning on having a Valentine's Day party at Hobbs Cafe with her customers on February 14th, 2023 at 5pm. She is gathering party material, and is telling everyone to join the party at Hobbs Cafe on February 14th, 2023, from 5pm to 7pm.
Lifestyle: Isabella Rodriguez goes to bed around 11pm, awakes up around 6am.
Daily plan requirement: Isabella Rodriguez opens Hobbs Cafe at 8am everyday, and works at the counter until 8pm, at which point she closes the cafe.
Current Date: Monday February 13

In general, Isabella Rodriguez goes to bed around 11pm, awakes up around 6am.
Today is Monday February 13. Here is Isabella's plan today in broad-strokes (with the time of the day. e.g., have a lunch at 12:00 pm, watch TV from 7 to 8 pm): 1) wake up and complete the morning routine at 6:00 am, 2) 

~~~ output    ----------------------------------------------------
['wake up and complete the morning routine at 6:00 am', 'Open Hobbs Cafe and work at the counter from 8:00 am to 8:00 pm', 'Have a lunch at 12:00 pm', "Prepare for the Valentine's Day party", 'Watch TV from 7 to 8 pm'] 

=== END ==========================================================

When I visit http://localhost:8000/, it shows "Your environment server is up and running!", and the first time I enter http://localhost:8000/simulator_home, it also displays normally. However, when I refresh http://localhost:8000/simulator_home or click on an NPC's detail and then go back, the page cannot be displayed, and it outputs "Please start the backend first.". This prevents me from observing the NPC's activities in the visualization page.

kuanyi-ng commented 1 year ago

Tried to understand why this happened (I still do not have a solution yet). The followings are my findings:

  1. Whenever we start a new simulation, ReverieServer is initialized, and the file curr_step.json is created. a. https://github.com/joonspk-research/generative_agents/blob/main/reverie/backend_server/reverie.py#L140-L154
  2. Then we visit http://localhost:8000/simulator_home (for the first time), and we can see the map because curr_step.json exists. a. https://github.com/joonspk-research/generative_agents/blob/7373f4eb5d604b1d75b062193a56e279f5bf8201/environment/frontend_server/translator/views.py#L105
  3. However, curr_step.json is deleted during our first visit. a. https://github.com/joonspk-research/generative_agents/blob/7373f4eb5d604b1d75b062193a56e279f5bf8201/environment/frontend_server/translator/views.py#L120
  4. If we refresh http://localhost:8000/simulator_home or come back from the page about agent's detail, we are shown the error page of "Please start the backend first." because curr_step.json does not exist a. https://github.com/joonspk-research/generative_agents/blob/7373f4eb5d604b1d75b062193a56e279f5bf8201/environment/frontend_server/translator/views.py#L109-L112
pjq commented 1 year ago

Saw the same error.

zijinoier commented 1 year ago

same problem here

yurifrl commented 1 year ago

I don't know the purpose of os.remove(f_curr_step) but if you remove it the reload works

MAC1713 commented 1 year ago

I had ask Claude this question: Response 1:

The reason for deleting the f_curr_step file after generating the map is mainly the following two points:

The f_curr_step file is used to record the current step number of the simulation. When entering the home function after generating the map, the current sim_code and step number will be re-read here. So the previous f_curr_step file is no longer needed and deleting it avoids referencing old data. Deleting f_curr_step can serve as a reset flag. When entering the home function, it indicates the start of a new simulation process and the previous step numbers can all be discarded. Specifically:

In the process_environment function, the frontend sends the current step and environment information to the backend for saving. Then in update_environment, the backend sends the latest agent action information to the frontend. This is a two-way interaction process, with the current step number passed each time. When a complete simulation ends, it will enter the home function, at which point the previous simulation step number information is no longer needed. So at the beginning of the home function, the f_curr_step file is deleted as a reset flag. Then the new sim_code and step number are re-read, starting a new simulation process. If f_curr_step is not deleted, the old step number from the previous simulation will be constantly referenced, rather than starting from scratch, resulting in errors. So the main purpose is to RESET and enter a new simulation loop, avoid referencing old step data, which is why f_curr_step needs to be deleted after generating the map.

Response 2:

The reason is that deleting the f_curr_step file will cause the homepage check for that file's existence to fail.

Specifically:

In the home view function, it first checks if the f_curr_step file exists:

python

Copy code if not check_if_file_exists(f_curr_step): context = {} template = "home/error_start_backend.html" return render(request, template, context) If the file does not exist, an error page will be returned showing "Please start the backend first."

However, as mentioned earlier, the f_curr_step file is deleted after process_environment finishes, as a reset flag.

So if the user directly refreshes the homepage after deleting f_curr_step, the file will not exist.

This will lead to the error page prompting to start the backend service first.

In other words, after deleting f_curr_step, the backend must be properly restarted to regenerate f_curr_step before loading the homepage again to avoid this error.

Directly refreshing the homepage will result in an error due to the missing f_curr_step file.

Therefore, in the process design, care must be taken to handle errors after deleting temporary files, to avoid confusing errors when users directly refresh pages. It is best to automatically redirect/navigate to a proper starting page after deleting files.

Response 3:

Here are some ways to avoid direct page refresh errors after file deletion:

Automatically redirect to a proper starting page after deleting f_curr_step, such as a page guiding the user to start the backend service. When checking for file missing on the homepage, do not directly return error page. Guide user to backend start page or give a more friendly prompt to explicitly restart the backend. Before file deletion, set a flag in the session indicating the file is deleted. Homepage should check this flag rather than file existence to determine error page display. Modify homepage logic to not strictly depend on f_curr_step file, have other ways to maintain state to avoid file missing issues. Use database to maintain state rather than file. Database state persists after file deletion, homepage can check database status. Use cache or message queue to handle state instead of temporary file. Preload the starting page in advance. After file deletion, serve the preloaded page directly, avoiding file existence checks. In summary, the key is to have additional state management and guiding measures after deleting the temporary state file, to avoid file missing errors when users directly access the homepage. Providing better user prompts or auto redirects can greatly improve user experience.

ShadowTeamCN commented 1 year ago

same issue

MAC1713 commented 1 year ago

简单的说就是每次代码运行的时候都会新建f_curr_step文件,网页前端调用这个文件确定初始状态步数后就删除这个文件,防止下次运行的时候读取上一次模拟运行的步数,所以运行的时候f_curr_step是已经被删除的状态的,只有重新运行代码才会新建,网页刷新后会去调用这个f_curr_step文件确认初始步数,但是这个文件已经被删除了,所以就报错了。刷新的接口和新建模拟的读数据的接口是同一个导致的。所以只有关闭后重新启动才会正常。

Simply put, every time the code runs, it will create a new f_curr_step file. The web frontend will call this file to determine the initial status step count, and then delete this file to prevent reading the step count from the previous simulation run the next time the code executes. So during code execution, the f_curr_step file is in a deleted state, it will only be recreated when rerunning the code. The web page refresh will call this f_curr_step file again to confirm the initial step count, but since this file has already been deleted, it results in an error. The refresh interface and the new simulation data reading interface are the same one, which caused this issue.So it will work normally only after shutting down and restarting.

MAC1713 commented 1 year ago

same issue

简单的说就是每次代码运行的时候都会新建f_curr_step文件,网页前端调用这个文件确定初始状态步数后就删除这个文件,防止下次运行的时候读取上一次模拟运行的步数,所以运行的时候f_curr_step是已经被删除的状态的,只有重新运行代码才会新建,网页刷新后会去调用这个f_curr_step文件确认初始步数,但是这个文件已经被删除了,所以就报错了。刷新的接口和新建模拟的读数据的接口是同一个导致的。所以只有关闭后重新启动才会正常。

HananJackie commented 12 months ago

I have the same problem.

the simulation seems to run, I see the output on the console, but on my browser it states "Please start the backend first."

1359246372 commented 10 months ago

same issue

简单的说就是每次代码运行的时候都会新建f_curr_step文件,网页前端调用这个文件确定初始状态步数后就删除这个文件,防止下次运行的时候读取上一次模拟运行的步数,所以运行的时候f_curr_step是已经被删除的状态的,只有重新运行代码才会新建,网页刷新后会去调用这个f_curr_step文件确认初始步数,但是这个文件已经被删除了,所以就报错了。刷新的接口和新建模拟的读数据的接口是同一个导致的。所以只有关闭后重新启动才会正常。

最后一句“所以只有关闭后重新启动才会正常。” 请问是关闭和重启哪一个才可以正常? 是Environment Server还是 Simulation Server? 我重启 Simulation Server, 会让我重新输入角色及new simulation的名字,这和刚创建的就会冲突。请问怎么解决?