Open-Source Astro Integration for Similar Posts

Summary

Pixel shares a guide to turning an Astro blog’s Similar Posts feature into astro-content-vectors, an open-source integration for ranking content entries by embedding similarity. It covers OpenAI embeddings, relevance thresholds, cosine scores, and SQLite caching, making related-post recommendations easier to configure across Astro projects.

DEV Community

Turning My Astro Similar Posts Feature into an Open Source Integration

For a while, the code behind my blog's Similar Posts component lived inside my site. It started as a way to recommend related articles, then grew to include a relevance threshold and a SQLite cache. Those earlier posts documented how I built it: Adding AI-powered similar posts to an Astro blog Refining the recommendations with similarity scores Caching OpenAI embeddings with SQLite After using that implementation on my own site, I wanted to make the same capability easier to add to another Astro project. That meant turning site-specific utilities into an integration that other sites could configure and call. One thing which helped me decide to do this was feedback on Bluesky: Also, I have been open-sourcing other parts of my site's source code, and this seemed like a useful next project. Hopefully, others can find it useful for quickly adding similar post recommendations to their Astro sites. From a Site Feature to an Astro Integration The first version of Similar Posts was deliberately small. I sent each article's text to OpenAI's embedding API, compared the resulting vectors with cosine similarity, and displayed the closest matches. When some recommendations looked wrong, I added a threshold and a way to inspect pairwise scores. Then, to avoid paying to embed unchanged articles on every build, I cached each vector in SQLite under Astro's .astro directory. That sequence worked for one site, but it left several decisions embedded in my site's files: which collection to read, which model to call, where the cache lived, how to handle a missing API key, and what to do when the provider was unavailable. Moving the feature into an Astro integration gave those choices a reusable configuration surface and let the integration handle the build-time work. The result is astro-content-vectors, an integration for ranking Astro content entries by embedding similarity. It targets static builds: the vectors are generated while Astro builds the site, and the recommendations can be rendered into the resulting pages without a runtime vector service. The current package requires Astro 7 or later and Node.js 20 or later for its build process. Comparing to Other Similar Projects This package is one of several ways to add embedding-based recommendations to an Astro site. The projects below solve a similar reader-facing problem, but they make different choices about where vectors live, how they are generated, and how recommendations get attached to content. Here are the main differences I found when I compared their documented approaches: ::csv{src="./project-comparison.csv"} The overlap is the goal—semantic recommendations for Astro content—not an identical architecture. For a small static site, an in-process ranking pass and local SQLite cache may be enough. If you want locally executed Transformers.js models, compare Astro Related Content; if you already operate Qdrant and want recommendations integrated into a loader, compare Astro Recommend Article. The best fit depends on where you want embedding generation and vector storage to run. Choosing a Provider for Each Environment I wanted local development to work with a model running in LM Studio and production builds to use OpenAI. The integration uses the OpenAI SDK for the embeddings request; LM Studio works because its local server exposes an OpenAI-compatible API. Here's the shape of the configuration I use on this site: import { defineConfig } from "astro/config";
import astroContentVectors from "astro-content-vectors";

export default defineConfig({
integrations: [
astroContentVectors({
collections: ["blog"],
provider: {
type: "openai",
model: "text-embedding-3-small",
},
environments: {
development: {
provider: {
type: "lmstudio",
baseUrl: "http://127.0.0.1:1234/v1",
model: "text-embedding-nomic-embed-text-v1.5",
},
},
},
fallback: {
baseUrl: "http://1...
Open-Source Astro Integration for Similar Posts
Powered by Readybot.io

The latest from Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …