掘金 人工智能 49秒前
Generative agents 代码分析 记忆
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

文章详细阐述了AI系统中记忆的三个核心检索维度:新近度(Recency)、重要性(Importance)和相关性(Relevance)。新近度通过指数衰减函数衡量记忆的“新鲜度”,重要性则依靠语言模型评估事件的深刻程度。相关性通过计算记忆与当前情境的语义相似度来确定。这三个因素经过标准化和加权组合,共同决定了记忆的检索优先级,确保AI能够有效地提取最适合当前情境的信息。文章还介绍了空间记忆、联想记忆和短期记忆等不同类型的记忆存储方式,并以Isabella Rodriguez的案例展示了这些概念的实际应用。

✨ **新近度(Recency)**:记忆的“新鲜度”通过指数衰减函数计算,最近访问的记忆获得更高分数,反映了时间对记忆保留的影响,如使用0.995的衰减因子。

⭐ **重要性(Importance)**:通过语言模型对记忆进行评分(1-10),区分平凡与核心记忆。例如,日常琐事得分低,而重大事件得分高,如“整理房间”得2分,“向喜欢的人约会”得8分。

🔗 **相关性(Relevance)**:通过计算记忆描述与当前查询的嵌入向量之间的余弦相似度来衡量。与当前情境高度相关的记忆将获得更高的分数。

⚖️ **加权检索**:最终记忆检索分数是新近度、重要性和相关性这三个因素的加权组合。在示例实现中,所有权重(α)均设置为1,以确保适合模型上下文窗口的记忆被优先选取。

🧠 **记忆类型**:文章介绍了多种记忆形式,包括存储具体地点和物品信息的“空间记忆”,通过向量存储和节点记录的“联想记忆”,以及包含个人信息和当前状态的“短期记忆(scratch)”。

记忆的基本概念

Recency

最近访问过的记忆对象将会获得更高的分数,这样一来,刚刚发生的事件或者今天早上的事件很可能仍然在虚拟人的注意力范围内。在我们的实现中,我们将Recency视为一个指数衰减函数,根据上次检索记忆以来的沙盒游戏小时数来计算。在本文中衰减因子为0.995。

Importance

给虚拟人认为重要的记忆对象分配更高的分数来区分平凡的记忆和核心记忆。例如,对于日常时间将得到较低的重要性分数,比如在自己的房间吃早餐。与他人分手这样的事情将得到较高的分数。有许多可能的重要性评分实现方式;我们发现直接询问语言模型输出一个整数分数是有效的。完整的提示如下:

在 1 到 10 的范围内,其中 1 是纯粹平凡的(例如,刷牙、整理床铺),10 是印象深刻的事情(例如,分手、大学录取),对以下一段记忆的可能深刻意义进行评分。

记忆:在超市购买东西

评分: <填写>

对于“整理房间”,这个提示返回整数值 2,对于“向你喜欢的人约会”,返回整数值 8。重要性分数是在创建记忆对象时生成的。

Relevance

Relevance 为与当前情况相关的 memory 对象分配更高的分数。什么是相关的取决于 “Related to what?”的答案,因此我们在查询 memory 上设置相关性。

例如,如果查询是学生正在与同学讨论化学考试要学习什么,则关于他们早餐的记忆对象应该具有低相关性,而关于老师和学校作业的记忆对象应该具有较高的相关性。在我们的实现中,我们使用语言模型来生成每个内存的文本描述的嵌入向量。然后,我们将相关性计算为 memory 的嵌入向量和查询 memory 的嵌入向量之间的余弦相似度。

为了计算最终的检索分数,我们将 Recency(新近度)、Importance(重要性)和 Relevance(相关性)分数标准化到 [0,1] 使用最小-最大缩放的范围。检索函数将所有记忆作为三个元素的加权组合进行评分:

。在我们的实现中,所有 α s 都设置为 1。适合语言模型上下文窗口的排名靠前的记忆将包含在提示中。

各种类型的记忆

空间记忆 spatia memory

{  "the Ville": {    "Hobbs Cafe": {      "cafe": [        "refrigerator",        "cafe customer seating",        "cooking area",        "kitchen sink",        "behind the cafe counter",        "piano"      ]    },    "Isabella Rodriguez's apartment": {      "main room": [        "bed",        "desk",        "refrigerator",        "closet",        "shelf"      ],      "bathroom": [        "shower",        "bathroom sink",        "toilet"      ]    },    "The Rose and Crown Pub": {      "pub": [        "shelf",        "refrigerator",        "bar customer seating",        "behind the bar counter",        "kitchen sink",        "cooking area",        "microphone"      ]    },    "Carlos Gomez's apartment": {      "main room": [        "desk",        "bed"      ],      "bathroom": [      ]    }  }}

联想记忆 associative memory

# 向量记忆存储elf.embeddings = json.load(open(f_saved + "/embeddings.json"))# 记忆节点nodes_load = json.load(open(f_saved + "/nodes.json"))# 记录 event 和 thought 的强度(时间长度表示?)kw_strength_load = json.load(open(f_saved + "/kw_strength.json"))    if kw_strength_load["kw_strength_event"]:       self.kw_strength_event = kw_strength_load["kw_strength_event"]    if kw_strength_load["kw_strength_thought"]:       self.kw_strength_thought = kw_strength_load["kw_strength_thought"]

短期记忆 scratch

{  "vision_r": 8,  "att_bandwidth": 8,  "retention": 8,  "curr_time": "February 14, 2023, 00:02:20",  "curr_tile": [    73,    14  ],  "daily_plan_req": "1. Wake up at 6am and complete morning routine (shower, brush teeth, get dressed) by 6:30am. 2. Arrive at Hobbs Cafe by 7:30am to do final preparations for the Valentine's Day party. 3. Welcome and greet customers as they arrive for the party starting at 5pm. 4. Ensure that food and drinks are well-stocked and available throughout the evening. 5. Continuously check on guests and provide excellent customer service during the event. 6. Close the cafe at 8pm and do a final walkthrough to ensure everything is in order before heading home.",  "name": "Isabella Rodriguez",  "first_name": "Isabella",  "last_name": "Rodriguez",  "age": 34,  "innate": "friendly, outgoing, hospitable",  "learned": "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": "Status: Today is the day! Isabella Rodriguez is filled with excitement as the Valentine's Day party at Hobbs Cafe is finally here. She has been tirelessly planning and preparing for this event, and now it's time to bring it to life. The party is set to start at 5pm at Hobbs Cafe, and Isabella has ensured that everything is carefully arranged and ready to create a memorable evening for everyone. She can't wait to welcome her customers and friends to join the festivities and celebrate love together. Isabella is confident that her efforts will pay off and that the party will be a great success.",  "lifestyle": "Isabella Rodriguez goes to bed around 11pm, awakes up around 6am.",  "living_area": "the Ville:Isabella Rodriguez's apartment:main room",  "concept_forget": 100,  "daily_reflection_time": 180,  "daily_reflection_size": 5,  "overlap_reflect_th": 4,  "kw_strg_event_reflect_th": 10,  "kw_strg_thought_reflect_th": 9,  "recency_w": 1,  "relevance_w": 1,  "importance_w": 1,  "recency_decay": 0.995,  "importance_trigger_max": 150,  "importance_trigger_curr": 122,  "importance_ele_n": 20,  "thought_count": 5,  "daily_req": [    "wake up and complete the morning routine at 6:00 am",    "open Hobbs Cafe at 8:00 am",    "have lunch with her staff at 12:00 pm",    "attend to guests at the cafe from 8:00 am to 8:00 pm",    "take a long walk after closing the cafe at 8:00 pm"  ],  "f_daily_schedule": [    [      "sleeping",      360    ],    [      "waking up and completing her morning routine",      60    ],    [      "arriving at Hobbs Cafe to do final preparations for the Valentine's Day party",      60    ],    [      "opening Hobbs Cafe and attending to guests",      60    ],    [      "having lunch with her staff",      60    ],    [      "attending to guests at the cafe",      120    ],    [      "having lunch with her staff",      60    ],    [      "attending to guests at the cafe",      240    ],    [      "welcoming and greeting customers as they arrive for the Valentine's Day party",      60    ],    [      "attending to guests at the cafe",      120    ],    [      "closing Hobbs Cafe and doing a final walkthrough to ensure everything is in order",      60    ],    [      "taking a long walk",      120    ],    [      "going to bed",      60    ]  ],  "f_daily_schedule_hourly_org": [    [      "sleeping",      360    ],    [      "waking up and completing her morning routine",      60    ],    [      "arriving at Hobbs Cafe to do final preparations for the Valentine's Day party",      60    ],    [      "opening Hobbs Cafe and attending to guests",      60    ],    [      "having lunch with her staff",      60    ],    [      "attending to guests at the cafe",      120    ],    [      "having lunch with her staff",      60    ],    [      "attending to guests at the cafe",      240    ],    [      "welcoming and greeting customers as they arrive for the Valentine's Day party",      60    ],    [      "attending to guests at the cafe",      120    ],    [      "closing Hobbs Cafe and doing a final walkthrough to ensure everything is in order",      60    ],    [      "taking a long walk",      120    ],    [      "going to bed",      60    ]  ],  "act_address": "the Ville:Isabella Rodriguez's apartment:main room:bed",  "act_start_time": "February 13, 2023, 21:11:00",  "act_duration": 180,  "act_description": "sleeping",  "act_pronunciatio": "\ud83d\udca4",  "act_event": [    "Isabella Rodriguez",    "is",    "sleep"  ],  "act_obj_description": "being used",  "act_obj_pronunciatio": "\ud83d\udcbb",  "act_obj_event": [    "bed",    "be",    "used"  ],  "chatting_with": null,  "chat": null,  "chatting_with_buffer": {    "Klaus Mueller": -1525,    "Maria Lopez": -1189  },  "chatting_end_time": null,  "act_path_set": true,  "planned_path": []}

