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.
More Related questions...