Mem0 JavaScript and TypeScript Integration

Use the Mem0 Node.js SDK from a JavaScript or TypeScript application with Oracle AI Vector Search as the memory vector store.

Contents

Requirements

Use Node.js 18 or later. The Mem0 Node.js SDK requires the mem0ai package.

The Oracle provider uses the node-oracledb driver to connect to Oracle AI Database. Use Oracle AI Database 23.4 or later, and use Oracle Client 23.4 or later when the driver runs in Thick mode.

Install the Packages

npm install mem0ai oracledb

Quick Start

Import Memory from mem0ai/oss, then add and search memories asynchronously.

import { Memory } from "mem0ai/oss";

const memory = new Memory();

const messages = [
  { role: "user", content: "I enjoy science-fiction movies." },
  {
    role: "assistant",
    content: "I will suggest science-fiction movies in the future.",
  },
];

await memory.add(messages, { userId: "alice" });

const results = await memory.search("movie recommendations", {
  filters: {
    user_id: "alice",
  },
});

console.log(results);

Use a userId, agentId, or another supported scope when you add and search memories. Scoping searches helps prevent one user’s memories from being returned to another user.

Configure Oracle AI Vector Search

Pass the Oracle vector-store configuration when you create Memory. The JavaScript SDK uses camelCase configuration names.

import { Memory } from "mem0ai/oss";

const memory = new Memory({
  embedder: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY ?? "",
      model: "text-embedding-3-small",
    },
  },
  vectorStore: {
    provider: "oracledb",
    config: {
      collectionName: "mem0",
      embeddingModelDims: 1536,
      connectionParams: {
        user: "mem0_user",
        password: process.env.DB_PASSWORD ?? "",
        connectString: process.env.DB_CONNECT_STRING ?? "localhost:1521/FREEPDB1",
      },
    },
  },
  llm: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY ?? "",
      model: "gpt-4-turbo-preview",
    },
  },
  historyDbPath: "memory.db",
});

The default configuration creates the mem0 collection and an HNSW vector index. Set the embedding dimension to match the configured embedding model.

Connect to Oracle AI Database

Pass an existing oracledb.Connection or oracledb.Pool as client when your application manages the connection or pool.

import oracledb from "oracledb";
import { Memory } from "mem0ai/oss";

const pool = await oracledb.createPool({
  user: "mem0_user",
  password: process.env.DB_PASSWORD,
  connectString: process.env.DB_CONNECT_STRING,
});

const memory = new Memory({
  vectorStore: {
    provider: "oracledb",
    config: {
      client: pool,
      collectionName: "mem0",
      embeddingModelDims: 1536,
    },
  },
});

When you provide client, Mem0 ignores connectionParams and useConnectionPool. Mem0 does not close a connection or pool that your application created.

Create HNSW and IVF Indexes

Set indexType to HNSW or IVF, and set indexParameters for the selected index type. Set doCreateIndex to false when you want exact search or manage the index yourself.

const memory = new Memory({
  vectorStore: {
    provider: "oracledb",
    config: {
      connectionParams: {
        user: "mem0_user",
        password: process.env.DB_PASSWORD,
        connectString: process.env.DB_CONNECT_STRING,
      },
      indexType: "HNSW",
      indexParameters: {
        neighbors: 32,
        efconstruction: 200,
      },
      indexAccuracy: 95,
    },
  },
});

Use neighbors and efconstruction for HNSW indexes. For IVF indexes, use neighborPartitions, samplesPerPartition, and minVectorsPerPartition.

Filter Memories by Metadata

Use metadata filters to limit memory retrieval to records that match application context. Mem0 combines fields in filters with AND.

const results = await memory.search("movie recommendations", {
  filters: {
    user_id: "alice",
    category: { in: ["movies", "books"] },
    rating: { gte: 4 },
  },
});

Supported filter types include scalar equality, field existence with "*", comparisons (eq, ne, gt, gte, lt, lte), membership (in, nin), string matching (contains, icontains), and logical groups (AND, OR, NOT). Filters run against the JSON payload column.

Interpret Search Scores

Oracle returns a distance from VECTOR_DISTANCE, which Mem0 converts to a score where higher values indicate greater similarity. Scores from COSINE and other non-negative metrics range from 0 through 1. DOT returns the inner product, so its scores can fall outside that range.

Review Configuration Options

Provide either connectionParams or an existing connection or pool as client.

Option Description Default
connectionParams Oracle connection settings, such as user, password, and connectString. Required unless client is provided. None
useConnectionPool Creates a connection pool from connectionParams. true
client Existing oracledb.Connection or oracledb.Pool. Required unless connectionParams is provided. None
collectionName Oracle table that stores vectors and JSON payloads. mem0
embeddingModelDims Dimension of embedding vectors. 1536
distanceMetric Index and search distance metric: COSINE, EUCLIDEAN, EUCLIDEAN_SQUARED, DOT, HAMMING, or MANHATTAN. COSINE
doCreateIndex Creates a vector index for the collection. true
indexType Vector index type: HNSW or IVF. HNSW
indexName Name of the vector index. <collectionName>_VEC_IDX
indexParameters Tuning parameters for the selected index type. None
indexAccuracy Target index accuracy from 1 through 100. None

Reference Links