Back to all posts

How I learned Generative AI basics

·7 min read

How I learned Gen AI basics

There are two ways to learn Generative AI: start with the high-level frameworks, or build from the ground up. I chose the latter.

I learned Generative AI by building production-grade applications from the ground up using raw Python and the Groq API, before abstracting with frameworks. I started by mastering stateless LLM interactions and streaming, progressed to building autonomous agentic loops and multi-modal pipelines, implemented enterprise RAG with PDF parsing and Vector Databases, secured the apps against prompt injection, and finally orchestrated everything using LangChain. Because I built the underlying architecture manually first, I understand exactly how to debug, optimize, and scale these systems in production.

Here is the phase-by-phase breakdown of my journey:


Phase 1: Core LLM Architecture & State Management

Project 1: Raw API Integration & Stateless Foundations

  • What I Built: A foundational CLI chatbot using the Groq Python SDK and the llama-3.3-70b-versatile model.
  • Technical Deep Dive: I bypassed all frameworks to interact directly with the RESTful chat completions endpoint. I learned firsthand that LLMs are inherently stateless, they possess no persistent memory between requests. I manually constructed the JSON payload containing the messages array, distinguishing between user and assistant roles. I also gained practical exposure to tokenization, understanding that APIs bill and process data in tokens (sub-word units) rather than characters or words, and observed how context windows dictate the maximum payload size.
  • Production Relevance: Every GenAI framework (LangChain, LlamaIndex) is just a wrapper around this exact raw API call. Understanding the base payload structure allows me to debug framework errors, optimize token usage, and integrate with new providers instantly.

Project 2: Stateful Memory & System Prompt Engineering

  • What I Built: A multi-turn conversational bot with persistent memory and a defined persona ("Grumpy Pirate").
  • Technical Deep Dive: I engineered a custom state management layer using Python lists and a while True event loop to simulate memory. On every turn, I appended both the user’s input and the AI’s response to the messages list before sending it back to the API. I also mastered the system role, using it to inject persistent behavioral instructions, tone constraints, and formatting rules that the model adhered to across all subsequent turns without requiring repetition in user prompts.
  • Production Relevance: This is the exact architecture underlying every commercial chatbot. Managing this message array efficiently (and knowing when to truncate or summarize it to avoid exceeding context limits) is a core backend engineering skill.

Phase 2: Breaking the Text Barrier

Project 3: Tool Calling / Function Calling

  • What I Built: An AI assistant capable of executing local Python functions (weather lookup, math calculation) based on natural language requests.
  • Technical Deep Dive: I implemented the industry-standard "Two-Step Dance" for function calling. First, I defined a strict JSON Schema (tools array) describing available functions, their parameters, and types. Second, I set tool_choice="auto" and intercepted the model's structured JSON response requesting a tool execution. My Python code parsed this JSON via json.loads(), executed the corresponding local function, and appended the result back into the message history using the specialized "role": "tool" message type with a matching tool_call_id. The model then read this result to generate a final natural language response.
  • Production Relevance: This is how AI connects to databases, CRMs, and internal APIs. It transforms LLMs from text generators into deterministic software orchestrators.

Project 4: Naive RAG & Context Injection

  • What I Built: An HR assistant that answers questions strictly from a private, hardcoded knowledge base.
  • Technical Deep Dive: I solved the hallucination problem for proprietary data by implementing basic Retrieval-Augmented Generation. I built a naive lexical retriever (keyword matching) to find relevant documents, then used Python f-strings to dynamically inject the retrieved text directly into the system prompt. Crucially, I engineered a guardrail instruction: "Answer ONLY using the provided context. If unknown, say so." This forced the model to ground its responses in my data rather than its pre-trained weights.
  • Production Relevance: This is the foundational pattern for enterprise AI. While the retrieval method was naive, the architecture of context injection and grounding instructions is identical to production systems.

Project 5: Multi-Modal Vision Processing

  • What I Built: An image analysis bot capable of interpreting visual data alongside text.
  • Technical Deep Dive: I transitioned from flat string payloads to complex, nested JSON structures. The content field became an array of objects, each typed as either "text" or "image_url". I handled external media fetching via URLs and learned Base64 encoding to transmit local binary image files as UTF-8 strings within the API payload. I also encountered and resolved real-world infrastructure issues like HTTP 403 Forbidden errors caused by server-side bot protection on image hosts.
  • Production Relevance: Multi-modal processing is critical for document understanding, visual QA, accessibility tools, and e-commerce. Handling mixed-media payloads and encoding edge cases is a required skill for modern AI apps.

Phase 3: Production UX & Web Interfaces

Project 6: Streaming & Perceived Latency Optimization

  • What I Built: A real-time typewriter-effect CLI interface that streams tokens as they’re generated.
  • Technical Deep Dive: I eliminated blocking API calls by enabling stream=True, which returns a generator object instead of a complete response. I iterated over incoming chunks, extracting text from chunk.choices[0].delta.content (the incremental token delta). I solved terminal buffering issues using print(text, end="", flush=True) to force immediate stdout rendering, preventing batched output that destroys perceived performance.
  • Production Relevance: Streaming reduces Time-to-First-Token (TTFT) from seconds to milliseconds. This is non-negotiable for user-facing AI products; users perceive streamed responses as significantly faster even if total generation time is identical.

