Skip to content

Agent Zero AI framework

short memory ids

frdelec455647189e
diff --git a/python/helpers/guids.py b/python/helpers/guids.py
new file mode 100644
index 000000000..f0def4b4c
--- /dev/null
+++ b/python/helpers/guids.py
@@ -0,0 +1,4 @@
+import random, string
+
+def generate_id(length: int = 8) -> str:
+ return "".join(random.choices(string.ascii_letters + string.digits, k=length))
diff --git a/python/helpers/memory.py b/python/helpers/memory.py
index fcbcf302f..0eb203cbb 100644
--- a/python/helpers/memory.py
+++ b/python/helpers/memory.py
@@ -2,6 +2,7 @@ from datetime import datetime
from typing import Any, List, Sequence
from langchain.storage import InMemoryByteStore, LocalFileStore
from langchain.embeddings import CacheBackedEmbeddings
+from python.helpers import guids
# from langchain_chroma import Chroma
from langchain_community.vectorstores import FAISS
@@ -24,7 +25,6 @@ import numpy as np
from python.helpers.print_style import PrintStyle
from . import files
from langchain_core.documents import Document
-import uuid
from python.helpers import knowledge_import
from python.helpers.log import Log, LogItem
from enum import Enum
@@ -361,7 +361,7 @@ class Memory:
return ids[0]
async def insert_documents(self, docs: list[Document]):
- ids = [str(uuid.uuid4()) for _ in range(len(docs))]
+ ids = [self._generate_doc_id() for _ in range(len(docs))]
timestamp = self.get_timestamp()
if ids:
@@ -378,6 +378,13 @@ class Memory:
def _save_db(self):
Memory._save_db_file(self.db, self.memory_subdir)
+ def _generate_doc_id(self):
+ while True:
+ doc_id = guids.generate_id(10) # random ID
+ if not self.db.get_by_ids(doc_id): # check if exists
+ return doc_id
+
+
@staticmethod
def _save_db_file(db: MyFaiss, memory_subdir: str):
abs_dir = Memory._abs_db_dir(memory_subdir)