Build a GenAI-Powered Question & Answer Generator with LangChain, Groq, and FAISS
From the publisher’s feed, collected in the ZeroLabs newsroom.
Turn any PDF into an intelligent exam preparation assistant using Retrieval-Augmented Generation (RAG)IntroductionWhat if you could upload a textbook, research paper, or lecture note and instantly generate high-quality exam questions along with accurate answers?That’s exactly what we’re building in this project.In this article, we’ll walk through a complete Generative AI Question & Answer Creator Application built using:PythonLangChainGroq LLMsHugging Face EmbeddingsFAISS Vector DatabaseRetrieval-Augmented Generation (RAG)The application takes a PDF document as input, extracts its content, generates meaningful questions, stores document chunks in a vector database, and retrieves relevant context to generate accurate answers.This project is an excellent portfolio project for anyone interested in:Generative AI EngineeringLLM ApplicationsRAG PipelinesLangChain DevelopmentAI-Powered Education ToolsBy the end of this article, you’ll understand how modern GenAI pipelines work in production-like systems.Project ArchitectureImage by AuthorStep 1: Setting Up the EnvironmentWe first load environment variables and configure the Groq API key.import osfrom dotenv import load_dotenvload_dotenv()GROQ_API_KEY = os.getenv("GROQ_API_KEY")os.environ["GROQ_API_KEY"] = GROQ_API_KEYUsing environment variables is a clean and secure way to manage API keys.Step 2: Loading the PDF DocumentWe use LangChain’s PyPDFLoader to load the PDF.from langchain_community.document_loaders import PyPDFLoaderfile_path = "data/statistics.pdf"loader = PyPDFLoader(file_path)data = loader.load()Each page becomes a LangChain Document object.Next, we combine all page contents into a single string.content = ""for page in data: content += page.page_contentAt this stage, the entire PDF becomes machine-readable text.Step 3: Splitting the Text into ChunksLLMs cannot process extremely large documents efficiently in a single prompt.That’s why chunking is one of the most important steps in any RAG pipeline.In this project, we use TokenTextSplitter.from langchain_text_splitters import TokenTextSplitterfrom transformers import AutoTokenizer# Load tokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")text_splitter = TokenTextSplitter.from_huggingface_tokenizer( tokenizer, chunk_size=200, chunk_overlap=50)chunks = text_splitter.split_text(content)Why Chunking MattersChunking improves:Retrieval qualityContext relevanceEmbedding performanceLLM response accuracyThe overlap helps preserve contextual continuity between chunks.Step 4: Converting Chunks into DocumentsLangChain vector stores expect data in Document format.from langchain_core.documents import Documentdocument_chunks = [Document(page_content=chunk) for chunk in chunks]Now each chunk becomes independently searchable.Step 5: Initializing the LLMThis project uses Groq-hosted models through LangChain.from langchain_groq import ChatGroqllamaChatModel = ChatGroq( model="llama-3.3-70b-versatile", temperature=0.3)openaiChatModel = ChatGroq( model="openai/gpt-oss-120b", temperature=0.3)Why Groq?Groq provides:Extremely fast inferenceLow latencyStrong open-source LLM supportCost-efficient experimentationFor GenAI applications requiring quick responses, Groq is becoming increasingly popular.For question generation, we directly pass content to model. For Answer generation, we store the content in vector database and do retrieval.Step 6: Designing the Question Generation PromptPrompt engineering is the heart of any LLM application.The application uses a carefully designed prompt template.question_prompt_template = """You are an expert at creating questions based on given materials and documentation.Your goal is to prepare a student for their exam by generating 10 questions.You do this by asking questions about the text below:------------{text}------------Create questions that will prepare the students for their exams.Make sure not to loose any important information.Respond with only questions. No other information.QUESTIONS:"""We then convert it into a LangChain PromptTemplate.from langchain_core.prompts import PromptTemplatequestion_prompt = PromptTemplate( template = question_prompt_template, input_variables = ["text"] )A good prompt dramatically improves output quality.Step 7: Refining Questions Using LCELThe project uses a refinement strategy to improve generated questions.refine_question_template = """You are an expert at creating questions based on materials and documentation.Your goal is to help a student to prepare for their exam by generating 10 questions.We have recieved some questions to a certain extent : {existing_answer}We have the option to refine the existing questions or add the new ones deleting irrelevant questions (only if necessary) with some more context below.------------{text}------------Given the new context, refine the original questions in English.If the context is not helpful, please provide the original question.Respond with only 10 questions. No other information.QUESTIONS:"""refine_question_prompt = PromptTemplate( input_variables = ["existing_answer","text"], template = refine_question_template)This allows iterative improvement of generated questions.Step 8: Generating Questionsfrom langchain_core.output_parsers import StrOutputParser# Define the chains using LCELinitial_chain = question_prompt | openaiChatModel | StrOutputParser()refine_chain = refine_question_prompt | openaiChatModel | StrOutputParser()print("Generating questions using LCEL pattern...")# Start with the first chunkcurrent_questions = initial_chain.invoke({"text": document_chunks[0].page_content})# Iteratively refine with the rest of the chunksfor i, doc in enumerate(document_chunks[1:], start=2): print(f"Refining with chunk {i}/{len(document_chunks)}...") current_questions = refine_chain.invoke({ "existing_answer": current_questions, "text": doc.page_content })print("\nFinal Questions Generated!")print(current_questions)questions = current_questions.split("\n\n")Step 9: Creating EmbeddingsTo enable semantic search, we convert chunks into vector embeddings.from langchain_huggingface.embeddings import HuggingFaceEmbeddingsembedding_model = HuggingFaceEmbeddings( model_name="sentence-transformers/all-MiniLM-L6-v2")Why Embeddings?Embeddings convert text into numerical vector representations.This enables:Semantic similarity searchContext retrievalIntelligent document searchRAG pipelinesStep 10: Building the FAISS Vector StoreNext, we store embeddings in FAISS.from langchain_community.vectorstores import FAISSvector_store = FAISS.from_documents( document_chunks, embedding_model)Why FAISS?FAISS is one of the most widely used vector databases because it is:FastLightweightEfficient for similarity searchEasy to integrate with LangChainThis becomes the knowledge base for answer generation.Step 11: Retrieval-Augmented Generation (RAG)This is where the project becomes truly powerful.Instead of asking the LLM to answer from memory, we retrieve relevant document chunks.That retrieved context is then passed to the LLM.This significantly improves:AccuracyHallucination reductionContext awarenessReliabilityfrom langchain_core.runnables import RunnablePassthroughdef format_docs(docs): return "\n\n".join(doc.page_content for doc in docs)prompt = PromptTemplate.from_template( """Answer the question based only on the context below:{context}Question: {question}""")retriever = vector_store.as_retriever()answer_chain = ( { "context": retriever | format_docs, "question": RunnablePassthrough() } | prompt | openaiChatModel | StrOutputParser())This modern LCEL RAG pipeline structure is modular and production-friendly.Step 12: Generating Final AnswersFinally, the application loops through generated questions and produces answers.# Answer each question and save to a filefor question in questions: answer = answer_chain.invoke(question) with open("answers.txt","a") as file: file.write(question+"\n") file.write("Answer:"+answer+"\n") file.write("-------------------------------\n")At this stage, the system behaves like an AI-powered study assistant.Key TakeawaysThis project demonstrates how modern Generative AI systems are built using:LLMsEmbeddingsVector databasesRetrieval-Augmented GenerationPrompt engineeringLangChain orchestrationRather than building a simple chatbot, this application focuses on solving a real-world educational problem.It combines multiple GenAI concepts into a single practical pipeline.If you’re learning GenAI engineering, building projects like this is one of the best ways to understand how production AI systems work.Final ThoughtsThe future of education will heavily involve AI-powered personalization.If you’d like to see the complete end-to-end implementation of this GenAI application — including the FastAPI backend, RAG pipeline, and full source code — check out the GitHub repository below.GitHub - a-pt/Question-Answer-Creator-Application: 🚀 An AI-powered Q&A generator that transforms PDF documents into exam-ready questions and answers using LangChain, Groq LLMs, and RAG pipelines. Features iterative refinement for high-quality output and instant PDF preview.Build a GenAI-Powered Question & Answer Generator with LangChain, Groq, and FAISS was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.