Project 7: Streamlit Web UI & Reactive State Management

  • What I Built: A full ChatGPT-style web interface with persistent chat history and streaming output.
  • Technical Deep Dive: I migrated to Streamlit and immediately confronted its reactive re-execution architecture: the entire script runs top-to-bottom on every interaction. I solved state persistence using st.session_state, a server-side dictionary that survives re-runs. I also built a custom generator wrapper around the Groq stream to prevent Streamlit’s st.write_stream() from dumping raw JSON when encountering non-OpenAI stream objects, ensuring clean token-by-token rendering in the UI.
  • Production Relevance: Understanding reactive frameworks and session state is essential for building AI dashboards, internal tools, and customer-facing interfaces rapidly without frontend expertise.

Phase 4: Security, Autonomy & Deployment

Project 8: Guardrails & Prompt Injection Defense

  • What I Built: A two-tier safety filter that blocks jailbreaks, metaphor attacks, and malicious intent before reaching the main LLM.
  • Technical Deep Dive: I architected a Safety Sandwich using a lightweight 8B model as a dedicated bouncer. To defeat prompt injection, I implemented Chain-of-Thought (CoT) reasoning, forcing the bouncer to explicitly analyze intent step-by-step before outputting a verdict tag ([VERDICT: SAFE/UNSAFE]). I also used XML delimiters (<user_input>) to create hard boundaries between system instructions and untrusted user payloads, preventing attackers from overwriting system prompts. Temperature was locked at 0.0 for deterministic classification.
  • Production Relevance: AI security is a top enterprise priority. CoT guardrails and delimiter-based prompt hardening are industry best practices for mitigating jailbreaks, data leakage, and toxic outputs.

Project 9: Document Parsing & Chunking Pipelines

  • What I Built: An automated PDF ingestion system that extracts text and prepares it for RAG.
  • Technical Deep Dive: I used PyPDF2 to extract raw text from binary PDF files, handling the complexities of document structure. I implemented naive character-based chunking to split large documents into smaller segments that fit within the LLM’s context window. I experimented with chunk sizes to observe the trade-off between context fragmentation (chunks too small) and the "Lost in the Middle" phenomenon (chunks too large).
  • Production Relevance: Real-world enterprise data lives in messy PDFs, Word docs, and HTML. Building robust parsing and chunking pipelines is the most time-consuming and critical part of any production RAG system.

Project 10: Autonomous Agentic Loops

  • What I Built: A financial analyst agent that autonomously plans, executes tools, observes results, and iterates until completing a multi-step task.
  • Technical Deep Dive: I built a true Agentic Loop using a while loop orchestrator. Unlike simple chatbots, this agent maintained an internal planning cycle: send history → check for tool calls → execute tools → append observations → repeat. The agent autonomously decided when it had sufficient information to stop and provide a final answer. I implemented critical circuit breakers (max_iterations) to prevent infinite loops, runaway API costs, and cascading failures.
  • Production Relevance: Agents are the frontier of GenAI. Understanding agentic loops, state management, and safety limits is essential for building autonomous workflows, research assistants, and automated operational tools.

Project 11: Production Deployment & Secret Management

  • What I Built: A secure, cloud-hosted web application with proper credential management and dependency declaration.
  • Technical Deep Dive: I eliminated hardcoded secrets by implementing Environment Variables via .env files and python-dotenv, loading credentials securely with os.getenv(). I configured .gitignore to prevent secret leakage to version control. I declared dependencies in requirements.txt for reproducible builds. Finally, I deployed to Streamlit Community Cloud, injecting secrets via the platform’s secret manager rather than committing them to code.
  • Production Relevance: This is baseline software engineering hygiene. No AI app reaches production without proper secret management, dependency pinning, and secure deployment pipelines.

Phase 5: Enterprise Ecosystem & Orchestration

Project 12: Semantic RAG & Vector Databases

  • What I Built: A semantic search system that retrieves documents based on conceptual meaning rather than exact keywords.
  • Technical Deep Dive: I replaced lexical search with Semantic Search using ChromaDB. Documents were automatically converted into high-dimensional vector embeddings (numerical representations of meaning). Queries were similarly embedded, and retrieval was performed using cosine similarity to find conceptually related documents regardless of vocabulary overlap. This solved the synonym problem (e.g., "dog" retrieving documents about "canines").
  • Production Relevance: Vector databases are the backbone of enterprise RAG. Understanding embeddings, similarity search, and vector storage is mandatory for any AI engineer working with private knowledge bases.

Project 13: LangChain LCEL Orchestration

  • What I Built: A complete RAG pipeline orchestrated in ~10 lines of code using LangChain Expression Language.
  • Technical Deep Dive: I abstracted my raw RAG implementation using LCEL, chaining components with the pipe operator (|): retriever | prompt | llm | output_parser. I used RunnablePassthrough to pass user queries through the chain unchanged, ChatPromptTemplate for declarative prompt formatting, and StrOutputParser to extract clean text from model responses. Because I had built everything manually first, I understood exactly what each abstraction was doing under the hood.
  • Production Relevance: LangChain is the most widely adopted GenAI orchestration framework. Knowing LCEL, and more importantly, knowing what it abstracts away, makes me effective in teams that use it while retaining the ability to drop to raw code when frameworks fail or add unacceptable latency.

Building these systems manually gave me a comprehensive, technically precise narrative of my entire GenAI foundation. This level of detail is what separates candidates who watched tutorials from engineers who actually build and scale systems.

You might also like