AI / Core OpenAI Codex Application Fundamentals Interview Questions
What is retrieval-augmented generation (RAG) and how do you implement it with OpenAI?
Retrieval-Augmented Generation (RAG) is a technique that enhances a language model's responses by providing relevant context retrieved from an external knowledge base at query time. Instead of relying solely on the model's training data, RAG retrieves up-to-date or private information and includes it in the prompt.
from openai import OpenAI client = OpenAI() # ---- FULL RAG PIPELINE ---- # Step 1: Embed and index your documents (do this once) def embed_documents(texts: list[str]) -> list[list[float]]: response = client.embeddings.create( model="text-embedding-3-large", input=texts, ) return [item.embedding for item in response.data] docs = [ "Our refund policy: returns accepted within 30 days with receipt.", "API rate limits: 3500 RPM for Tier 2 customers.", "Python 3.12 introduced GIL opt-out via Py_GIL_DISABLED=1.", ] doc_embeddings = embed_documents(docs) # Store doc_embeddings + docs in a vector database (Pinecone, Weaviate, pgvector...) # Step 2: At query time, retrieve relevant chunks user_query = "Can I return a product after 2 months?" query_embedding = embed_documents([user_query])[0] # similarity_search(query_embedding) -> returns top-k relevant docs relevant_docs = ["Our refund policy: returns accepted within 30 days with receipt."] # Step 3: Generate with context response = client.responses.create( model="gpt-5.5", instructions="Answer based only on the provided context. If unsure, say so.", input=f"Context:\n{chr(10).join(relevant_docs)}\n\nQuestion: {user_query}", ) print(response.output_text) # "Based on our policy, returns are only accepted within 30 days with a receipt, # so a return after 2 months would not be eligible."
OpenAI's built-in RAG via file_search: instead of building the embedding + vector search pipeline yourself, you can upload files to OpenAI and use the file_search built-in tool in the Responses API. OpenAI handles chunking, embedding, and retrieval automatically. This is simpler but less customisable than a self-managed vector store.
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...
