Remember those old choose-your-adventure books that we used to read where we get to choose what happens next in a story? The story followed our choices and it kept the reader invested in whatever exciting choice was selected. This project is inspired by it.
A raspberry Pi Pico is used to display a part of a story and then you get to choose how to continue it. The project uses Ollama on a PC to generate the story and you get to continue it by selecting options on a Pico. The Pico and the PC communicate via serial communication. A lot of restrictions are placed on the LLM which include controlling the amount of words it can use for each generated text, making for very interesting stories and options to choose from. What's more, different LLMs can be chosen from, which will show various kinds of stories.
1. Getting startedThere are two main pieces of hardware used for this project. A Windows PC and a Raspberry Pi Pico with an OLED screen. The PC will need Ollama installed in it along with an IDE to write the code into the Pi Pico. I used VS Code and Thonny. The diagram below highlights the hardware and software components used.
The PC side requires no setting up, but there will be some straightforward setting up on the Raspberry Pi Pico side. You will need to connect the Pico's GPIO pins to the female pins on the OLED screen module as shown in the image below.
Ollama is an incredible tool that is used to run LLMs on your own device. This gives you the freedom to use it at your own discretion and without the need for an internet connection. You are also free to choose from a wide variety of different LLMs for the project.
Firstly, you will need to install Ollama by visiting their webpage at https://ollama.com/ and proceeding to the downloads section. Click on the 'Windows' option and either paste the suggested command in PowerShell or click on 'Download for Windows'.
Once it is downloaded, proceed with the installation. Once that is complete, you should be able to see the Ollama application. For this guide, however, we will be using the command prompt terminal. Open a command prompt terminal and type 'ollama' and a list of available commands should pop up.
Next step would be installing a model. Ollama has an extensive list of models to choose from and they are listed down in https://ollama.com/search. Each model has its strengths that can be useful for various applications. For this particular project, llama3.1 was used ( https://ollama.com/library/llama3.1 ) because it is strong in a wide range of areas. To install this model, you need to run the following command.
ollama run llama3.1If the model is not yet installed, running the command above will install it and if it is already installed, the terminal will allow you to message the model similar to how you'd converse with an LLM online. The difference here is that the LLM is running locally.
Ollama will expose an HTTP API on localhost, which means that the installed LLM can be interacted with by sending requests using any code that can send HTTP requests (Postman, Python code, etc.). We will be using this capability to interact with llama3.1 through a Raspberry Pi Pico. In order to do this, open up a command terminal and run
ollama serveThis will start the Ollama server and it will show some important information such as the port that Ollama is running on.
Next, we need to customize the downloaded model to behave as a storyteller. For this, we need to create a Modelfile, which is a blueprint used to create amended models. There are a number of parameters that can be entered into the Modelfile, but we will be sticking to three important ones. These include the base model to fetch from, a value to set how creative the model will be in its responses, and the system message. In this project, the system message will carry information on how to limit the story's sections to fit to the Raspberry Pi Pico's OLED screen, and how to send options to carry the story forward.
FROM llama3.1:8b
# set the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 1
# set the system message
SYSTEM """
You are a story telling AI. Your task is to tell part of a story not exceeding 17 words each time you are prompted.
You will first receive a prompt from the user which should be considered the general theme of the story.
After telling part of the story, you must give the user 2 choices on how to continue the story.
Each choice should not exceed 4 words.
After giving the two choices, wait for the user to select one of them before continuing the story.
Your responses must be in the following format:
"QUESTION|{statement}|{option1}|{option2}"
Where:
QUESTION is a fixed keyword.
statement is the story part you are telling (max 17 words).
option1 is the first choice (max 4 words).
option2 is the second choice (max 4 words).
If you get a response of 1 or 2 from the user, continue the story based on the selected option.
If you receive 3 from the user, end the story with a conclusion not exceeding 17 words.
This concluding response must be in the following format:
"QUESTION|{statement}|{option1}|{option2}"
Where:
QUESTION is a fixed keyword.
statement is the conclusion (max 17 words).
option1 should be an empty string.
option2 should be an empty string.
You must strictly follow this format and not deviate from it.
"""Be sure to save this file in a known location. Following this, open a command terminal in the location where this file is open and run the following command.
ollama create storyteller -f ./ModelfileBe sure to replace './Modelfile' with a different path if your command terminal is open from a different location. Also, feel free to change 'storyteller' to any name you prefer. Once this command is run, you can run the command below to interact with this newly created custom model of yours.
ollama run storyteller4. Preparing python code for Ollama modelOnce the Ollama LLM is up and running on your PC, you'll need to create the ollama_init.py library which will be used in your PC side python script to interact with the LLM. It allows you to create an instance based on a genre of your choosing. The LLM will then return a message with a set of options for the user to choose from, which will then be fed into the LLM, and the cycle will go on.
5. Setting up PC side and Pico side codeThe PC side and Pico side will have relatively similar sets of code which will help to communicate with each other via serial communication. pc_serial_comm.py which runs on the PC side has the PCSide class which will connect with the port for the Raspberry Pi Pico, at a predetermined baud rate. You are able to add up to 3 genres into the code and whichever one you select will be used in the story that is generated.
On the Raspberry Pi Pico's side, main.py will help relay choices selected by the user to the PC, and display story parts that are sent from the PC to the Pico. The code is structured such that if the LLM sends data with the word 'QUESTION|', it will split the content into the story, and two options which will allow the story to progress. If the user presses button B, that sends a message to the LLM indicating that the story needs to be given a conclusion, and that ending will be displayed on the OLED screen.
6. TestingConnect the Pico to the PC, identify the port number it is connected to (this can easily be confirmed by opening the device manager), move the files in the 'pico' folder in the git repo to the Pico via Thonny, and keep the files in the 'pc' folder in a location in the PC of your choosing. It is recommended that the libraries required to run pc_serial_comm.py be installed in a venv on your PC. Amend the 'port' variable in the PCSide instance created in pc_serial_comm.py and run the script. You should see the Pico's OLED screen display a greeting screen, followed by genre options for you to choose and to begin the story. You will be able to carry on the story for as long as you'd like by selecting options by pressing the up and down arrows and pressing 'A' to select an option, and you can end the story by pressing the 'B' button, which should end the story with an appropriate ending.
And there you have it! A very simple story writer. There are limitations, such as the fact that you'd need a serial connection to the PC running the LLM, and the story itself is limited in its creativity by the size of the LLM, the LLM used, and the amount of characters that can be displayed at a time on the screen. Regardless, it is an interesting little way to showcase LLMs on a Pico and a nifty way to learn about LLMs.





Comments