npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

langchain-memory-util

v1.0.0

Published

A reusable LangChain memory utility with support for local file and S3 storage, session-based memory management

Readme

LangChain Memory Utility

A reusable LangChain memory utility with support for local file and S3 storage, session-based memory management, and automatic conversation history tracking.

Features

  • Flexible Storage: Store memory in local files or AWS S3
  • Session-based Memory: Maintain separate memory for each session
  • LangChain Integration: Compatible with LangChain's memory interface
  • Automatic History: Automatically store and retrieve conversation history
  • Easy to Use: Simple API for creating and managing memory

Installation

npm install langchain-memory-util

Quick Start

Local File Storage

const { MemoryStore, SessionMemory } = require('langchain-memory-util');

// Create a local file-based memory store
const store = new MemoryStore({ 
  type: 'local', 
  basePath: './memory' 
});

// Create session memory
const sessionId = 'user123';
const memory = new SessionMemory({ sessionId, store });

// Save conversation context
await memory.saveContext('Hello!', 'Hi there!');
await memory.saveContext('How are you?', 'I am doing well, thank you!');

// Retrieve history
const history = await memory.getHistory();
console.log(history);

S3 Storage

const { MemoryStore, SessionMemory } = require('langchain-memory-util');

// Create an S3-based memory store
const store = new MemoryStore({
  type: 's3',
  bucket: 'my-memory-bucket',
  s3Config: {
    region: 'us-east-1',
    credentials: {
      accessKeyId: 'YOUR_ACCESS_KEY',
      secretAccessKey: 'YOUR_SECRET_KEY'
    }
  }
});

const memory = new SessionMemory({ sessionId: 'user123', store });

With LangChain LLM

const { MemoryStore, SessionMemory } = require('langchain-memory-util');
const { ChatOpenAI } = require('@langchain/openai');
const { ConversationChain } = require('langchain/chains');

// Setup memory
const store = new MemoryStore({ type: 'local', basePath: './memory' });
const memory = new SessionMemory({ sessionId: 'chat1', store });

// Create LLM with memory
const llm = new ChatOpenAI({
  openAIApiKey: process.env.OPENAI_API_KEY,
  temperature: 0.7
});

const chain = new ConversationChain({ llm, memory });

// Conversations are automatically stored
const response = await chain.call({ input: 'Hello, how are you?' });
console.log(response.response);

// Retrieve full conversation history
const history = await memory.getHistory();
console.log('Full conversation:', history);

API Reference

MemoryStore

Constructor

new MemoryStore(config)

Config Options:

  • type (string): 'local' or 's3' (default: 'local')
  • basePath (string): Directory path for local storage (default: './memory')
  • bucket (string): S3 bucket name (required for S3 type)
  • s3Config (object): AWS S3 configuration object (required for S3 type)

Methods

  • save(sessionId, data): Save data for a session
  • load(sessionId): Load data for a session

SessionMemory

Constructor

new SessionMemory({ sessionId, store })

Parameters:

  • sessionId (string): Unique identifier for the session
  • store (MemoryStore): Memory store instance

Methods

  • saveContext(input, output): Save a conversation turn
  • loadMemoryVariables(): Load memory variables (LangChain compatible)
  • getHistory(): Retrieve full conversation history
  • init(): Initialize memory (called automatically)

Examples

Run the included examples:

# Local storage example
npm test

# LLM integration example
npm run example

License

MIT