Skip to content

A high-performance Swift wrapper for sqlite-vec, providing type-safe vector similarity search and operations directly in SQLite.

License

Notifications You must be signed in to change notification settings

brunogama/SwiftSQLiteVec

Repository files navigation

SwiftSQLiteVec

Swift Platform License SPM

A high-performance Swift wrapper for sqlite-vec, providing type-safe vector similarity search and operations directly in SQLite.

Features

  • 🚀 High Performance: Leveraging SIMD optimizations and Metal acceleration for 3-5x speedup on batch operations
  • 🔍 Vector Similarity Search: Support for cosine, L2, and Hamming distance metrics
  • 📊 Multiple Vector Types: Float32, Int8, and binary bit vectors
  • 🛡️ Type Safety: Full Swift type safety with compile-time guarantees
  • 🔒 Enterprise Ready: Comprehensive audit logging and privacy features
  • ⚡ Async/Await Support: Modern Swift concurrency with Swift 6 compatibility
  • 📦 Easy Integration: Simple Package Manager integration with GRDB

Quick Start

Package Manager

Add SwiftSQLiteVec to your Package.swift dependencies:

dependencies: [
    .package(url: "https://github.com/your-username/SwiftSQLiteVec.git", from: "0.0.5")
]

Basic Usage

import SwiftSQLiteVec
import GRDB

// Initialize database with vector extensions
let dbQueue = DatabaseQueue()
try dbQueue.setupSQLiteVec()

// Create a vector table
let vectorDef = VectorColumnDefinition(
    name: "embedding",
    dimensions: 1536,
    elementType: .float32
)

try dbQueue.createVec0Table(
    tableName: "documents",
    columns: [vectorDef],
    distanceMetric: .cosine
)

// Insert vectors with metadata
try dbQueue.insertDocument(
    tableName: "documents",
    id: "doc1",
    embedding: [0.1, 0.2, 0.3, /* ... */],
    metadata: ["title": "Hello World", "category": "greeting"]
)

// Perform similarity search
let results = try dbQueue.similaritySearch(
    tableName: "documents",
    queryVector: [0.15, 0.25, 0.35, /* ... */],
    limit: 10,
    threshold: 0.8
)

for result in results {
    print("Document: \(result.id), Distance: \(result.distance)")
    print("Metadata: \(result.metadata)")
}

Documentation

Comprehensive documentation is available in the Documentation.docc directory, including:

Performance

SwiftSQLiteVec provides significant performance optimizations:

  • SIMD Acceleration: 3-5x speedup on batch operations using vDSP
  • Metal Processing: GPU acceleration for large vector operations
  • Zero-Copy Operations: Direct memory access for optimal performance
  • Batch Operations: 10-50x speedup for bulk operations

Benchmarks show 10-100x performance improvement over equivalent Swift implementations due to optimized C code and zero-copy operations.

Requirements

  • Swift 5.9+
  • macOS 12.0+, iOS 15.0+, or Linux
  • GRDB 6.0+

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

License

SwiftSQLiteVec is released under the MIT License.

Acknowledgments

  • sqlite-vec - The underlying SQLite extension for vector operations
  • GRDB - The excellent SQLite toolkit for Swift

About

A high-performance Swift wrapper for sqlite-vec, providing type-safe vector similarity search and operations directly in SQLite.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published