The pipeline from question to answer
Embedding documents
Each document (or chunk of a document) is converted into a list of numbers — an embedding — using a specialised model. If the representation is too small, you lose information. If it is too large, retrieval slows down. Finding the right size is a key engineering decision.
Storing and searching
Embeddings go into a vector database designed for fast similarity search. When a user asks a question, their query is also embedded. The system finds the documents whose embeddings are closest to the query embedding using a distance metric like cosine similarity.
Augmenting the prompt
The retrieved documents are injected into a prompt template: 'Using the following documents, answer the user's question. If the answer is not in the documents, say I don't know.' This grounds the model and reduces hallucination.
Sourcing precision
You can extend the prompt template to ask for exact citations: 'Quote the specific passage you used and provide the page or chapter reference.' This makes the system traceable and builds user trust.
If the embedding of your question is far from the embedding of the right document, what goes wrong?