cortexdb

package
v2.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package cortexdb provides a lightweight SQLite-based vector database for Go AI projects

Example (Embedder)

Example_embedder shows how to use the Embedder interface

package main

import (
	"context"
	"fmt"
	"math"

	"github.com/liliang-cn/cortexdb/v2/pkg/cortexdb"
)

// DummyEmbedder is a simple embedder for testing purposes.
// It generates deterministic vectors based on text content.
// In production, replace this with OpenAI, Ollama, or other embedding providers.
type DummyEmbedder struct {
	dim int
}

func NewDummyEmbedder(dim int) *DummyEmbedder {
	return &DummyEmbedder{dim: dim}
}

func (d *DummyEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {

	vector := make([]float32, d.dim)

	for i := range vector {

		seed := float64(0)
		for j, b := range text {
			seed += float64(b) * float64(j+1) * float64(i+1)
		}
		vector[i] = float32(math.Sin(seed * 0.001))
	}

	norm := float32(0)
	for _, v := range vector {
		norm += v * v
	}
	norm = float32(math.Sqrt(float64(norm)))
	if norm > 0 {
		for i := range vector {
			vector[i] /= norm
		}
	}

	return vector, nil
}

func (d *DummyEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error) {
	vectors := make([][]float32, len(texts))
	for i, text := range texts {
		vec, err := d.Embed(ctx, text)
		if err != nil {
			return nil, err
		}
		vectors[i] = vec
	}
	return vectors, nil
}

func (d *DummyEmbedder) Dim() int {
	return d.dim
}

func main() {
	ctx := context.Background()

	// 1. Open database with an embedder
	db, err := cortexdb.Open(
		cortexdb.DefaultConfig("test.db"),
		cortexdb.WithEmbedder(NewDummyEmbedder(128)),
	)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	// 2. Insert text - embedding is generated automatically
	err = db.InsertText(ctx, "doc1", "The quick brown fox jumps over the lazy dog", nil)
	if err != nil {
		panic(err)
	}

	// 3. Search using text - query embedding is generated automatically
	results, err := db.SearchText(ctx, "fox jumps", 5)
	if err != nil {
		panic(err)
	}

	for _, r := range results {
		fmt.Printf("Score: %.3f, Content: %s\n", r.Score, r.Content)
	}
}
Example (TextOnly)

Example_textOnly shows how to use text-only search without an embedder

package main

import (
	"context"
	"fmt"

	mrand "math/rand"

	"github.com/liliang-cn/cortexdb/v2/pkg/cortexdb"
)

func main() {
	ctx := context.Background()

	// 1. Open database WITHOUT an embedder
	db, err := cortexdb.Open(cortexdb.DefaultConfig("test.db"))
	if err != nil {
		panic(err)
	}
	defer db.Close()

	// 2. Insert vectors manually (you need to provide the vectors)
	vector := make([]float32, 128)
	for i := range vector {
		vector[i] = mrand.Float32() // Just for demo - use real embeddings
	}

	quick := db.Quick()
	id, err := quick.Add(ctx, vector, "The quick brown fox jumps over the lazy dog")
	if err != nil {
		panic(err)
	}
	fmt.Println("Inserted:", id)

	// 3. Search using FTS5 text search (no embedding needed!)
	results, err := db.SearchTextOnly(ctx, "fox OR dog", cortexdb.TextSearchOptions{
		TopK: 5,
	})
	if err != nil {
		panic(err)
	}

	for _, r := range results {
		fmt.Printf("Score: %.3f, Content: %s\n", r.Score, r.Content)
	}
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmbedderNotConfigured is returned when text operations are called
	// but no embedder was configured during initialization.
	ErrEmbedderNotConfigured = errors.New("cortexdb: embedder not configured, use WithEmbedder option or call vector methods directly")

	// ErrEmptyText is returned when an empty text string is provided.
	ErrEmptyText = errors.New("cortexdb: empty text provided")

	// ErrEmbeddingFailed is returned when the embedder fails to produce a vector.
	ErrEmbeddingFailed = errors.New("cortexdb: embedding failed")
)

Errors related to embedder operations

Functions

This section is empty.

Types

type BaseEmbedder

type BaseEmbedder struct {
	// contains filtered or unexported fields
}

BaseEmbedder provides a default implementation of EmbedBatch that calls Embed for each text. Embedders can embed this to get batch support for free.

func (*BaseEmbedder) Dim

func (b *BaseEmbedder) Dim() int

Dim returns the dimension of vectors.

func (*BaseEmbedder) Embed

func (b *BaseEmbedder) Embed(ctx context.Context, text string) ([]float32, error)

Embed calls the underlying embed function for a single text.

func (*BaseEmbedder) EmbedBatch

func (b *BaseEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

EmbedBatch provides a default batch implementation using goroutines.

type Config

type Config struct {
	Path         string              // Database file path
	Dimensions   int                 // Vector dimensions (0 for auto-detect)
	SimilarityFn core.SimilarityFunc // Similarity function (default: cosine)
	IndexType    core.IndexType      // Index type (HNSW, IVF, Flat)
}

Config represents database configuration

func DefaultConfig

func DefaultConfig(path string) Config

DefaultConfig returns default configuration

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB represents a SQLite vector database instance

func Open

func Open(config Config, opts ...Option) (*DB, error)

Open opens or creates a vector database. Additional options can be passed to configure the database, such as WithEmbedder.

func (*DB) Close

func (db *DB) Close() error

Close closes the database

func (*DB) Graph

func (db *DB) Graph() *graph.GraphStore

Graph returns the graph store interface

func (*DB) HybridSearchText

func (db *DB) HybridSearchText(ctx context.Context, query string, topK int) ([]core.ScoredEmbedding, error)

HybridSearchText performs hybrid search combining vector and keyword matching. Requires an embedder to be configured via WithEmbedder option.

func (*DB) Info

func (db *DB) Info() DBInfo

Info returns information about the database configuration. It includes the database path, vector dimensions, index type, and other non-sensitive configuration parameters.

func (*DB) InsertText

func (db *DB) InsertText(ctx context.Context, id string, text string, metadata map[string]string) error

InsertText inserts text with automatic embedding generation. Requires an embedder to be configured via WithEmbedder option.

func (*DB) InsertTextBatch

func (db *DB) InsertTextBatch(ctx context.Context, texts map[string]string, metadata map[string]string) error

InsertTextBatch inserts multiple texts with automatic embedding generation. Requires an embedder to be configured via WithEmbedder option.

func (*DB) Quick

func (db *DB) Quick() *Quick

Quick creates a simple interface for quick operations

func (*DB) SearchText

func (db *DB) SearchText(ctx context.Context, query string, topK int) ([]core.ScoredEmbedding, error)

SearchText performs similarity search using text query. Requires an embedder to be configured via WithEmbedder option.

func (*DB) SearchTextInCollection

func (db *DB) SearchTextInCollection(ctx context.Context, collection string, query string, topK int) ([]core.ScoredEmbedding, error)

SearchTextInCollection performs similarity search using text query within a collection. Requires an embedder to be configured via WithEmbedder option.

func (*DB) SearchTextOnly

func (db *DB) SearchTextOnly(ctx context.Context, query string, opts TextSearchOptions) ([]core.ScoredEmbedding, error)

SearchTextOnly performs pure FTS5 full-text search without embeddings. This is useful when you don't have an embedding model but still want RAG capabilities.

func (*DB) Vector

func (db *DB) Vector() core.Store

Vector returns the core vector store interface

type DBInfo

type DBInfo struct {
	Path           string                    `json:"path"`
	Dimensions     int                       `json:"dimensions"`
	IndexType      string                    `json:"indexType"`
	SimilarityFn   string                    `json:"similarityFn"`
	Embedder       string                    `json:"embedder,omitempty"`
	HNSW           core.HNSWConfig           `json:"hnsw,omitempty"`
	IVF            core.IVFConfig            `json:"ivf,omitempty"`
	TextSimilarity core.TextSimilarityConfig `json:"textSimilarity,omitempty"`
	Quantization   core.QuantizationConfig   `json:"quantization,omitempty"`
}

DBInfo provides information about the database instance

type Embedder

type Embedder interface {
	// Embed converts a single text string into a vector.
	Embed(ctx context.Context, text string) ([]float32, error)

	// EmbedBatch converts multiple texts into vectors in a single call.
	// This is optional but recommended for better performance with batch operations.
	EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)

	// Dim returns the dimension of vectors produced by this embedder.
	Dim() int
}

Embedder defines the interface for text-to-vector embedding. Users can implement this interface to integrate any embedding model (OpenAI, Ollama, local models, etc.) with cortexdb.

type Option

type Option func(*DB)

Option is a functional option for configuring the DB.

func WithEmbedder

func WithEmbedder(e Embedder) Option

WithEmbedder configures the DB with an embedder for text operations. When set, you can use InsertText, SearchText and other text-based methods.

type Quick

type Quick struct {
	// contains filtered or unexported fields
}

Quick is a simplified interface for common operations

func (*Quick) Add

func (q *Quick) Add(ctx context.Context, vector []float32, content string) (string, error)

Add adds a vector with automatic ID generation

func (*Quick) AddText

func (q *Quick) AddText(ctx context.Context, text string, metadata map[string]string) (string, error)

AddText adds text with automatic ID generation and embedding. Requires an embedder to be configured.

func (*Quick) AddTextToCollection

func (q *Quick) AddTextToCollection(ctx context.Context, collection string, text string, metadata map[string]string) (string, error)

AddTextToCollection adds text to a specific collection with automatic ID generation. Requires an embedder to be configured.

func (*Quick) AddToCollection

func (q *Quick) AddToCollection(ctx context.Context, collection string, vector []float32, content string) (string, error)

AddToCollection adds a vector to a specific collection with automatic ID generation

func (*Quick) Info

func (q *Quick) Info() DBInfo

Info returns information about the database configuration

func (*Quick) Search

func (q *Quick) Search(ctx context.Context, query []float32, topK int) ([]core.ScoredEmbedding, error)

Search performs similarity search

func (*Quick) SearchInCollection

func (q *Quick) SearchInCollection(ctx context.Context, collection string, query []float32, topK int) ([]core.ScoredEmbedding, error)

SearchInCollection performs similarity search within a collection

func (*Quick) SearchText

func (q *Quick) SearchText(ctx context.Context, query string, topK int) ([]core.ScoredEmbedding, error)

SearchText performs similarity search using text query. Requires an embedder to be configured.

func (*Quick) SearchTextInCollection

func (q *Quick) SearchTextInCollection(ctx context.Context, collection string, query string, topK int) ([]core.ScoredEmbedding, error)

SearchTextInCollection performs similarity search using text query within a collection. Requires an embedder to be configured.

func (*Quick) SearchTextOnly

func (q *Quick) SearchTextOnly(ctx context.Context, query string, topK int) ([]core.ScoredEmbedding, error)

SearchTextOnly performs pure FTS5 full-text search without embeddings. This works even without an embedder configured.

type TextSearchOptions

type TextSearchOptions struct {
	Collection string
	TopK       int
	Threshold  float64 // Minimum relevance score (0.0 - 1.0)
}

TextSearchOptions defines options for text-only search

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL