From 88b2d5fb2d298895f81f3ea9ce212f387b5154a3 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Sun, 30 Apr 2023 14:25:06 +1200 Subject: [PATCH] Remove global pre_index from summary_memory. --- autogpt/agent/agent.py | 1 + autogpt/llm/chat.py | 1 + autogpt/memory_management/summary_memory.py | 20 ++++++++------------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/autogpt/agent/agent.py b/autogpt/agent/agent.py index a049b9d2f1..a64fbbc8fe 100644 --- a/autogpt/agent/agent.py +++ b/autogpt/agent/agent.py @@ -56,6 +56,7 @@ class Agent: cfg = Config() self.ai_name = ai_name self.memory = memory + self.last_memory_index = 0 self.full_message_history = full_message_history self.next_action_count = next_action_count self.command_registry = command_registry diff --git a/autogpt/llm/chat.py b/autogpt/llm/chat.py index ad499e3bde..e66a06821f 100644 --- a/autogpt/llm/chat.py +++ b/autogpt/llm/chat.py @@ -157,6 +157,7 @@ def chat_with_ai( newly_trimmed_messages = get_newly_trimmed_messages( full_message_history=full_message_history, current_context=current_context, + prev_index=agent.last_memory_index ) memory = update_running_summary(newly_trimmed_messages) current_context.insert(insertion_index, memory) diff --git a/autogpt/memory_management/summary_memory.py b/autogpt/memory_management/summary_memory.py index d63c01278a..3dcc4885d8 100644 --- a/autogpt/memory_management/summary_memory.py +++ b/autogpt/memory_management/summary_memory.py @@ -10,25 +10,22 @@ current_memory = "" def get_newly_trimmed_messages( - full_message_history: List[Dict[str, str]], current_context: List[Dict[str, str]] -) -> Tuple[List[Dict[str, str]], int]: + full_message_history: List[Dict[str, str]], current_context: List[Dict[str, str]], last_memory_index: int) -> Tuple[List[Dict[str, str]], int]: """ This function returns a list of dictionaries contained in full_message_history - with an index higher than prev_index that are absent from current_context, and the new index. + with an index higher than last_memory_index that are absent from current_context, and the new index. Args: full_message_history (list): A list of dictionaries representing the full message history. current_context (list): A list of dictionaries representing the current context. - prev_index (int): An integer representing the previous index. + last_memory_index (int): An integer representing the previous index. Returns: - list: A list of dictionaries that are in full_message_history with an index higher than prev_index and absent from current_context. + list: A list of dictionaries that are in full_message_history with an index higher than last_memory_index and absent from current_context. int: The new index value for use in the next loop. """ - global prev_index - - # Select messages in full_message_history with an index higher than prev_index - new_messages = [msg for i, msg in enumerate(full_message_history) if i > prev_index] + # Select messages in full_message_history with an index higher than last_memory_index + new_messages = [msg for i, msg in enumerate(full_message_history) if i > last_memory_index] # Remove messages that are already present in current_context new_messages_not_in_context = [ @@ -36,14 +33,13 @@ def get_newly_trimmed_messages( ] # Find the index of the last message processed - new_index = prev_index + new_index = last_memory_index if new_messages_not_in_context: last_message = new_messages_not_in_context[-1] new_index = full_message_history.index(last_message) - prev_index = new_index - return new_messages_not_in_context + return new_messages_not_in_context, new_index def update_running_summary(new_events: List[Dict]) -> str: