Retrieval-Augmented Generation (RAG) with Python

Lesson 8 of 9

LLM Response

Every earlier step exists to arrive at this one: handing the fully augmented prompt to an actual LLM and getting back a grounded, context-aware answer.

Sending the prompt

from openai import OpenAI

client = OpenAI()

def generate_answer(prompt):
    response = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role': 'user', 'content': prompt}],
    )
    return response.choices[0].message.content

answer = generate_answer(prompt)
print(answer)

This needs a real OpenAI API key and network access, so it's read-only here, everything from Data Collection through Augmentation in the earlier lessons runs directly in your browser without any API key at all.

Why this produces a better answer

Without RAG, asking an LLM "How do I reset my password?" gets a generic, plausible-sounding answer about password resets in general, not specific to Kodstigen, since the model has never seen your FAQ. With the augmented prompt from the previous lesson, the model is handed your actual FAQ text as context and instructed to answer only from it, so the response reflects your real password-reset process (the specific email address, the one-hour expiry), not a generic guess.

The outcome

This is the entire point of the 8-step pipeline: more reliable, grounded answers. The LLM still does the language generation, understanding the question, composing a fluent response, but the facts in that response come from your own retrieved data, not from what the model happened to memorize (or half-remember) during training.

TIP

Everything from raw_documents through build_prompt is real, runnable Python you've now written from scratch. The only piece that needs an external service is this final call, swapping toy_embed for real OpenAI embeddings and adding this last API call is genuinely all it takes to turn this into a production RAG system, that's exactly what the final project does next.

📝 LLM Response Quiz

Passing score: 70%
  1. 1.Why does the RAG-augmented prompt produce a more useful answer than asking the LLM the question directly?

  2. 2.In this pipeline, the LLM call is the only step that requires a real API key and network access.

  3. 3.The outcome of a working RAG pipeline is more reliable, ____ answers.