Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
Asok Framework Documentation
Logo
Asok Framework Documentation

Documentation Sections

  • Getting Started
    • Getting Started
    • Routing
    • Request Handling
    • Templates
    • Middleware
    • Configurations
  • User Guide
    • ORM Basics
    • Advanced ORM Features
    • Database Migrations
    • Forms
    • Advanced Forms Features
    • Form Actions
    • Validation
    • Serialization (Schema)
    • File Storage
    • Authentication
    • Advanced Authentication
    • Sessions
    • Security Headers
    • CORS & Gzip
    • Rate Limit
    • Reactive Components
    • Transitions
    • WebSockets
    • Email Service
    • Caching
    • Background Tasks
    • Scheduled Tasks
    • Internationalization (i18n)
  • API & Component Reference
    • CLI Reference
    • Data Tables
    • Developer Toolbar (Asok Console)
    • Utilities
    • Component API Reference
    • SEO & Metadata Management
    • Tailwind CSS
  • Advanced Topics & Architecture
    • Native Vector Search
    • Security Audit
    • HTML Streaming
    • Scoped JS & CSS
    • Intelligent Prefetching
    • Asok Directives
    • Admin Interface
    • API Development
    • Async & Concurrency
    • Request Context & current_request
    • SSG & ISR (Static Site Generation & Incremental Static Regeneration)
    • Extension System
  • Deployment & Operations
    • Deployment
    • Testing
    • Logging
    • Optimization
    • Static Versioning
    • Production Build System
Back to top
View this page
Edit this page

Native Vector Search¶

Keywords: semantic search, vector search, embeddings, similarity search, vector distance, cosine similarity, sqlite vector

Asok includes a built-in native Vector Search implementation that avoids heavy external dependencies or extensions where possible. This makes it a practical option for building AI-powered features like semantic search, recommendation engines, and RAG (Retrieval-Augmented Generation) applications.

1. Defining a Vector Field¶

To store embeddings (vectors), use the Field.Vector type. You must specify the number of dimensions.

from asok import Model, Field

class Article(Model):
    title = Field.String()
    content = Field.Text()
    # 1536 is a common dimension for OpenAI embeddings
    embedding = Field.Vector(dimensions=1536)

2. Storing Vectors¶

Asok handles the complex task of serializing your Python lists into compact binary blobs for you.

# Just pass a standard list of floats
embedding_data = [0.1, -0.2, 0.45, ...] 

Article.create(
    title="Hello Vector",
    embedding=embedding_data
)

3. Querying with .nearest()¶

The .nearest() method allows you to find the most similar rows based on a target vector. It uses optimized SQL-level calculations.

Cosine Similarity (Recommended for AI)¶

This measures the angle between vectors. Perfect for semantic search.

query_vector = [0.05, 0.1, ...]

# Find the 5 most similar articles
results = Article.query().nearest("embedding", query_vector).limit(5).get()

Euclidean Distance¶

Measures the straight-line distance between points.

# Find the physically closest items
results = Article.query().nearest("embedding", target, metric="euclidean").get()

4. How it works¶

Asok injects custom mathematics functions directly into the SQLite engine.

  • Cosine Similarity: Returns a score between -1 and 1 (where 1 is identical).

  • Euclidean Distance: Returns the geometric distance.

Because these calculations happen inside the database engine, they are highly efficient. By default, they run using standard library functions in pure Python. However, if numpy is installed in your environment, Asok will automatically detect it and use it to accelerate the vector math (dot products, norms, etc.) by a factor of 10x to 100x. This allows you to scale to larger tables with no additional code changes.

Next
Security Audit
Previous
Advanced Topics & Architecture
Copyright © 2026, Mpia M. and the Asok Community
Made with Sphinx and @pradyunsg's Furo
On this page
  • Native Vector Search
    • 1. Defining a Vector Field
    • 2. Storing Vectors
    • 3. Querying with .nearest()
      • Cosine Similarity (Recommended for AI)
      • Euclidean Distance
    • 4. How it works