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

Code before:
        else:
            DELETE_CHAT_HISTORY_QUERY = "DELETE FROM ec_chat_history WHERE app_id=?"
            params = (app_id,)

        self.cursor.execute(DELETE_CHAT_HISTORY_QUERY, params)
        self.connection.commit()

    def get(
        self, app_id, session_id: str = "default", num_rounds=10, fetch_all: bool = False, display_format=False
    ) -> list[ChatMessage]:
        """
        Get the most recent num_rounds rounds of conversations
        between human and AI, for a given app_id.
        """

        if fetch_all:
            QUERY = """
                SELECT * FROM ec_chat_history
                WHERE app_id=?
                ORDER BY created_at DESC
            """
            params = (app_id,)
        else:
            QUERY = """
                SELECT * FROM ec_chat_history
                WHERE app_id=? AND session_id=?
                ORDER BY created_at DESC
                LIMIT ?
            """
            params = (app_id, session_id, num_rounds)

        self.cursor.execute(
            QUERY,
            params,
        )

        results = self.cursor.fetchall()
        history = []
        for result in results:
            app_id, _, session_id, question, answer, metadata, timestamp = result
            metadata = self._deserialize_json(metadata=metadata)
            # Return list of dict if display_format is True
            if display_format:
                history.append(
                    {
                        "session_id": session_id,
                        "human": question,
                        "ai": answer,
                        "metadata": metadata,
                        "timestamp": timestamp,
                    }

Review comment (refactor):
You can refactor this code to avoid repetition like this:

```python
base_query = """
    SELECT * FROM ec_chat_history
    WHERE app_id=?
"""

if fetch_all:
    additional_query = "ORDER BY created_at DESC"
    params = (app_id,)
else:
    additional_query = "AND session_id=? ORDER BY created_at DESC LIMIT ?"
    params = (app_id, session_id, num_rounds)

QUERY = base_query + additional_query
```

Code after:
        else:
            DELETE_CHAT_HISTORY_QUERY = "DELETE FROM ec_chat_history WHERE app_id=?"
            params = (app_id,)

        self.cursor.execute(DELETE_CHAT_HISTORY_QUERY, params)
        self.connection.commit()

    def get(
        self, app_id, session_id: str = "default", num_rounds=10, fetch_all: bool = False, display_format=False
    ) -> list[ChatMessage]:
        """
        Get the chat history for a given app_id.

        param: app_id - The app_id to get chat history
        param: session_id (optional) - The session_id to get chat history. Defaults to "default"
        param: num_rounds (optional) - The number of rounds to get chat history. Defaults to 10
        param: fetch_all (optional) - Whether to fetch all chat history or not. Defaults to False
        param: display_format (optional) - Whether to return the chat history in display format. Defaults to False
        """

        base_query = """
            SELECT * FROM ec_chat_history
            WHERE app_id=?
        """

        if fetch_all:
            additional_query = "ORDER BY created_at DESC"
            params = (app_id,)
        else:
            additional_query = """
                AND session_id=?
                ORDER BY created_at DESC
                LIMIT ?
            """
            params = (app_id, session_id, num_rounds)

        QUERY = base_query + additional_query

        self.cursor.execute(
            QUERY,
            params,
        )

        results = self.cursor.fetchall()
        history = []
        for result in results:
            app_id, _, session_id, question, answer, metadata, timestamp = result
            metadata = self._deserialize_json(metadata=metadata)
            # Return list of dict if display_format is True
            if display_format:
                history.append(