Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions 010_course_material/01_course_introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generative AI with Python

## 1. Course Introduction

### 1.1 Course Overview

![1770304750827.png](./images/1770304750827.png)

### System Setup

![1770317842758.png](./images/1770317842758.png)

![1770318020099.png](./images/1770318020099.png)
27 changes: 27 additions & 0 deletions 010_course_material/02_llm_introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generative AI with Python

## 2. LLM - Introduction

### 2.1 LLM Introduction

![1770389187224.png](./images/1770389187224.png)

![1770389217431.png](./images/1770389217431.png)

![1770389507588.png](./images/1770389507588.png)

![1770389567212.png](./images/1770389567212.png)

### 2.2 Classical NLP Models vs. LLM

![1770389939080.png](./images/1770389939080.png)

![1770389959448.png](./images/1770389959448.png)

### 2.3 Narrow AI Achievements

![1770390278773.png](./images/1770390278773.png)

### 2.4 Model Performance and Capabilities

![1770391230138.png](./images/1770391230138.png)
102 changes: 102 additions & 0 deletions 010_course_material/03_llm_deep_dive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Generative AI with Python

## 3. LLM - Deep Dive

### 3.1 Model Training Process

![1776035231939.png](./1776035231939.png)

![1776035369175.png](./1776035369175.png)

Instead of just extending sentences by adding new words, LLMs are now trained on large corpuses of instruction dataset that teaches the model how to respond to different instructions.

![1776035383359.png](./1776035383359.png)

![1776035515821.png](./1776035515821.png)

### 3.2 Model Improvement Options

Now, we're going to take a look at different ways to improve the model output.And here this is already a foresighton different sections that we're going to cover at some later point.But first things first,if you use a model and you are not satisfied with the outputbecause you did not put enough effort into your user query in the first place.So that would be the simplest part called direct prompting.And we will see in this lecturehow you could improve the output.So the first thing would bethat you want to improve it bymodifying at the beginning of this chain.So the train is that you have the user query, it's passed to the LMand the LLM is providing some output.So one thing would bethat the problem is at the beginning of the train,if the beginning of the train is that you were quite sloppy in providing the queryand it was maybe not specific enough.There are many different waysto improve your query.All of this is running under the umbrella called Prompt Engineering.So you can put a lot of effort into tuning your prompt.Andthat would be all trying to change the chain at the beginning.But this might not change or might not solve the problemif the model doesn't have the answer. So, if in the model weights the answer is not included,then you cannot solve it with prompt engineering.And herethe technique called retrieval augmented generation is coming into play.This is working in the following wayand we will spend a lot of time on this.But here just quickly on a very high level,we are starting out again with the user query.Andthen the user query is passedin two different ways to the model.One way is the direct passing to the large language model.The other one is that you make a bypassand the user query is passed to some external sourcefrom the external source which could be a vector database or an internet search,you get back certain results which are very relevant for answering the question.Thenthe model is provided with this output as well.And with this contextual information,the model is now capable of answering the questionand providing a good output.So this isthe core idea of retriever augmented generation.And in the majority of cases, this is the way to go.But in specific cases, it might be thatfine tuning. The modelis a better approach.In that case, it would be two stagesthe first stage would be this horizontal path.So you would start out with your external data source.It is passed to the large language modeland some so called finetuned large language model is created.So you can see here now that thisinformation from the external sourceis now going to end up as**parametric weights of the model.**And now once the model has been trained,the query is running as usual.So the user is passing inthe question in its query,then this is passed to the finetuned large language modeland the model itself is then providing the output.So this would be the idea of finetuning some large language modeland the difficulty is from left to right. So the most simple one is of course direct prompting,putting not much effort into it,but you are not satisfied with the result. Thenprompt engineering is the nextmore difficult approachRsystems are still a bit more difficult. But again,with the difficulty, the performance is increasingand fine tuning is probably the most complicatedpart of it.And as I said, usually not necessary. So, we are going to work mostly with Rsystems in the scope.

![1776035778454.png](./1776035778454.png)

### 3.3 Model Providers

![1776036637966.png](./1776036637966.png)

### 3.4 Model Benchmarking

![1776036693109.png](./1776036693109.png)

LLM Leaderboard: [https://arena.ai/leaderboard](https://https://arena.ai/leaderboard)

![1776036879224.png](./1776036879224.png)

![1776036893209.png](./1776036893209.png)

### 3.5 Interaction with LLMs

#### 3.5.1 Python

![1776254636958.png](./1776254636958.png)

![1776254663109.png](./1776254663109.png)

#### 3.5.2 Groq

![1776255870273.png](./1776255870273.png)

![1776260183492.png](./1776260183492.png)

#### 3.5.3 OpenAI

[LangChain](https://docs.langchain.com/oss/python/langchain/overviewhttps://)

![1776260430717.png](./1776260430717.png)

![1776260488538.png](./1776260488538.png)

![1776260542065.png](./1776260542065.png)

![1776260557979.png](./1776260557979.png)

![1776260757283.png](./1776260757283.png)****

![1776260814914.png](./1776260814914.png)

[OpenAI Models](https://developers.openai.com/api/docs/modelshttps://)

![1776261601248.png](./1776261601248.png)

#### 3.5.4 Gemini



![1776264998797.png](./1776264998797.png)

### 3.6 Message Types

![1776265312028.png](./1776265312028.png)

![1776267895497.png](./1776267895497.png)

![1776268495732.png](./1776268495732.png)

[Adding a System Message to Groq](https://console.groq.com/playgroundhttps://)

![1776268821290.png](./1776268821290.png)

### 3.7 LLM Parameters

![1776269156247.png](./1776269156247.png)

![1776270468357.png](./1776270468357.png)

![1776270608661.png](./1776270608661.png)

![1776270848195.png](./1776270848195.png)

Using Groq playground to change the model parameters.


![1776271024282.png](./1776271024282.png)
63 changes: 63 additions & 0 deletions 010_course_material/04_llm_types_and_variants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generative AI with Python

## 4. LLM - Types and Variants

### 4.1 Local Use of Models

![1776278354938.png](./1776278354938.png)

![1776278368194.png](./1776278368194.png)

### 4.2 Large Multimodal Models

![1776279206115.png](./1776279206115.png)

![1776279399577.png](./1776279399577.png)

### 4.3 Large Video Models

![1776678626780.png](./1776678626780.png)

![1776678670330.png](./1776678670330.png)

![1776678702041.png](./1776678702041.png)

### 4.4 Tokenization

![1776678737727.png](./1776678737727.png)

![1776678775902.png](./1776678775902.png)

![1776678827341.png](./1776678827341.png)

Sub-word Tokenization

![1776678889441.png](./1776678889441.png)

[Open AI Tokenizer](https://platform.openai.com/tokenizerhttps://)

![1776678982087.png](./1776678982087.png)

### 4.5 Reasoning Models

In the case of reasoning models, we can see that the model is generating intermediate steps using chain of thoughts and then it uses several tokens to develop the logic, before generating the output. There is a possibility that the number of tokens it uses in generating the logic ends up consuming the tokens, hence the context window, so the output needs to be truncated.

![1776679249642.png](./1776679249642.png)

![1776679330183.png](./1776679330183.png)

### 4.6 Small Language Models

SLMs may be able to compete with LLMs because they are trained on very well curated datasets. They spend less time in training, but they tend to spend more time in inferences.

![1776679622848.png](./1776679622848.png)

### 4.7 Jailbreaking

This uses a technique called **Art Prompt**

![1776679784309.png](./1776679784309.png)

This uses a technique called **Math Prompt**

![1776679880161.png](./1776679880161.png)
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file added 010_course_material/1776035231939.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776035369175.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776035383359.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776035515821.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776035778454.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776036637966.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776036693109.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776036875089.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776036879224.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776036893209.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776254636958.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776254663109.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776255870273.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260180511.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260183492.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260430717.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260488538.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260542065.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260557979.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260757283.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776260814914.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776261601248.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776264998797.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776265312028.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776267895497.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 010_course_material/1776268495732.png
Binary file added 010_course_material/1776268821290.png
Binary file added 010_course_material/1776269156247.png
Binary file added 010_course_material/1776270468357.png
Binary file added 010_course_material/1776270608661.png
Binary file added 010_course_material/1776270848195.png
Binary file added 010_course_material/1776271024282.png
Binary file added 010_course_material/1776271082713.png
Binary file added 010_course_material/1776271087697.png
Binary file added 010_course_material/1776271145448.png
Binary file added 010_course_material/1776273124425.png
Binary file added 010_course_material/1776273213888.png
Binary file added 010_course_material/1776273327240.png
Binary file added 010_course_material/1776273498200.png
Binary file added 010_course_material/1776273524991.png
Binary file added 010_course_material/1776278354938.png
Binary file added 010_course_material/1776278368194.png
Binary file added 010_course_material/1776279206115.png
Binary file added 010_course_material/1776279399577.png
Binary file added 010_course_material/1776678626780.png
Binary file added 010_course_material/1776678670330.png
Binary file added 010_course_material/1776678702041.png
Binary file added 010_course_material/1776678737727.png
Binary file added 010_course_material/1776678775902.png
Binary file added 010_course_material/1776678827341.png
Binary file added 010_course_material/1776678889441.png
Binary file added 010_course_material/1776678982087.png
Binary file added 010_course_material/1776679249642.png
Binary file added 010_course_material/1776679330183.png
Binary file added 010_course_material/1776679622848.png
Binary file added 010_course_material/1776679784309.png
Binary file added 010_course_material/1776679880161.png
Binary file added 010_course_material/images/1770304750827.png
Binary file added 010_course_material/images/1770317842758.png
Binary file added 010_course_material/images/1770318020099.png
Binary file added 010_course_material/images/1770389187224.png
Binary file added 010_course_material/images/1770389217431.png
Binary file added 010_course_material/images/1770389507588.png
Binary file added 010_course_material/images/1770389567212.png
Binary file added 010_course_material/images/1770389939080.png
Binary file added 010_course_material/images/1770389959448.png
Binary file added 010_course_material/images/1770390278773.png
Binary file added 010_course_material/images/1770391230138.png
Binary file added 010_course_material/images/G.jpg
28 changes: 24 additions & 4 deletions 020_llm/28a_start_model_chat_groq.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
#%% packages
import os
from groq import Groq
from langchain_groq import ChatGroq
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(usecwd = True))
# %%
# %%
api_key = api_key=os.getenv("GROQ_API_KEY")
# %%
# Fetches models available on Groq's LPU inference engine
client = Groq(api_key=api_key)
response = client.models.list()

print("--- Groq Models ---")
for model in response.data:
# Groq provides additional metadata like 'context_window'
print(f"ID: {model.id:30} | Developer: {model.owned_by}")

# %%
# Model overview: https://console.groq.com/docs/models


MODEL_NAME = "llama-3.3-70b-versatile"
model = ChatGroq(model=MODEL_NAME,
temperature=0.7,
api_key=api_key)
# %% Run the model

res = model.invoke("What is the capital of France?")
# %% find out what is in the result

res.model_dump()
# %% only print content

res.content
# %%
18 changes: 16 additions & 2 deletions 020_llm/28b_start_model_chat_openai.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
#%% packages
import os
from dotenv import load_dotenv, find_dotenv
from langchain_openai import ChatOpenAI
load_dotenv(find_dotenv(usecwd=True))
from openai import OpenAI
#TODO: add package import
# %%
# Retrieve API key from environment variable
api_key=os.getenv("OPENAI_API_KEY")

# %%
# Model overview: https://console.groq.com/docs/models
MODEL_NAME = 'gpt-4o-mini'
#TODO: add the model


model = ChatOpenAI(model=MODEL_NAME,
temperature=0.7,
api_key=api_key)
# %% Run the model
res = model.invoke("What is a Generative AI?")
# %% find out what is in the result
res.model_dump()
# %% only print content
print(res.content)
# %%
client = OpenAI(api_key=api_key)
models = client.models.list()

print("--- OpenAI Models ---")
for model in models:
print(f"Model ID: {model.id} | Owned By: {model.owned_by}")
# %%
15 changes: 14 additions & 1 deletion 020_llm/28c_start_model_chat_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv(usecwd=True))
# TODO: add package import
import google.generativeai as genai
from langchain_google_genai import ChatGoogleGenerativeAI
# %%
api_key = api_key=os.getenv("GOOGLE_API_KEY")
# %%
# List all models
genai.configure(api_key=api_key)
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)

# %%
# Model overview: https://ai.google.dev/models/gemini
MODEL_NAME = 'gemini-1.5-flash'
MODEL_NAME = 'gemini-2.5-flash'
# TODO: add the model

model = ChatGoogleGenerativeAI(model=MODEL_NAME,
temperature=0.7,
api_key=api_key)
# %% Run the model
res = model.invoke("What is a Generative AI?")
# %% find out what is in the result
Expand Down
6 changes: 5 additions & 1 deletion 020_llm/45_start_local_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
# !ollama pull qwen3:4b

# %% model setup
model = ChatOllama(model="qwen3:4b", temperature=0.2, extract_reasoning=True)
model = ChatOllama(model="deepseek-r1:latest",
temperature=0.2,
extract_reasoning=True)
# %% invoke model
res = model.invoke("What is Ollama?")
# %%
res.model_dump()
# %% only print content
print(res.content)
# %%
47 changes: 45 additions & 2 deletions 020_llm/47_start_large_multimodal_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
# source: https://console.groq.com/docs/vision

#%% packages

from groq import Groq
import base64
import os

#%% Function to encode the image / model invocation
def encode_image(image_path):
"""
This function reads an image file and encodes it in base64 format,
which is suitable for sending to the Groq API. The encoded string
can then be included in the request payload when invoking the model.
"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")

# image_path = "rag_workflow.png"
image_path = "../images/G.jpg" # Path to your image
user_question = "How many documents are shown in this image?"
base64_image = encode_image(image_path)
# %%
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("GROQ_API_KEY not found in environment variables.")

client = Groq(api_key=api_key)
try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": user_question},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
},
},
],
},
],
model="meta-llama/llama-4-scout-17b-16e-instruct",
)

#%% check the response
except Exception as e:
print(f"An error occurred: {e}")

# %%
print("Model Output:\n")
print(chat_completion.choices[0].message.content)
# %%