Ollama Embeddings

Ollama embeddings turn text into a list of numbers (a vector) that captures its meaning. Texts with similar meaning get vectors that point in similar directions, which powers semantic search and RAG (retrieval-augmented generation).

Pull an Embedding Model

Embedding models are separate from chat models. Popular choices at the time of writing include nomic-embed-text, mxbai-embed-large and embeddinggemma.

ollama pull nomic-embed-text
success

The /api/embed Endpoint

input can be a single string or an array of strings for batching.

curl http://localhost:11434/api/embed -d '{
  "model": "nomic-embed-text",
  "input": ["The cat sat on the mat.", "A kitten rests on a rug."]
}'
{
  "model": "nomic-embed-text",
  "embeddings": [
    [0.0123, -0.0456, 0.0789, ...],
    [0.0119, -0.0441, 0.0802, ...]
  ]
}

Semantic Search in Python

import math
from ollama import embed

docs = ["How to reset my password", "Shipping times to Canada", "Refund policy for damaged items"]
query = "I forgot my login"

vecs = embed(model="nomic-embed-text", input=docs + [query]).embeddings
q = vecs[-1]

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    return dot / (math.sqrt(sum(x*x for x in a)) * math.sqrt(sum(y*y for y in b)))

best = max(range(len(docs)), key=lambda i: cosine(q, vecs[i]))
print(docs[best])
How to reset my password

The query shares no words with the best document, yet embeddings find it because the meanings match. In production, store vectors in a vector database (pgvector, Qdrant, Chroma) instead of a Python list.

Simple RAG Recipe

  • Split your documents into chunks of a few hundred words.
  • Embed each chunk once and store the vectors.
  • At question time, embed the question and fetch the top 3-5 chunks.
  • Put those chunks in the prompt and ask a chat model to answer only from them.

Frequently asked questions

Can I use a chat model like llama3.2 for embeddings?

Technically some can, but dedicated embedding models are smaller, faster and give much better search quality.

What happened to /api/embeddings?

It is the older single-input endpoint. /api/embed is the current one and supports batches.

Do I need to re-embed documents if I change models?

Yes. Vectors from different models are not comparable, so re-embed everything when switching.