📌 Module 3: The Brain of the Agent — LLM Fundamentals
1. Theory
Large
Language Models (LLMs) are at the heart of most modern AI agents.
They process text, reason about it, and generate responses
that guide the agent’s actions.
Key Concepts
- Tokenization → Breaking text into smaller units the model can understand.
- Embeddings → Vector representations of text for semantic understanding.
- Context Window → The limit on how much information the LLM can “see” at once.
- Prompt Engineering → Crafting instructions to get desired outputs.
LLM Types
- Local LLMs → Run entirely on your machine (e.g., LLaMA, Mistral)
- Cloud-based LLMs → Accessed via APIs (e.g., OpenAI GPT-4, Anthropic Claude)
2. Step-by-Step Windows Setup (For This Module)
- Install Transformers Library
2. pip install transformers
3. pip install sentence-transformers
- Download a Small Local Model (for quick testing)
5. from transformers import pipeline
6. gen = pipeline("text-generation", model="distilgpt2")
7. print(gen("Agentic AI is", max_length=20))
- Set Up an Embeddings Model
9. from sentence_transformers import SentenceTransformer
10.model = SentenceTransformer('all-MiniLM-L6-v2')
11.embeddings = model.encode("Agentic AI learns and acts")
12.print(embeddings[:5])
3. Examples
Example 1 — Few-Shot Prompt for Classification
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
print(classifier("Build an agent that schedules meetings", candidate_labels=["Productivity", "Gaming", "Education"]))
Example 2 — Summarizing a News Article
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
print(summarizer("Artificial Intelligence is transforming industries...", max_length=40, min_length=10))
Example 3 — Semantic Search Using Embeddings
from sklearn.metrics.pairwise import cosine_similarity
docs = ["AI helps businesses", "Cooking pasta", "Agentic AI automates tasks"]
query = "automation in AI"
doc_embeddings = [model.encode(doc) for doc in docs]
query_embedding = model.encode(query)
scores = cosine_similarity([query_embedding], doc_embeddings)
print(scores)
4. Exercises
- Create a prompt that classifies user queries into “Tech” or “Non-Tech”.
- Build a summarizer for PDF documents.
- Use embeddings to find the most relevant FAQ answer to a user’s question.
5. Best Practices
- Always test with small models before switching to expensive ones.
- Optimize prompts for clarity and structure.
6. Common Mistakes
- Sending too much data beyond the context window → truncated outputs.
- Using embeddings from one model with another model for similarity search.
7. Quiz
- What is the purpose of embeddings in LLMs?
- What’s the difference between few-shot and zero-shot classification?
- Why is the context window important?