LangChain Embeddings

LangChain embeddings turn text into lists of numbers (vectors) that capture meaning. Texts with similar meaning get vectors that point in similar directions, which lets you search by meaning instead of exact keywords.

Creating Embeddings with Ollama

from langchain_ollama import OllamaEmbeddings

emb = OllamaEmbeddings(model="nomic-embed-text")
vector = emb.embed_query("How do I reset my password?")
print(len(vector))
print(vector[:4])
Sample output:
768
[0.0213, -0.0457, 0.0089, 0.0631]

nomic-embed-text produces 768-dimensional vectors. Other good open-source options on Ollama are mxbai-embed-large and bge-m3 (multilingual). Hosted alternatives include OpenAIEmbeddings from langchain-openai.

embed_query vs embed_documents

  • embed_query(text): embeds one search query, returns one vector.
  • embed_documents([texts]): embeds many documents in a batch, returns a list of vectors.
  • Some models expect different prefixes for queries and documents; the integration handles this when supported.

Measuring Similarity

import math

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)))

docs = ["Click 'Forgot password' on the login page.",
        "Our office opens at 9 am.",
        "You can change your password in account settings."]
doc_vecs = emb.embed_documents(docs)
q = emb.embed_query("How do I reset my password?")
for d, v in zip(docs, doc_vecs):
    print(round(cosine(q, v), 3), d)
Sample output:
0.781 Click 'Forgot password' on the login page.
0.402 Our office opens at 9 am.
0.734 You can change your password in account settings.

Both password sentences score high even though neither contains the word "reset". That is semantic search. Scores from different embedding models are not comparable, so judge them relative to each other.

Caching Embeddings

Embedding the same text twice wastes time. CacheBackedEmbeddings (from langchain_classic or langchain.embeddings depending on version) can store vectors on disk, or you can simply persist them in a vector store, covered next.

Choosing an Embedding Model

  • Use the same model for indexing and querying; vectors from different models are incompatible.
  • Check the MTEB leaderboard for quality comparisons.
  • Pick a multilingual model if your documents are not in English.
  • Changing the model later means re-embedding all your documents.

Frequently asked questions

What is an embedding in LangChain?

A vector of floating-point numbers that represents the meaning of a text, produced by an embedding model through a LangChain Embeddings class.

Can I use free embeddings with LangChain?

Yes. OllamaEmbeddings runs open-source models locally, and HuggingFaceEmbeddings from langchain-huggingface runs sentence-transformers models.

Can I mix embedding models in one vector store?

No. All vectors in an index must come from the same model, because each model has its own vector space.