AI Assistant: Fine-Tuned GPT-2
Fine-tuning a small language model on dialogue, then serving it through three interfaces
Overview
My first hands-on language-model project, from 2023 to 2024: take GPT-2, fine-tune it on conversation data so it answers in a dialogue style, and put it behind interfaces people can actually use.
What I built
- Fine-tuning: GPT-2 (124M parameters) trained with Hugging Face Transformers and the
TrainerAPI on the public ConvAI2 dialogue dataset plus custom dialogues I wrote. That custom file has since been lost (the copy in the repository is empty). Causal language modelling, sequences of 128 tokens, 3 epochs, batch size 4, on a CPU. The run (above, August 2024) took 2,622 steps and 3.6 hours; training loss fell from 2.96 to 2.42 over the first 1.7 epochs and reached 2.21 by step 2,500 (the log is kept in the repository). - Serving: a Flask API (
/api/generate) that loads the fine-tuned model and generates replies. The settings asked for nucleus sampling (top-p 0.9, top-k 50, temperature 0.7), but see below: in 2024 they were never switched on. - Three interfaces on the same model: a web page, a Tkinter desktop app and a Streamlit app, plus an early React front end.
- Optional storage of the model in AWS S3.
What went wrong, and what changed
- The ConvAI2 and custom data had different structures; merging them took a preprocessing step for each.
- Early runs overfitted, which I corrected by adjusting the learning rate and the number of epochs.
Revisited, November 2026
I went back to the code in November 2026, with Claude as a pair programmer, and did what I had said I would do differently. Running it again turned up real bugs:
| Found | Changed |
|---|---|
The sampling settings were passed without do_sample=True, so the library ignored them and every reply was greedy: flat and repetitive. |
Sampling is on, with a test that fails if it is dropped. |
| Training joined each conversation into one line with no speakers, so replies ran on into the next turn. | Turns are labelled User: and Assistant:, and each reply is cut at the next speaker. |
| The React front end’s backend only reversed the input text; it never reached the model. | All four interfaces use one API, and the web page keeps the conversation history. |
| The requirements file was saved as UTF-16, which pip cannot read, and listed 130 packages the project never used. | Seven direct dependencies, 11 tests and GitHub Actions CI. |
| Replies were judged by reading them. | Whole conversations are held out and perplexity is compared with untuned GPT-2. |
Then I retrained it on more conversation. One epoch over three public dialogue datasets, ConvAI2, DailyDialog and Blended Skill Talk (18,363 conversations, 3.2 million tokens), took about 2.5 hours on a 2-core CPU. Every model is scored on conversations it never trained on:
| Model | ConvAI2 | DailyDialog | Blended Skill Talk |
|---|---|---|---|
| GPT-2, untuned | 23.33 | 25.10 | 34.07 |
| Fine-tuned on ConvAI2 only | 7.80 | 21.67 | 25.44 |
| Fine-tuned on all three | 7.95 | 11.43 | 17.81 |
The specialist stays slightly better on its own dataset (7.80 against 7.95), but the broader model is 47% lower on DailyDialog and 30% lower on Blended Skill Talk: it now fits ordinary written conversation, not one dataset’s style. Lower perplexity is not the same as helpful or true answers, and the chat shows it: the model takes turns and can be kind, but it forgets what it has just said.
DailyDialog is licensed for non-commercial use only, so the code is public and the trained weights are not. The same habit of measuring against a baseline on held-out data runs through my research studies.
Tech stack
Python, PyTorch, Hugging Face Transformers, Flask, Tkinter, Streamlit, React, pytest, GitHub Actions. AWS S3 storage was optional in the 2024 version.