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

Code before:
import json
import logging
import sqlite3
import time
import uuid
from typing import Any, Dict, List, Optional

from embedchain.constants import SQLITE_PATH
from embedchain.memory.message import ECBaseChatMessage
from embedchain.memory.utils import merge_metadata_dict

"""
app_id: saves the app_id of the embedchain app.
id: saves the id of the dialogue between user and model.
question: user query
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)
        )

Review comment (suggestion):
```suggestion
            CREATE TABLE IF NOT EXISTS chat_history (
```

To be consistent with other codebase. 

Code after:
import json
import logging
import sqlite3
import uuid
from typing import Any, Dict, List, Optional

from embedchain.constants import SQLITE_PATH
from embedchain.memory.message import ChatMessage
from embedchain.memory.utils import merge_metadata_dict

CHAT_MESSAGE_CREATE_TABLE_QUERY = """
            CREATE TABLE IF NOT EXISTS chat_history (
                app_id TEXT,
                id TEXT,
                question TEXT,
                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,