Skip to content

Commit

Permalink
[joonspk-research#24, joonspk-research#37] Implement P02 daily_plan
Browse files Browse the repository at this point in the history
  • Loading branch information
septagram committed Jan 13, 2024
1 parent 3c9564b commit 0fbd963
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 143 deletions.
2 changes: 1 addition & 1 deletion reverie/backend_server/persona/cognitive_modules/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def generate_first_daily_plan(persona, wake_up_hour):
'have dinner at 6:00 pm', 'watch TV from 7:00 pm to 8:00 pm']
"""
if debug: print ("GNS FUNCTION: <generate_first_daily_plan>")
return run_gpt_prompt_daily_plan(persona, wake_up_hour)[0]
return run_gpt_prompt_daily_plan(persona, wake_up_hour)


def generate_hourly_schedule(persona, wake_up_hour):
Expand Down
105 changes: 31 additions & 74 deletions reverie/backend_server/persona/prompt_template/run_gpt_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,80 +120,37 @@ def runner(*args: ArgsType) -> ReturnType:
fallback=lambda persona: "08:00"
)


def run_gpt_prompt_daily_plan(persona,
wake_up_hour,
test_input=None,
verbose=False):
"""
Basically the long term planning that spans a day. Returns a list of actions
that the persona will take today. Usually comes in the following form:
'wake up and complete the morning routine at 6:00 am',
'eat breakfast at 7:00 am',..
Note that the actions come without a period.
INPUT:
persona: The Persona class instance
OUTPUT:
a list of daily actions in broad strokes.
"""
def create_prompt_input(persona, wake_up_hour, test_input=None):
if test_input: return test_input
prompt_input = []
prompt_input += [persona.scratch.get_str_iss()]
prompt_input += [persona.scratch.get_str_lifestyle()]
prompt_input += [persona.scratch.get_str_curr_date_str()]
prompt_input += [persona.scratch.get_str_firstname()]
prompt_input += [f"{str(wake_up_hour)}:00 am"]
return prompt_input

def __func_clean_up(gpt_response, prompt=""):
cr = []
_cr = gpt_response.split(")")
for i in _cr:
if i[-1].isdigit():
i = i[:-1].strip()
if i[-1] == "." or i[-1] == ",":
cr += [i[:-1].strip()]
return cr

def __func_validate(gpt_response, prompt=""):
try: __func_clean_up(gpt_response, prompt="")
except:
return False
return True

def get_fail_safe():
fs = ['wake up and complete the morning routine at 6:00 am',
'eat breakfast at 7:00 am',
'read a book from 8:00 am to 12:00 pm',
'have lunch at 12:00 pm',
'take a nap from 1:00 pm to 4:00 pm',
'relax and watch TV from 7:00 pm to 8:00 pm',
'go to bed at 11:00 pm']
return fs



gpt_param = {"engine": "text-davinci-003", "max_tokens": 500,
"temperature": 1, "top_p": 1, "stream": False,
"frequency_penalty": 0, "presence_penalty": 0, "stop": None}
prompt_template = "persona/prompt_template/v2/daily_planning_v6.txt"
prompt_input = create_prompt_input(persona, wake_up_hour, test_input)
prompt = generate_prompt(prompt_input, prompt_template)
fail_safe = get_fail_safe()

output = safe_generate_response(prompt, gpt_param, 5, fail_safe,
__func_validate, __func_clean_up)
output = ([f"wake up and complete the morning routine at {wake_up_hour}:00 am"]
+ output)

if debug or verbose:
print_run_prompts(prompt_template, persona, gpt_param,
prompt_input, prompt, output)

return output, [output, prompt, gpt_param, prompt_input, fail_safe]

"""
Basically the long term planning that spans a day. Returns a list of actions
that the persona will take today. Usually comes in the following form:
'wake up and complete the morning routine at 6:00 am',
'eat breakfast at 7:00 am',..
Note that the actions come without a period.
INPUT:
persona: The Persona class instance
OUTPUT:
a list of daily actions in broad strokes.
"""
run_gpt_prompt_daily_plan = create_prompt_runner(
skill, "daily_planning_v6",
context_prep=lambda persona, wake_up_hour: {
"commonset": persona.scratch.get_str_iss(),
"date": persona.scratch.get_str_curr_date_str(),
"firstname": persona.scratch.get_str_firstname(),
"wake_up_hour": f"{str(wake_up_hour)}:00 am"
},
extractor=lambda output: [line for line in output.split('\n')[1:] if line.strip()] if len(output.split('\n')) > 1 else [output],
fallback=lambda persona, wake_up_hour: [
'wake up and complete the morning routine at 6:00 am',
'eat breakfast at 7:00 am',
'read a book from 8:00 am to 12:00 pm',
'have lunch at 12:00 pm',
'take a nap from 1:00 pm to 4:00 pm',
'relax and watch TV from 7:00 pm to 8:00 pm',
'go to bed at 11:00 pm',
]
)

def run_gpt_prompt_generate_hourly_schedule(persona,
curr_hour_str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"type": "completion",
"description": "Generate 1-2 emoji representing the given action",
"completion": {
"max_tokens": 15,
"temperature": 0,
"max_tokens": 500,
"temperature": 1,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
daily_planning_v6.txt
Let's consider {{$firstname}}:

Variables:
!<INPUT 0>! -- Commonset
!<INPUT 1>! -- Lifestyle
!<INPUT 2>! -- Reverie date time now
!<INPUT 3>! -- Persona first names
!<INPUT 4>! -- wake_up_hour
{{$commonset}}

<commentblockmarker>###</commentblockmarker>
!<INPUT 0>!

In general, !<INPUT 1>!
Today is !<INPUT 2>!. Here is !<INPUT 3>!'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 !<INPUT 4>!, 2)
We need to draft a daily plan for {{$firstname}} 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). The plan starts with waking up at {{$wake_up_hour}} and completing the morning routine, and it ends with going to sleep. What would be other items in the {{$firstname}}'s daily plan?
60 changes: 6 additions & 54 deletions reverie/backend_server/reverie.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os
import shutil
import traceback
import asyncio

from selenium import webdriver

Expand Down Expand Up @@ -414,7 +415,7 @@ def start_server(self, int_counter):
time.sleep(self.server_sleep)


def open_server(self):
async def open_server(self):
"""
Open up an interactive terminal prompt that lets you run the simulation
step by step and probe agent state.
Expand Down Expand Up @@ -624,56 +625,7 @@ def open_server(self):
target = input("Enter the name of the new simulation: ").strip()

rs = ReverieServer(origin, target)
rs.open_server()




















































# Create a new event loop and set it for the current context
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(rs.open_server())

0 comments on commit 0fbd963

Please sign in to comment.