LangMem

0.0.30 · active · verified Sun Apr 12

LangMem (version 0.0.30) is a Python library providing prebuilt utilities for memory management and retrieval, specifically designed for AI agents and LLM applications. It offers abstractions for integrating with various embedding models and vector stores, facilitating the creation of intelligent systems with long-term memory. The library is under active development with frequent minor updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `MemoryManager` with `OpenAIEmbeddings` and `Chroma` to add and recall memories. It assumes `openai` and `chromadb` are installed (e.g., via `pip install langmem[openai,chromadb]`) and `OPENAI_API_KEY` is configured in your environment. Note the use of `embedding_function` when initializing `Chroma`.

import os
from langmem import MemoryManager
from langmem.embeddings import OpenAIEmbeddings
from langmem.vectorstores import Chroma

# NOTE: For this example, you need to install 'openai' and 'chromadb'
# pip install langmem openai chromadb

# Ensure OPENAI_API_KEY is set in your environment variables.
# For testing, you can uncomment and set it directly:
# os.environ['OPENAI_API_KEY'] = 'YOUR_OPENAI_API_KEY'

openai_api_key = os.environ.get('OPENAI_API_KEY', '')
if not openai_api_key:
    print("Warning: OPENAI_API_KEY environment variable not set. OpenAIEmbeddings might fail.")

# Initialize embeddings and vector store
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
vectorstore = Chroma(embedding_function=embeddings) # Chroma requires embedding_function

# Initialize MemoryManager
memory_manager = MemoryManager(vectorstore=vectorstore)

# Add memories
memory_manager.add_memory("The quick brown fox jumps over the lazy dog.")
memory_manager.add_memory("The quick red bird flies high in the sky.")
memory_manager.add_memory("The lazy dog often naps under the oak tree.")

# Recall information
results = memory_manager.recall("What is the fox doing?")
print("\nRecall results for 'What is the fox doing?':", results)

results = memory_manager.recall("Where does the dog nap?")
print("\nRecall results for 'Where does the dog nap?':", results)

view raw JSON →