Introducing Memoripy: A Python Library Bringing Real Memory Functionality to AI Applications

2024-11-18

Artificial intelligence systems often face challenges in maintaining meaningful context during extended interactions, which is a significant hurdle for applications like chatbots and virtual assistants where conversational coherence is essential. Most traditional AI models operate in a stateless manner, concentrating solely on the current input and neglecting the continuity of previous interactions. This deficiency in effective memory leads to fragmented and inconsistent exchanges, impeding the creation of truly engaging AI systems that are sensitive to context.

To address this issue, we introduce Memoripy—a Python library that endows AI applications with authentic memory capabilities. Memoripy tackles the problem of sustaining conversational context by providing structured memory storage for AI systems, enabling them to efficiently store, recall, and build upon past interactions. It offers both short-term and long-term memory storage functionalities, allowing AI systems to retain the context of recent interactions while preserving important information over extended periods.

Memoripy constructs memory in a manner akin to human cognition, prioritizing recent events and retaining key information to ensure that interactions remain relevant and coherent over time. It organizes memory into short-term and long-term clusters, facilitating the prioritization of recent interactions for retrieval while maintaining important historical interactions for future reference. This approach prevents the AI from being overwhelmed by excessive data and ensures the accessibility of relevant information.

Additionally, Memoripy implements semantic clustering, grouping similar memories to enhance effective context retrieval. This feature enables AI systems to swiftly identify and associate related memories, thereby improving response quality. It also incorporates mechanisms for memory decay and reinforcement, where less important memories gradually fade while frequently accessed memories are strengthened, mirroring the principles of human memory.

Memoripy is designed with a strong emphasis on local storage, allowing developers to handle memory operations entirely within local infrastructure. This not only mitigates privacy concerns but also offers flexibility for integration with locally hosted language models and external services such as OpenAI and Ollama.

The following is an example of integrating Memoripy into an AI application:

from memoripy import MemoryManager, JSONStorage

def main():
    # Replace 'your-api-key' with your actual OpenAI API key
    api_key = "your-key"
    if not api_key:
        raise ValueError("Please set your OpenAI API key.")

    # Define chat and embedding models
    chat_model = "openai"  # Choose 'openai' or 'ollama' for chat
    chat_model_name = "gpt-4o-mini"  # Specific chat model name
    embedding_model = "ollama"  # Choose 'openai' or 'ollama' for embeddings
    embedding_model_name = "mxbai-embed-large"  # Specific embedding model name

    # Choose your storage option
    storage_option = JSONStorage("interaction_history.json")

    # Initialize the MemoryManager with the selected models and storage
    memory_manager = MemoryManager(
        api_key=api_key,
        chat_model=chat_model,
        chat_model_name=chat_model_name,
        embedding_model=embedding_model,
        embedding_model_name=embedding_model_name,
        storage=storage_option
    )

    # New user prompt
    new_prompt = "My name is Khazar"

    # Load the last 5 interactions from history (for context)
    short_term, _ = memory_manager.load_history()
    last_interactions = short_term[-5:] if len(short_term) >= 5 else short_term

    # Retrieve relevant past interactions, excluding the last 5
    relevant_interactions = memory_manager.retrieve_relevant_interactions(new_prompt, exclude_last_n=5)

    # Generate a response using the last interactions and retrieved interactions
    response = memory_manager.generate_response(new_prompt, last_interactions, relevant_interactions)

    # Display the response
    print(f"Generated response:\n{response}")

    # Extract concepts for the new interaction
    combined_text = f"{new_prompt} {response}"
    concepts = memory_manager.extract_concepts(combined_text)

    # Store this new interaction along with its embedding and concepts
    new_embedding = memory_manager.get_embedding(combined_text)
    memory_manager.add_interaction(new_prompt, response, new_embedding, concepts)

if __name__ == "__main__":
    main()

In this script, the MemoryManager is initialized with specified chat and embedding models, as well as storage options. When handling a new user prompt, the system retrieves relevant past interactions to generate a contextually appropriate response. The interaction, along with its embedding and extracted concepts, is then stored for future reference.

Memoripy represents a significant advancement in building more context-aware AI systems. By retaining and recalling relevant information, it enables the development of virtual assistants, conversational agents, and customer service systems that offer more consistent and personalized interactions. For example, a virtual assistant using Memoripy can remember user preferences or details from previous requests, providing more tailored responses. Preliminary evaluations indicate that AI systems incorporating Memoripy achieve higher user satisfaction by generating more coherent and contextually appropriate responses.

Furthermore, Memoripy's focus on local storage is crucial for privacy-sensitive applications, as it allows data to be handled securely without relying on external servers.

In summary, Memoripy enhances AI systems' ability to maintain context and coherence by providing authentic memory functions, marking an important step toward more sophisticated AI interactions. By constructing memory in a manner closely aligned with human cognitive processes, it paves the way for AI systems to adapt based on accumulated user interactions and deliver more personalized, context-aware experiences. This library equips developers with the necessary tools to enable AI not only to process inputs but also to learn meaningfully from interactions.