Can I use drizzle, pglite with mem0? #3776
Replies: 3 comments
-
|
Using Drizzle + PGlite with Mem0 is possible with a custom vector store. Here's how: ArchitectureImplementation// schema.ts - Drizzle schema for memories
import { pgTable, text, vector, timestamp, jsonb } from 'drizzle-orm/pg-core';
export const memories = pgTable('memories', {
id: text('id').primaryKey(),
userId: text('user_id').notNull(),
content: text('content').notNull(),
embedding: vector('embedding', { dimensions: 1536 }),
metadata: jsonb('metadata'),
createdAt: timestamp('created_at').defaultNow(),
});
// Create index for vector search
// CREATE INDEX ON memories USING ivfflat (embedding vector_cosine_ops);// pglite-store.ts
import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
import { memories } from './schema';
import { cosineDistance, desc, eq, sql } from 'drizzle-orm';
export class PGliteVectorStore {
private db;
constructor() {
const client = new PGlite();
// Enable pgvector extension
client.exec('CREATE EXTENSION IF NOT EXISTS vector');
this.db = drizzle(client);
}
async add(content: string, embedding: number[], userId: string, metadata?: any) {
const id = crypto.randomUUID();
await this.db.insert(memories).values({
id,
userId,
content,
embedding,
metadata
});
return id;
}
async search(queryEmbedding: number[], userId: string, limit = 10) {
const similarity = sql<number>`1 - (${cosineDistance(memories.embedding, queryEmbedding)})`;
return await this.db
.select({
id: memories.id,
content: memories.content,
metadata: memories.metadata,
score: similarity
})
.from(memories)
.where(eq(memories.userId, userId))
.orderBy(desc(similarity))
.limit(limit);
}
}Integration with Mem0# Python side - custom vector store config
from mem0 import Memory
config = {
"vector_store": {
"provider": "custom",
"config": {
"url": "http://localhost:3000/api/vectors", # Your PGlite API
}
}
}
m = Memory.from_config(config)Benefits of PGlite
More on local-first patterns: https://github.com/KeepALifeUS/autonomous-agents |
Beta Was this translation helpful? Give feedback.
-
|
PGlite + Mem0 in Electron is a great combo for offline-first apps! The challenge: Mem0 pgvector provider expects a network connection, but PGlite is embedded. Solution: Custom vector store adapter import { BaseVectorStore } from "mem0ai/oss";
import { pglite, db } from "./your-pglite-setup";
class PGliteVectorStore extends BaseVectorStore {
async addVectors(vectors: number[][], texts: string[], metadata: any[]) {
for (let i = 0; i < vectors.length; i++) {
await db.insert(memoriesTable).values({
embedding: vectors[i],
content: texts[i],
metadata: metadata[i],
});
}
}
async similaritySearch(query: number[], k: number) {
const result = await pglite.query(`
SELECT content, metadata,
embedding <=> $1::vector AS distance
FROM memories
ORDER BY distance
LIMIT $2
`, [JSON.stringify(query), k]);
return result.rows;
}
}Then use it: const memory = new Memory({
vectorStore: {
provider: "custom",
instance: new PGliteVectorStore()
},
// ... rest of config
});For Drizzle schema: export const memories = pgTable("memories", {
id: serial("id").primaryKey(),
content: text("content"),
embedding: vector("embedding", { dimensions: 1536 }),
metadata: jsonb("metadata"),
});We build offline-capable AI apps at Revolution AI — PGlite is perfect for desktop deployments! |
Beta Was this translation helpful? Give feedback.
-
|
PGlite with mem0 in Electron is tricky since mem0 expects a real Postgres connection string. The challenge: Options: 1. Use mem0 with Chroma/local vector store instead const memory = new Memory({
vectorStore: {
provider: "chroma",
config: {
collectionName: "memories",
path: join(app.getPath("userData"), "chroma")
}
},
// ...
});2. Custom adapter wrapping PGlite // Create a mem0-compatible wrapper
class PGliteVectorStore {
constructor(private pglite: PGlite) {}
async add(vectors, payloads) {
await this.pglite.query(
"INSERT INTO memories (embedding, payload) VALUES ..."
);
}
async search(queryVector, limit) {
return this.pglite.query(
"SELECT * FROM memories ORDER BY embedding <=> $1 LIMIT $2",
[queryVector, limit]
);
}
}3. Run embedded Postgres via PGlite HTTP server import { PGlite } from "@electric-sql/pglite";
import { PGliteServer } from "@electric-sql/pglite/server";
const server = new PGliteServer(pglite, { port: 5432 });
await server.start();
// Now mem0 can connect to localhost:5432Recommended for Electron: We build desktop AI apps at Revolution AI — Chroma works great for offline-first Electron apps. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I'm working an Electron desktop app, and the database system uses drizzle and pglite
The following code is Drizzle configuration:
The follows code is Pglite configuration:
I'm wondering how to connect DB to mem0
Beta Was this translation helpful? Give feedback.
All reactions