← All Posts

Embedding Models: OpenAI, Cohere, BGE, E5

What Are Embeddings?

An embedding is a fixed-length dense vector in ℝd that captures semantic meaning. Two texts about the same concept yield vectors with high cosine similarity, even when they share few lexical tokens. Modern embedding models are trained via contrastive learning — positive pairs (query, relevant passage) are pulled together while negatives are pushed apart — producing representations that excel at nearest-neighbor retrieval tasks.

The choice of embedding model directly determines RAG retrieval quality. A poor encoder that conflates unrelated concepts will surface irrelevant chunks, forcing the LLM to hallucinate. Conversely, a high-quality encoder with strong domain alignment makes even a simple top-k retrieval strategy competitive with elaborate re-ranking pipelines.

Terminology: "Bi-encoder" encodes query and document independently (fast, scalable). "Cross-encoder" encodes the pair jointly (accurate, but too slow for first-stage retrieval).

OpenAI Embeddings

OpenAI offers two production embedding models: text-embedding-3-small (1536 dimensions, ~$0.02/1M tokens) and text-embedding-3-large (3072 dimensions, ~$0.13/1M tokens). Both support Matryoshka Representation Learning (MRL) — you can request shorter vectors (e.g., 256 or 512 dims) via the dimensions parameter, trading a small accuracy drop for significant storage and latency savings.

The v3 family improved over text-embedding-ada-002 across all MTEB benchmarks, with 3-large achieving a MTEB average of 64.6 versus ada-002's 61.0. The API is straightforward — a single HTTP call returns a list of float vectors, one per input text. Rate limits and token counts apply per-request.

Watch out: OpenAI embeddings have an 8191-token input limit. Longer documents must be chunked before embedding — do not silently truncate.

Cohere Embed v3

Cohere's embed-english-v3.0 and embed-multilingual-v3.0 output 1024-dimensional vectors and natively support input types — you specify whether the input is a search_document or search_query, which triggers asymmetric encoding that boosts retrieval accuracy. Cohere reports that v3 achieves state-of-the-art results on BEIR, outperforming OpenAI's ada-002 on 11 of 15 datasets.

A distinguishing feature is the built-in compression options. You can request int8 or binary quantisation directly from the API, reducing storage by 4× or 32× respectively. Binary embeddings enable blazing-fast Hamming-distance search in memory-constrained environments, with a full-precision re-ranking pass on the shortlist.

Open-Source: BGE & E5

For teams that need on-premise deployment or want to avoid per-token API costs, BGE (BAAI General Embedding) and E5 (Microsoft) are the leading open-source options. Both are available in small (~33M params, 384 dims), base (~110M, 768 dims), and large (~335M, 1024 dims) variants on Hugging Face.

BGE-large-en-v1.5 holds a top position on the MTEB leaderboard among open models. It was trained with RetroMAE pre-training followed by contrastive fine-tuning with hard negatives mined from cross-encoder scores. E5-mistral-7b-instruct pushes the envelope further by adapting a full LLM as an encoder, achieving parity with some commercial APIs at the cost of higher inference compute.

Running these models locally with sentence-transformers or ONNX Runtime gives you full control over batching, quantisation (INT8 / FP16), and hardware placement (GPU vs CPU). For large-scale offline indexing jobs, this eliminates API rate-limit bottlenecks entirely.

Choosing a Model

Model Dims Max Tokens MTEB Avg Cost (per 1M tokens) Self-Hostable
text-embedding-3-small1536819162.3$0.02No
text-embedding-3-large3072819164.6$0.13No
embed-english-v3.0102451264.5$0.10No
embed-multilingual-v3.0102451262.8$0.10No
BGE-large-en-v1.5102451263.6Free (compute)Yes
E5-large-v2102451262.2Free (compute)Yes
E5-mistral-7b-instruct40963276866.6Free (compute)Yes

When selecting a model, consider: (1) latency budget — API calls add network round-trip; local models add GPU inference time; (2) dimensionality — higher dims improve recall but increase vector DB storage and search cost; (3) domain alignment — run your own eval set on BEIR or a custom relevance benchmark before committing.

Tip: Use MRL with OpenAI's v3 models — request 256 dims for prototyping (cheap, fast) and scale to full dims for production retrieval quality.

Code Examples

OpenAI

import openai client = openai.OpenAI() def embed_openai(texts, model="text-embedding-3-small", dims=None): """Embed a list of texts via OpenAI. Supports MRL truncation.""" params = {"input": texts, "model": model} if dims: params["dimensions"] = dims resp = client.embeddings.create(**params) return [e.embedding for e in resp.data] vectors = embed_openai(["What is RAG?"], dims=256) print(f"Vector length: {len(vectors[0])}") # 256

Cohere

import cohere co = cohere.ClientV2() def embed_cohere(texts, input_type="search_document"): """Embed with Cohere v3 — specify input_type for asymmetric search.""" resp = co.embed( texts=texts, model="embed-english-v3.0", input_type=input_type, embedding_types=["float"] ) return resp.embeddings.float_ doc_vecs = embed_cohere(["RAG combines retrieval..."], "search_document") query_vec = embed_cohere(["What is RAG?"], "search_query")

BGE / E5 (Local)

from sentence_transformers import SentenceTransformer # BGE requires a "Represent this sentence:" prefix for queries model = SentenceTransformer("BAAI/bge-large-en-v1.5") def embed_bge(texts, is_query=False): """Embed with BGE. Prepend instruction for queries.""" if is_query: texts = [f"Represent this sentence for searching: {t}" for t in texts] return model.encode(texts, normalize_embeddings=True) # E5 uses "query:" and "passage:" prefixes e5 = SentenceTransformer("intfloat/e5-large-v2") def embed_e5(texts, prefix="passage: "): texts = [prefix + t for t in texts] return e5.encode(texts, normalize_embeddings=True) q = embed_e5(["What is RAG?"], prefix="query: ") d = embed_e5(["RAG combines retrieval..."], prefix="passage: ")
Next: Learn how to store and query these vectors at scale in Vector Databases.