Building a Document Q&A App: Upload Files, Ask Questions, Get Answers
Build a document Q&A app with AI: upload files, chunk and embed them, retrieve the right passages and answer with sources. A practical RAG tutorial.
By Galactic News Editorial · Published 2026-11-20T10:26:07+00:00 · Updated 2026-11-20T10:26:07+00:00
Building a Document Q&A App: Upload Files, Ask Questions, Get Answers
This guide shows how to build a document Q&A app with AI. A user uploads files, asks a question in plain language, and gets an answer drawn from those files. The pattern is called retrieval-augmented generation, or RAG. It works by finding the passages that match the question, then giving those passages to a language model so it answers from your documents, not from memory.
How a document Q&A app works
The app has five stages. First, it loads the uploaded files and extracts the text. Second, it splits the text into chunks. Third, it converts each chunk into a vector with an embedding model. Fourth, it stores those vectors in a vector database. Fifth, when a question arrives, it finds the most similar chunks and sends them to a language model with the question.
Embeddings are the key idea. An embedding model turns text into a list of numbers, a vector. Similar texts produce similar vectors. That lets the app search by meaning, not by keywords. A question about refund deadlines can match a paragraph about returns even if the words differ.
The language model never sees the whole document. It sees only the top few chunks. That keeps costs down and keeps answers grounded in the uploaded files. It also means the quality of the answer depends on the quality of the retrieval. Good chunking and good embeddings matter more than a bigger model.
Step 1: Upload files and extract text
Start with the file types your users actually have. PDFs are the most common. Word documents and plain text follow. Each format needs a parser. In Python, pypdf handles PDFs and docx2txt handles Word files. In a TypeScript stack, you can parse files on the server with a library for each format.
Accept uploads through a form, and store the files on the server, not in the browser. Keep API keys server-side too. Never call a model API from the browser, because the key would be visible to anyone who opens the page.
Give each document an ID, a title and an upload date. Keep that metadata with every chunk you create later. When the app answers, it can show which document the answer came from. That citation is what makes users trust the answer.
Step 2: Split documents into chunks
Chunking splits long text into pieces the embedding model can handle. A common chunk size is around 256 to 500 tokens, with some overlap between chunks so a sentence is not cut in half. Overlap of 50 tokens is a reasonable starting point.
Split on structure where you can. Paragraph breaks are better than fixed character counts. A chunk that contains one complete idea retrieves better than a chunk that contains half of two ideas. For a handbook, split by section. For a contract, split by clause.
Test your chunk size with real questions. If answers miss details that are in the document, your chunks may be too large and the key sentence may be buried. If answers mix up two topics, your chunks may be too small and lack context. Adjust one setting at a time.
Step 3: Create embeddings and store them
An embedding model converts each chunk into a vector. OpenAI's text-embedding-3-small is a common choice. It produces 1,536-dimensional vectors and is used in several published tutorials for this exact job. Keep the model name and the dimension count in your config. If you change the embedding model later, you must re-embed every chunk.
Store the vectors in a vector database. Chroma is the easiest starting point in Python. It stores data in a local folder and needs no separate server. For a hosted option, Pinecone is a common choice. For a local, private setup, you can run everything with Ollama: pull an embedding model such as bge-m3 or nomic-embed-text, generate vectors on your own machine, and keep all data local.
Store the chunk text and its metadata alongside each vector. The metadata should include the document ID, the page or section, and the chunk position. That is what lets the app show sources with its answers.
Step 4: Retrieve the right passages
When a question arrives, embed it with the same model you used for the chunks. Then search the vector store for the closest vectors. Cosine similarity is the standard measure. Retrieve the top three to five chunks. That number is called k. Start with k equal to 3, and raise it to 5 if answers miss context.
Retrieval quality is the part most builders under-invest in. Add a simple improvement early: filter by document. If a user asks about one uploaded file, search only that file's chunks. That alone removes most wrong answers in a multi-document app.
Another improvement is a relevance check. If the best chunk is only weakly similar to the question, the document probably does not contain the answer. Say so, instead of guessing. A clear "I could not find that in your documents" builds more trust than a confident wrong answer.
Step 5: Generate the answer with sources
Build a prompt with three parts: the question, the retrieved chunks, and an instruction to answer only from those chunks. Set the model temperature to 0 for factual answers. A temperature of 0 makes the output more deterministic, which is what you want when accuracy matters.
Ask the model to cite its sources. A simple format works: a short answer, then a list of the documents and sections it used. Show those citations in the interface. Users check them, and that checking is what turns a demo into a tool people rely on.
In LangChain, this pipeline is built from two chains: a document chain that formats the chunks into a prompt, and a retrieval chain that connects the retriever to the document chain. The retrieval chain takes the question, fetches the chunks, and returns the answer with the source documents attached.
Choosing your stack
| Component | Simple choice | Alternative | When to switch |
|---|---|---|---|
| Language | Python with LangChain | TypeScript with Next.js | Your team or deployment target prefers it |
| Embeddings | OpenAI text-embedding-3-small | Ollama bge-m3 or nomic-embed-text | You need data to stay on your machine |
| Vector store | Chroma, local folder | Pinecone, Weaviate or pgvector | You need scale, teams or hosted search |
| Chat model | GPT model via API | Local model via Ollama | Privacy rules or cost require local inference |
| File parsing | pypdf and docx2txt | Format-specific libraries | You add spreadsheets, slides or scans |
A fully local build is possible. Ollama runs embedding and chat models on your own machine, and Chroma stores the vectors locally. All data stays on the machine. That suits company handbooks, contracts and medical documents where sending files to an external API is not allowed.
Guardrails before you launch
Four guardrails separate a toy from a tool. First, keep keys server-side and never expose them in the browser. Second, show sources with every answer, so users can verify. Third, say clearly when the answer is not in the documents. Fourth, log questions and answers during a pilot, and review them weekly for wrong answers and missing documents.
Also plan for document updates. When a user uploads a new version of a file, delete the old chunks and re-embed the new ones. Stale chunks are the most common cause of wrong answers in a live app. Give each upload a version, and make re-indexing part of the upload flow.
Bottom line
Start with the simple pipeline: parse, chunk, embed, store, retrieve, answer. Use Chroma and a hosted embedding model for the first version. Add document filters and a clear "not found" response before you add features. A document Q&A app earns trust one accurate, sourced answer at a time.