CortexDB

CortexDB is a pure-Go, single-file AI memory and knowledge graph library. It uses SQLite as the storage kernel and exposes vector search, lexical search, RAG knowledge storage, agent memory workflows, RDF/SPARQL/RDFS/SHACL knowledge graph features, corpus-to-graph workflows, and MCP-aligned tool APIs.
It is designed for local-first AI agents that need durable memory without running a separate vector database, graph database, or MCP service stack.
Architecture
pkg/cortexdb
Core public DB facade: vectors, text search, knowledge, memory, KnowledgeMemory, KG, tools, MCP.
pkg/memoryflow
Agent memory workflow: transcript ingest, recall, wake-up context, diary, promotion.
pkg/graphflow
Corpus-to-graph workflow: extraction schema, build, analyze, report, export, HTML.
pkg/importflow
External structured-data import (CSV / SQL dumps / live Postgres-MySQL) into RAG + knowledge-graph foundations, AI-assisted mapping optional.
pkg/connector
Privacy gate over importflow: live Postgres/MySQL sources, PII classification + human-signed desensitization (masking, free-text redaction, reversible AES-256-GCM vault), and near-real-time CDC (polling / Postgres logical replication / MySQL binlog).
pkg/graph
Low-level graph engine: property graph, RDF triples/quads, SPARQL, RDFS, SHACL.
pkg/core
SQLite storage, embeddings, FTS5, vector indexes, chat/session primitives.
Use pkg/cortexdb first. Reach for pkg/memoryflow when building agent memory UX, pkg/graphflow when building graph extraction/report pipelines, and pkg/graph only when you need low-level RDF or property graph control.
Install
go get github.com/liliang-cn/cortexdb/v2
Quick Start
package main
import (
"context"
"fmt"
"log"
"github.com/liliang-cn/cortexdb/v2/pkg/cortexdb"
)
func main() {
db, err := cortexdb.Open(cortexdb.DefaultConfig("KnowledgeMemory.db"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
ctx := context.Background()
quick := db.Quick()
_, _ = quick.Add(ctx, []float32{0.1, 0.2, 0.9}, "SQLite is a single-file database.")
results, _ := quick.Search(ctx, []float32{0.1, 0.2, 0.8}, 1)
if len(results) > 0 {
fmt.Println(results[0].Content)
}
}
Choose the Right Layer
Need vectors / collections / FTS5? -> pkg/cortexdb / pkg/core
Need RAG knowledge storage/search? -> pkg/cortexdb SaveKnowledge/SearchKnowledge
Need chat/session memory workflow? -> pkg/memoryflow
Need RDF/SPARQL/RDFS/SHACL? -> pkg/cortexdb knowledge graph APIs
Need corpus-to-graph/report/export? -> pkg/graphflow
Need agent tools or MCP server? -> db.GraphRAGTools() / db.NewMCPServer()
Need low-level graph control? -> pkg/graph
High-Level Knowledge and Memory
_, _ = db.SaveKnowledge(ctx, cortexdb.KnowledgeSaveRequest{
KnowledgeID: "apollo-plan",
Title: "Apollo launch plan",
Content: "Alice owns Apollo. Apollo ships on Friday.",
ChunkSize: 24,
Entities: []cortexdb.ToolEntityInput{
{Name: "Alice", Type: "person", ChunkIDs: []string{"chunk:apollo-plan:000"}},
{Name: "Apollo", Type: "project", ChunkIDs: []string{"chunk:apollo-plan:000"}},
},
Relations: []cortexdb.ToolRelationInput{
{From: "Alice", To: "Apollo", Type: "owns"},
},
})
resp, _ := db.SearchKnowledge(ctx, cortexdb.KnowledgeSearchRequest{
Query: "Who owns Apollo?",
Keywords: []string{"Apollo", "Alice", "owns"},
RetrievalMode: cortexdb.RetrievalModeLexical,
TopK: 3,
})
_ = resp.Context
Without an embedder, CortexDB uses lexical retrieval and planner-provided keywords. With an embedder, the same high-level APIs can use semantic or hybrid retrieval.
RAG benchmark coverage is available in pkg/cortexdb:
go test ./pkg/cortexdb -run '^$' -bench 'BenchmarkRAG' -benchmem
Reference run on Apple M2 Pro, -benchtime=3x: