The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
[Bugfix] fix chat history management when app.reset (#1161)
Deven Patelf582c1fe25da
diff --git a/docs/api-reference/pipeline/delete.mdx b/docs/api-reference/pipeline/delete.mdxindex 2c65e5f45..96d79554f 100644--- a/docs/api-reference/pipeline/delete.mdx+++ b/docs/api-reference/pipeline/delete.mdx@@ -2,7 +2,7 @@title: 🗑 delete----`delete_chat_history()` method allows you to delete all previous messages in a chat history.+`delete_session_chat_history()` method allows you to delete all previous messages in a chat history.## Usage@@ -15,5 +15,5 @@ app.add("https://www.forbes.com/profile/elon-musk")app.chat("What is the net worth of Elon Musk?")-app.delete_chat_history()+app.delete_session_chat_history()```\ No newline at end of filediff --git a/embedchain/embedchain.py b/embedchain/embedchain.pyindex 4d8e49768..fbd1a5a12 100644--- a/embedchain/embedchain.py+++ b/embedchain/embedchain.py@@ -663,13 +663,17 @@ class EmbedChain(JSONSerializable):self.db.reset()self.cursor.execute("DELETE FROM data_sources WHERE pipeline_id = ?", (self.config.id,))self.connection.commit()- self.delete_chat_history()+ self.delete_all_chat_history(app_id=self.config.id)# Send anonymous telemetryself.telemetry.capture(event_name="reset", properties=self._telemetry_props)def get_history(self, num_rounds: int = 10, display_format: bool = True):return self.llm.memory.get(app_id=self.config.id, num_rounds=num_rounds, display_format=display_format)- def delete_chat_history(self, session_id: str = "default"):+ def delete_session_chat_history(self, session_id: str = "default"):self.llm.memory.delete(app_id=self.config.id, session_id=session_id)self.llm.update_history(app_id=self.config.id)++ def delete_all_chat_history(self, app_id: str):+ self.llm.memory.delete(app_id=app_id)+ self.llm.update_history(app_id=app_id)diff --git a/embedchain/memory/base.py b/embedchain/memory/base.pyindex c453a3514..9bfa04f2d 100644--- a/embedchain/memory/base.py+++ b/embedchain/memory/base.py@@ -53,7 +53,7 @@ class ChatHistory:logging.info(f"Added chat memory to db with id: {memory_id}")return memory_id- def delete(self, app_id: str, session_id: str):+ def delete(self, app_id: str, session_id: Optional[str] = None):"""Delete all chat history for a given app_id and session_id.This is useful for deleting chat history for a given user.@@ -63,8 +63,14 @@ class ChatHistory::return: None"""- DELETE_CHAT_HISTORY_QUERY = "DELETE FROM ec_chat_history WHERE app_id=? AND session_id=?"- self.cursor.execute(DELETE_CHAT_HISTORY_QUERY, (app_id, session_id))+ if session_id:+ DELETE_CHAT_HISTORY_QUERY = "DELETE FROM ec_chat_history WHERE app_id=? AND session_id=?"+ params = (app_id, session_id)+ 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, num_rounds=10, display_format=False) -> list[ChatMessage]:@@ -99,7 +105,7 @@ class ChatHistory:history.append(memory)return history- def count(self, app_id: str, session_id: str):+ def count(self, app_id: str, session_id: Optional[str] = None):"""Count the number of chat messages for a given app_id and session_id.@@ -108,8 +114,14 @@ class ChatHistory::return: The number of chat messages for a given app_id and session_id"""- QUERY = "SELECT COUNT(*) FROM ec_chat_history WHERE app_id=? AND session_id=?"- self.cursor.execute(QUERY, (app_id, session_id))+ if session_id:+ QUERY = "SELECT COUNT(*) FROM ec_chat_history WHERE app_id=? AND session_id=?"+ params = (app_id, session_id)+ else:+ QUERY = "SELECT COUNT(*) FROM ec_chat_history WHERE app_id=?"+ params = (app_id,)++ self.cursor.execute(QUERY, params)count = self.cursor.fetchone()[0]return countdiff --git a/pyproject.toml b/pyproject.tomlindex fc1305a4f..28c0979a1 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "embedchain"-version = "0.1.61"+version = "0.1.62"description = "Data platform for LLMs - Load, index, retrieve and sync any unstructured data"authors = ["Taranjeet Singh <[email protected]>",diff --git a/tests/memory/test_chat_memory.py b/tests/memory/test_chat_memory.pyindex d41bfbd1e..bcf660126 100644--- a/tests/memory/test_chat_memory.py+++ b/tests/memory/test_chat_memory.py@@ -59,9 +59,26 @@ def test_delete_chat_history(chat_memory_instance):chat_memory_instance.add(app_id, session_id, chat_message)+ session_id_2 = "test_session_2"++ for i in range(1, 6):+ human_message = f"Question {i}"+ ai_message = f"Answer {i}"++ chat_message = ChatMessage()+ chat_message.add_user_message(human_message)+ chat_message.add_ai_message(ai_message)++ chat_memory_instance.add(app_id, session_id_2, chat_message)+chat_memory_instance.delete(app_id, session_id)assert chat_memory_instance.count(app_id, session_id) == 0+ assert chat_memory_instance.count(app_id) == 5++ chat_memory_instance.delete(app_id)++ assert chat_memory_instance.count(app_id) == 0@pytest.fixture