Skip to main content
When building web apps with Sonamu, you’ll implement features like these:
  • Knowledge base: “Find similar documents”
  • E-commerce: “Products similar to this one”
  • Content: “Related articles recommendation”
  • Customer support: “Find similar questions”
Traditional keyword search (LIKE '%keyword%') has limitations:
  • Searching “TypeScript framework” won’t find “Node.js API library”
  • Vulnerable to typos (“typescript” vs “tyepscript”)
  • Doesn’t handle synonyms (“framework” vs “library”)
Semantic search is needed. For this, we use vector search.

Why pgvector?

To implement vector search, you need a database that can store and search vectors.

Options

1. You’re already using PostgreSQL
Sonamu is PostgreSQL + Knex based. No need to add a separate database for vector search. 2. Can be used with existing data
You can mix vector search with regular SQL. A separate DB would require data synchronization. 3. Simple infrastructure
  • Pinecone: Separate API, cost, sync
  • pgvector: Just install extension, no additional cost
4. Natural integration with Sonamu Model

What is pgvector?

pgvector is an extension that allows PostgreSQL to store and search vector (embedding) data. Key features:
  • vector(N) data type (N-dimensional vector)
  • Similarity operators (<=>, <->, <#>)
  • Indexes (IVFFlat, HNSW)

Required Package Installation

Packages:
  • pgvector: PostgreSQL pgvector type support (use with Knex)
  • voyageai: Voyage AI embeddings (recommended for Korean)
  • @ai-sdk/openai: OpenAI embeddings (optional)

PostgreSQL Extension Installation

Installation by Environment

Enable Extension

Connect to PostgreSQL and enable the extension:

Applying to Sonamu Project

1. Environment Variables

2. Verify Sonamu Config

Sonamu already uses PostgreSQL. No additional configuration needed.

3. Create Table with Knex Migration

Create a vector table using Sonamu’s Migration:
Run:

4. Creating a Vector Table from Scratch

If creating a new table:

Understanding Vector Dimensions

Different embedding models have different vector dimensions:
Match dimensions when creating tables:

Index - Create Later

Important: Create the index after sufficient data has accumulated.

Why Later?

The index won’t be optimized without data. After 100+ data entries:
Parameters:
  • m = 16: Number of connections (default, usually OK)
  • ef_construction = 64: Search size during construction

IVFFlat Index (Faster Build)

If HNSW is too slow:

Practical Scenario

Scenario: Building a Knowledge Base

You’re building an internal knowledge base with Sonamu. Step 1: Table Design
Step 2: Data Entry (later)
Step 3: Create Index (after 100+ data entries)
Step 4: Search API (later)

Cautions

Cautions when setting up pgvector:
  1. Dimension match: Table and embedding model dimensions must be the same
  2. Index comes later: Create after 100+ data entries
  3. Allow NULL: May not be able to create embeddings for all documents immediately
  4. Manage with Migration: Use Migration instead of direct SQL
  5. Extension version: 0.5.1 or higher recommended

Next Steps

pgvector installation is complete. Now it’s time to generate embeddings and implement search.

Generating Embeddings

Creating embeddings with Voyage AI in Sonamu

Vector Search

Implementing search API in Sonamu Model