Introduction to Generative AI

Understanding Large Language Models and Their Applications
Module 2

Agenda

  • What is an LLM?
  • How LLMs Work (Tokens, Attention)
  • Demo: Hello Gen AI

Agenda (continued)

  • LLM Models
  • LLM and Cloud Services
  • Lab: Building Our First Gen AI Application

What is an LLM?

"A large language model is a language model trained with self-supervised machine learning on a vast amount of text, designed for natural language processing tasks, especially language generation."

— Wikipedia

How LLM Applications Work

  • Use Transformer Architecture
  • The model tries to predict the text that will return back to the user
  • Using self attention to optimize the result

Transformer Architecture

Input Embedding Encoder Multi-Head Attention Feed Forward Network Decoder Masked Multi-Head Attention Cross Attention Feed Forward Output Probabilities Output Embedding Context

Tokenization

"Hello, how are you?" Split Hello , how are you Embed 15496 11 703 389 345 Tokens: Token IDs:

Self-Attention Mechanism

The cat sat on mat The 0.1 cat 0.4 sat 0.3 on 0.1 mat 0.1 Input Tokens Attention weights for "cat" Output (Context-Aware Representations)

Demo: Hello Gen AI

localhost:3000/hello-genai Hello Gen AI 🤖 Hello! Can you explain what you are? Hi! I'm a Gen AI assistant powered by a Large Language Model. I can understand and generate human-like text to help answer questions! Type your message here...

Code Walkthrough: Setup

import requests
import json

# Setup API credentials
API_KEY = "YOUR_API_KEY_HERE"
API_URL = "https://generativelanguage.googleapis.com/..."

def ask_gemini(prompt: str):
    """Sends a prompt to the Gemini API"""

Import necessary libraries and configure API credentials to connect with Google's Gemini model.

Code Walkthrough: Building the Request

# Construct the request payload
payload = {
    "contents": [
        {
            "parts": [
                {"text": prompt}
            ]
        }
    ]
}

# Set up headers with the API key
headers = {
    "Content-Type": "application/json"
}

Structure the request with the user's prompt formatted according to Gemini API specifications.

Code Walkthrough: API Call

# Complete URL including the API key parameter
full_url = f"{API_URL}?key={API_KEY}"

try:
    # Make the POST request
    response = requests.post(
        full_url, 
        headers=headers, 
        data=json.dumps(payload)
    )
    response.raise_for_status()
    
    # Parse the JSON response
    result = response.json()

Send the HTTP POST request to the Gemini API and handle the response.

Code Walkthrough: Response Handling

    # Safely extract the generated text
    generated_text = result.get('candidates', [{}])[0]\
        .get('content', {})\
        .get('parts', [{}])[0]\
        .get('text', 'No response text found.')
    
    print("--- Gemini Response ---")
    print(generated_text)

except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

Extract the generated text from the API response and implement error handling.

Code Walkthrough: Running the Application

if __name__ == "__main__":
    # Define your question here
    my_question = "What are the three most 
                   interesting facts about 
                   the ocean?"
    
    # Call the function with your question
    ask_gemini(my_question)

Execute the program by calling the ask_gemini() function.

Script Output: Gemini Response

--- Gemini Response ---

## The Three Most Interesting Facts About the Ocean

### 1. The Majority of Life and Terrain is Unknown (The 95% Mystery)

Despite centuries of exploration, humanity has explored and mapped less than 5% 
of the global ocean. This means that approximately 95% of the deep-sea 
environment and the life within it remains a complete mystery.

### 2. The Ocean is the World's Largest Museum (The Deep Cold Storage)

The deep sea acts as an immense, cold, dark, and low-oxygen preservative 
environment, making it the perfect repository for history, geology, and even 
atmospheric changes.

### 3. The Ocean Powers Our Weather and Produces Half Our Oxygen

The ocean is the primary engine driving global climate and sustaining life on 
Earth through fundamental chemical and physical processes.

The model returns detailed, structured responses based on the prompt.