Class Shared Memory Tool

Hi CrewAI folks!
Does anyone implement “shared memory tools” on CrewAI with SQLite or any other DB?
Is there any other simple way to do this with multiple agents?
In my little experience, the system always informed me that there was not enough memory to perform tasks. But I do not know if this is a prompt issue.
Tks!

class SharedMemoryTool:
persist_file: str = “/pathway to my drive/SQLite/shared_memory.json”
memory_store: Dict[str, Any] = {}

def __init__(self):
    """
    Inicializa a memória compartilhada, carregando dados de um arquivo persistente, se existir.
    """
    self._load_memory()

def _load_memory(self):
    """
    Carrega a memória compartilhada a partir de um arquivo JSON.
    """
    try:
        if os.path.exists(self.persist_file):
            with open(self.persist_file, 'r') as file:
                SharedMemoryTool.memory_store = json.load(file)
            logging.info("Memória compartilhada carregada com sucesso.")
    except (IOError, json.JSONDecodeError) as e:
        logging.error(f"Erro ao carregar memória: {e}")
        SharedMemoryTool.memory_store = {}

def _save_memory(self):
    """
    Salva a memória compartilhada em um arquivo JSON.
    """
    try:
        with open(self.persist_file, 'w') as file:
            json.dump(SharedMemoryTool.memory_store, file)
        logging.info("Memória compartilhada salva com sucesso.")
    except IOError as e:
        logging.error(f"Erro ao salvar memória: {e}")

def get(self, key: str) -> Optional[Any]:
    return SharedMemoryTool.memory_store.get(key, None)

def set(self, key: str, value: Any):
    SharedMemoryTool.memory_store[key] = value
    self._save_memory()

My first thought is a custom tool that all Tasks/Agents could have access to.
I am pesently working on using a neo4j GraphDB as a knowledge graph, but could easily be used to provide a static memory store.
Not as simple as using Sql-light + json, but comes with far more functionality/control.
Your memoy issue is probably more to do with context size, than prompt. Depends on what you want to save in memory, or how much ‘context’ you want to maintain.

Just a thought!

1 Like

Dabnis, Tks!!! New perspectives!