Commit 74c3db9f authored by jbleher's avatar jbleher
Browse files

Slides added

parent 23694b6c
Loading
Loading
Loading
Loading
+127 −102
Original line number Diff line number Diff line
%% Cell type:markdown id:eb416ca5 tags:

# MNIST Neural Network Implementation in Python

%% Cell type:markdown id:4186ae33 tags:

You have a dataset containing handwritten digits from the MNIST dataset. Each digit is represented as a vector of 784 pixel values (originally a 28×28 image) ranging from 0 (white) to 255 (black).

Your goal is to construct a simple neural network to classify these images.

%% Cell type:markdown id:35c57ea6 tags:

<p style="float: left; margin: 20px 10px 10px 20px;padding-right:20px;">
<img src="neuronal_network.png" alt="Strcture of the Neuronal Network" width="550"/>
</p>
The network structure is:

- **Input Layer:** 784 neurons (pixels)
- **Hidden Layer:** 10 neurons using the ReLU activation function
- ReLU function: $ \operatorname{ReLU}(z) = \max(0, z) $
- The input layer has dimensions $ 784 \times n $, where $n$ is the number of observations.

- The first layer (input to hidden) computation:
$$
\mathbf{A}_1 = \operatorname{ReLU}\left(\mathbf{W}_1 \mathbf{A}_0 + \mathbf{b}_1\right)
$$
where $\mathbf{W}_1$ is a $10 \times 784$ weight matrix, and $\mathbf{b}_1$ is a $10 \times 1$ bias vector.

- The second layer (hidden to output) uses the Softmax activation function:
$$
\mathbf{A}_2 = \text{softmax}(\mathbf{Z}_2) = \text{softmax}(\mathbf{W}_2 \mathbf{A}_1 + \mathbf{b}_2)
$$

where $\mathbf{W}_2$ is a $10 \times 10$ matrix, $\mathbf{b}_2$ a $10 \times 1$ vector, and the Softmax function is defined as:
$$
\sigma(\mathbf{z})_i = \frac{\exp(z_i)}{\sum_i \exp(z_i)}.
$$

- The output layer represents the predicted digit (0–9) as the neuron with the highest score.

%% Cell type:markdown id:ecc8ba2a tags:

## Training Procedure

Use gradient descent and backpropagation to update parameters. Apply the following update rules with a learning rate $\alpha = 0.1$:
$$
\begin{align}
\mathbf{W}_1 &:= \mathbf{W}_1 - \alpha\, \mathrm{d}\mathbf{W}_1, \\
\mathbf{b}_1 &:= \mathbf{b}_1 - \alpha\, \mathrm{d}\mathbf{b}_1, \\
\mathbf{W}_2 &:= \mathbf{W}_2 - \alpha\, \mathrm{d}\mathbf{W}_2, \\
\mathbf{b}_2 &:=\mathbf{b}_2 - \alpha\, \mathrm{d}\mathbf{b}_2
\end{align}
$$
Compute the gradients using backpropagation:
$$
\begin{align}
\mathrm{d}\mathbf{Z}_2 = \mathbf{A}_2 - \mathbf{Y}, \\
 \mathrm{d}\mathbf{W}_2 = \frac{1}{n} \mathrm{d}\mathbf{Z}_2 \mathbf{A}_1^\prime, \\
 \mathrm{d}\mathbf{b}_2 = \frac{1}{n} \mathrm{d}\mathbf{Z}_2 \mathbf{1}_m, \\
\mathrm{d}\mathbf{Z}_1 = \mathbf{W}_2^\prime \mathrm{d}\mathbf{Z}_2 \odot \operatorname{ReLU}^\prime(\mathbf{Z}_1),  \\
 \mathrm{d}\mathbf{W}_1 = \frac{1}{n} \mathrm{d}\mathbf{Z}_1 \mathbf{X}^\prime, \\
 \mathrm{d}\mathbf{b}_1 = \frac{1}{n} \mathrm{d}\mathbf{Z}_1 \mathbf{1}_m
 \end{align}
$$

%% Cell type:markdown id:ce0ceda6 tags:

## Import Required Libraries
We start by importing necessary Python libraries for numerical computation, data handling, and visualization.

%% Cell type:code id:6c7a038a tags:

``` python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pickle
```

%% Cell type:markdown id:af55e379 tags:

## Load Data
We load the MNIST dataset, which contains images of handwritten digits. Each image is represented by 784 pixel values.

%% Cell type:code id:012469fa tags:

``` python
mnist = pd.read_pickle("mnist.pkl.gz")
```

%% Cell type:markdown id:e871a22c tags:

## Inspect Data

Let's have a look at the data and plot the 28x28 pixel images.

%% Cell type:code id:ee0507b2 tags:

``` python
# Select a sample image from the dataset
sample_index = 0  # you can change this index to view a different image
# Get the pixel values, reshape into 28x28 (assuming the DataFrame's non-label columns are ordered correctly)
sample_image = mnist.drop("Label", axis=1).iloc[sample_index].values.reshape(28, 28)

# Plot the image
plt.imshow(sample_image, cmap='gray')
plt.title(f"Digit: {mnist['Label'].iloc[sample_index]}")
plt.axis('off')  # optional: hides the axis for a cleaner view
plt.show()
```

%% Output


%% Cell type:markdown id:a696fbf0 tags:

## Data Preprocessing
Normalize pixel values to the range [0, 1] and convert labels to one-hot encoding.

%% Cell type:code id:a044995f tags:

``` python
X = mnist.drop("Label", axis=1).values / 255
y = pd.get_dummies(mnist["Label"]).values
```

%% Cell type:markdown id:6c88849b tags:

## The Task
- Split the data into training and test sets.
- Define the activation functions and the necessary derivatives.
- Define functions for forward and backpropagation.
- Train your neural network using gradient descent for at least 500 iterations.
- Output the accuracy every 5th iteration.
- Use learning rate $\alpha = 0.1$.

%% Cell type:markdown id:c136ef0c tags:

## Split Data
Split the dataset into training and test sets to evaluate the model performance on unseen data.

%% Cell type:code id:aa452588 tags:

``` python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```

%% Cell type:markdown id:872f6ede tags:

## Activation Functions
Define the ReLU activation function and its derivative for the hidden layer, and the softmax function for the output layer.

%% Cell type:code id:46bdd911 tags:

``` python
def ReLU(Z):
    return np.maximum(0, Z)

def ReLU_derivative(Z):
    return Z > 0

def softmax(Z):
    expZ = np.exp(Z - np.max(Z, axis=0, keepdims=True))
    return expZ / np.sum(expZ, axis=0, keepdims=True)
```

%% Cell type:markdown id:34fee4cc tags:

## Initialize Network Parameters
Initialize weights and biases with small random values.

%% Cell type:code id:6e971e6c tags:

``` python
def init_params():
    W1 = np.random.randn(10, 784) * 0.01
    b1 = np.zeros((10, 1))
    W2 = np.random.randn(10, 10) * 0.01
    b2 = np.zeros((10, 1))
    return W1, b1, W2, b2
```

%% Cell type:markdown id:3168052c tags:

## Forward Propagation
Define the forward propagation function, which calculates outputs based on current network parameters.

%% Cell type:code id:2b1b6b20 tags:

``` python
def forward_prop(W1, b1, W2, b2, X):
    Z1 = W1.dot(X) + b1
    A1 = ReLU(Z1)
    Z2 = W2.dot(A1) + b2
    A2 = softmax(Z2)
    return Z1, A1, Z2, A2
```

%% Cell type:markdown id:8bf20d02 tags:

## Backward Propagation
Calculate gradients using backpropagation to update parameters.

%% Cell type:code id:8864edb3 tags:

``` python
def back_prop(Z1, A1, A2, W2, X, Y):
    m = X.shape[1]
    dZ2 = A2 - Y
    dW2 = (1/m) * dZ2.dot(A1.T)
    db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = W2.T.dot(dZ2) * ReLU_derivative(Z1)
    dW1 = (1/m) * dZ1.dot(X.T)
    db1 = (1/m) * np.sum(dZ1, axis=1, keepdims=True)
    return dW1, db1, dW2, db2
```

%% Cell type:markdown id:73101d4c tags:

## Accuracy Calculation
Define a function to measure the accuracy of predictions.

%% Cell type:code id:5a4cf2ea tags:

``` python
def accuracy(W1, b1, W2, b2, X, Y):
    _, _, _, A2 = forward_prop(W1, b1, W2, b2, X)
    predictions = np.argmax(A2, axis=0)
    labels = np.argmax(Y, axis=0)
    return np.mean(predictions == labels)
```

%% Cell type:markdown id:cbd9156e tags:

## Model Training
Train the neural network using gradient descent and print accuracy every 5 iterations.

%% Cell type:code id:aa6f9c74 tags:

``` python
W1, b1, W2, b2 = init_params()
X_train, X_test, y_train, y_test = X_train.T, X_test.T, y_train.T, y_test.T

for i in range(501):
    Z1, A1, Z2, A2 = forward_prop(W1, b1, W2, b2, X_train)
    dW1, db1, dW2, db2 = back_prop(Z1, A1, A2, W2, X_train, y_train)

    W1 -= 0.1 * dW1
    b1 -= 0.1 * db1
    W2 -= 0.1 * dW2
    b2 -= 0.1 * db2

    if i % 5 == 0:
        acc = accuracy(W1, b1, W2, b2, X_train, y_train)
        print(f"Iteration {i}, Training-Accuracy: {acc:.4f}")

print("Final accuracy on test dataset:", accuracy(W1, b1, W2, b2, X_test, y_test))
```

%% Output

    Iteration 0, Training-Accuracy: 0.1595
    Iteration 5, Training-Accuracy: 0.1390
    Iteration 10, Training-Accuracy: 0.1226
    Iteration 15, Training-Accuracy: 0.1331
    Iteration 20, Training-Accuracy: 0.1758
    Iteration 25, Training-Accuracy: 0.2520
    Iteration 30, Training-Accuracy: 0.3235
    Iteration 35, Training-Accuracy: 0.3579
    Iteration 40, Training-Accuracy: 0.3653
    Iteration 45, Training-Accuracy: 0.3570
    Iteration 50, Training-Accuracy: 0.3435
    Iteration 55, Training-Accuracy: 0.3316
    Iteration 60, Training-Accuracy: 0.3236
    Iteration 65, Training-Accuracy: 0.3202
    Iteration 70, Training-Accuracy: 0.3251
    Iteration 75, Training-Accuracy: 0.3408
    Iteration 80, Training-Accuracy: 0.3614
    Iteration 85, Training-Accuracy: 0.3876
    Iteration 90, Training-Accuracy: 0.4244
    Iteration 95, Training-Accuracy: 0.4846
    Iteration 100, Training-Accuracy: 0.5282
    Iteration 105, Training-Accuracy: 0.5669
    Iteration 110, Training-Accuracy: 0.6061
    Iteration 115, Training-Accuracy: 0.6414
    Iteration 120, Training-Accuracy: 0.6689
    Iteration 125, Training-Accuracy: 0.6880
    Iteration 130, Training-Accuracy: 0.7020
    Iteration 135, Training-Accuracy: 0.7146
    Iteration 140, Training-Accuracy: 0.7268
    Iteration 145, Training-Accuracy: 0.7359
    Iteration 150, Training-Accuracy: 0.7452
    Iteration 155, Training-Accuracy: 0.7529
    Iteration 160, Training-Accuracy: 0.7600
    Iteration 165, Training-Accuracy: 0.7663
    Iteration 170, Training-Accuracy: 0.7720
    Iteration 175, Training-Accuracy: 0.7775
    Iteration 180, Training-Accuracy: 0.7823
    Iteration 185, Training-Accuracy: 0.7865
    Iteration 190, Training-Accuracy: 0.7914
    Iteration 195, Training-Accuracy: 0.7951
    Iteration 200, Training-Accuracy: 0.7987
    Iteration 205, Training-Accuracy: 0.8022
    Iteration 210, Training-Accuracy: 0.8058
    Iteration 215, Training-Accuracy: 0.8088
    Iteration 220, Training-Accuracy: 0.8116
    Iteration 225, Training-Accuracy: 0.8144
    Iteration 230, Training-Accuracy: 0.8178
    Iteration 235, Training-Accuracy: 0.8206
    Iteration 240, Training-Accuracy: 0.8236
    Iteration 245, Training-Accuracy: 0.8261
    Iteration 250, Training-Accuracy: 0.8283
    Iteration 255, Training-Accuracy: 0.8310
    Iteration 260, Training-Accuracy: 0.8338
    Iteration 265, Training-Accuracy: 0.8364
    Iteration 270, Training-Accuracy: 0.8384
    Iteration 275, Training-Accuracy: 0.8405
    Iteration 280, Training-Accuracy: 0.8429
    Iteration 285, Training-Accuracy: 0.8451
    Iteration 290, Training-Accuracy: 0.8473
    Iteration 295, Training-Accuracy: 0.8494
    Iteration 300, Training-Accuracy: 0.8514
    Iteration 305, Training-Accuracy: 0.8532
    Iteration 310, Training-Accuracy: 0.8550
    Iteration 315, Training-Accuracy: 0.8562
    Iteration 320, Training-Accuracy: 0.8579
    Iteration 325, Training-Accuracy: 0.8594
    Iteration 330, Training-Accuracy: 0.8610
    Iteration 335, Training-Accuracy: 0.8623
    Iteration 340, Training-Accuracy: 0.8636
    Iteration 345, Training-Accuracy: 0.8648
    Iteration 350, Training-Accuracy: 0.8662
    Iteration 355, Training-Accuracy: 0.8674
    Iteration 360, Training-Accuracy: 0.8687
    Iteration 365, Training-Accuracy: 0.8697
    Iteration 370, Training-Accuracy: 0.8707
    Iteration 375, Training-Accuracy: 0.8715
    Iteration 380, Training-Accuracy: 0.8726
    Iteration 385, Training-Accuracy: 0.8734
    Iteration 390, Training-Accuracy: 0.8744
    Iteration 395, Training-Accuracy: 0.8753
    Iteration 400, Training-Accuracy: 0.8761
    Iteration 405, Training-Accuracy: 0.8769
    Iteration 410, Training-Accuracy: 0.8779
    Iteration 415, Training-Accuracy: 0.8786
    Iteration 420, Training-Accuracy: 0.8790
    Iteration 425, Training-Accuracy: 0.8799
    Iteration 430, Training-Accuracy: 0.8807
    Iteration 435, Training-Accuracy: 0.8815
    Iteration 440, Training-Accuracy: 0.8820
    Iteration 445, Training-Accuracy: 0.8825
    Iteration 450, Training-Accuracy: 0.8831
    Iteration 455, Training-Accuracy: 0.8837
    Iteration 460, Training-Accuracy: 0.8840
    Iteration 465, Training-Accuracy: 0.8849
    Iteration 470, Training-Accuracy: 0.8854
    Iteration 475, Training-Accuracy: 0.8861
    Iteration 480, Training-Accuracy: 0.8864
    Iteration 485, Training-Accuracy: 0.8869
    Iteration 490, Training-Accuracy: 0.8875
    Iteration 495, Training-Accuracy: 0.8881
    Iteration 500, Training-Accuracy: 0.8888
    Final accuracy on test dataset: 0.8870714285714286
    Iteration 0, Training-Accuracy: 0.0954
    Iteration 5, Training-Accuracy: 0.1324
    Iteration 10, Training-Accuracy: 0.1149
    Iteration 15, Training-Accuracy: 0.1146
    Iteration 20, Training-Accuracy: 0.1193
    Iteration 25, Training-Accuracy: 0.1353
    Iteration 30, Training-Accuracy: 0.1704
    Iteration 35, Training-Accuracy: 0.2402
    Iteration 40, Training-Accuracy: 0.3256
    Iteration 45, Training-Accuracy: 0.4185
    Iteration 50, Training-Accuracy: 0.4374
    Iteration 55, Training-Accuracy: 0.4296
    Iteration 60, Training-Accuracy: 0.4184
    Iteration 65, Training-Accuracy: 0.4047
    Iteration 70, Training-Accuracy: 0.3947
    Iteration 75, Training-Accuracy: 0.3954
    Iteration 80, Training-Accuracy: 0.4077
    Iteration 85, Training-Accuracy: 0.4245
    Iteration 90, Training-Accuracy: 0.4447
    Iteration 95, Training-Accuracy: 0.4726
    Iteration 100, Training-Accuracy: 0.5153
    Iteration 105, Training-Accuracy: 0.5624
    Iteration 110, Training-Accuracy: 0.6006
    Iteration 115, Training-Accuracy: 0.6288
    Iteration 120, Training-Accuracy: 0.6505
    Iteration 125, Training-Accuracy: 0.6677
    Iteration 130, Training-Accuracy: 0.6819
    Iteration 135, Training-Accuracy: 0.6951
    Iteration 140, Training-Accuracy: 0.7054
    Iteration 145, Training-Accuracy: 0.7159
    Iteration 150, Training-Accuracy: 0.7272
    Iteration 155, Training-Accuracy: 0.7365
    Iteration 160, Training-Accuracy: 0.7459
    Iteration 165, Training-Accuracy: 0.7536
    Iteration 170, Training-Accuracy: 0.7617
    Iteration 175, Training-Accuracy: 0.7699
    Iteration 180, Training-Accuracy: 0.7772
    Iteration 185, Training-Accuracy: 0.7833
    Iteration 190, Training-Accuracy: 0.7894
    Iteration 195, Training-Accuracy: 0.7947
    Iteration 200, Training-Accuracy: 0.8002
    Iteration 205, Training-Accuracy: 0.8050
    Iteration 210, Training-Accuracy: 0.8099
    Iteration 215, Training-Accuracy: 0.8142
    Iteration 220, Training-Accuracy: 0.8178
    Iteration 225, Training-Accuracy: 0.8214
    Iteration 230, Training-Accuracy: 0.8244
    Iteration 235, Training-Accuracy: 0.8279
    Iteration 240, Training-Accuracy: 0.8307
    Iteration 245, Training-Accuracy: 0.8337
    Iteration 250, Training-Accuracy: 0.8363
    Iteration 255, Training-Accuracy: 0.8388
    Iteration 260, Training-Accuracy: 0.8411
    Iteration 265, Training-Accuracy: 0.8432
    Iteration 270, Training-Accuracy: 0.8456
    Iteration 275, Training-Accuracy: 0.8479
    Iteration 280, Training-Accuracy: 0.8499
    Iteration 285, Training-Accuracy: 0.8520
    Iteration 290, Training-Accuracy: 0.8539
    Iteration 295, Training-Accuracy: 0.8557
    Iteration 300, Training-Accuracy: 0.8573
    Iteration 305, Training-Accuracy: 0.8592
    Iteration 310, Training-Accuracy: 0.8608
    Iteration 315, Training-Accuracy: 0.8620
    Iteration 320, Training-Accuracy: 0.8639
    Iteration 325, Training-Accuracy: 0.8655
    Iteration 330, Training-Accuracy: 0.8668
    Iteration 335, Training-Accuracy: 0.8679
    Iteration 340, Training-Accuracy: 0.8691
    Iteration 345, Training-Accuracy: 0.8704
    Iteration 350, Training-Accuracy: 0.8715
    Iteration 355, Training-Accuracy: 0.8728
    Iteration 360, Training-Accuracy: 0.8738
    Iteration 365, Training-Accuracy: 0.8747
    Iteration 370, Training-Accuracy: 0.8756
    Iteration 375, Training-Accuracy: 0.8765
    Iteration 380, Training-Accuracy: 0.8775
    Iteration 385, Training-Accuracy: 0.8784
    Iteration 390, Training-Accuracy: 0.8792
    Iteration 395, Training-Accuracy: 0.8799
    Iteration 400, Training-Accuracy: 0.8807
    Iteration 405, Training-Accuracy: 0.8813
    Iteration 410, Training-Accuracy: 0.8821
    Iteration 415, Training-Accuracy: 0.8831
    Iteration 420, Training-Accuracy: 0.8835
    Iteration 425, Training-Accuracy: 0.8840
    Iteration 430, Training-Accuracy: 0.8846
    Iteration 435, Training-Accuracy: 0.8852
    Iteration 440, Training-Accuracy: 0.8857
    Iteration 445, Training-Accuracy: 0.8865
    Iteration 450, Training-Accuracy: 0.8872
    Iteration 455, Training-Accuracy: 0.8880
    Iteration 460, Training-Accuracy: 0.8888
    Iteration 465, Training-Accuracy: 0.8895
    Iteration 470, Training-Accuracy: 0.8901
    Iteration 475, Training-Accuracy: 0.8905
    Iteration 480, Training-Accuracy: 0.8910
    Iteration 485, Training-Accuracy: 0.8916
    Iteration 490, Training-Accuracy: 0.8920
    Iteration 495, Training-Accuracy: 0.8923
    Iteration 500, Training-Accuracy: 0.8927
    Final accuracy on test dataset: 0.8913571428571428
