Overview

Appendix G. Building a chat interface

In this appendix, the book shows how to turn the earlier from-scratch model code into a simple browser-based chat app using Chainlit. The idea is to keep everything local while gaining a more convenient interface than a notebook or terminal, and to reuse the same model-loading and text-generation code developed in earlier chapters. The appendix also points readers to two ready-made scripts: one that handles isolated prompts and one that preserves conversation history.

The setup is straightforward: install Chainlit, place the app code in a standalone Python file, and launch it from the terminal. The single-turn version is compact and mostly wraps the existing generation functions with Chainlit event handlers, but it only feeds the current user message to the model even though the browser may display prior exchanges. It also supports loading either the default official weights or a custom checkpoint through an environment variable, making it useful for interactively testing different chapter checkpoints as long as the tokenizer matches.

The multi-turn version extends the same interface so the model can actually remember previous turns. It does this by storing conversation history, converting that history into the Qwen chat format, trimming old tokens when needed to fit the context window, and then appending the assistant’s reply back into the session state. The result is a true conversational loop in which later answers can depend on earlier ones, though the book cautions that this approach is best suited to the official reasoning model rather than distilled checkpoints that were not trained for persistent multi-turn chat.

Figure G.1 Chainlit interface for the Qwen3 model, which mimics the ChatGPT interface.
Figure G.2 Example response in the Chainlit interface.
Figure G.3 Example response in the Chainlit interface with multiple prompts and responses.

FAQ

What is the Chainlit chat interface used for in Appendix G?It provides a small browser-based, ChatGPT-like interface for interacting with the book’s LLMs and reasoning models more conveniently than using a notebook cell or terminal.
What package does the chat interface use?The interface is built with the open-source Chainlit Python package.
What are the two chat interface scripts included in the supplementary materials?The two variants are qwen3_chat_interface.py for single-turn interactions and qwen3_chat_interface_multiturn.py for multi-turn interactions with conversation history.
How do I install Chainlit?You can install it with pip install chainlit, or with uv add chainlit. If you are working in the book repository, you can also install the optional dependency with uv sync --extra extra or pip install -e ".[extra]".
How do I run a Chainlit app from a Python script?Save the code in a standalone .py file such as app.py and run it with chainlit run app.py. If you use uv, the equivalent command is uv run chainlit run app.py.
Why does Chainlit need the code to live in a Python script instead of a notebook?Chainlit imports the script as a module and looks for special functions decorated with @chainlit.on_chat_start and @chainlit.on_message, so it expects a normal script entry point rather than notebook cells.
How does the single-turn interface work?It accepts each user message independently, encodes only the current message.content, and streams the model output back to the browser. Earlier messages may appear in the UI, but they are not included in the model input.
How can I use a custom checkpoint with the single-turn script?Set the CHECKPOINT_PATH environment variable to the path of the .pth file. The script will then load that checkpoint instead of the default weights, as long as WHICH_MODEL matches the tokenizer expected by the checkpoint.
What is the main difference between single-turn and multi-turn chat?Single-turn chat treats every message as independent, while multi-turn chat prepends previous user and assistant messages to the prompt so the model can condition on conversation history.
When should I use the multi-turn interface instead of the single-turn one?Use the multi-turn version with the official Qwen3 reasoning checkpoint when you want conversation memory. For quick testing or for custom chapter 6, 7, or 8 checkpoints, the single-turn script is usually the safer choice.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Reasoning Model (From Scratch) ebook for free