From e035706091ebef6626ca38265c47200e3a42cc5e Mon Sep 17 00:00:00 2001 From: didi <2020201387@ruc.edu.cn> Date: Fri, 29 Sep 2023 17:49:31 +0800 Subject: [PATCH] =?UTF-8?q?9.29=20=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完善了一下AgentMemory的Add方法(反思与Plan中都要用),save load方法(需要跟组员对修改新的一版);添加了Scratch类属性与方法(给了一版文档介绍);修改了STrole中STrolecontext属性,添加了Strole中Retrieve方法 --- examples/st_game/memory/agent_memory.py | 243 +++++++++-- examples/st_game/memory/scratch.py | 535 +++++++++++++++++++++++- examples/st_game/roles/st_role.py | 11 +- examples/st_game/utils/check.py | 14 + 4 files changed, 771 insertions(+), 32 deletions(-) create mode 100644 examples/st_game/utils/check.py diff --git a/examples/st_game/memory/agent_memory.py b/examples/st_game/memory/agent_memory.py index ecf6190d4..a56100ee7 100644 --- a/examples/st_game/memory/agent_memory.py +++ b/examples/st_game/memory/agent_memory.py @@ -10,16 +10,17 @@ from datetime import datetime class BasicMemory(Message): - def __init__(self, memory_id: str, memory_count: int, type_count: int, memory_type: str, depth: int, content: str, + def __init__(self, memory_id: str, memory_count: int, type_count: int, memory_type: str, depth: int, created: datetime, expiration: datetime, subject: str, predicate: str, object: str, - embedding_key: str, poignancy: int, keywords: list, filling: list): + content: str, embedding_key: str, poignancy: int, keywords: list, filling: list, + cause_by = ""): """ BasicMemory继承于MG的Message类,其中content属性替代description属性 Message类中对于Chat类型支持的非常好,对于Agent个体的Perceive,Reflection,Plan支持的并不多 在Type设计上,我们延续GA的三个种类,但是对于Chat种类的对话进行特别设计(具体怎么设计还没想好) """ - super().__init__(content) + super().__init__(content,cause_by=cause_by) """ 从父类中继承的属性 content: str # 记忆描述 @@ -43,8 +44,41 @@ class BasicMemory(Message): self.embedding_key: str = embedding_key # 内容与self.content一致 self.poignancy: int = poignancy # importance值 self.keywords: list = keywords # keywords - self.filling: list = filling # None或者列表 + self.filling: list = filling # 装的与之相关联的memory_id的列表 + def save_to_dict(self) -> dict: + """ + 将MemoryBasic类转化为字典,用于存储json文件 + 这里需要注意,cause_by跟GA不兼容,所以需要做一个格式转换 + """ + memory_dict = dict() + node_id = self.memory_id + + memory_dict[node_id] = dict() + memory_dict[node_id]["node_count"] = self.memory_count + memory_dict[node_id]["type_count"] = self.type_count + memory_dict[node_id]["type"] = self.type + memory_dict[node_id]["depth"] = self.depth + + memory_dict[node_id]["cmemory_dicteated"] = self.created.strftime('%Y-%m-%d %H:%M:%S') + memory_dict[node_id]["expiration"] = None + if self.expiration: + memory_dict[node_id]["expiration"] = (self.expiration + .strftime('%Y-%m-%d %H:%M:%S')) + + memory_dict[node_id]["subject"] = self.subject + memory_dict[node_id]["predicate"] = self.predicate + memory_dict[node_id]["object"] = self.object + + memory_dict[node_id]["description"] = self.description + memory_dict[node_id]["embedding_key"] = self.embedding_key + memory_dict[node_id]["poignancy"] = self.poignancy + memory_dict[node_id]["keywords"] = list(self.keywords) + memory_dict[node_id]["filling"] = self.filling + if self.cause_by: + memory_dict[node_id]["cause_by"] = self.cause_by + + return memory_dict class AgentMemory(Memory): """ @@ -68,25 +102,82 @@ class AgentMemory(Memory): self.thought_keywords = dict() self.chat_keywords = dict() - self.strength_event_keywords = dict() # 不知道具体作用,所以没有删除 - self.strength_thought_keywords = dict() + self.kw_strength_event = dict() # 关键词影响存储 + self.kw_strength_thought = dict() - self.embeddings = json.load(open(memory_saved + "/embeddings.json")) - self.load() + self.load(memory_saved) - def save(self): + def save(self,memory_saved:str): """ 将MemormyBasic类存储为Nodes.json形式。复现GA中的Kw Strength.json形式 - @张凯补充一个可调用的函数 + 这里添加一个路径即可 """ - pass - def load(self): + memory_json = dict() + for i in range(len(self.storage)): + memory_node = self.storage[i] + memory_json.update(memory_node) + with open(memory_saved+"/nodes.json", "w") as outfile: + json.dump(memory_json, outfile) + + with open(memory_saved+"/embeddings.json", "w") as outfile: + json.dump(self.embeddings, outfile) + + strength_json = dict() + strength_json["kw_strength_event"] = self.kw_strength_event + strength_json["kw_strength_thought"] = self.kw_strength_thought + with open(memory_saved+"/kw_strength.json", "w") as outfile: + json.dump(strength_json, outfile) + + + def load(self,memory_saved:str): """ 将GA的JSON解析,填充到AgentMemory类之中 """ - pass + self.embeddings = json.load(open(memory_saved + "/embeddings.json")) + memory_load = json.load(open(memory_saved + "/nodes.json")) + for count in range(len(memory_load.keys())): + node_id = f"node_{str(count+1)}" + node_details = memory_load[node_id] + node_type = node_details["type"] + created = datetime.datetime.strptime(node_details["created"], + '%Y-%m-%d %H:%M:%S') + expiration = None + if node_details["expiration"]: + expiration = datetime.datetime.strptime(node_details["expiration"], + '%Y-%m-%d %H:%M:%S') + + if node_details["cause_by"]: + cause_by = node_details["cause_by"] + + s = node_details["subject"] + p = node_details["predicate"] + o = node_details["object"] + + description = node_details["description"] + embedding_pair = (node_details["embedding_key"], + self.embeddings[node_details["embedding_key"]]) + poignancy =node_details["poignancy"] + keywords = set(node_details["keywords"]) + filling = node_details["filling"] + + if node_type == "event": + self.add_event(created, expiration, s, p, o, + description, keywords, poignancy, embedding_pair, filling) + elif node_type == "chat": + self.add_chat(created, expiration, s, p, o, + description, keywords, poignancy, embedding_pair, filling,cause_by) + elif node_type == "thought": + self.add_thought(created, expiration, s, p, o, + description, keywords, poignancy, embedding_pair, filling) + + strength_keywords_load = json.load(open(memory_saved + "/kw_strength.json")) + if strength_keywords_load["kw_strength_event"]: + self.kw_strength_event = strength_keywords_load["kw_strength_event"] + if strength_keywords_load["kw_strength_thought"]: + self.kw_strength_thought = strength_keywords_load["kw_strength_thought"] + def add(self, memory_basic: BasicMemory): """ @@ -97,37 +188,131 @@ class AgentMemory(Memory): return self.storage.append(memory_basic) if memory_basic.cause_by: - self.index[memory_basic.cause_by].append(memory_basic) + self.index[memory_basic.cause_by][0:0] = [memory_basic] return if memory_basic.type == "thought": - self.thought_list.append(memory_basic) + self.thought_list[0:0] = [memory_basic] return if memory_basic.type == "event": - self.event_list.append(memory_basic) + self.event_list[0:0] = [memory_basic] - def add_chat(self): + + def add_chat(self, created, expiration, s, p, o, + content, keywords, poignancy, + embedding_pair, filling, + cause_by): """ 调用add方法,初始化chat,在创建的时候就需要调用embeeding函数 """ - pass + memory_count = len(self.storage) + 1 + type_count = len(self.thought_list) + 1 + memory_type = "chat" + memory_id = f"memory_{str(memory_count)}" + depth = 1 - def add_thought(self): + memory_node = BasicMemory(memory_id, memory_count, type_count, memory_type, depth, + created, expiration, + s, p ,o, + content, embedding_pair[0], + poignancy, keywords, filling, + cause_by) + + keywords = [i.lower() for i in keywords] + for kw in keywords: + if kw in self.chat_keywords: + self.chat_keywords[kw][0:0] = [memory_node] + else: + self.chat_keywords[kw] = [memory_node] + + self.add(memory_node) + + self.embeddings[embedding_pair[0]] = embedding_pair[1] + return memory_node + + + def add_thought(self, created, expiration, s, p, o, + content, keywords, poignancy, + embedding_pair, filling): """ 调用add方法,初始化thought """ - pass + memory_count = len(self.storage) + 1 + type_count = len(self.thought_list) + 1 + memory_type = "event" + memory_id = f"memory_{str(memory_count)}" + depth = 1 + + try: + if filling: + depth_list = [memory_node.depth for memory_node in self.storage if memory_node.memory_id in filling ] + depth += max(depth_list) + except: + pass - def add_event(self): + memory_node = BasicMemory(memory_id, memory_count, type_count, memory_type, depth, + created, expiration, + s, p ,o, + content, embedding_pair[0], + poignancy, keywords, filling) + + keywords = [i.lower() for i in keywords] + for kw in keywords: + if kw in self.thought_keywords: + self.thought_keywords[kw][0:0] = [memory_node] + else: + self.thought_keywords[kw] = [memory_node] + + self.add(memory_node) + + if f"{p} {o}" != "is idle": + for kw in keywords: + if kw in self.kw_strength_thought: + self.kw_strength_thought[kw] += 1 + else: + self.kw_strength_thought[kw] = 1 + + self.embeddings[embedding_pair[0]] = embedding_pair[1] + return memory_node + + + def add_event(self, created, expiration, s, p, o, + content, keywords, poignancy, + embedding_pair, filling): """ 调用add方法,初始化event """ - pass + memory_count = len(self.storage) + 1 + type_count = len(self.event_list) + 1 + memory_type = "event" + memory_id = f"memory_{str(memory_count)}" + depth = 0 + + if "(" in content: + content = (" ".join(content.split()[:3]) + + " " + + content.split("(")[-1][:-1]) + + memory_node = BasicMemory(memory_id, memory_count, type_count, memory_type, depth, + created, expiration, + s, p ,o, + content, embedding_pair[0], + poignancy, keywords, filling) - def retrive(self,): - """ - 调用 - """ - pass + keywords = [i.lower() for i in keywords] + for kw in keywords: + if kw in self.event_keywords: + self.event_keywords[kw][0:0] = [memory_node] + else: + self.event_keywords[kw] = [memory_node] + + self.add(memory_node) -if __name__ == "__main__": - \ No newline at end of file + if f"{p} {o}" != "is idle": + for kw in keywords: + if kw in self.kw_strength_event: + self.kw_strength_event[kw] += 1 + else: + self.kw_strength_event[kw] = 1 + + self.embeddings[embedding_pair[0]] = embedding_pair[1] + return memory_node diff --git a/examples/st_game/memory/scratch.py b/examples/st_game/memory/scratch.py index 00da03dd6..d0d13002e 100644 --- a/examples/st_game/memory/scratch.py +++ b/examples/st_game/memory/scratch.py @@ -2,5 +2,536 @@ # -*- coding: utf-8 -*- # @Desc : Scratch类实现(角色信息类) -class Scratch(): - pass \ No newline at end of file +import datetime +import json +import sys +sys.path.append('../../') + +from ..utils.check import check_if_file_exists + +class Scratch: + def __init__(self, f_saved): + # 类别1:人物超参 + self.vision_r = 4 + self.att_bandwidth = 3 + self.retention = 5 + + # 类别2:世界信息 + self.curr_time = None + self.curr_tile = None + self.daily_plan_req = None + + # 类别3:人物角色的核心身份 + self.name = None + self.first_name = None + self.last_name = None + self.age = None + # L0 permanent core traits. + self.innate = None + # L1 stable traits. + self.learned = None + # L2 external implementation. + self.currently = None + self.lifestyle = None + self.living_area = None + + # 类别4:旧反思变量 + self.concept_forget = 100 + self.daily_reflection_time = 60 * 3 + self.daily_reflection_size = 5 + self.overlap_reflect_th = 2 + self.kw_strg_event_reflect_th = 4 + self.kw_strg_thought_reflect_th = 4 + + # 类别5:新反思变量 + self.recency_w = 1 + self.relevance_w = 1 + self.importance_w = 1 + self.recency_decay = 0.99 + self.importance_trigger_max = 150 + self.importance_trigger_curr = self.importance_trigger_max + self.importance_ele_n = 0 + self.thought_count = 5 + + # 类别6:个人计划 + self.daily_req = [] + self.f_daily_schedule = [] + self.f_daily_schedule_hourly_org = [] + + # 类别7:当前动作 + self.act_address = None + self.act_start_time = None + self.act_duration = None + self.act_description = None + self.act_pronunciatio = None + self.act_event = (self.name, None, None) + + self.act_obj_description = None + self.act_obj_pronunciatio = None + self.act_obj_event = (self.name, None, None) + + self.chatting_with = None + self.chat = None + self.chatting_with_buffer = dict() + self.chatting_end_time = None + + self.act_path_set = False + self.planned_path = [] + + if check_if_file_exists(f_saved): + # If we have a bootstrap file, load that here. + scratch_load = json.load(open(f_saved)) + + self.vision_r = scratch_load["vision_r"] + self.att_bandwidth = scratch_load["att_bandwidth"] + self.retention = scratch_load["retention"] + + if scratch_load["curr_time"]: + self.curr_time = datetime.datetime.strptime(scratch_load["curr_time"], + "%B %d, %Y, %H:%M:%S") + else: + self.curr_time = None + self.curr_tile = scratch_load["curr_tile"] + self.daily_plan_req = scratch_load["daily_plan_req"] + + self.name = scratch_load["name"] + self.first_name = scratch_load["first_name"] + self.last_name = scratch_load["last_name"] + self.age = scratch_load["age"] + self.innate = scratch_load["innate"] + self.learned = scratch_load["learned"] + self.currently = scratch_load["currently"] + self.lifestyle = scratch_load["lifestyle"] + self.living_area = scratch_load["living_area"] + + self.concept_forget = scratch_load["concept_forget"] + self.daily_reflection_time = scratch_load["daily_reflection_time"] + self.daily_reflection_size = scratch_load["daily_reflection_size"] + self.overlap_reflect_th = scratch_load["overlap_reflect_th"] + self.kw_strg_event_reflect_th = scratch_load["kw_strg_event_reflect_th"] + self.kw_strg_thought_reflect_th = scratch_load["kw_strg_thought_reflect_th"] + + self.recency_w = scratch_load["recency_w"] + self.relevance_w = scratch_load["relevance_w"] + self.importance_w = scratch_load["importance_w"] + self.recency_decay = scratch_load["recency_decay"] + self.importance_trigger_max = scratch_load["importance_trigger_max"] + self.importance_trigger_curr = scratch_load["importance_trigger_curr"] + self.importance_ele_n = scratch_load["importance_ele_n"] + self.thought_count = scratch_load["thought_count"] + + self.daily_req = scratch_load["daily_req"] + self.f_daily_schedule = scratch_load["f_daily_schedule"] + self.f_daily_schedule_hourly_org = scratch_load["f_daily_schedule_hourly_org"] + + self.act_address = scratch_load["act_address"] + if scratch_load["act_start_time"]: + self.act_start_time = datetime.datetime.strptime( + scratch_load["act_start_time"], + "%B %d, %Y, %H:%M:%S") + else: + self.curr_time = None + self.act_duration = scratch_load["act_duration"] + self.act_description = scratch_load["act_description"] + self.act_pronunciatio = scratch_load["act_pronunciatio"] + self.act_event = tuple(scratch_load["act_event"]) + + self.act_obj_description = scratch_load["act_obj_description"] + self.act_obj_pronunciatio = scratch_load["act_obj_pronunciatio"] + self.act_obj_event = tuple(scratch_load["act_obj_event"]) + + self.chatting_with = scratch_load["chatting_with"] + self.chat = scratch_load["chat"] + self.chatting_with_buffer = scratch_load["chatting_with_buffer"] + if scratch_load["chatting_end_time"]: + self.chatting_end_time = datetime.datetime.strptime( + scratch_load["chatting_end_time"], + "%B %d, %Y, %H:%M:%S") + else: + self.chatting_end_time = None + + self.act_path_set = scratch_load["act_path_set"] + self.planned_path = scratch_load["planned_path"] + + + def save(self, out_json): + """ + Save persona's scratch. + + INPUT: + out_json: The file where we wil be saving our persona's state. + OUTPUT: + None + """ + scratch = dict() + scratch["vision_r"] = self.vision_r + scratch["att_bandwidth"] = self.att_bandwidth + scratch["retention"] = self.retention + + scratch["curr_time"] = self.curr_time.strftime("%B %d, %Y, %H:%M:%S") + scratch["curr_tile"] = self.curr_tile + scratch["daily_plan_req"] = self.daily_plan_req + + scratch["name"] = self.name + scratch["first_name"] = self.first_name + scratch["last_name"] = self.last_name + scratch["age"] = self.age + scratch["innate"] = self.innate + scratch["learned"] = self.learned + scratch["currently"] = self.currently + scratch["lifestyle"] = self.lifestyle + scratch["living_area"] = self.living_area + + scratch["concept_forget"] = self.concept_forget + scratch["daily_reflection_time"] = self.daily_reflection_time + scratch["daily_reflection_size"] = self.daily_reflection_size + scratch["overlap_reflect_th"] = self.overlap_reflect_th + scratch["kw_strg_event_reflect_th"] = self.kw_strg_event_reflect_th + scratch["kw_strg_thought_reflect_th"] = self.kw_strg_thought_reflect_th + + scratch["recency_w"] = self.recency_w + scratch["relevance_w"] = self.relevance_w + scratch["importance_w"] = self.importance_w + scratch["recency_decay"] = self.recency_decay + scratch["importance_trigger_max"] = self.importance_trigger_max + scratch["importance_trigger_curr"] = self.importance_trigger_curr + scratch["importance_ele_n"] = self.importance_ele_n + scratch["thought_count"] = self.thought_count + + scratch["daily_req"] = self.daily_req + scratch["f_daily_schedule"] = self.f_daily_schedule + scratch["f_daily_schedule_hourly_org"] = self.f_daily_schedule_hourly_org + + scratch["act_address"] = self.act_address + scratch["act_start_time"] = (self.act_start_time + .strftime("%B %d, %Y, %H:%M:%S")) + scratch["act_duration"] = self.act_duration + scratch["act_description"] = self.act_description + scratch["act_pronunciatio"] = self.act_pronunciatio + scratch["act_event"] = self.act_event + + scratch["act_obj_description"] = self.act_obj_description + scratch["act_obj_pronunciatio"] = self.act_obj_pronunciatio + scratch["act_obj_event"] = self.act_obj_event + + scratch["chatting_with"] = self.chatting_with + scratch["chat"] = self.chat + scratch["chatting_with_buffer"] = self.chatting_with_buffer + if self.chatting_end_time: + scratch["chatting_end_time"] = (self.chatting_end_time + .strftime("%B %d, %Y, %H:%M:%S")) + else: + scratch["chatting_end_time"] = None + + scratch["act_path_set"] = self.act_path_set + scratch["planned_path"] = self.planned_path + + with open(out_json, "w") as outfile: + json.dump(scratch, outfile, indent=2) + + + def get_f_daily_schedule_index(self, advance=0): + """ + We get the current index of self.f_daily_schedule. + + Recall that self.f_daily_schedule stores the decomposed action sequences + up until now, and the hourly sequences of the future action for the rest + of today. Given that self.f_daily_schedule is a list of list where the + inner list is composed of [task, duration], we continue to add up the + duration until we reach "if elapsed > today_min_elapsed" condition. The + index where we stop is the index we will return. + + INPUT + advance: Integer value of the number minutes we want to look into the + future. This allows us to get the index of a future timeframe. + OUTPUT + an integer value for the current index of f_daily_schedule. + """ + # We first calculate teh number of minutes elapsed today. + today_min_elapsed = 0 + today_min_elapsed += self.curr_time.hour * 60 + today_min_elapsed += self.curr_time.minute + today_min_elapsed += advance + + x = 0 + for task, duration in self.f_daily_schedule: + x += duration + x = 0 + for task, duration in self.f_daily_schedule_hourly_org: + x += duration + + # We then calculate the current index based on that. + curr_index = 0 + elapsed = 0 + for task, duration in self.f_daily_schedule: + elapsed += duration + if elapsed > today_min_elapsed: + return curr_index + curr_index += 1 + + return curr_index + + + def get_f_daily_schedule_hourly_org_index(self, advance=0): + """ + We get the current index of self.f_daily_schedule_hourly_org. + It is otherwise the same as get_f_daily_schedule_index. + + INPUT + advance: Integer value of the number minutes we want to look into the + future. This allows us to get the index of a future timeframe. + OUTPUT + an integer value for the current index of f_daily_schedule. + """ + # We first calculate teh number of minutes elapsed today. + today_min_elapsed = 0 + today_min_elapsed += self.curr_time.hour * 60 + today_min_elapsed += self.curr_time.minute + today_min_elapsed += advance + # We then calculate the current index based on that. + curr_index = 0 + elapsed = 0 + for task, duration in self.f_daily_schedule_hourly_org: + elapsed += duration + if elapsed > today_min_elapsed: + return curr_index + curr_index += 1 + return curr_index + + + def get_str_iss(self): + """ + ISS stands for "identity stable set." This describes the commonset summary + of this persona -- basically, the bare minimum description of the persona + that gets used in almost all prompts that need to call on the persona. + + INPUT + None + OUTPUT + the identity stable set summary of the persona in a string form. + EXAMPLE STR OUTPUT + "Name: Dolores Heitmiller + Age: 28 + Innate traits: hard-edged, independent, loyal + Learned traits: Dolores is a painter who wants live quietly and paint + while enjoying her everyday life. + Currently: Dolores is preparing for her first solo show. She mostly + works from home. + Lifestyle: Dolores goes to bed around 11pm, sleeps for 7 hours, eats + dinner around 6pm. + Daily plan requirement: Dolores is planning to stay at home all day and + never go out." + """ + commonset = "" + commonset += f"Name: {self.name}\n" + commonset += f"Age: {self.age}\n" + commonset += f"Innate traits: {self.innate}\n" + commonset += f"Learned traits: {self.learned}\n" + commonset += f"Currently: {self.currently}\n" + commonset += f"Lifestyle: {self.lifestyle}\n" + commonset += f"Daily plan requirement: {self.daily_plan_req}\n" + commonset += f"Current Date: {self.curr_time.strftime('%A %B %d')}\n" + return commonset + + + def get_str_name(self): + return self.name + + + def get_str_firstname(self): + return self.first_name + + + def get_str_lastname(self): + return self.last_name + + + def get_str_age(self): + return str(self.age) + + + def get_str_innate(self): + return self.innate + + + def get_str_learned(self): + return self.learned + + + def get_str_currently(self): + return self.currently + + + def get_str_lifestyle(self): + return self.lifestyle + + + def get_str_daily_plan_req(self): + return self.daily_plan_req + + + def get_str_curr_date_str(self): + return self.curr_time.strftime("%A %B %d") + + + def get_curr_event(self): + if not self.act_address: + return (self.name, None, None) + else: + return self.act_event + + + def get_curr_event_and_desc(self): + if not self.act_address: + return (self.name, None, None, None) + else: + return (self.act_event[0], + self.act_event[1], + self.act_event[2], + self.act_description) + + + def get_curr_obj_event_and_desc(self): + if not self.act_address: + return ("", None, None, None) + else: + return (self.act_address, + self.act_obj_event[1], + self.act_obj_event[2], + self.act_obj_description) + + + def add_new_action(self, + action_address, + action_duration, + action_description, + action_pronunciatio, + action_event, + chatting_with, + chat, + chatting_with_buffer, + chatting_end_time, + act_obj_description, + act_obj_pronunciatio, + act_obj_event, + act_start_time=None): + self.act_address = action_address + self.act_duration = action_duration + self.act_description = action_description + self.act_pronunciatio = action_pronunciatio + self.act_event = action_event + + self.chatting_with = chatting_with + self.chat = chat + if chatting_with_buffer: + self.chatting_with_buffer.update(chatting_with_buffer) + self.chatting_end_time = chatting_end_time + + self.act_obj_description = act_obj_description + self.act_obj_pronunciatio = act_obj_pronunciatio + self.act_obj_event = act_obj_event + + self.act_start_time = self.curr_time + + self.act_path_set = False + + + def act_time_str(self): + """ + Returns a string output of the current time. + + INPUT + None + OUTPUT + A string output of the current time. + EXAMPLE STR OUTPUT + "14:05 P.M." + """ + return self.act_start_time.strftime("%H:%M %p") + + + def act_check_finished(self): + """ + Checks whether the self.Action instance has finished. + + INPUT + curr_datetime: Current time. If current time is later than the action's + start time + its duration, then the action has finished. + OUTPUT + Boolean [True]: Action has finished. + Boolean [False]: Action has not finished and is still ongoing. + """ + if not self.act_address: + return True + + if self.chatting_with: + end_time = self.chatting_end_time + else: + x = self.act_start_time + if x.second != 0: + x = x.replace(second=0) + x = (x + datetime.timedelta(minutes=1)) + end_time = (x + datetime.timedelta(minutes=self.act_duration)) + + if end_time.strftime("%H:%M:%S") == self.curr_time.strftime("%H:%M:%S"): + return True + return False + + + def act_summarize(self): + """ + Summarize the current action as a dictionary. + + INPUT + None + OUTPUT + ret: A human readable summary of the action. + """ + exp = dict() + exp["persona"] = self.name + exp["address"] = self.act_address + exp["start_datetime"] = self.act_start_time + exp["duration"] = self.act_duration + exp["description"] = self.act_description + exp["pronunciatio"] = self.act_pronunciatio + return exp + + + def act_summary_str(self): + """ + Returns a string summary of the current action. Meant to be + human-readable. + + INPUT + None + OUTPUT + ret: A human readable summary of the action. + """ + start_datetime_str = self.act_start_time.strftime("%A %B %d -- %H:%M %p") + ret = f"[{start_datetime_str}]\n" + ret += f"Activity: {self.name} is {self.act_description}\n" + ret += f"Address: {self.act_address}\n" + ret += f"Duration in minutes (e.g., x min): {str(self.act_duration)} min\n" + return ret + + + def get_str_daily_schedule_summary(self): + ret = "" + curr_min_sum = 0 + for row in self.f_daily_schedule: + curr_min_sum += row[1] + hour = int(curr_min_sum/60) + minute = curr_min_sum%60 + ret += f"{hour:02}:{minute:02} || {row[0]}\n" + return ret + + + def get_str_daily_schedule_hourly_org_summary(self): + ret = "" + curr_min_sum = 0 + for row in self.f_daily_schedule_hourly_org: + curr_min_sum += row[1] + hour = int(curr_min_sum/60) + minute = curr_min_sum%60 + ret += f"{hour:02}:{minute:02} || {row[0]}\n" + return ret diff --git a/examples/st_game/roles/st_role.py b/examples/st_game/roles/st_role.py index 047f9545b..8f0c47f7f 100644 --- a/examples/st_game/roles/st_role.py +++ b/examples/st_game/roles/st_role.py @@ -21,15 +21,18 @@ from ..memory.agent_memory import AgentMemory from ..actions.dummy_action import DummyAction from ..actions.user_requirement import UserRequirement from ..maze_environment import MazeEnvironment +from ..memory.retrieve import agent_retrieve +from ..memory.scratch import Scratch class STRoleContext(RoleContext): env: 'MazeEnvironment' = Field(default=None) memory: AgentMemory = Field(default=AgentMemory) + scratch: Scratch = Field(default=Scratch) class STRole(Role): - + # 继承Role类,Role类继承RoleContext,这里的逻辑需要认真考虑 # add a role's property structure to store role's age and so on like GA's Scratch. def __init__(self, @@ -65,6 +68,12 @@ class STRole(Role): # TODO observe info from maze_env pass + async def retrieve(self, query, n = 30 ,topk = 4): + # TODO retrieve memories from agent_memory + retrieve_memories = agent_retrieve(self._rc.memory, self._rc.scratch.curr_time, self._rc.scratch.recency_decay, query, n, topk) + return retrieve_memories + + async def plan(self): # TODO make a plan diff --git a/examples/st_game/utils/check.py b/examples/st_game/utils/check.py new file mode 100644 index 000000000..0a806fe2d --- /dev/null +++ b/examples/st_game/utils/check.py @@ -0,0 +1,14 @@ +def check_if_file_exists(curr_file): + """ + Checks if a file exists + ARGS: + curr_file: path to the current csv file. + RETURNS: + True if the file exists + False if the file does not exist + """ + try: + with open(curr_file) as f_analysis_file: pass + return True + except: + return False \ No newline at end of file