Last updated
Generating the Answer
Definition: Generation is the "G" in RAG — sending the augmented prompt to a language model, which produces the final answer grounded in the retrieved context.
What this step looks like in real code
In practice you call an LLM API with the prompt you built in the previous lesson. It cannot run here (it needs an API key and an internet call), but this is the shape of it:
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
)
print(response.choices[0].message.content)
The full RAG loop, assembled
- Embed the question
- Retrieve the top chunks from the vector database
- Build the augmented prompt (context + question)
- Send it to the model
- Return the grounded answer — ideally with a citation to the source chunk
💡 Cite your sources: good RAG apps show which document each fact came from, so users can trust and verify the answer.
Ad · responsive