appendix-g

Appendix G. Building a chat interface

 

The supplementary code repository for this book includes a small browser-based chat interface built with the open-source Chainlit Python package (https://github.com/Chainlit/chainlit) to interact with the various LLMs and reasoning models in this book via a ChatGPT-like interface, as shown in the screenshot in figure G.1.

Figure G.1 Chainlit interface for the Qwen3 model, which mimics the ChatGPT interface.

This interface shown in figure G.1 can be useful when we want a more convenient way to interact with the models than typing prompts into a notebook cell or terminal.

Building upon the Chainlit library, the implementation is relatively straightforward. Instead of printing tokens to the terminal, we wrap the same model loading and generation functions from the earlier chapters inside a small web application, which stays local on our computer and runs in our browser.

In practice, this means that we can reuse the from-scratch Qwen3Model implementation and the chapter 2 streaming helper while letting Chainlit handle the browser UI and message events.

The supplementary materials contain two variants in chG/01_main-chapter-code (https://github.com/rasbt/reasoning-from-scratch/tree/main/chG/01_main-chapter-code):

  • qwen3_chat_interface.py, which is a single-turn interface;
  • qwen3_chat_interface_multiturn.py, which stores conversation history and supports multi-turn interactions.

G.1 Installing Chainlit

G.2 Running the code as a script

G.3 Downloading the scripts

G.4 The regular single-turn script

G.5 Running the single-turn script

G.6 The multi-turn interface

G.6.1 What multi-turn means

G.6.2 The multi-turn variant

G.6.3 How the multi-turn script uses history

G.6.4 Recommendations