From 34261a15835390c5c464cef88c4a42b52a88b739 Mon Sep 17 00:00:00 2001 From: AbTrax <45964236+AbTrax@users.noreply.github.com> Date: Mon, 1 May 2023 23:16:26 +1000 Subject: [PATCH] Fix side effects on message history (#3619) Co-authored-by: Reinier van der Leer --- autogpt/memory_management/summary_memory.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/autogpt/memory_management/summary_memory.py b/autogpt/memory_management/summary_memory.py index 9e6126377be8..754c09ba7c05 100644 --- a/autogpt/memory_management/summary_memory.py +++ b/autogpt/memory_management/summary_memory.py @@ -1,3 +1,4 @@ +import copy import json from typing import Dict, List, Tuple @@ -44,7 +45,9 @@ def get_newly_trimmed_messages( return new_messages_not_in_context, new_index -def update_running_summary(current_memory: str, new_events: List[Dict]) -> str: +def update_running_summary( + current_memory: str, new_events: List[Dict[str, str]] +) -> str: """ This function takes a list of dictionaries representing new events and combines them with the current summary, focusing on key and potentially important information to remember. The updated summary is returned in a message @@ -61,17 +64,23 @@ def update_running_summary(current_memory: str, new_events: List[Dict]) -> str: update_running_summary(new_events) # Returns: "This reminds you of these events from your past: \nI entered the kitchen and found a scrawled note saying 7." """ + # Create a copy of the new_events list to prevent modifying the original list + new_events = copy.deepcopy(new_events) + # Replace "assistant" with "you". This produces much better first person past tense results. for event in new_events: if event["role"].lower() == "assistant": event["role"] = "you" + # Remove "thoughts" dictionary from "content" content_dict = json.loads(event["content"]) if "thoughts" in content_dict: del content_dict["thoughts"] event["content"] = json.dumps(content_dict) + elif event["role"].lower() == "system": event["role"] = "your computer" + # Delete all user messages elif event["role"] == "user": new_events.remove(event)