[Improvement] Add support for reloading history for an existing app (#930)

This commit is contained in:
Deshraj Yadav
2023-11-09 15:17:51 -08:00
committed by GitHub
parent 654fd8d74c
commit 17129e2eaa
5 changed files with 25 additions and 13 deletions

View File

@@ -62,7 +62,7 @@ class ECChatMemory:
)
self.connection.commit()
def get_recent_memories(self, app_id, num_rounds=10) -> List[ChatMessage]:
def get_recent_memories(self, app_id, num_rounds=10, display_format=False) -> List[ChatMessage]:
"""
Get the most recent num_rounds rounds of conversations
between human and AI, for a given app_id.
@@ -82,12 +82,16 @@ class ECChatMemory:
results = self.cursor.fetchall()
history = []
for result in results:
app_id, id, question, answer, metadata, timestamp = result
app_id, _, question, answer, metadata, timestamp = result
metadata = self._deserialize_json(metadata=metadata)
memory = ChatMessage()
memory.add_user_message(question, metadata=metadata)
memory.add_ai_message(answer, metadata=metadata)
history.append(memory)
# Return list of dict if display_format is True
if display_format:
history.append({"human": question, "ai": answer, "metadata": metadata, "timestamp": timestamp})
else:
memory = ChatMessage()
memory.add_user_message(question, metadata=metadata)
memory.add_ai_message(answer, metadata=metadata)
history.append(memory)
return history
def _serialize_json(self, metadata: Dict[str, Any]):

View File

@@ -69,4 +69,4 @@ class ChatMessage(JSONSerializable):
self.ai_message = BaseMessage(content=message, creator="ai", metadata=metadata)
def __str__(self) -> str:
return f"{self.human_message} | {self.ai_message}"
return f"{self.human_message}\n{self.ai_message}"