Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Avdvg InjectGuard Embedding Model Initialization

From Leeroopedia
Knowledge Sources
Domains NLP, Embeddings, Security
Last Updated 2026-02-14 16:00 GMT

Overview

A technique for converting textual inputs into dense vector representations using pre-trained sentence embedding models, enabling downstream semantic similarity computations.

Description

Embedding model initialization is the process of loading a pre-trained sentence-level embedding model into memory and configuring it for inference. In the context of prompt injection detection, the embedding model transforms both known malicious prompts and incoming user inputs into fixed-dimensional vectors in a shared semantic space. This enables comparison via distance metrics (e.g., L2 distance) rather than exact string matching, making the detection system robust to paraphrasing and syntactic variation.

The key design decisions during initialization include:

  • Model selection: Choosing a model that balances embedding quality against inference latency and memory footprint. Lightweight models like all-MiniLM-L6-v2 (22M parameters, 384-dimensional output) are preferred for real-time detection over larger models.
  • Device placement: Assigning the model to a specific compute device (CPU or GPU) for inference throughput.
  • Normalization: Whether to L2-normalize output embeddings, which converts L2 distance comparisons into cosine similarity equivalents.

Usage

Use this principle when building any system that requires semantic comparison of text inputs. It is the foundational step for vector similarity pipelines, retrieval-augmented generation (RAG), semantic search, and content moderation systems. In the InjectGuard context, it is specifically used to project prompts into a shared vector space for injection detection.

Theoretical Basis

Sentence embeddings map variable-length text sequences to fixed-dimensional vectors:

f:𝒮d

Where 𝒮 is the set of all possible text strings and d is the embedding dimension (384 for all-MiniLM-L6-v2).

When embeddings are L2-normalized (f(s)2=1), the L2 distance between two normalized embeddings is monotonically related to cosine similarity:

f(a)f(b)22=2(1cos(f(a),f(b)))

This means that a threshold on L2 distance in normalized embedding space is equivalent to a threshold on cosine similarity, simplifying the detection logic.

Pseudo-code:

# Abstract algorithm for embedding initialization
model = load_pretrained_sentence_encoder(model_id, device)
model.set_normalization(enabled=True)
# model is now ready to encode: vector = model.encode(text)

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment