Skip to main content
This assignment is due on Tuesday, April 2, 2024 before 11:59PM.
You can download the materials for this assignment here:

Homework 4: Building an AI Game Master

Author: Andrew Zhu (

In this homework, we’ll use Kani to build a simple Game Master (GM) for the Mausritter TTRPG. Your GM will help walk you through creating a mouse and rolling dice to overcome challenges in the game. Along the way, you’ll learn the basics of the Kani library, function calling with LLMs, and how a LLM can use structured information to act as a game state manager.

We won’t implement more complex systems like combat or spellcasting in this homework, but extending this system to make a more comprehensive GM (e.g. adding inventory tracking, combat, spellcasting, structured tracking of NPCs, …) would make for a great class final project.

This assignment will have 3 main parts:

  1. Gain familiarity with Kani and function calling - You will learn how Kani can be used to expose external APIs to LLMs by connecting GPT-4 with a dice rolling library.
  2. Use function calling to build a character creator agent - We’ll show how LLMs can be used to generate structured data by creating an agent to walk a new player through the Mausritter character creation process. Your agent will be able to roll dice for the player, help translate natural language to game mechanics, and save their character sheet once it’s created.
  3. Put everything together in an AI GM - We’ll use function calling and the structured character created by your agent to create a grounded AI GM. It will have the ability to reference your character sheet and roll saves based on your mouse’s stats. Finally, we’ll see how a grounded AI GM compares to a prompting-only approach.

What You’ll Need

Using our Starter code

We have provided a Python Notebook for HW4. I recommend using Visual Studio Code for this homework. You’ll be making changes to the HW4.ipynb file. You should read through the whole Python Notebook carefully, and add your code to all places marked TODO.

Environment Setup

To run this code in your Python IDE, you’ll need to install a couple python packages via pip, and you’ll need to set the OPENAI_API_KEY environment variable to your Helicone key.

First download the notebook, then create a virtual environment and install the dependencies. Here’s how I recommend that you set up your environment:

$ mkdir hw4
$ cd hw4
$ python3.10 -m venv venv
$ source venv/bin/activate
(venv) $ pip install jupyter 'kani[openai]' d20

If you’re using VS code then you can send your OPENAI_API_KEY to it when you launch it from the command line:

$ cd path/to/hw4
$ source venv/bin/activate
(venv) $ HELICONE_API_KEY=sk-helicone-XXXXXXX-XXXXXX code .

You should replace sk-helicone-XXXXXXX-XXXXXX with your Helicone key which you can find here.

What to submit

Please submit the following:

  • Completed hw4.ipynb
  • The most recent transcripts of your character creator agent and AI GMs: creator-TIME.json, gm-TIME.json , and gm-structureless-TIME.json
  • The images you generated of your character (saved in the dalle/ folder)
  • A that describes your high-level implementation of each part of the homework and any interesting findings

You should submit your completed homework to Gradescope. You can work in teams. Only one team member should submit - be sure to specify who your partner was when you make your submission. If your group is >2 students, you should do one extension per additional teammate and describe what you did in the homework writeup. The extensions are up to you!

Recommended readings

Kani: A Lightweight and Highly Hackable Framework for Building Language Model Applications - Andrew Zhu, Liam Dugan, Alyssa Hwang, Chris Callison-Burch. NLP-OSS @ EMNLP 2024.