🔍 Code Extractor

class SimpleChatMemory

Maturity: 45

A simple chat memory manager that stores and retrieves conversation history between users and assistants with configurable history limits.

File:
/tf/active/vicechatdev/OneCo_hybrid_RAG copy.py
Lines:
685 - 716
Complexity:
simple

Purpose

SimpleChatMemory provides a lightweight mechanism for maintaining conversational context in chatbot applications. It stores user and assistant message pairs, automatically manages history size by trimming old messages when limits are exceeded, and formats the conversation history for injection into prompts. This is useful for maintaining context in multi-turn conversations with language models while controlling memory usage.

Source Code

class SimpleChatMemory:
    """
    A bare-bones memory mechanism that stores a list of 
    user and assistant messages.
    """
    def __init__(self, max_history: int = 3):
        self.messages = []
        self.max_history = max_history

    def save_context(self, user_msg: Dict[str, str], assistant_msg: Dict[str, str]):
        """
        Expects something like: 
        user_msg = {'role': 'user', 'content': 'Hello'}
        assistant_msg = {'role': 'assistant', 'content': 'Hi!'}
        """
        self.messages.append(user_msg)
        self.messages.append(assistant_msg)
        # Trim messages if we exceed max_history*2
        if len(self.messages) > self.max_history * 2:
            self.messages = self.messages[-(self.max_history):]

    def get_formatted_history(self) -> str:
        """
        Return the last N pairs of messages as a formatted string 
        that can be injected into a prompt.
        """
        lines = []
        for msg in self.messages[-(self.max_history * 2):]:
            role = msg['role']
            content = msg['content']
            lines.append(f"{role.title()}: {content}")
        return "\n".join(lines)

Parameters

Name Type Default Kind
bases - -

Parameter Details

max_history: Maximum number of message pairs (user + assistant) to retain in memory. Default is 3, meaning up to 3 user messages and 3 assistant responses (6 total messages) are kept. When exceeded, older messages are automatically removed to maintain this limit.

Return Value

Instantiation returns a SimpleChatMemory object. The get_formatted_history() method returns a string containing formatted conversation history with each message on a new line in the format 'Role: content'. The save_context() method returns None and modifies internal state.

Class Interface

Methods

__init__(self, max_history: int = 3)

Purpose: Initialize the SimpleChatMemory instance with an empty message list and configurable history limit

Parameters:

  • max_history: Maximum number of message pairs to retain (default: 3)

Returns: None (constructor)

save_context(self, user_msg: Dict[str, str], assistant_msg: Dict[str, str])

Purpose: Store a user-assistant message pair and automatically trim history if it exceeds the maximum limit

Parameters:

  • user_msg: Dictionary with 'role' key set to 'user' and 'content' key containing the user's message text
  • assistant_msg: Dictionary with 'role' key set to 'assistant' and 'content' key containing the assistant's response text

Returns: None (modifies internal messages list)

get_formatted_history(self) -> str

Purpose: Retrieve the conversation history as a formatted string suitable for prompt injection

Returns: A newline-separated string where each line contains a message in the format 'Role: content' (e.g., 'User: Hello' or 'Assistant: Hi there')

Attributes

Name Type Description Scope
messages List[Dict[str, str]] List storing all conversation messages as dictionaries with 'role' and 'content' keys, maintained in chronological order instance
max_history int Maximum number of message pairs (user + assistant) to retain in memory before trimming occurs instance

Dependencies

  • typing

Required Imports

from typing import Dict

Usage Example

# Create a memory instance with max 3 message pairs
memory = SimpleChatMemory(max_history=3)

# Save a conversation turn
user_msg = {'role': 'user', 'content': 'What is Python?'}
assistant_msg = {'role': 'assistant', 'content': 'Python is a programming language.'}
memory.save_context(user_msg, assistant_msg)

# Save another turn
user_msg2 = {'role': 'user', 'content': 'Is it easy to learn?'}
assistant_msg2 = {'role': 'assistant', 'content': 'Yes, Python is beginner-friendly.'}
memory.save_context(user_msg2, assistant_msg2)

# Get formatted history for prompt injection
history = memory.get_formatted_history()
print(history)
# Output:
# User: What is Python?
# Assistant: Python is a programming language.
# User: Is it easy to learn?
# Assistant: Yes, Python is beginner-friendly.

Best Practices

  • Always pass messages as dictionaries with 'role' and 'content' keys to ensure proper formatting
  • Set max_history based on your token budget and context window size of the LLM you're using
  • Call save_context() after each complete conversation turn (user message + assistant response)
  • Use get_formatted_history() to retrieve conversation context before making LLM calls
  • Be aware that the trimming logic in save_context() has a bug: it should use `self.messages[-(self.max_history * 2):]` instead of `self.messages[-(self.max_history):]` to properly maintain max_history pairs
  • The class maintains mutable state (messages list), so create separate instances for different conversation threads
  • Messages are stored in chronological order, with oldest messages trimmed first when limit is exceeded

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ChatSession 60.8% similar

    A class that manages a chat session associated with a specific document section, tracking messages, context documents, references, and timestamps.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function conversation_example 60.6% similar

    Demonstrates a multi-turn conversational RAG system with chat history management, showing how follow-up questions are automatically optimized based on conversation context.

    From: /tf/active/vicechatdev/docchat/example_usage.py
  • function api_clear_memory 58.7% similar

    Flask API endpoint that clears the chat memory from the server-side chat engine, removing all stored conversation messages.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function add_message_to_session 57.2% similar

    Adds a message to a chat session with thread-safe locking, storing role, content, timestamp, and optional metadata/references, then persists the session to disk.

    From: /tf/active/vicechatdev/docchat/app.py
  • class ChatSession_v1 55.9% similar

    A dataclass representing a chat session associated with a specific text section in a document, managing conversation messages, context, and references.

    From: /tf/active/vicechatdev/vice_ai/models.py
← Back to Browse