Session 1 – Introduction to Generative AI and Large Language Models (LLMs)

Objective

By the end of this session, you’ll understand what Generative AI and LLMs are, their real-world applications, and how to use popular tools like ChatGPT and DALLE to explore their capabilities. Additionally, you’ll learn to use Python to interact with Generative AI models via APIs. ????


1. What is Generative AI?

Generative AI is like an artist that creates something new based on what it has learned. It uses large datasets to learn patterns and generate new outputs, like text, images, or music. ????????


How Does Generative AI Work?

  1. Training Phase: The AI model learns patterns from massive datasets, such as text, images, or sounds.
  2. Generation Phase: Once trained, the AI creates new outputs based on a user’s prompt.

Example in Action

Think of how you write an essay:

  • Input (Prompt): “Write about the future of technology.”
  • Output (Essay): A thoughtful piece with predictions and ideas.

Generative AI works similarly but much faster and smarter! ????


2. Applications of Generative AI

Let’s dive into some real-world examples:

  1. Content Creation: Tools like ChatGPT can write blogs, emails, or even jokes.
    • Example: A company uses ChatGPT to draft product descriptions, saving time and effort.
  2. Image Generation: DALLE creates stunning visuals from text prompts, perfect for designers and marketers.
    • Example: Creating visuals for a futuristic campaign without hiring an artist.
  3. Chatbots: AI-powered bots provide 24/7 customer support.
    • Example: Banks use chatbots to assist customers with account queries.
  4. Marketing: Personalized email campaigns driven by AI.
    • Example: Generating unique subject lines to improve email open rates.

3. Hands-On Activity

Step 1: Exploring ChatGPT

Follow these steps to try out text generation:

  1. Visit ChatGPT.
  2. Enter a prompt like:”Write a short description for an eco-friendly water bottle.”
  3. Observe the output and experiment with different prompts.

Step 2: Generating Images with DALLE

  1. Visit DALLE.
  2. Enter a prompt like:”A futuristic cityscape with flying cars at sunset.”
  3. Watch DALLE bring your imagination to life! ????️

Part 4: Python Integration with OpenAI API

Step 1: Install the OpenAI Library

!pip install openai

Requirement already satisfied: openai in /usr/local/lib/python3.11/dist-packages (1.59.9)
Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from openai) (1.9.0)
Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from openai) (0.28.1)
Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from openai) (0.8.2)
Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.11/dist-packages (from openai) (2.10.6)
Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/dist-packages (from openai) (4.67.1)
Requirement already satisfied: typing-extensions<5,>=4.11 in /usr/local/lib/python3.11/dist-packages (from openai) (4.12.2)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio<5,>=3.5.0->openai) (3.10)
Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->openai) (2024.12.14)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.7)
Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)
Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)
Requirement already satisfied: pydantic-core==2.27.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=1.9.0->openai) (2.27.2)

Step 2: Write a Script for Text Generation

import openai

# Set up the API key
openai.api_key = 'your_openai_api_key'

# Generate text using GPT
response = openai.Completion.create(
    engine="text-davinci-003",
    prompt="Write a product description for a smartwatch with fitness tracking.",
    max_tokens=50
)

print("Generated Text:", response.choices[0].text.strip())
---------------------------------------------------------------------------
APIRemovedInV1                            Traceback (most recent call last)
<ipython-input-173-a395f7a35b56> in <cell line: 0>()
      5 
      6 # Generate text using GPT
----> 7 response = openai.Completion.create(
      8     engine="text-davinci-003",
      9     prompt="Write a product description for a smartwatch with fitness tracking.",

/usr/local/lib/python3.11/dist-packages/openai/lib/_old_api.py in __call__(self, *_args, **_kwargs)
     37 
     38     def __call__(self, *_args: Any, **_kwargs: Any) -> Any:
---> 39         raise APIRemovedInV1(symbol=self._symbol)
     40 
     41 

APIRemovedInV1: 

You tried to access openai.Completion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

Output:

A sleek smartwatch with cutting-edge fitness tracking, heart rate monitoring, and sleep analysis to keep you active and healthy.

Step 3: Generating Images Using DALLE API

import openai

# Set up the API key
openai.api_key = 'your_openai_api_key'

# Generate an image using DALLE
response = openai.Image.create(
    prompt="A futuristic cityscape with flying cars at sunset.",
    n=1,
    size="1024x1024"
)

image_url = response['data'][0]['url']
print("Generated Image URL:", image_url)
---------------------------------------------------------------------------
APIRemovedInV1                            Traceback (most recent call last)
<ipython-input-174-1cdefcd5fe7b> in <cell line: 0>()
      5 
      6 # Generate an image using DALLE
----> 7 response = openai.Image.create(
      8     prompt="A futuristic cityscape with flying cars at sunset.",
      9     n=1,

/usr/local/lib/python3.11/dist-packages/openai/lib/_old_api.py in __call__(self, *_args, **_kwargs)
     37 
     38     def __call__(self, *_args: Any, **_kwargs: Any) -> Any:
---> 39         raise APIRemovedInV1(symbol=self._symbol)
     40 
     41 

APIRemovedInV1: 

You tried to access openai.Image, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

Output:

A link to the generated image will appear, which you can view in your browser.


Part 5: Discussion

Q: What makes Generative AI so powerful?

  • Scale of Learning: It trains on massive datasets (think billions of words or images).
  • Creativity: It can produce ideas that feel human-made.

Q: What are its limitations?

  • Bias: It may reflect biases in the training data.
  • Context: Sometimes, it generates outputs that lack real-world understanding.

Part 6: Wrap-Up & Reflection

What We Learned Today

  • Generative AI Overview: What it is and how it works.
  • Applications: From content creation to customer support.
  • Hands-On: Tried out ChatGPT and DALLE, and explored Python integration.

Quick Activity:

Share a unique prompt you experimented with today in class and discuss the results!