Lab Workbook: Building a Minimal Viable Product (MVP) of ChatGPT

Last edited 172 days ago by Peter Sigurdson

Learning Outcomes:

How to make your own Minimal Viable product, "toy" version of Chat GTP so you will understand the principles


This lab workbook is designed to help students understand the principles behind ChatGPT by guiding them through the creation of their own minimal viable product (MVP) or "toy" version.

Table of Contents

Introduction to ChatGPT
Preparing the Dataset
Preprocessing the Text Data
Building a Simple Language Model
Training the Model
Generating Text with the Model
Evaluating the Model

1. Introduction to ChatGPT

In this section, provide a brief overview of ChatGPT, its applications, and its limitations. This will help students understand the context of the project they are about to undertake.

2. Preparing the Dataset

Before students can build their own MVP, they need a dataset to train their model. In this section, guide them through the process of selecting and downloading relevant text data. Some possible options are:
Project Gutenberg (
Common Crawl (
The Brown Corpus (

3. Preprocessing the Text Data

In this section, teach students how to preprocess the data by:
Tokenizing the text
Removing special characters and numbers
Converting text to lowercase
Creating a vocabulary of unique words

4. Building a Simple Language Model

Learning outcomes:
Examine the concept of a simple language model, such as an n-gram model.
Walk students through the process of building an n-gram model, including:
Selecting the n-gram size (e.g., bigrams or trigrams)
Calculating the probabilities of each n-gram
Creating a probability distribution for text generation

5. Training the Model

Learning outcomes:
Examine the process of training our simple language model using the preprocessed dataset.
Explain the importance of splitting the data into training and validation sets.

6. Generating Text with the Model

Learning outcomes:
How to use their trained model to generate text by:
Selecting a seed word or phrase
Generating the next word based on the probability distribution
Repeating the process to generate a sequence of words
Explain how the "temperature" parameter can be used to control the randomness and creativity of the generated text.

7. Evaluating the Model

Learning outcomes:
Discuss methods for evaluating the performance of the language model, such as perplexity or human evaluation.
Encourage students to analyze the strengths and weaknesses of their MVP and compare it to ChatGPT.

8. Conclusion

Wrap up the lab workbook by discussing the lessons learned from building a simple MVP of ChatGPT. Encourage students to think about potential improvements to their model and explore more advanced techniques, such as deep learning and transformer models.

Where and how do I host this code?

To create a minimal viable product (MVP) of ChatGPT using Python, you can use the nltk library for text preprocessing and an n-gram model for text generation. For hosting the code, you can use a Jupyter Notebook, GitHub, or an online Python IDE like
Here's a step-by-step guide on how to create the MVP:

Install the required libraries

pip install nltk

Import the necessary modules

import nltkfrom nltk import FreqDistfrom nltk.util import ngramsfrom random import choices

Load and preprocess the dataset

# Load the dataset (replace this with your own dataset)with open('your_dataset.txt', 'r', encoding='utf-8') as file: text =
# Tokenize the texttokens = nltk.word_tokenize(text.lower())
# Remove special characters and numberstokens = [token for token in tokens if token.isalpha()]

Create a bigram model

# Generate bigramsbigrams = list(ngrams(tokens, 2))
# Calculate bigram frequenciesbigram_freq = FreqDist(bigrams)

Generate text using the bigram model

def generate_text(seed, bigram_freq, num_words=20): current_word = seed generated_text = [current_word]
for _ in range(num_words): # Get the next word based on the current word next_words = [bigram[1] for bigram in bigram_freq if bigram[0] == current_word] next_word_freqs = [bigram_freq[bigram] for bigram in bigram_freq if bigram[0] == current_word]
if not next_words: break
# Choose the next word using the bigram frequencies next_word = choices(next_words, next_word_freqs)[0] generated_text.append(next_word) current_word = next_word
return ' '.join(generated_text)
# Generate text with a seed wordgenerated_text = generate_text('artificial', bigram_freq)print(generated_text)

To host the code, you can create a Jupyter Notebook and run the code cells in order. Alternatively, you can use an online Python IDE like ( and run the code there. To share the project, you can create a GitHub repository and upload the code files or share the link with your students.


Here is a more detailed and specific guide to help a second-term student with some introductory Python knowledge build a basic language model using bigrams.

Import necessary libraries:

import reimport nltkfrom nltk.tokenize import word_tokenizefrom nltk.util import ngramsfrom collections import Counter, defaultdictfrom random import choices

Download the necessary NLTK data:'punkt')

Load a text dataset (use any .txt file containing a large amount of text, e.g., a book or a collection of articles):

with open('your_text_file.txt', 'r', encoding='utf-8') as f: text =

Preprocess the text:
def preprocess_text(text): text = text.lower() text = re.sub(r'\d+', '', text) text = re.sub(r'[^\w\s]', '', text) return text
cleaned_text = preprocess_text(text)

Tokenize words and create bigrams:
tokens = word_tokenize(cleaned_text)bigrams = list(ngrams(tokens, 2))

Count the bigram frequencies:

bigram_freq = defaultdict(int)for bigram in bigrams: bigram_freq[bigram] += 1

Generate text using the bigram model:
def generate_text(seed_word, bigram_freq, num_words=50): current_word = seed_word generated_text = [current_word]
for i in range(num_words - 1): next_words = [bigram[1] for bigram in bigram_freq if bigram[0] == current_word] next_word_freqs = [bigram_freq[bigram] for bigram in bigram_freq if bigram[0] == current_word]
if not next_words: break
next_word = choices(next_words, next_word_freqs)[0] generated_text.append(next_word) current_word = next_word
return ' '.join(generated_text)

Test the text generation:
generated_text = generate_text('artificial', bigram_freq)print(generated_text)

This code will build a simple bigram language model that can generate text. Although this model is not as comprehensive as ChatGPT, it provides a framework that can be extended and improved upon by students as they gain more knowledge and experience in natural language processing and machine learning.

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.