Skip to main content
llama.cpp provides multiple ways to generate embeddings - high-dimensional vector representations of text that capture semantic meaning. These embeddings are essential for semantic search, similarity comparison, and retrieval-augmented generation (RAG).

Overview

Embeddings convert text into dense numerical vectors that preserve semantic relationships. Similar texts produce similar embeddings, making them useful for:
  • Semantic search: Find documents by meaning rather than keyword matching
  • Similarity measurement: Compare texts for similarity
  • Clustering: Group similar documents together
  • Classification: Train classifiers on embedding features
  • Retrieval-Augmented Generation (RAG): Retrieve relevant context for LLM prompts

Quick Start

1

Start the server

Launch llama-server with an embedding model:
2

Generate embeddings

Make a request to the embeddings endpoint:
3

Process the results

The response contains normalized embedding vectors:

Using llama-server

The server provides both OpenAI-compatible and custom embedding endpoints.

Starting an Embedding Server

The --embeddings flag restricts the server to only support embedding use cases. Use this flag with dedicated embedding models for optimal performance.

Pooling Types

Pooling determines how token embeddings are combined into a single vector:
string
default:"model default"
Pooling method for embeddings:
  • none: Return embeddings for all tokens (no pooling)
  • mean: Average of all token embeddings
  • cls: Use the CLS token embedding
  • last: Use the last token embedding
  • rank: For reranking models

OpenAI-Compatible API

The /v1/embeddings endpoint follows the OpenAI API specification.

Single Input

Multiple Inputs (Batching)

Response Format

The /v1/embeddings endpoint requires a pooling type other than none and returns normalized embeddings using the Euclidean norm.

Custom Embedding API

The /embedding endpoint provides more flexibility than the OpenAI-compatible endpoint.

Basic Request

Normalization Options

integer
default:"2"
Normalization method for embeddings:
  • -1: No normalization
  • 0: Max absolute (scale to int16 range)
  • 1: Taxicab / L1 norm
  • 2: Euclidean / L2 norm (default)
  • >2: P-norm with specified p value

Non-OpenAI /embeddings Endpoint

The /embeddings endpoint (without /v1) supports all pooling types including none:
Response format (pooling = none):

Using llama-embedding CLI

The llama-embedding command-line tool generates embeddings directly without running a server.

Basic Usage

Output Formats

Multiple Inputs

Generate embeddings for multiple texts using a separator:

Advanced Options

Similarity Calculation

Once you have embeddings, calculate similarity using cosine similarity:

Cosine Similarity Formula

For normalized embeddings (L2 norm), this simplifies to the dot product:

Python Example

JavaScript Example

Multimodal Embeddings

Some models support generating embeddings from images or audio in addition to text.

Image Embeddings

See the Multimodal documentation for details on image and audio input formats.

Embedding Models

Popular embedding models available in GGUF format:
  • sentence-transformers/all-MiniLM-L6-v2: Lightweight, fast, 384 dimensions
  • BAAI/bge-small-en-v1.5: Strong performance, 384 dimensions
  • BAAI/bge-base-en-v1.5: Balanced quality/speed, 768 dimensions
  • BAAI/bge-large-en-v1.5: High quality, 1024 dimensions
  • Alibaba-NLP/gte-large-en-v1.5: Excellent for retrieval, 1024 dimensions
  • intfloat/e5-large-v2: Strong general-purpose, 1024 dimensions

Finding GGUF Embedding Models

Search Hugging Face for GGUF embedding models:

Using with llama-server

Reranking

Reranking models score document relevance for a given query, useful for improving search results.

Starting a Reranking Server

Reranking API

Response:
  • BAAI/bge-reranker-v2-m3: Multilingual reranking
  • BAAI/bge-reranker-large: English reranking

Use Cases

1

Index documents

Generate embeddings for all documents in your corpus:
2

Embed query

Generate embedding for the search query:
3

Find similar documents

Calculate similarity and rank:

RAG (Retrieval-Augmented Generation)

1

Build vector database

Store document embeddings in a vector database (FAISS, Pinecone, Weaviate, etc.)
2

Retrieve context

For a user query, find the most similar documents
3

Generate response

Pass retrieved documents as context to the LLM:

Document Clustering

Performance Optimization

Batch Processing

GPU Acceleration

Caching

For repeated queries, cache embeddings to avoid recomputation:

Troubleshooting

Error: “Pooling type required”

Ensure you specify a pooling method:

Poor Embedding Quality

  • Ensure you’re using a proper embedding model (not a chat/completion model)
  • Check that the pooling method matches the model’s training
  • Verify normalization is enabled for similarity comparisons

Low Throughput

  • Increase batch size: -ub 8192 -b 4096
  • Enable GPU offload: -ngl 99
  • Use smaller embedding models
  • Process documents in batches via the API

See Also

Server

Full server API documentation

Multimodal

Image and audio embeddings

CLI Tool

Command-line inference

Speculative Decoding

Speed up text generation