Fix side effects on message history (#3619)

Co-authored-by: Reinier van der Leer <github@pwuts.nl>
This commit is contained in:
AbTrax
2023-05-01 23:16:26 +10:00
committed by GitHub
parent d8968ae899
commit 34261a1583

View File

@@ -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)