# Memoripy

> Short-term and long-term memory for AI apps, with decay and concept graphs

- Category: Agents & Automation / Agent Memory
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/caspianmoon/memoripy
- Canonical page: https://ai-tldr.dev/tools/memoripy/

## In short

Memoripy is a Python library that manages and retrieves context-aware memory for AI applications. It keeps interactions in both short-term and long-term storage, then surfaces the most relevant past ones when you handle a new prompt. It sits in the Agent Memory category of the Agents & Automation tools and is open-source, written in Python and licensed Apache-2.0.

## Overview

Memoripy is a Python library that manages and retrieves context-aware memory for AI applications. It keeps interactions in both short-term and long-term storage, then surfaces the most relevant past ones when you handle a new prompt.

It is aimed at developers building chat assistants or agents that need to remember earlier conversations. Memoripy works with OpenAI, Azure OpenAI, OpenRouter, and Ollama APIs for generating embeddings and extracting concepts.

As a memory framework, it goes beyond a plain history log: it models memory decay so older items fade, reinforces frequently used memories, and builds a concept graph to retrieve associated interactions by relevance.

## What it does

- Manages memory as short-term or long-term based on usage and relevance
- Retrieves relevant past interactions using embeddings, concepts, and cosine similarity
- Extracts concepts and generates embeddings via OpenAI or Ollama models
- Builds a concept graph and uses spreading activation for relevance-based retrieval
- Clusters similar memories into semantic groups with hierarchical clustering
- Decays unused memories over time while reinforcing those accessed often

## Getting started

Install Memoripy from PyPI, then wire a chat model, an embedding model, and a storage backend into a MemoryManager.

1. Install with pip

   Memoripy and its dependencies (openai, faiss-cpu, numpy, networkx, scikit-learn, langchain, ollama) are installed automatically.

```bash
pip install memoripy
```

2. Initialize the MemoryManager

   Create a MemoryManager with a chat model, an embedding model, and a storage option such as JSONStorage or InMemoryStorage.

```python
from memoripy import MemoryManager, JSONStorage
from memoripy.implemented_models import OpenAIChatModel, OllamaEmbeddingModel

api_key = "your-key"
storage_option = JSONStorage("interaction_history.json")

memory_manager = MemoryManager(
    OpenAIChatModel(api_key, "gpt-4o-mini"),
    OllamaEmbeddingModel("mxbai-embed-large"),
    storage=storage_option,
)
```

3. Retrieve memory and store the interaction

   Pull recent and relevant interactions to build context, generate a response, then add the new interaction with its embedding and concepts.

```python
new_prompt = "My name is Khazar"

short_term, _ = memory_manager.load_history()
last_interactions = short_term[-5:] if len(short_term) >= 5 else short_term
relevant = memory_manager.retrieve_relevant_interactions(new_prompt, exclude_last_n=5)

response = memory_manager.generate_response(new_prompt, last_interactions, relevant)

combined_text = f"{new_prompt} {response}"
concepts = memory_manager.extract_concepts(combined_text)
embedding = memory_manager.get_embedding(combined_text)
memory_manager.add_interaction(new_prompt, response, embedding, concepts)
```

## When to use it

- Give a chatbot recall of earlier turns so it stays consistent across a long conversation
- Let an AI assistant remember user facts and preferences between sessions via JSON storage
- Surface related past interactions by concept rather than just the most recent ones
- Run memory locally with Ollama embeddings when you want to avoid sending data to a hosted API
