Documentation
¶
Overview ¶
Package cortexdb provides a lightweight, embeddable vector database for Go AI projects.
cortexdb is a 100% pure Go library designed to be the storage kernel for RAG (Retrieval-Augmented Generation) systems. Built on SQLite using modernc.org/sqlite (NO CGO REQUIRED!), it provides vector storage, full-text search (FTS5), knowledge graphs, and chat memory management in a single database file.
Key Features ¶
- 🚀 RAG-Ready - Built-in support for Documents, Chat Sessions, and Messages.
- 🔍 Hybrid Search - Combine Vector Search (HNSW/IVF) with Keyword Search (FTS5) using RRF fusion.
- 🧠 Memory Efficient - Built-in SQ8 quantization reduces RAM usage by 75%.
- 🛡️ Secure - Row-Level Security via ACL fields and SQL-level push-down filtering.
- 🔧 100% Pure Go - Easy cross-compilation and zero-dependency deployment.
- 🕸️ GraphRAG - Advanced graph operations for complex relationship-based retrieval.
- 🔌 MCP Stdio Server - Expose CortexDB tools to external LLMs through the official Model Context Protocol Go SDK.
- 🧠 High-level Knowledge & Memory APIs - Simplified SaveKnowledge and SaveMemory operations for building agentic workflows.
- 🧭 Semantic Router - Intent routing with configurable similarity and thresholds.
- 🧠 Hindsight - Biomimetic agent memory with TEMPR retrieval.
Quick Start ¶
import (
"context"
"github.com/liliang-cn/cortexdb/v2/pkg/cortexdb"
)
func main() {
// 1. Open database with default configuration
config := cortexdb.DefaultConfig("vectors.db")
db, _ := cortexdb.Open(config)
defer db.Close()
// 2. Use the Quick interface for simple operations
ctx := context.Background()
quick := db.Quick()
// Add vector
quick.Add(ctx, []float32{0.1, 0.2, 0.3}, "Go is awesome")
// Search
results, _ := quick.Search(ctx, []float32{0.1, 0.2, 0.28}, 5)
}
RAG and Hybrid Search ¶
cortexdb provides high-level APIs for building RAG applications:
import "github.com/liliang-cn/cortexdb/v2/pkg/core"
// Perform hybrid search (Vector + Full-Text Search)
results, err := db.Vector().HybridSearch(ctx, queryVec, "search term", core.HybridSearchOptions{
TopK: 5,
})
Knowledge and Memory ¶
Build agentic systems with higher-level persistence:
// Save a document and its knowledge artifacts (chunks, entities)
db.SaveKnowledge(ctx, cortexdb.KnowledgeSaveRequest{
KnowledgeID: "doc_1",
Title: "CortexDB Overview",
Content: "CortexDB is a lightweight vector database...",
})
// Store a memory record with scope and importance
db.SaveMemory(ctx, cortexdb.MemorySaveRequest{
MemoryID: "mem_1",
Content: "Alice prefers window seats.",
Importance: 4.5,
})
Chat Memory ¶
Built-in conversation history management:
// Add a message to a session
db.Vector().AddMessage(ctx, &core.Message{
SessionID: "session_123",
Role: "user",
Content: "How do I use cortexdb?",
})
Advanced Configuration ¶
Configure indexing and quantization for production:
config := cortexdb.DefaultConfig("data.db")
config.IndexType = core.IndexTypeHNSW // or core.IndexTypeIVF
config.SimilarityFn = core.CosineSimilarity
config.Dimensions = 1536
db, err := cortexdb.Open(config)
For deeper control (quantization, logger, text similarity), use core.Config with core.NewWithConfig.
Observability ¶
cortexdb supports structured logging via core.Config.Logger when using core.NewWithConfig.
MCP Tool Calling ¶
Run as an MCP stdio server to expose tools to LLM clients:
if err := db.RunMCPStdio(ctx, cortexdb.MCPServerOptions{}); err != nil {
log.Fatal(err)
}
Semantic Router ¶
Route user intent before expensive LLM calls:
import (
"context"
"github.com/liliang-cn/cortexdb/v2/pkg/core"
semanticrouter "github.com/liliang-cn/cortexdb/v2/pkg/semantic-router"
)
router, _ := semanticrouter.NewRouter(
semanticrouter.NewMockEmbedder(1536),
semanticrouter.WithThreshold(0.82),
semanticrouter.WithSimilarityFunc(core.CosineSimilarity),
)
_ = router.Add(&semanticrouter.Route{
Name: "refund",
Utterances: []string{"我要退款", "申请退款"},
})
result, _ := router.Route(context.Background(), "我要退款")
_ = result
Hindsight Memory ¶
Long-term agent memory with TEMPR retrieval:
import "github.com/liliang-cn/cortexdb/v2/pkg/hindsight"
sys, _ := hindsight.New(&hindsight.Config{DBPath: "agent_memory.db"})
_ = sys
For more detailed examples, see the examples/ directory.
Index ¶
Constants ¶
const Version = "2.16.0"
Version represents the current version of the cortexdb library.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
cortexdb-mcp-stdio
command
|
|
|
examples
|
|
|
advanced_memory
command
|
|
|
benchmark
command
|
|
|
benchmark_ivf
command
|
|
|
chat_memory
command
|
|
|
document_clustering
command
|
|
|
graphrag_embedder
command
|
|
|
hindsight
command
Package main demonstrates using the Hindsight memory system.
|
Package main demonstrates using the Hindsight memory system. |
|
hybrid_search
command
|
|
|
image_search
command
|
|
|
knowledge_graph
command
|
|
|
llm_integration
command
|
|
|
llm_tool_calling
command
|
|
|
multi_collection
command
|
|
|
rag_system
command
|
|
|
rdf_knowledge_graph
command
|
|
|
rdfs_inference
command
|
|
|
semantic-router
command
Package main demonstrates the semantic-router usage.
|
Package main demonstrates the semantic-router usage. |
|
semantic_search
command
|
|
|
simple_usage
command
|
|
|
sparql_updates
command
|
|
|
structured_data
command
|
|
|
text_api
command
|
|
|
internal
|
|
|
pkg
|
|
|
core
Package core provides advanced search capabilities
|
Package core provides advanced search capabilities |
|
cortexdb
Package cortexdb provides a lightweight SQLite-based vector database for Go AI projects
|
Package cortexdb provides a lightweight SQLite-based vector database for Go AI projects |
|
geo
Package geo provides geo-spatial indexing and search capabilities for cortexdb
|
Package geo provides geo-spatial indexing and search capabilities for cortexdb |
|
hindsight
Package hindsight: chat.go provides a thin wrapper around the cortexdb session/message API that optionally auto-triggers fact extraction.
|
Package hindsight: chat.go provides a thin wrapper around the cortexdb session/message API that optionally auto-triggers fact extraction. |
|
index
Package index provides vector indexing implementations
|
Package index provides vector indexing implementations |
|
quantization
Package quantization provides vector compression techniques
|
Package quantization provides vector compression techniques |
|
semantic-router
Package semantic-router provides a semantic routing layer for LLM applications.
|
Package semantic-router provides a semantic routing layer for LLM applications. |