+156 −148

File changed.

Preview size limit exceeded, changes collapsed.

+1 −2
Original line number Diff line number Diff line
%% Cell type:markdown id:0a23ac74-2509-4920-a88a-5975d05074a3 tags:

# Simple Retrieval Augmented Generation

In this first notebook, we will build a simple retrieval augmented generation sytem.
For this purpose, we will get a Wikipedia page as context, and feed this to a LLM.
The LLM will be mistral and is running on the AIDAHO server aidaho-edu.uni-hohenheim.de/ollama

%% Cell type:markdown id:0f877f1d-38a0-4135-b798-57032665d0fa tags:

## The modules

%% Cell type:code id:23bae9fd-cc5c-46cc-b503-9d025f42b158 tags:

``` python
from langchain_community.llms import Ollama               # This module if for talking to LLMs
from langchain_community.vectorstores import Chroma       # This module delivers the vector database
from langchain_core.output_parsers import StrOutputParser # This module allows to parse the output from the LLM (string converstion, error handling,...)
from langchain_core.prompts import PromptTemplate         # This module is used to build prompt templates (placeholder management).
from langchain_core.runnables import RunnablePassthrough  # Handy tool, to just passthrough text
import requests                                           # A module to make HTTP requets
from bs4 import BeautifulSoup                             # A module to parse through HTML code.
```

%% Cell type:markdown id:1e9450a5-b36d-43c7-b682-1bc5b1792ecc tags:

## Get web content

1. Use the get function from the requests package to retrieve the url: "https://en.wikipedia.org/wiki/Bukidnon_State_University" store the result in an object called response
2. Use the function BeaufifulSoup to parse the content object from the response. Use the "html.parser". Call the result soup.
3. Invoke the function find on soup. Find the "div" with  {"class":"mw-body-content"}.
4. Extract the text into different strigns. Use a blank space as the seperator.
5. Wrap everything in a function called *fetch_web_content(url)*.

%% Cell type:code id:866611e7-d6ef-4952-97a4-59a3cfa51ab8 tags:

``` python
def fetch_web_content(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")

    text_content = soup.find("div", {"class":"mw-body-content"}).get_text(separator=" ")
    return(text_content)
```

%% Cell type:markdown id:51bb7d3e-f619-43c1-a5a3-338df35a407d tags:

## Preprocess web content
1. In the result from fetch_web_content replace line brakes "\n with a blank.
2. Use the function strip to remove leading and trailing whitespaces.

%% Cell type:code id:73ff5f8f-6754-4d60-9547-0288e592a232 tags:

``` python
def preprocess_content(content):
    processed_content = content.replace("\n", ' ').strip()
    return processed_content

```

%% Cell type:markdown id:f032e270-344f-4116-8e8e-cf9c8f7b3893 tags:

## Wrap Wikipedia Content
Write a function that has no argument and just uses the url "https://en.wikipedia.org/wiki/Bukidnon_State_University" to call the *fetch_web_content* function from above and then invokes the *preprocess_content* on the result.
The resulting object should be returned by the function

%% Cell type:code id:468b2b91-5426-46f3-9736-0a899bed4133 tags:

``` python
def get_wikipedia_data():
    url = "https://en.wikipedia.org/wiki/Bukidnon_State_University"
    return preprocess_content(fetch_web_content(url))
```

%% Cell type:markdown id:0342f171-212d-4cc7-8924-4cf3e701ab3b tags:

## Baseprompt
Write a base brompt that uses the placeholder {context}, {question}.

%% Cell type:code id:03e284f9-b222-4afb-847c-102ba9a1f099 tags:

``` python
prompt = PromptTemplate.from_template(
    """You are ab AI assistant who answes the questions with the provided context.
    """You are an AI assistant who answers the questions with the provided context.

    Context:
    {context}

    Question:
    {question}

    Answer:
    """)
```

%% Cell type:markdown id:93c7c6af-361f-4d9e-a48b-987b14d6d57e tags:

## Setup the LLM
We will use a mistral instance hosted on the AIDAHO servers https://aidaho-edu.uni-hohenheim.de/ollama

%% Cell type:code id:eede178b-6147-4510-b6e3-c5c96779f115 tags:

``` python
llm = Ollama(
    base_url="https://aidaho-edu.uni-hohenheim.de/ollama",
    model="mistral"
    )
```

%% Output

    /tmp/ipykernel_24488/3516312034.py:1: LangChainDeprecationWarning: The class `Ollama` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0. An updated version of the class exists in the :class:`~langchain-ollama package and should be used instead. To use it run `pip install -U :class:`~langchain-ollama` and import as `from :class:`~langchain_ollama import OllamaLLM``.
      llm = Ollama(

%% Cell type:markdown id:2fc5d826-75a7-439e-a34a-947051b572e5 tags:

## Make the chain

A chain in Langchain can be defined by concatenating the elements with a "|".

1. A dictionary with context and question will be needed. This will be fed into the prompt which again is fed into the llm. With the function StrOutputParser() the result is parsed. This entire chain can be stored into a chain object.
2. Define your query.
3.

%% Cell type:code id:bc251b61-a5f2-4f58-b88b-62baab9deebc tags:

``` python
chain = {"context": lambda q: get_wikipedia_data(), "question": RunnablePassthrough()} | prompt | llm | StrOutputParser()

query = "Could you tell me something about the BSU a really enthusiastic way?"

print(chain.invoke(query))
```

%% Output

     Absolutely! Let me take you on an exciting journey through the heart of Mindanao, to the remarkable institution known as Bukidnon State University (BSU)! Nestled in the lush landscapes and vibrant culture of Bukidnon Province, this university is not just a place of learning, but a melting pot of ideas, innovation, and community spirit.
    
       With its rich history tracing back to 1978, BSU has been consistently shaping the minds of students, fostering their creativity, and nurturing their passions. It is a dynamic hub where science meets art, technology merges with tradition, and global perspectives intertwine with local wisdom.
    
       From its humble beginnings as a College of Arts and Sciences, BSU has expanded into various fields, offering over 50 undergraduate courses and 18 graduate programs across nine colleges. Whether you're interested in agriculture, business, education, engineering, or nursing, BSU promises a comprehensive learning experience tailored to your ambitions.
    
       But Bukidnon State University is more than just academics – it's about experiences that last a lifetime! Engage in various student organizations, participate in community outreach programs, and make memories that will stay with you forever. The university boasts state-of-the-art facilities, modern classrooms, and a serene campus that provides an ideal environment for personal growth and intellectual exploration.
    
       So if you're seeking a university that combines academic excellence with cultural richness, Bukidnon State University is the perfect choice! Join us at BSU, where dreams take flight, friendships blossom, and possibilities are endless!

%% Cell type:code id:47824947-cf42-4f92-b468-b1737d7284b5 tags:

``` python
chain = {"context": lambda q: get_wikipedia_data(), "question": RunnablePassthrough()} | prompt

print(chain.invoke(query))
```

%% Output

    text='You are ab AI assistant who answes the questions with the provided context.\n    \n    Context:\n    Public university in Bukidnon, Philippines       Bukidnon State University Pamantasang Pampamahalaan ng Bukidnon \xa0 ( Filipino ) Kinatumhaang Panggamhanan sa Bukidnon \xa0 ( Cebuano ) Former names Bukidnon Provincial High School (1924–1929) Bukidnon Normal School (1929–1976) Bukidnon State College (1976–2007) Motto Educate. Innovate. Lead Type State university Established 1924 ; 101\xa0years ago \xa0( 1924 ) Affiliation AACCUP PASUC MASCUF Chairman Ronald Adamat President Joy Mirasol Vice-president Hazel Jean Abejuala ( Academic Affairs ) Carina Joane Barroso ( Research, Extension and Innovations ) Lincoln Tan ( Culture, Arts, Sports and Student Services ) Dante Victoria, Jr ( Administration and Finance ) Academic staff 650 Administrative staff 1500 Students 16,000+ Location Fortich St.,  Malaybalay ,  Bukidnon ,  Philippines 8°09′24″N   125°07′25″E \ufeff / \ufeff 8.1568°N 125.1236°E \ufeff /  8.1568; 125.1236 Campus 6.0 km² (BukSU Main Campus)    ( with several Extension and External Studies Center in different parts of Mindanao ) Colors \xa0  Navy Blue    \xa0  White Nickname BukSU Wild Cats [ a ] Website buksu .edu .ph Location in Mindanao Show map of Mindanao mainland Location in the Philippines Show map of Philippines   Bukidnon State University , abbreviated as  BSU  and colloquially referred to as  BukSU , is a provincial state university in  Malaybalay City ,  Bukidnon ,  Philippines . Formerly named Bukidnon State College, it became a university in 2007. The other university in the province of Bukidnon is  Central Mindanao University  in  Musuan ,  Maramag .      History [ edit ]   Former logo of Bukidnon State University, still used in some official documents and academic works   Bukidnon State University started as a two-year secondary school named Bukidnon Provincial High School in 1924. In 1928,  it offered a four-year secondary normal curriculum and was renamed Bukidnon Normal School (BNS). During that time, it  became one of the five full-fledged secondary normal schools in the country. The student population was 70 for the entire four-year secondary course. Although the curriculum is American in orientation, the majority of the students are native inhabitants of Malaybalay. [ 1 ] [ 2 ] [ 3 ] [ unreliable source? ]  Upon the authority of the Director of Education, William H. Pickell, who was an American school superintendent for Bukidnon, established the school. It was first set up to train elementary school teachers for the provinces of  Northern Mindanao  and the neighboring regions.    Bukidnon State University main entrance   Bukidnon State University   The school was closed on December 9, 1941, due to  World War II . It was reactivated on September 1, 1945, but since its facilities were completely damaged, classes were held in army tents at the provincial capitol grounds and in private homes. [ 4 ]  Rehabilitation work for the school\'s facilities lasted until the 1950s using the money from the war damage claims.  In 1952–1953, the secondary normal curriculum was phased out giving way to a two-year special education curriculum. The gradual elimination of the secondary education ended in 1956 with the full implementation of the two-year collegiate course. The first batch, comprising nine students, graduated the Bachelor of Science in Elementary Education in March 1957. In the same year, the two-year special curriculum was phased out and the school gained the status of a degree-granting institution. In the same year, a kindergarten school was also established.  In 1960, the graduate department was created, offering a master\'s degree in education to encourage teachers to take advanced studies. Ten years later, external graduate studies centers were organized in a number of provinces and cities outside of Bukidnon in cooperation with the school divisions of the Department of Education to extend the services of the college to working teachers wanted to enroll in courses leading to Master of Arts in education.  On December 15, 1961, Bukidnon Normal School was declared separate school division by virtue of Circular No. 33, s. 1961 issued by the Bureau of Public Schools, thus ending 37 years of administrative control by the division superintendent of schools for Bukidnon. In 1969, Master of Arts was offered in the school and in 1971, a Bachelor of Science in Secondary Education (BSSE) was offered. With the establishment of the BSE in Secondary Education, a high school laboratory, serving as its laboratory school, was established on the same year.  In 1969, Benjamin Tabios, former House Representative of  Bukidnon , filed House Bill 18779 proposing the change of the school\'s name from Bukidnon Normal School to Bukidnon Normal College, but was not enacted. A new bill sponsored by Cesar Fortich, House of Representative of Bukidnon, in 1972 converting the school to a state college—Bukidnon State College—although its conversion was approved, it was cancelled after the declaration of  Martial Law in the Philippines  on September 21, 1972.  On June 14, 1976, Presidential Decree No. 944 converted the Bukidnon Normal School into a chartered state college and changing its name to Bukidnon State College, [ 2 ] [ 5 ]  together with other schools in the country such as the Cebu Normal School, Leyte Normal School and Northern Luzon Teachers College into chartered state colleges. Jaime M. Gellor was appointed as the first president of the school. He served as president from 1976 to 1986.  The  EDSA Revolution  saw a change in the leadership of the school. The former president of the school was ousted and replaced by Godofredo L. Ycaro as officer-in-charge of the college. Teresita T. Tumapon was then inaugurated as the new president of the college in December 1986. During her time, linkages were opened for the school both in national and international academic circles. This period of time also saw the improvement of the school\'s infrastructure, educational facilities, equipment and technology. Foreign experts, brought about by the internal linkages of the college, were brought in the school improving the schools technology and instruction.  On February 1, 1999,  Victor M. Barroso , was then appointed as the new school president. The college has five schools offering undergraduate programs: School of Education, School of Arts and Sciences, School of Business Administration and Information Technology and the School of Graduate Studies. The School of Community Education and Industrial Technology and School of Nursing and the School of Law was later established.  On May 15, 2007, during the fiesta celebration of  Malaybalay ,  Gloria Macapagal Arroyo , President of the Philippines, signed Republic Act 9456  [ 2 ] [ 6 ]  converting Bukidnon State College into Bukidnon State University, with endorsement from the House of Representatives and the Senate. BSU formally inaugurated its university status on June 14, 2007.  The university has its own radio station DXBU 104.5. [ 7 ]   As of August 26, 2022, there are 16 campuses of the Bukidnon State University system. [ 8 ]   On July 23, 2024, the Advancing Research and Technological Innovation Fabrication Laboratory was established at Bukidnon State University. [ 9 ]  In December, BukSU - Secondary Education Department formed an  alliance  with the 4DTS  Diamond Division  in  teacher education . [ 10 ]     Secondary Laboratory School Annex Campus [ edit ]   In August 2024, the Secondary Laboratory School was relocated to Barangay Casisang Annex Campus for School Year 2024–2025, from its 50-year-old building. Founded in 1974, the SLS three-story new edifice features security equipment, a gymnasium and a solar power system. SLS Department Head Desiree A. Barroso announced its capacity for 387 junior and senior high school students. Board of Regents Resolution No. 2751, Series of 2024 duly authorized BukSU to retain its senior high school program in the SLS. [ 11 ]     Gallery [ edit ]         Mathematics And Natural Science Building beside College of Arts and Sciences Building.         The University Covered Court for Physical Education Course.         The Data Center in front of College of Education.         The University Gym.         The University Library.         The BSU-Elementary School Laboratory.         Going to BSU-Secondary School Laboratory.         The Guest House Building going to BukSU Valley.         The College of Law beside University Library         The BukSU-Elementary School laboratory Quadrangle.         Alubijid  Campus       See also [ edit ]   Bukidnon State University Chorale   Notes [ edit ]       ^   Main campus only       References [ edit ]       ^   "Bukidnon State College" . Archived from  the original  on November 29, 2014.     ^  a   b   c   Balane, Walter I. (June 2, 2007).  "Bukidnon State College, now a university" . MindaNews . Retrieved  October 9,  2008 . [ permanent dead link \u200d ]     ^   "Bukidnon" . Retrieved  October 9,  2008 .     ^   "Bukidnon State College" .  Manila Bulletin . June 23, 2006. Archived from  the original  on October 12, 2007 . Retrieved  October 7,  2008 .     ^   "Presidential Decree 944" . Philippine Government. June 14, 1976 . Retrieved  October 9,  2008 .     ^   "Republic Act 9456: An Act Converting Bukidnon State College into Bukidnon State University"   (PDF) . Philippine Government. February 19, 2007. Archived from  the original   (PDF)  on June 29, 2011 . Retrieved  October 7,  2008 .     ^   "DXBU 104.5 FM Engages Public through Isibya-Ipaambit 2016 - Bukidnon State University" .  Bukidnon State University . June 15, 2016 . Retrieved  October 16,  2018 .     ^   " \'Satellite campuses officially integrated in BukSU System\' " . August 26, 2022.     ^   Region 10 (July 30, 2024).  "DTI Buk turns over P4.6 million Fab Lab to Bukidnon State University" .  Department of Trade and Industry Philippines . Retrieved  July 31,  2024 . {{ cite web }} :  CS1 maint: numeric names: authors list ( link )     ^   "BukSU College of Education Partners with Philippine Army in Teacher Training" .  Bukidnon State University . January 6, 2024 . Retrieved  January 6,  2024 .     ^   "Secondary Laboratory School Relocates to New Building on Annex Campus" . buksu.edu.ph. August 5, 2024 . Retrieved  August 5,  2024 .       External links [ edit ]         Wikimedia Commons has media related to  Bukidnon State University .     Official website   A special place for teaching and learning   Manila Bulletin News   v t e   Philippine Association of State Universities and Colleges NCR   EARIST   MPC   PNU   PhilSCA   PUP   RTU   TUP   UP   Luzon   DMMMSU   UIP   MMSU   NLPSC   PangSU   UNP   UAbra   ASC   BSU   IFSU   KSU   MPSPC   BSC   CSU   ISU   NVSU   QSU   ASCOT   BPSU   BASC   BulSU   CLSU   DHVSU   NEUST   PSAU   PMMA   PRMSU   TAU   TSU   BatSU   CvSU   LSPU   SLSU   URS   MSU   MinSU   OMSC   PSU   RSU   WPU   BU   UCN   CBSUA   CatSU   DEBESMSCAT   ParSU   PSUB   SSU   SEAUTech   Visayas   ASU   CapSU   CHMSU   CPSU   GSU   ISAT-U   ISUFST   NISU   NONESCOST   UA   WVSU   BISU   CNU   CTU   NOrSU   SSC   BiPSU   ESSU   EVSU   LNU   NwSSU   PIT   SSU   SouthernLeyteStateU   TUPV   UEP   VSU   Mindanao   JRMSU   JHCSC   MSU-Buug   WMSU   ZPPSU   ZSCMST   BukSU   CPSC   CMU   ICPSC   MSU-IIT   MSU-Naawan   NMSCST   USTP   CVSC   DNSC   DSSC   DOrSU   SPAMAST   USEP   CFCST   MSU-GSC   SKSU   USM   ADSSU   CarSU   NEMSU   SSCT   AMPSC   BasSC   MSU Main   MSU-Maguindanao   MSU-TCTO   SSC   TRAC     v t e State Colleges and Universities Athletic Association NCR   EARIST   MPC   PNU   PhilSCA   PUP   RTU   TUP   UP   Luzon   DMMMSU   ISPSU   MMSU   NLPSC   PSU   UNP   SUAbra   ASC   BSU   IFSU   KSU   MPSPC   BSC   CSU   ISU   NVSU   QSU   ASCOT   BPSU   BASC   BulSU   CLSU   DHVSU   NEUST   PSAU   PMMA   PRMSU   TAU   TSU   BatSU   CvSU   LSPU   SLSU   URS   MSU   MinSU   OMSU   PSU   RSU   WPU   BU   UCN   CBSUA   CatSU   DEBESMSCAT   ParSU   PSUB   SorSU   SEAUTech   Visayas   ASU   CapSU   CHMSU   CPSU   GSU   ISAT-U   ISUFST   NISU   NONESCOST   TUPV   UA   WVSU   BISU   CNU   CTU   NOrSU   SSC   UP Cebu   BiPSU   ESSU   EVSU   LNU   NwSSU   PIT   SSU   SouthernLeyteStateU   UEP   VSU   Mindanao   JRMSU   JHCSC   MSU-Buug   WMSU   ZPPSU   ZSCMST   BukSU   CPSC   CMU   MSU-IIT   MSU-Naawan   NMSCST   USTP   DOSC   DNSC   DSSC   DOrSU   SPAMAST   USEP   MSU-GSC   SKSU   USM   ADSSU   CarSU   NEMSU   SSCT   AMPSC   BasSC   CFCST   CoSU   MSU Main   MSU-Maguindanao   MSU-TCTO   SSC   TRAC     v t e Mindanao  state colleges and universities State colleges   Adiong Memorial Polytechnic State College   Basilan State College   Camiguin Polytechnic State College   Cotabato Foundation College of Science and Technology   Davao de Oro State College   Davao del Norte State College   Davao del Sur State College   J.H. Cerilles State College   Northwestern Mindanao State College of Science and Technology   Southern Philippines Agri-Business and Marine and Aquatic School of Technology   Sulu State College   Tawi-Tawi Regional Agricultural College   Zamboanga State College of Marine Sciences and Technology   State universities   Agusan del Sur State University   Bukidnon State University   Caraga State University   Central Mindanao University   Cotabato State University   Davao Oriental State University   Jose Rizal Memorial State University   Mindanao State University   Philippine Normal University   Sultan Kudarat State University   Surigao del Norte State University   North Eastern Mindanao State University   University of the Philippines Mindanao   University of Science and Technology of Southern Philippines   University of Southeastern Philippines   University of Southern Mindanao   Western Mindanao State University   Zamboanga Peninsula Polytechnic State University     Authority control databases   ISNI           Retrieved from " https://en.wikipedia.org/w/index.php?title=Bukidnon_State_University&oldid=1267690863 "\n    \n    Question:\n    Could you tell me something about the BSU a really enthusiastic way?\n    \n    Answer:\n    '
+7.19 MiB

File added.

No diff preview for this file type.