AILabStore
All guides

Building

Embeddings and vector databases

Understand embeddings, choose an embedding model, and store and search vectors with Qdrant, pgvector or Chroma.

8 min read · Updated November 1, 2026

Embeddings turn text (and images) into lists of numbers that capture meaning. Texts with similar meaning end up close together, which makes embeddings the foundation of semantic search, RAG, recommendations, clustering and deduplication. This guide explains how to pick an embedding model and how to store and query vectors in the most common databases.

Creating embeddings

The embeddings endpoint is OpenAI-compatible:

import os
from openai import OpenAI

client = OpenAI(base_url="https://api.ailabstore.cc/v1", api_key=os.environ["AILABSTORE_API_KEY"])

resp = client.embeddings.create(
    model="openai/text-embedding-3-small",
    input=["How do I reset my password?", "Steps to change your login credentials"],
)
a, b = (d.embedding for d in resp.data)
print(len(a))  # 1536
const resp = await client.embeddings.create({
  model: "openai/text-embedding-3-small",
  input: ["How do I reset my password?", "Steps to change your login credentials"],
});
console.log(resp.data[0].embedding.length);

Measuring similarity

Cosine similarity is the standard measure. Values near 1 mean very similar:

import math

def cosine(u, v):
    dot = sum(x * y for x, y in zip(u, v))
    return dot / (math.sqrt(sum(x * x for x in u)) * math.sqrt(sum(y * y for y in v)))

print(round(cosine(a, b), 3))  # high, despite sharing almost no words

Choosing an embedding model

Model Strengths
openai/text-embedding-3-small Low cost, solid quality, good default
openai/text-embedding-3-large Higher accuracy, supports shortened dimensions
voyage/voyage-3.5 Excellent retrieval quality
voyage/voyage-code-3 Code search
cohere/embed-v4 Multilingual, text and images
jina/jina-embeddings-v4 Multimodal and multilingual
google/gemini-embedding-001 Strong multilingual performance
qwen/qwen3-embedding-8b Published weights, strong benchmarks

Guidelines:

  • Start with a mid-priced general model and evaluate on your own queries before optimising.
  • Match the language: for non-English content, prefer models trained to be multilingual.
  • Check the public MTEB leaderboard on Hugging Face for task-specific comparisons, but trust your own evaluation more.
  • Never mix models in one index. Vectors from different models live in different spaces. Changing models means re-embedding everything.

Reducing dimensions

Some models support shorter vectors, which cut storage and speed up search with a small quality trade-off:

resp = client.embeddings.create(
    model="openai/text-embedding-3-large",
    input="quarterly revenue report",
    dimensions=1024,
)

Storing vectors

You can keep vectors in a dedicated vector database or alongside your relational data. All of the options below are available as managed tools in the Enterprise package.

Qdrant

from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct, Filter, FieldCondition, MatchValue

qdrant = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])

qdrant.create_collection("articles", vectors_config=VectorParams(size=1536, distance=Distance.COSINE))

qdrant.upsert("articles", points=[
    PointStruct(id=1, vector=a, payload={"text": "How do I reset my password?", "lang": "en"}),
])

hits = qdrant.query_points(
    "articles",
    query=b,
    limit=5,
    query_filter=Filter(must=[FieldCondition(key="lang", match=MatchValue(value="en"))]),
).points

pgvector (PostgreSQL)

If your data already lives in Postgres, pgvector keeps everything in one place:

psql "$DATABASE_URL" <<'SQL'
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE documents (
  id bigserial PRIMARY KEY,
  content text NOT NULL,
  embedding vector(1536)
);
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);
SQL
import psycopg

with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
    conn.execute(
        "INSERT INTO documents (content, embedding) VALUES (%s, %s::vector)",
        ("How do I reset my password?", str(a)),
    )
    rows = conn.execute(
        "SELECT content, 1 - (embedding <=> %s::vector) AS score "
        "FROM documents ORDER BY embedding <=> %s::vector LIMIT 5",
        (str(b), str(b)),
    ).fetchall()

The <=> operator is cosine distance; 1 - distance gives similarity.

Chroma

Chroma is the quickest way to prototype:

import chromadb

chroma = chromadb.HttpClient(host=os.environ["CHROMA_HOST"])
col = chroma.get_or_create_collection("faq", metadata={"hnsw:space": "cosine"})
col.add(ids=["1"], embeddings=[a], documents=["How do I reset my password?"])
print(col.query(query_embeddings=[b], n_results=3))

Choosing a vector database

Need Good choice
Already on Postgres, under ~10M vectors pgvector
Rich filtering and hybrid search Qdrant or Weaviate
Billions of vectors, GPU indexing Milvus
Prototype or small app Chroma

Indexing at scale

  • Batch requests: send up to a few hundred inputs per embeddings call.
  • Cache embeddings: hash the chunk text and skip re-embedding unchanged content.
  • Store the model name with each vector so you know what to re-embed later.
  • Use HNSW indexes for fast approximate search, and tune ef/m only if recall is too low.

Next steps