Skip to main content

The Problem with Sonamu Search API

You’re building a knowledge base app with Sonamu:
What happens when a user searches for β€œTypeScript framework”?
  • β€œSonamu is a TypeScript framework” - Found
  • β€œSonamu is a Node.js API library” - Not found (different keywords)
  • β€œSonamu is a TS framework” - Not found (abbreviation)
  • β€œTypeScript framwork” - Not found (typo)
Limitations of keyword search:
  • Doesn’t handle synonyms
  • Fails when expressions differ
  • Vulnerable to typos
  • Fails when meaning is the same but words differ

Semantic Search is Needed

β€œTypeScript framework” and β€œNode.js API library” are semantically similar, even though the keywords are different. How can computers understand this meaning? Embeddings

What are Embeddings?

Embeddings convert text into high-dimensional numerical arrays (vectors). Semantically similar texts are placed close together in vector space. Key points:
  • Converting to numbers enables distance calculation
  • Close vectors = semantically similar texts
  • Distant vectors = semantically different texts

Flow in Sonamu

The Embedding Class

Sonamu provides an Embedding class to easily create embeddings:

Which Provider Should You Choose?

Voyage AI vs OpenAI

Sonamu supports two embedding providers:

Selection Criteria for Sonamu Projects

Recommend Voyage AI:
  • Korean services (excellent Korean performance)
  • Long document processing (32,000 tokens)
  • Search accuracy matters (asymmetric embeddings)
Recommend OpenAI:
  • Global services (balanced multilingual support)
  • Already using OpenAI API

Environment Setup

1. Install Packages

2. Configure API Keys

Get API keys from each provider’s website:

Using in Sonamu Model

Generating Embeddings When Saving Documents

Flow:
  1. User uploads document via POST /documents
  2. Sonamu API generates embedding via Voyage AI
  3. PostgreSQL stores text + embedding together

Search API (to be implemented later)

Asymmetric Embeddings (Voyage AI)

Voyage AI distinguishes between document and query embeddings.

Why the Distinction?

Document:
  • Long text
  • Detailed information
  • Storage purpose
Query:
  • Short text
  • Search terms
  • Search purpose
Documents and queries have different characteristics. Voyage AI considers this to provide more accurate search results (10-15% improvement).

Usage in Sonamu

OpenAI does not support asymmetric embeddings:

Batch Processing

When processing multiple documents at once:
Automatic splitting:
  • Voyage AI: 128 at a time
  • OpenAI: 100 at a time
Even 1000 documents are automatically split and processed.

Progress Display

You can show progress when processing many documents:

Practical Scenario

Scenario: Customer Support Knowledge Base

You’re building a customer support system with Sonamu. Step 1: Document Upload API
Step 2: Search API
Step 3: Handling User Requests

Error Handling

Cost Considerations

Token Calculation

Cost Estimation

Voyage AI ($0.13 per 1M tokens):
OpenAI ($0.02 per 1M tokens):

Cost Reduction Tips

1. Caching
2. Deduplication

Cautions

Cautions when using embeddings in Sonamu:
  1. API key required: Set environment variables
  2. Dimension match: Must match DB schema
  3. document vs query: Voyage distinguishes, OpenAI ignores
  4. Token limits: Chunking needed for very long texts
    • Voyage AI: 32,000 tokens
    • OpenAI: 8,191 tokens
  5. NULL handling: Can store NULL on embedding failure
  6. Cost monitoring: Track token usage

Next Steps

Embedding generation is complete. Now it’s time to implement the search API in Sonamu Model.

pgvector Setup

Creating PostgreSQL vector tables

Vector Search

Implementing search API in Sonamu Model

Chunking

Splitting long documents