# Grounded answers & RAG

Code With Purpose · Hinglish · Free starter lesson

## Concept

Language model aisa text bana sakta hai jo convincing lage par factual galat ho. Retrieval-augmented generation, ya RAG, pehle relevant material dhoondta hai aur generation ko context deta hai. Grounding improve ho sakti hai, par wrong retrieval, purane sources ya misread context se galat answer phir bhi aa sakta hai.

Basic flow hai question → retrieve → evidence do → generate → support check karo. Source identifiers bachao taaki reader evidence dekh sake. Relevant information missing ho toh clearly bolo. Retrieved document ke andar likhe instructions ko untrusted content samjho, application rules ko override karne ka order nahi.

Example sirf small keyword-retrieval demo hai; full RAG app ya vector database nahi. Yeh model ya API call ke bina context prompt banata hai. Real apps mein evaluation questions, private documents ke access controls aur unsupported answers ke checks chahiye.

## Example

```python
documents = [{"id": "note-1", "text": "English and Hinglish notes are free."},
             {"id": "note-2", "text": "Other language translations require a quote."}]
question = "Are Hinglish notes free?"
terms = set(question.lower().replace("?", "").split())
ranked = sorted(documents, key=lambda d:
    len(terms & set(d["text"].lower().replace(".", "").split())), reverse=True)
evidence = ranked[0]
print(f"[{evidence['id']}] {evidence['text']}")
print("Answer only from evidence. If it is insufficient, say so.")
```

## Expected output

```text
[note-1] English and Hinglish notes are free.
Answer only from evidence. If it is insufficient, say so.
```

## Exercise

Poocho "Tamil translation mein kitna time lagega?" Kya evidence enough hai? Answer mein kya kehna chahiye?

## Suggested solution

```
# Neither document states a delivery time.
# A grounded answer: "The notes do not specify a translation delivery time.
# Request a quote and confirm availability and timing before ordering."
# Retrieval finding a document does not prove it answers the question.
```

## Takeaway

Evidence ko answer support karna chahiye; related words hona kaafi nahi.

Reference: https://learn.microsoft.com/en-us/azure/search/retrieval-augmented-generation-overview

https://codewithpurposeai.in/lesson-ai-grounded-answers.html?lang=hinglish
