Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 ML + AI  ·  Lesson 35

Generative AI and LLM Basics

In this lesson: How generative AI and large language models work — tokens, training, prompts and limits — explained simply. In Hindi & English.

What is Generative AI?

Generative AI creates new content – text, images, code, audio – instead of only classifying or predicting a number. A Large Language Model (LLM) is a generative model for text: ChatGPT, Claude, Gemini and Llama are LLMs. Underneath, an LLM does one simple thing again and again: predict the next piece of text.

Traditional MLGenerative AI
Is this email spam? → yes/noWrite a polite reply to this email
Predict house price → ₹48 lakhDescribe this house for a property ad
Trained for one taskOne model handles many tasks through prompts

Step 1: text becomes tokens

Models do not read letters or whole words; they read tokens – common pieces of words. “unbelievable” may become “un”, “believ”, “able”. Each token is turned into a number (an id) and then into a vector. Hindi often needs more tokens than English for the same sentence, which is why Hindi prompts can cost more and fill the context faster.

Python – tokens.py
# a toy tokenizer: split into words and give each new word an id
sentence = "the shop sells fresh sweets and the shop opens early"
vocab = {}
ids = []
for word in sentence.split():
    if word not in vocab:
        vocab[word] = len(vocab)
    ids.append(vocab[word])

print("tokens:", sentence.split())
print("ids   :", ids)
print("vocabulary size:", len(vocab))

# bytes show why non-English text is “longer” for machines
for text in ["sweet", "मिठाई"]:
    print(f"{text!r}: {len(text)} characters, {len(text.encode('utf-8'))} UTF-8 bytes")
tokens: ['the', 'shop', 'sells', 'fresh', 'sweets', 'and', 'the', 'shop', 'opens', 'early'] ids : [0, 1, 2, 3, 4, 5, 0, 1, 6, 7] vocabulary size: 8 'sweet': 5 characters, 5 UTF-8 bytes 'मिठाई': 5 characters, 15 UTF-8 bytes

Step 2: predicting the next word

The simplest language model counts which word follows which. This bigram model learns from 7 sentences about a sweet shop and then writes new text. Real LLMs use the same idea but look at thousands of previous tokens with a neural network (the Transformer) trained on trillions of tokens.

Python – tiny_language_model.py
import random
from collections import Counter, defaultdict

corpus = "the shop opens at nine . the shop sells fresh sweets . fresh sweets are made every morning . the owner makes kaju katli every morning . customers buy fresh kaju katli . the shop closes at nine at night . customers love the shop ."
words = corpus.split()

# 1. TRAINING: count next-word frequencies
next_counts = defaultdict(Counter)
for current, nxt in zip(words, words[1:]):
    next_counts[current][nxt] += 1

print("after 'fresh':", dict(next_counts["fresh"]))
print("after 'the'  :", dict(next_counts["the"]))

# 2. GENERATION: repeatedly sample the next word
def generate(start, length=10, seed=1):
    rng = random.Random(seed)
    out = [start]
    for _ in range(length):
        options = next_counts[out[-1]]
        if not options:
            break
        out.append(rng.choices(list(options), weights=options.values())[0])
    return " ".join(out)

for seed in (1, 2, 3):
    print("→", generate("the", seed=seed))
after 'fresh': {'sweets': 2, 'kaju': 1} after 'the' : {'shop': 4, 'owner': 1} → the shop . customers buy fresh sweets are made every morning → the owner makes kaju katli . customers love the shop closes → the shop closes at nine at nine . customers buy fresh

Notice that it produces sentences that never appeared in the training text (“customers buy fresh sweets are made…”). Fluent-sounding but sometimes wrong text is exactly why LLMs can hallucinate.

Step 3: temperature – creative or predictable

The model outputs a score for every possible next token. Softmax turns scores into probabilities, and temperature sharpens or flattens them before sampling.

Python – temperature.py
import math

next_words = ["sweets", "kaju", "snacks", "flowers"]
scores = [3.0, 2.2, 1.0, -1.0]          # model scores (logits) after "the shop sells fresh"

def softmax(values, temperature):
    exps = [math.exp(v / temperature) for v in values]
    total = sum(exps)
    return [e / total for e in exps]

for t in (0.2, 1.0, 2.0):
    probs = softmax(scores, t)
    row = "  ".join(f"{w}={p:.0%}" for w, p in zip(next_words, probs))
    print(f"temperature {t}: {row}")
temperature 0.2: sweets=98% kaju=2% snacks=0% flowers=0% temperature 1.0: sweets=62% kaju=28% snacks=8% flowers=1% temperature 2.0: sweets=46% kaju=31% snacks=17% flowers=6%
TemperatureBehaviourUse for
0 – 0.3Almost always the top choice; consistentFacts, code, data extraction, grading
0.7 – 1.0BalancedChat, explanations, emails
> 1Surprising, sometimes nonsenseBrainstorming, poems, names

