mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
已完成注释修改
This commit is contained in:
parent
e7c966653e
commit
3b42ab42a0
4 changed files with 27 additions and 27 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc : MemoryBasic,AgentMemory实现
|
||||
# @Desc : BasicMemory,AgentMemory实现
|
||||
|
||||
from metagpt.memory.memory import Memory
|
||||
from metagpt.schema import Message
|
||||
|
|
@ -8,14 +8,14 @@ import json
|
|||
from datetime import datetime
|
||||
|
||||
|
||||
class MemoryBasic(Message):
|
||||
class BasicMemory(Message):
|
||||
|
||||
def __init__(self, memory_id: str, memory_count: int, type_count: int, memory_type: str, depth: int, content: int,
|
||||
creaetd: datetime, expiration: datetime,
|
||||
subject: str, predicate: str, object: str,
|
||||
embedding_key: str, poignancy: int, keywords: list, filling: list):
|
||||
"""
|
||||
MemoryBasic继承于MG的Message类,其中content属性替代description属性
|
||||
BasicMemory继承于MG的Message类,其中content属性替代description属性
|
||||
Message类中对于Chat类型支持的非常好,对于Agent个体的Perceive,Reflection,Plan支持的并不多
|
||||
在Type设计上,我们延续GA的三个种类,但是对于Chat种类的对话进行特别设计(具体怎么设计还没想好)
|
||||
"""
|
||||
|
|
@ -27,23 +27,23 @@ class MemoryBasic(Message):
|
|||
cause_by 接受一个Action类,在此项目中,每个Agent需要有一个基础动作[Receive] 用于接受假对话Message;而每个Agent需要有独一无二的动作类,用以接受真对话Message
|
||||
"""
|
||||
self.memory_id: str = memory_id # 记忆ID
|
||||
self.memory_count: int = memory_count # 第几个记忆,实际数值与Memory相等,但是类型为整数
|
||||
self.memory_count: int = memory_count # 第几个记忆,实际数值与Memory相等
|
||||
self.type_count: int = type_count # 第几种记忆,类型为整数(具体不太理解如何生成的)
|
||||
self.memory_type: str = memory_type # 记忆类型,使用Field,包含 event,thought,chat三种类型
|
||||
self.memory_type: str = memory_type # 记忆类型,包含 event,thought,chat三种类型
|
||||
self.depth: str = depth # 记忆深度,类型为整数
|
||||
|
||||
self.created: datetime = creaetd # 创建时间
|
||||
self.expiration: datetime = expiration # 记忆失效时间,默认为空()
|
||||
self.last_accessed: datetime = creaetd # 上一次调用的时间,初始化时候与self.created一致
|
||||
|
||||
self.subject: str = subject # 主语,str类型
|
||||
self.predicate: str = predicate # 谓语,str类型
|
||||
self.object: str = object # 宾语,str类型
|
||||
self.subject: str = subject # 主语
|
||||
self.predicate: str = predicate # 谓语
|
||||
self.object: str = object # 宾语
|
||||
|
||||
self.embedding_key: str = embedding_key # 内容与self.content一致
|
||||
self.poignancy: int = poignancy # importance值,整数类型
|
||||
self.keywords: list = keywords # keywords,列表
|
||||
self.filling: list = filling # None或者列表
|
||||
self.poignancy: int = poignancy # importance值
|
||||
self.keywords: list = keywords # keywords
|
||||
self.filling: list = filling # None或者列表
|
||||
|
||||
|
||||
class AgentMemory(Memory):
|
||||
|
|
@ -60,7 +60,7 @@ class AgentMemory(Memory):
|
|||
@李嵩@张凯 这里的storage是List,你们需要写一个JSON转化器,将List修改为node.json一致的格式
|
||||
"""
|
||||
super.__init__()
|
||||
self.storage: list[MemoryBasic] = [] # 重写Stroage,存储MemoryBasic所有节点
|
||||
self.storage: list[BasicMemory] = [] # 重写Stroage,存储BasicMemory所有节点
|
||||
self.event_list = [] # 存储event记忆
|
||||
self.thought_list = [] # 存储thought记忆
|
||||
|
||||
|
|
@ -71,27 +71,27 @@ class AgentMemory(Memory):
|
|||
self.strength_event_keywords = dict() # 不知道具体作用,所以没有删除
|
||||
self.strength_thought_keywords = dict()
|
||||
|
||||
self.embeddings = json.load(open(memory_saved + "/embeddings.json")) # dict类型,load embedding.json
|
||||
self.memory_load()
|
||||
self.embeddings = json.load(open(memory_saved + "/embeddings.json"))
|
||||
self.load()
|
||||
|
||||
|
||||
def memory_save(self):
|
||||
def save(self):
|
||||
"""
|
||||
将MemormyBasic类存储为Nodes.json形式。复现GA中的Kw Strength.json形式
|
||||
@张凯补充一个可调用的函数
|
||||
"""
|
||||
pass
|
||||
|
||||
def memory_load(self):
|
||||
def load(self):
|
||||
"""
|
||||
将GA的JSON解析,填充到AgentMemory类之中
|
||||
"""
|
||||
pass
|
||||
|
||||
def add(self, memory_basic: MemoryBasic):
|
||||
def add(self, memory_basic: BasicMemory):
|
||||
"""
|
||||
Add a new message to storage, while updating the index
|
||||
重写add方法,修改原有的Message类为MemoryBasic类,并添加不同的记忆类型添加方式
|
||||
重写add方法,修改原有的Message类为BasicMemory类,并添加不同的记忆类型添加方式
|
||||
"""
|
||||
if memory_basic in self.storage:
|
||||
return
|
||||
|
|
@ -5,19 +5,19 @@
|
|||
import datetime
|
||||
from numpy import dot
|
||||
from numpy.linalg import norm
|
||||
from associative_memory import AgentMemory, MemoryBasic
|
||||
from examples.st_game.memory.agent_memory import AgentMemory, BasicMemory
|
||||
from utils.utils import embedding_tools
|
||||
|
||||
|
||||
def agent_retrieve(agent_memory: AgentMemory, curr_time: datetime.datetime, memory_forget: float, query: str, n: int = 30, topk: int = 4) -> list[MemoryBasic]:
|
||||
def agent_retrieve(agent_memory: AgentMemory, curr_time: datetime.datetime, memory_forget: float, query: str, n: int = 30, topk: int = 4) -> list[BasicMemory]:
|
||||
"""
|
||||
Retrieve需要集合Role使用,原因在于Role才具有AgentMemory,scratch
|
||||
逻辑:Role调用该函数,self._rc.AgentMemory,self._rc.scratch.curr_time,self._rc.scratch.memory_forget
|
||||
输入希望查询的内容与希望回顾的条数,返回TopK条高分记忆,即List[MemoryBasic]
|
||||
输入希望查询的内容与希望回顾的条数,返回TopK条高分记忆,即List[BasicMemory]
|
||||
|
||||
Score_lists示例
|
||||
{
|
||||
"memory": memories[i], MemoryBasic类
|
||||
"memory": memories[i], BasicMemory类
|
||||
"importance": memories[i].poignancy
|
||||
"recency": 衰减因子计算结果
|
||||
"relevance": 搜索结果
|
||||
|
|
@ -34,7 +34,7 @@ def agent_retrieve(agent_memory: AgentMemory, curr_time: datetime.datetime, memo
|
|||
score_list = normalize_score_floats(score_list, 0, 1)
|
||||
|
||||
total_dict = {}
|
||||
gw = [1, 1, 1] # 三个因素的权重,重要性,近因性,相关性
|
||||
gw = [1, 1, 1] # 三个因素的权重,重要性,近因性,相关性
|
||||
for i in range(len(score_list)):
|
||||
total_score = (score_list[i]['importance'] * gw[0] +
|
||||
score_list[i]['recency'] * gw[1] +
|
||||
|
|
@ -50,7 +50,7 @@ def agent_retrieve(agent_memory: AgentMemory, curr_time: datetime.datetime, memo
|
|||
def top_highest_x_values(d, x):
|
||||
"""
|
||||
输入字典,Topx
|
||||
返回以字典值排序,字典键组成的List[MemoryBasic]
|
||||
返回以字典值排序,字典键组成的List[BasicMemory]
|
||||
"""
|
||||
top_v = [item[0] for item in sorted(d.items(), key=lambda item: item[1], reverse=True)[:x]]
|
||||
return top_v
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
|
||||
from wrapper_prompt import special_response_generate, prompt_generate
|
||||
from memory.scratch import Scratch
|
||||
from memory.associative_memory import MemoryBasic
|
||||
from examples.st_game.memory.agent_memory import BasicMemory
|
||||
import json
|
||||
|
||||
|
||||
def get_poignancy_action(scratch: Scratch, content: MemoryBasic.content) -> str:
|
||||
def get_poignancy_action(scratch: Scratch, content: BasicMemory.content) -> str:
|
||||
"""
|
||||
衡量事件心酸度
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ from pathlib import Path
|
|||
from metagpt.roles.role import Role, RoleContext
|
||||
from metagpt.schema import Message
|
||||
|
||||
from ..memory.associative_memory import AgentMemory
|
||||
from ..memory.agent_memory import AgentMemory
|
||||
from ..actions.dummy_action import DummyAction
|
||||
from ..actions.user_requirement import UserRequirement
|
||||
from ..maze_environment import MazeEnvironment
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue