Repository: mem0ai/mem0
File: embedchain/memory/base.py (Python)

Code before:
answer: ai response
created_at: unix formatted timestamp of creation.
metadata: stringified JSON of metadata dictionary.
"""
CHAT_MEMORY_CREATE_TABLE_QUERY = """
            CREATE TABLE IF NOT EXISTS chat_memory (
                app_id TEXT,
                id TEXT,
                question TEXT,
                answer TEXT,
                created_at REAL,
                metadata TEXT,
                PRIMARY KEY (id, app_id)
            )
            """


class ECChatMemory:
    def __init__(self) -> None:
        with sqlite3.connect(SQLITE_PATH) as self.connection:
            self.cursor = self.connection.cursor()

            self.cursor.execute(CHAT_MEMORY_CREATE_TABLE_QUERY)
            self.connection.commit()

    def add(self, app_id, chat_memory: ECBaseChatMessage) -> Optional[str]:
        memory_id = str(uuid.uuid4())
        created_at = time.time()
        metadata = self._serialize_json(
            merge_metadata_dict(chat_memory.human_message.metadata, chat_memory.ai_message.metadata)
        )
        ADD_CHAT_MESSAGE_QUERY = """
            INSERT INTO chat_memory (app_id, id, question, answer, created_at, metadata)
            VALUES (?, ?, ?, ?, ?, ?)
        """
        self.cursor.execute(
            ADD_CHAT_MESSAGE_QUERY,
            (
                app_id,
                memory_id,
                chat_memory.human_message.content,
                chat_memory.ai_message.content,
                created_at,
                metadata,
            ),
        )
        self.connection.commit()
        logging.info(f"Added chat memory to db with id: {memory_id}")
        return memory_id

    def delete_chat_history(self, app_id: str):

Review comment (suggestion):
```suggestion
    def add(self, app_id, chat_message: ChatMessage) -> Optional[str]:
```

Code after:
                answer TEXT,
                metadata TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                PRIMARY KEY (id, app_id)
            )
            """


class ECChatMemory:
    def __init__(self) -> None:
        with sqlite3.connect(SQLITE_PATH) as self.connection:
            self.cursor = self.connection.cursor()

            self.cursor.execute(CHAT_MESSAGE_CREATE_TABLE_QUERY)
            self.connection.commit()

    def add(self, app_id, chat_message: ChatMessage) -> Optional[str]:
        memory_id = str(uuid.uuid4())
        metadata_dict = merge_metadata_dict(chat_message.human_message.metadata, chat_message.ai_message.metadata)
        if metadata_dict:
            metadata = self._serialize_json(metadata_dict)
        ADD_CHAT_MESSAGE_QUERY = """
            INSERT INTO chat_history (app_id, id, question, answer, metadata)
            VALUES (?, ?, ?, ?, ?)
        """
        self.cursor.execute(
            ADD_CHAT_MESSAGE_QUERY,
            (
                app_id,
                memory_id,
                chat_message.human_message.content,
                chat_message.ai_message.content,
                metadata if metadata_dict else "{}",
            ),
        )
        self.connection.commit()
        logging.info(f"Added chat memory to db with id: {memory_id}")
        return memory_id

    def delete_chat_history(self, app_id: str):
        DELETE_CHAT_HISTORY_QUERY = """
            DELETE FROM chat_history WHERE app_id=?
        """
        self.cursor.execute(
            DELETE_CHAT_HISTORY_QUERY,
            (app_id,),
        )
        self.connection.commit()

    def get_recent_memories(self, app_id, num_rounds=10) -> List[ChatMessage]:
        """