Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 5 min read Announcement mediumAdept

Announcing pg_tiktoken: A Postgres Extension for Fast BPE Tokenization

pg_tiktoken is a Postgres extension on Neon that wraps OpenAI's tiktoken BPE tokenizer, exposing tiktoken_encode (returns token IDs) and tiktoken_count (returns token counts) for encodings such as cl100k_base, p50k_base, p50k_edit and r50k_base or their model names. A practical pattern stores chat message history with a precomputed n_tokens column, then uses a window-function cumulative sum ordered by recency to select the most recent messages that fit within a model's context budget (MAX_HISTORY_TOKENS = model max minus system and completion tokens) before calling gpt-3.5-turbo.

Assumes: SQL window functionsOpenAI chat API basics

Related posts

  • tokenizers v1: encode, decode and scaling, measured

    Hugging Face mediumAdept

    tokenizers v1 encodes text 3 to 30 times faster than v0.23 single-threaded on an Apple M4 Max and scales at 76% of linear across eight cores, while producing identical token IDs and keeping the same API. The gains come from replacing regex pre-tokenization with SIMD bitstream operations (bitcannon) for common BPE patterns, a thread-local word cache for repeated pre-tokens, an allocation-free merge loop using an intrusive linked list in a scratch buffer with ranks packed into 64-bit integers, batched model calls, and per-thread sub-pools for parallel encoding. The benchmark methodology separates load time, verifies output by hashing IDs, pins to physical cores, and warns that 'warm' benchmarks on repeated versus distinct documents measure very different workloads.

  • BM25 is only as good as your tokens

    Neon mediumAdept

    Lakebase Search's new lakebase_tokenizer extension moves Postgres text-search synonym and stop-word dictionaries from server-side files into SQL tables, so managed and branchable Neon databases can define custom vocabularies with INSERT. Because BM25 only scores the tokens a dictionary produces, unifying variants like k8s/kube/kubernetes, stripping accents and dropping filler words changes both recall and IDF-driven scores. In a support-ticket example, the query 'pg creds zurich' goes from one match to three, and 'please help, kube pods crashing' goes from zero AND-filtered matches to two. Merged synonyms also lower inflated rarity scores for rare spellings.

  • Tokenization in Transformers v5: Simpler, Clearer, and More Modular

    Hugging Face mediumAdept

    Transformers v5 redesigns tokenizers so that tokenizer architecture (normalizer, pre-tokenizer, model, post-processor, decoder) is separated from the trained vocabulary, much as PyTorch separates model architecture from weights. The walkthrough covers the tokenization pipeline stages, the BPE, Unigram and WordPiece algorithms, and how the transformers wrapper adds chat templates, special tokens, truncation and padding on top of the Rust tokenizers library. It also lays out the class hierarchy: PreTrainedTokenizerBase as the common interface, with TokenizersBackend, PythonBackend and SentencePieceBackend beneath it. The v5 changes consolidate on a single fast backend and make tokenizers easier to inspect, customize and train from scratch.

  • Token Pooling-Aware Training for Multi-Vector Embedding Models

    Hugging Face hardMaster

    Pooling-aware training teaches a multi-vector (ColBERT-style) encoder to keep retrieval-relevant information when document token vectors are merged by Ward hierarchical clustering, improving quality at a fixed storage budget. On a ModernBERT-base model trained on 120k MS MARCO triplets, pool_factor=4 retains 94.6% of unpooled NanoBEIR nDCG@10 with ordinary training versus 97.8% with a loss that averages unpooled and pooled retrieval objectives, while cutting document vectors 4x. Sentence Transformers provides HierarchicalTokenPooling (pool_factor, max_tokens or calibrated distance_threshold), a straight-through estimator for gradients through clustering, a MultiVectorTokenPoolingLoss wrapper, and optional CUDA kernels that make Ward clustering 30-150x faster than SciPy.

  • Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres

    Neon mediumAdept

    A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function. A window-function query caps retrieved context at a cumulative 1,500 tokens to fit the 4,096-token model limit, and an ivfflat index (lists ≈ 4·sqrt(rows)) speeds up the similarity search. Neon's WebSocket-based serverless driver works around Edge Functions' lack of TCP, and streaming completions via SSE parsing return answers faster. User feedback is stored for later fine-tuning.

The quest for software engineering knowledge.

Look:
Layout: