Python / Python Modern Generative AI and Agents Interview Questions
What embedding models should you use for production RAG systems, and how do you choose between OpenAI and open-source options?
The embedding model is one of the most consequential choices in a RAG system — it determines retrieval quality, cost, latency, and whether data leaves your infrastructure. The right choice depends on your data volume, sensitivity, quality requirements, and deployment environment.
| Model | Provider | Dimension | Speed | Cost | Best for |
|---|---|---|---|---|---|
| text-embedding-3-small | OpenAI API | 1536 | Fast (API) | $0.02/1M tokens | Balanced quality/cost; most RAG apps |
| text-embedding-3-large | OpenAI API | 3072 | Fast (API) | $0.13/1M tokens | Highest quality; small corpora |
| BAAI/bge-large-en-v1.5 | HuggingFace (local) | 1024 | Fast GPU | Free | Private data; high-quality open-source |
| sentence-transformers/all-MiniLM-L6-v2 | HuggingFace (local) | 384 | Very fast CPU | Free | Low latency; smaller corpora |
| nomic-ai/nomic-embed-text-v1.5 | HuggingFace / API | 768 | Fast | Free/API | Long documents (8192 tokens) |
# ── OpenAI embeddings (best quality, external API)
from langchain_openai import OpenAIEmbeddings
oai_embed = OpenAIEmbeddings(
model='text-embedding-3-small',
dimensions=512, # can reduce from 1536 for speed/cost (Matryoshka)
)
# ── Local HuggingFace embeddings (private, free)
from langchain_huggingface import HuggingFaceEmbeddings
hf_embed = HuggingFaceEmbeddings(
model_name='BAAI/bge-large-en-v1.5',
model_kwargs={'device': 'cuda'},
encode_kwargs={'normalize_embeddings': True},
)
# ── Direct sentence-transformers usage
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('BAAI/bge-small-en-v1.5', device='cuda')
texts = ['Hello world', 'Machine learning']
embeds = model.encode(texts, batch_size=64, normalize_embeddings=True)
print(embeds.shape) # (2, 384)
# ── Benchmark retrieval quality on your own data before committing
# BEIR benchmark: standardised RAG retrieval evaluation
# https://huggingface.co/spaces/mteb/leaderboard — MTEB leaderboard
# Quick retrieval quality check
query = 'What is machine learning?'
corpus = ['ML is a type of AI', 'The sky is blue', 'Neural networks learn from data']
q_embed = model.encode(query, normalize_embeddings=True)
c_embed = model.encode(corpus, normalize_embeddings=True)
scores = c_embed @ q_embed
ranked = sorted(zip(scores, corpus), reverse=True)
print(ranked)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
