Prompt engineering has a versioning problem that code solved decades ago.
Say you ship a base prompt and users start customizing it for their needs. Later, you release an improved version with better instructions. Now users face an impossible choice: lose their customizations, or miss your improvements.
This is the same problem git merge solves for code.
betterprompt implements semantic three-way merge for natural language. It treats sentences as the unit of change (like git treats lines) but uses embeddings to understand meaning. This is primarily for prompt engineering but could make sense inside shared text editors or documentation systems.
import { merge } from '@nim-ai/betterprompt';
const result = await merge(baseV1, baseV2, userCustom, {
conflictStrategy: "prefer-c" // preserve user customizations
});Given three versions - original base, upgraded base, user customization - it produces a merged result that incorporates both the upgrade and the user's changes.
- Segment - Break text into semantic units (sentences, paragraphs, or sections)
- Embed - Generate vector embeddings for each unit using Model2Vec
- Align - Match units across versions using cosine similarity
- Merge - Apply three-way merge logic with conflict detection
- Reconstruct - Rebuild the merged text preserving formatting
The merge logic follows familiar rules:
| Original → Upgraded | Original → User | Result |
|---|---|---|
| unchanged | unchanged | Keep as-is |
| changed | unchanged | Apply upgrade |
| unchanged | changed | Preserve customization |
| changed | changed | Conflict (configurable) |
When both versions modify the same content, you choose the strategy: prefer user, prefer upgrade, concatenate both, or defer for manual resolution.
The key constraint was avoiding LLM calls for the core merge. LLMs are slow, expensive, and introduce non-determinism.
Instead, betterprompt bundles Model2Vec embeddings (7.5MB) with a character-frequency fallback. Typical merges complete in under 100ms. The library runs in Node.js and browsers via ONNX Runtime.
LLMs are supported as optional extensions for conflict resolution when you want smarter handling of complex conflicts.
Live Demo - paste your prompts and see the merge in action.
npm install @nim-ai/betterprompt