记忆存储

记忆节点的数据结构

class ConceptNode:   def __init__(self,               node_id, node_count, type_count, node_type, depth,               created, expiration,                s, p, o,                description, embedding_key, poignancy, keywords, filling):     self.node_id = node_id# 编号    self.node_count = node_count    self.type_count = type_count    self.type = node_type # 类型,event/chat/thought,分别对应 add_event/add_chat/add_thought 方法    self.depth = depth# ??    self.created = created    self.expiration = expiration# 到期时间    self.last_accessed = self.created    self.subject = s# 三元组,主语    self.predicate = p# 谓语    self.object = o# 宾语    self.description = description    self.embedding_key = embedding_key    self.poignancy = poignancy    self.keywords = keywords    self.filling = filling

记忆明文

记忆节点的 description 字段内容会同时转换为向量数据保存在 embeddings.json中,方便后面做检索 Reveriev :

{  "node_147": {    "node_count": 147,    "type_count": 2,    "type": "chat",    "depth": 0,    "created": "2023-02-13 11:22:40",    "expiration": null,    "subject": "Klaus Mueller",    "predicate": "chat with",    "object": "Isabella Rodriguez",    "description": "conversing about a conversation about Isabella inviting Klaus to her Valentine's Day party at Hobbs Cafe on February 14th, 2023 from 5pm to 7pm.",    "embedding_key": "conversing about a conversation about Isabella inviting Klaus to her Valentine's Day party at Hobbs Cafe on February 14th, 2023 from 5pm to 7pm.",    "poignancy": 6,    "keywords": ["Klaus Mueller", "Isabella Rodriguez"],    "filling": [      [        "Isabella Rodriguez",        "Hi Klaus! How are you enjoying your meal? I wanted to let you know that I'm planning a Valentine's Day party at Hobbs Cafe on February 14th, 2023 from 5pm to 7pm. I would love for you to join us!"      ],      [        "Klaus Mueller",        "Oh, hi Isabella! I'm doing well, thank you. The meal is delicious as always. A Valentine's Day party sounds fun. I'd love to join! Thank you for inviting me."      ]    ]  },  "node_94": {    "node_count": 94,    "type_count": 12,    "type": "thought",    "depth": 2,    "created": "2023-02-13 09:15:10",    "expiration": "2023-03-15 09:15:10",    "subject": "Klaus Mueller",    "predicate": "have",    "object": "strong relationship",    "description": "Klaus Mueller and Maria Lopez have a strong relationship",    "embedding_key": "Klaus Mueller and Maria Lopez have a strong relationship",    "poignancy": 7,    "keywords": ["have", "Klaus Mueller", "strong relationship"],    "filling": ["node_10", "node_11", "node_9"]  },  <!-- -->  "node_11": {    "node_count": 11,    "type_count": 4,    "type": "thought",    "depth": 1,    "created": "2023-02-13 00:00:20",    "expiration": "2023-03-15 00:00:20",    "subject": "Klaus Mueller",    "predicate": "be",    "object": "close friends and classmates",    "description": "Klaus Mueller is close friends and classmates with Maria Lopez.",    "embedding_key": "Klaus Mueller is close friends and classmates with Maria Lopez.",    "poignancy": 6,    "keywords": ["Klaus Mueller", "be", "close friends and classmates"],    "filling": null  },  "node_10": {    "node_count": 10,    "type_count": 3,    "type": "thought",    "depth": 1,    "created": "2023-02-13 00:00:20",    "expiration": "2023-03-15 00:00:20",    "subject": "Klaus Mueller",    "predicate": "have been",    "object": "friends",    "description": "Klaus Mueller and Maria Lopez have been friends for more than 2 years.",    "embedding_key": "Klaus Mueller and Maria Lopez have been friends for more than 2 years.",    "poignancy": 7,    "keywords": ["friends", "Klaus Mueller", "have been"],    "filling": null  },  "node_9": {    "node_count": 9,    "type_count": 2,    "type": "thought",    "depth": 1,    "created": "2023-02-13 00:00:20",    "expiration": "2023-03-15 00:00:20",    "subject": "Klaus Mueller",    "predicate": "have",    "object": "crush on Maria Lopez",    "description": "Klaus Mueller has a crush on Maria Lopez.",    "embedding_key": "Klaus Mueller has a crush on Maria Lopez.",    "poignancy": 6,    "keywords": ["have", "Klaus Mueller", "crush on Maria Lopez"],    "filling": null  },}

记忆向量化

key + 向量值,其中 key 对应 node.embedding_key

{    "Isabella Rodriguez is idle": [        -0.03697006031870842,        -0.010967422276735306,        0.0015322132967412472,        -0.00994198489934206,        -0.014587010256946087,        0.002556801540777087,    ]}

自我认知(角色定义)

所有记忆最终都是赋予一个具体 persona 的。

# 构造角色,如果有角色相关的本地记忆,则加载记忆,没有就创建新的记忆# environment/frontend_server/storage/July1_the_ville_isabella_maria_klaus-step-3-20/personas/Isabella Rodriguez/bootstrap_memory# 空间记忆f_s_mem_saved = f"{folder_mem_saved}/bootstrap_memory/spatial_memory.json"self.s_mem = MemoryTree(f_s_mem_saved)# 联想记忆f_a_mem_saved = f"{folder_mem_saved}/bootstrap_memory/associative_memory"    self.a_mem = AssociativeMemory(f_a_mem_saved)# 短期记忆,包含了个人信息scratch_saved = f"{folder_mem_saved}/bootstrap_memory/scratch.json"self.scratch = Scratch(scratch_saved)

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AI记忆 新近度 重要性 相关性 记忆检索
相关文章