[Bugfix] fix chat history management when app.reset (#1161)

Co-authored-by: Deven Patel <deven298@yahoo.com>
This commit is contained in:
Deven Patel
2024-01-12 11:10:25 +05:30
committed by GitHub
parent 785929c502
commit f582c1fe25
5 changed files with 44 additions and 11 deletions

View File

@@ -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 telemetry
self.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)

View File

@@ -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 count