Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Save redis memory state, with the default being to wipe on start still.

BillSchumachercce79695fa43
diff --git a/scripts/config.py b/scripts/config.py
index 77498d6c91..8c582a1572 100644
--- a/scripts/config.py
+++ b/scripts/config.py
@@ -64,8 +64,9 @@ class Config(metaclass=Singleton):
self.redis_host = os.getenv("REDIS_HOST")
self.redis_port = os.getenv("REDIS_PORT")
self.redis_password = os.getenv("REDIS_PASSWORD")
+ self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True'
# Note that indexes must be created on db 0 in redis, this is not configureable.
-
+
self.memory_backend = os.getenv("MEMORY_BACKEND", 'pinecone')
# Initialize the OpenAI API client
openai.api_key = self.openai_api_key
diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py
index 162b9269bd..e7021066fa 100644
--- a/scripts/memory/redismem.py
+++ b/scripts/memory/redismem.py
@@ -44,7 +44,8 @@ class RedisMemory(MemoryProviderSingleton):
password=redis_password,
db=0 # Cannot be changed
)
- self.redis.flushall()
+ if cfg.wipe_redis_on_start:
+ self.redis.flushall()
try:
self.redis.ft("gpt").create_index(
fields=SCHEMA,
@@ -55,7 +56,9 @@ class RedisMemory(MemoryProviderSingleton):
)
except Exception as e:
print("Error creating Redis search index: ", e)
- self.vec_num = 0
+ existing_vec_num = self.redis.get('vec_num')
+ self.vec_num = int(existing_vec_num.decode('utf-8')) if\
+ existing_vec_num else 0
def add(self, data: str) -> str:
"""
@@ -72,10 +75,13 @@ class RedisMemory(MemoryProviderSingleton):
b"data": data,
"embedding": vector
}
- self.redis.hset(f"gpt:{self.vec_num}", mapping=data_dict)
+ pipe = self.redis.pipeline()
+ pipe.hset(f"gpt:{self.vec_num}", mapping=data_dict)
_text = f"Inserting data into memory at index: {self.vec_num}:\n"\
f"data: {data}"
self.vec_num += 1
+ pipe.set('vec_num', self.vec_num)
+ pipe.execute()
return _text
def get(self, data: str) -> Optional[List[Any]]: