Grounded Answer PromptAsk for evidence references and an explicit unsupported outcome
# Prompt template
Use only the evidence below for factual claims.
If the evidence is insufficient, say what is missing.
Attach source IDs to the claims they support.
Question: {question}
Evidence: {context}
Context Composition ChoicesChoose how retrieved evidence enters generation
Four ways evidence enters generation
02 · Preparing Documents for Search
§ Loading
Document LoadingA loader reads a source and returns content with metadata
Loaders normalize heterogeneous sources into documents
Document RecordPreserve identity and provenance alongside the text
Demo: Filtered RetrievalChange k and the filter; inspect which version becomes evidence
Remove approved from the filter. Does the obsolete 7 bar rule appear?
Search P-18 and update the equipment filter together.
Run code to inspect its output.
Ready
LangChain.js · JavaScript · Ctrl/Cmd+Enter · Offline replay uses fixed responses; local mode calls your model
Answer: Filtered RetrievalThe baseline output comes from executing the shown code
{"embedding": "Local token-presence vectors (not learned)", "hits": [{"source": "p17-v4", "text": "P-17 pressure alarm threshold is 5 bar. Stop if pressure exceeds 5 bar. Use Celsius for temperature."}, {"source": "p17-vibration", "text": "P-17 vibration inspection: record vibration and temperature before changing the operating speed."}]}
Query ReformulationA rewritten query can recover evidence missed by the original wording
Original → Rewrite → Search → Merge
Caveat: Query DriftA rewrite can silently change the question
Retrieval succeeded on the wrong question
Evidence Selection MethodsChoose the operation by what the candidate set still lacks
Choose by what the candidate set still lacks
RerankingA reranker scores the query together with each candidate
Reranking reorders only what retrieval found
Maximum Marginal RelevanceMMR balances query relevance with novelty among selected chunks
Diverse candidates reduce repeated evidence
MMR ObjectivePenalize similarity to evidence that has already been selected
MMR trades relevance against repetition
Context CompressionCompression removes irrelevant text before generation
Only selected evidence enters the answer prompt
Caveat: Compression LossCompressing away a condition can change a safety instruction
Compression removed the condition
Complete RAG ApplicationSearch approved records, build context, and invoke the answer chain
const chunks = await new RecursiveCharacterTextSplitter({
chunkSize: 100, chunkOverlap: 15
}).splitDocuments(manuals);
const store = await MemoryVectorStore.fromDocuments(chunks, makeEmbeddings());
const retriever = RunnableLambda.from(q => store.similaritySearch(q, 2,
d => d.metadata.equipment === "P-17" && d.metadata.approved));
const prompt = ChatPromptTemplate.fromMessages([
["system", "Use only evidence. Cite source IDs. If missing, return unknown."],
["human", "Question: {question}\nEvidence: {context}"]
]);
const model = makeModel(["P-17: 5 bar [p17-v4]"]);
const chain = prompt.pipe(model).pipe(new StringOutputParser());
const question = "What is the P-17 pressure alarm threshold?";
const hits = await retriever.invoke(question);
const context = hits.map(d => `[${d.metadata.id}] ${d.pageContent}`).join("\n");
return {mode:MODEL_MODE, sources:hits.map(d=>d.metadata.id),
answer:await chain.invoke({question,context})};
§ Evaluation and Updates
Question Answering PipelineA QA pipeline combines a retriever with an answer generator
A question produces evidence before an answer
Conversational RetrievalResolve the current question before retrieving new evidence
Conversation history helps form the retrieval question
Caveat: Follow-Up AmbiguityHistory can resolve a pronoun to the wrong equipment
"Its" points to two pumps
RAG Evaluation LayersDiagnose the first stage that loses the required evidence
A wrong answer may start with parsing or retrieval
Retrieval Recall at kRecall measures how many relevant chunks are retrieved
Relevance labels come before the score
Answer EvaluationCorrectness and groundedness are different questions
A cited answer still needs claim-level checks
Case study: Versioned ManualsA correct fact from an obsolete manual can be the wrong answer
Entity, material and version decide the answer
Index RefreshSource changes must reach chunks, vectors, and cached answers
Detect → Transform → Index → Validate
Caveat: Stale ChunksDeleting a source file may leave its indexed text searchable
Deleting the file left its chunks searchable
RAG Release BundleVersion the corpus and index together with the answer system
Corpus → Index → Retrieval → Generation
Takeaway: Retrieve the evidence before trusting the answer
1Index content together with identity, versions, and access metadata
2Evaluate retrieval before changing the answer prompt
3Return supported answers and keep the index current
← → / Space · S notes · O overview · F fullscreen · number + Enter