Skip to content

Commit a24e243

Browse files
author
yunjiu
committed
[feature][memory] add global_msg and modified function
1 parent 1cda8f0 commit a24e243

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

muagent/connector/memory_manager.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,4 +857,46 @@ def tbasedoc2Memory(self, r_docs) -> Memory:
857857
# content = message.customed_kargs.pop(key)
858858
# if content is not None:
859859
# message.setattr(key, content)
860-
return memory
860+
return memory
861+
862+
863+
def init_global_msg(self, chat_index: str, role_name: str, role_content: str, role_type: str = "global_value") -> bool:
864+
msg = Message(chat_index=chat_index, role_name=role_name, role_type=role_type, role_content=role_content)
865+
try:
866+
self.append(msg)
867+
return True
868+
except Exception as e:
869+
logger.error(f"Failed to initialize global message: {e}")
870+
return False
871+
872+
def get_msg_by_role_name(self, chat_index: str, role_name: str) -> Optional[Message]:
873+
memory = self.get_memory_pool(chat_index)
874+
for msg in memory.messages:
875+
if msg.role_name == role_name:
876+
return msg
877+
return None
878+
879+
def get_msg_content_by_role_name(self, chat_index: str, role_name: str) -> Optional[str]:
880+
message = self.get_msg_by_role_name(chat_index, role_name)
881+
if message == None:
882+
return None
883+
else:
884+
return message.role_content
885+
886+
def update_msg_content_by_rule(self, chat_index: str, role_name: str, new_content: str,update_rule: str) -> bool:
887+
message = self.get_msg_by_role_name(chat_index, role_name)
888+
889+
if message == None:
890+
return False
891+
892+
prompt = f"{new_content}{message.role_content}{update_rule}"
893+
894+
model = getChatModelFromConfig(self.llm_config)
895+
896+
new_role_content = model.predict(prompt)
897+
898+
if new_role_content is not None:
899+
message.role_content = new_role_content
900+
return True
901+
else:
902+
return False

0 commit comments

Comments
 (0)