Performance: Chat history management
This concept affects the responsiveness and memory usage of chat applications by managing how conversation data is stored and retrieved.
from collections import deque class ChatMemory: def __init__(self, max_messages=50): self.history = deque(maxlen=max_messages) def add_message(self, message): self.history.append(message) def get_history(self): return list(self.history)
class ChatMemory: def __init__(self): self.history = [] def add_message(self, message): self.history.append(message) def get_history(self): return self.history
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unlimited chat history in memory | High (many nodes if rendered) | Multiple reflows as history grows | High paint cost due to large DOM | [X] Bad |
| Limited chat history with deque | Low (fixed number of nodes) | Single reflow on update | Low paint cost with small DOM | [OK] Good |
ConversationBufferMemory in Langchain chat applications?ConversationBufferMemory instance in Langchain?ConversationBufferMemory() follows this pattern; others are invalid or do not exist.print(memory.load_memory_variables({})) output?
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message('Hello')
memory.chat_memory.add_ai_message('Hi there!')from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.add_user_message('Hello')add_user_message is not directly on ConversationBufferMemory but on its chat_memory attribute.memory.chat_memory.add_user_message(), not memory.add_user_message().ConversationBufferMemory is best to achieve this?memory.chat_memory.messages after each addition (e.g., memory.chat_memory.messages = memory.chat_memory.messages[-3:]) keeps only the last 3.Pass the test and this pattern moves unknown