short memory ids

frdel committed Aug 28, 2025 at 09:42 UTC 05a0803cb7a77b323bf3a9bd5cd35ba50b0442fd
2 files changed +13 -2
python/helpers/guids.py new
+4
@@ -0,0 +1,4 @@
1 +import random, string
2 +
3 +def generate_id(length: int = 8) -> str:
4 + return "".join(random.choices(string.ascii_letters + string.digits, k=length))
python/helpers/memory.py
+9 -2
@@ -2,6 +2,7 @@ from datetime import datetime
2 from typing import Any, List, Sequence
3 from langchain.storage import InMemoryByteStore, LocalFileStore
4 from langchain.embeddings import CacheBackedEmbeddings
5 +from python.helpers import guids
6
7 # from langchain_chroma import Chroma
8 from langchain_community.vectorstores import FAISS
@@ -24,7 +25,6 @@ import numpy as np
25 from python.helpers.print_style import PrintStyle
26 from . import files
27 from langchain_core.documents import Document
27 -import uuid
28 from python.helpers import knowledge_import
29 from python.helpers.log import Log, LogItem
30 from enum import Enum
@@ -361,7 +361,7 @@ class Memory:
361 return ids[0]
362
363 async def insert_documents(self, docs: list[Document]):
364 - ids = [str(uuid.uuid4()) for _ in range(len(docs))]
364 + ids = [self._generate_doc_id() for _ in range(len(docs))]
365 timestamp = self.get_timestamp()
366
367 if ids:
@@ -378,6 +378,13 @@ class Memory:
378 def _save_db(self):
379 Memory._save_db_file(self.db, self.memory_subdir)
380
381 + def _generate_doc_id(self):
382 + while True:
383 + doc_id = guids.generate_id(10) # random ID
384 + if not self.db.get_by_ids(doc_id): # check if exists
385 + return doc_id
386 +
387 +
388 @staticmethod
389 def _save_db_file(db: MyFaiss, memory_subdir: str):
390 abs_dir = Memory._abs_db_dir(memory_subdir)