How an LLM is built

  1. Pre-training: read a huge amount of public text and code, learning to predict the next token. This gives knowledge and language skill.
  2. Fine-tuning: train further on examples of good instructions and answers, so it follows requests.
  3. Feedback training (e.g. RLHF): people rate answers and the model learns to be more helpful and safer.
  4. Use: you send a prompt; the model generates one token at a time until it stops.
TermMeaning
ParametersThe learned numbers inside the network (billions in modern LLMs)
Context windowHow many tokens the model can see at once (prompt + answer)
EmbeddingA vector that represents the meaning of a token or sentence
HallucinationConfident but false output
RAGRetrieval-Augmented Generation: fetch your own documents and give them to the model with the question

Step 4: the idea behind RAG in 20 lines

LLMs do not know your school’s fee rules or your shop’s prices. RAG first searches your own documents for the most relevant text and puts it into the prompt. Here is the retrieval half with simple word-overlap scoring (real systems use embeddings).

Python – rag_retrieval.py
documents = {
    "fees": "School fees are due on the 10th of every month. A late fee of Rs 100 applies after the 15th.",
    "timing": "Classes run from 8:00 am to 2:00 pm, Monday to Saturday.",
    "transport": "Bus service is available for Khurja and Bulandshahr routes at Rs 900 per month.",
}

def retrieve(question, k=1):
    q = set(question.lower().replace("?", "").split())
    scored = sorted(documents.items(), key=lambda d: -len(q & set(d[1].lower().split())))
    return [text for _, text in scored[:k]]

question = "What is the late fee if I pay after the 15th?"
context = retrieve(question)[0]
prompt = f"""Answer using ONLY the context. If the answer is not there, say you don't know.
Context: {context}
Question: {question}"""
print(prompt)
Answer using ONLY the context. If the answer is not there, say you don't know. Context: School fees are due on the 10th of every month. A late fee of Rs 100 applies after the 15th. Question: What is the late fee if I pay after the 15th?

This prompt is then sent to an LLM. Because the answer is in the context, the model can answer correctly and you can show the source to the user.

Calling a real LLM from Python

Every provider (Anthropic, OpenAI, Google) has a Python SDK. The pattern is the same: install the SDK, keep your API key in an environment variable, send messages, read the text reply. Model names change often – copy the current one from the provider’s documentation.

Python – call_llm.py (needs your own API key)
# pip install anthropic       (set ANTHROPIC_API_KEY and LLM_MODEL in your environment)
import os
import anthropic

client = anthropic.Anthropic()                      # reads ANTHROPIC_API_KEY automatically
reply = client.messages.create(
    model=os.environ["LLM_MODEL"],                  # e.g. the current model name from the docs
    max_tokens=300,
    temperature=0.3,
    system="You are a patient tutor for Class 10 students. Answer in simple Hindi and English.",
    messages=[{"role": "user", "content": "Explain photosynthesis in 5 short points."}],
)
print(reply.content[0].text)
⚠️ Before you build with LLMs

API calls cost money per token – set spending limits. Never send passwords, Aadhaar numbers or private student data. Check important facts, and tell users when content is AI-generated.

Strengths and limits

LLMs are good atLLMs struggle with
Explaining, summarising, translatingExact arithmetic on long numbers (use code/tools)
Drafting emails, notes, codeVery recent events after training
Turning messy text into structured dataPrivate facts they never saw (use RAG)
Brainstorming many ideas fastBeing sure when they are wrong

Next: learn to write better instructions in Prompt Engineering, and read AI Ethics before building anything for real users.

Practice tasks

  1. Train the bigram model on 10 sentences about your school and generate 5 lines.
  2. Change the temperature values and find the point where “sweets” drops below 50%.
  3. Add two more documents to the RAG example and ask a question that needs the transport document.
  4. Write down three questions where an LLM is likely to hallucinate, and why.

Summary

  • LLMs turn text into tokens and predict the next token again and again.
  • Training = pre-training on huge text + fine-tuning + human feedback.
  • Temperature controls randomness; low for facts, higher for ideas.
  • RAG adds your own documents to the prompt to reduce hallucinations.
  • Protect keys, costs and private data; always verify important answers.

Frequently Asked Questions

Does an LLM understand like a human?
It has learned very rich patterns of language and knowledge, which lets it reason through many problems, but it has no personal experience and can be confidently wrong. Treat it as a powerful assistant whose work you check.
Can I run an LLM on my own laptop?
Yes, smaller open models (for example Llama, Mistral or Gemma families) run locally with tools such as Ollama or LM Studio. They are free and private but less capable than the largest cloud models and need enough RAM.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPrompt Engineering Basics Your progress: 35 / 66 (53%) · Lesson 36 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free