Back to Mem0

mem0-ts

mem0-ts/src/oss/README.md

2.0.125.0 KB
Original Source

mem0-ts

A TypeScript implementation of the mem0 memory system, using OpenAI for embeddings and completions.

Features

  • Memory storage and retrieval using vector embeddings
  • Fact extraction from text using GPT-4
  • SQLite-based history tracking
  • Optional graph-based memory relationships
  • TypeScript type safety
  • Built-in OpenAI integration with default configuration
  • In-memory vector store implementation
  • Extensible architecture with interfaces for custom implementations

Installation

  1. Clone the repository:
bash
git clone <repository-url>
cd mem0-ts
  1. Install dependencies:
bash
npm install
  1. Set up environment variables:
bash
cp .env.example .env
# Edit .env with your OpenAI API key
  1. Build the project:
bash
npm run build

Usage

Basic Example

typescript
import { Memory } from "mem0-ts";

// Create a memory instance with default OpenAI configuration
const memory = new Memory();

// Or with minimal configuration (only API key)
const memory = new Memory({
  embedder: {
    config: {
      apiKey: process.env.OPENAI_API_KEY,
    },
  },
  llm: {
    config: {
      apiKey: process.env.OPENAI_API_KEY,
    },
  },
});

// Or with custom configuration
const memory = new Memory({
  embedder: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY,
      model: "text-embedding-3-small",
    },
  },
  vectorStore: {
    provider: "memory",
    config: {
      collectionName: "custom-memories",
    },
  },
  llm: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY,
      model: "gpt-4-turbo-preview",
    },
  },
});

// Add a memory
await memory.add("The sky is blue", "user123");

// Search memories
const results = await memory.search("What color is the sky?", "user123");

Default Configuration

The memory system comes with sensible defaults:

  • OpenAI embeddings with text-embedding-3-small model
  • In-memory vector store
  • OpenAI GPT-4 Turbo for LLM operations
  • SQLite for history tracking

You only need to provide API keys - all other settings are optional.

Methods

  • add(messages: string | Message[], userId?: string, ...): Promise<SearchResult>
    • Options include metadata, infer, and expirationDate (a YYYY-MM-DD date after which the memory is treated as expired).
  • search(query: string, userId?: string, ...): Promise<SearchResult>
    • Expired memories are omitted unless you pass showExpired: true.
  • get(memoryId: string): Promise<MemoryItem | null>
    • Fetching by ID returns the memory even if it has expired.
  • getAll(options): Promise<SearchResult>
    • Expired memories are omitted unless you pass showExpired: true.
  • update(memoryId: string, config: string | UpdateMemoryOptions): Promise<{ message: string }>
    • UpdateMemoryOptions is { text?, data?, metadata?, expirationDate? }. At least one must be provided; omitted fields are left untouched, and expirationDate: null clears an existing expiry. data is a deprecated alias for text.
    • Passing a bare string is shorthand for { text }, so update(memoryId, "new text") still works.
  • delete(memoryId: string): Promise<{ message: string }>
  • deleteAll(userId?: string, ...): Promise<{ message: string }>
  • history(memoryId: string): Promise<any[]>
  • reset(): Promise<void>
typescript
// Replace the content
await memory.update(memoryId, { text: "Alex now prefers decaf coffee" });

// Update metadata only, leaving the stored text untouched
await memory.update(memoryId, { metadata: { category: "preferences" } });

// Expire the memory on a given day, or clear an existing expiry
await memory.update(memoryId, { expirationDate: "2030-01-31" });
await memory.update(memoryId, { expirationDate: null });

// Include expired memories in reads
await memory.getAll({ filters: { user_id: "alice" }, showExpired: true });
await memory.search("coffee", {
  filters: { user_id: "alice" },
  showExpired: true,
});

Try the Example

We provide a comprehensive example in examples/basic.ts that demonstrates all the features including:

  • Default configuration usage
  • In-memory vector store
  • PGVector store (with PostgreSQL)
  • Qdrant vector store
  • Redis vector store
  • Memory operations (add, search, update, delete)

To run the example:

bash
npm run example

You can use this example as a template and modify it according to your needs. The example includes:

  • Different vector store configurations
  • Various memory operations
  • Error handling
  • Environment variable usage

Development

  1. Build the project:
bash
npm run build
  1. Clean build files:
bash
npm run clean

Extending

The system is designed to be extensible. You can implement your own:

  • Embedders by implementing the Embedder interface
  • Vector stores by implementing the VectorStore interface
  • Language models by implementing the LLM interface

License

MIT

Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a new Pull Request