Overview
GPT Code Interpreter allows you to run your own code on a GPT server. It's great for trying out simple functions. Did you know that you can upload your own interpreter and have GPT run it? It takes some work, but you can have GPT write code and tests, then run them on an actual CircuitPython interpreter. Not all the features are there, and you don't have access to any real hardware. This is highly experimental at this point. Difficulty level: Advanced.
Equipment
- CircuitPython interpreter binary
ports/unix
x86-64. Follow the instructions for Building CircuitPython. You need to build with the same GLIBC as Code Interpreter which is currently 2.31. I was able to build it using Debian 11 (bullseye). - The interpreter is named 'micropython' but it is CircuitPython.
- ChatGPT+ account: Code Interpreter is a plus feature.
Instructions
Follow these instructions to start a chat with access to your CircuitPython interpreter. commands
should be entered directly into the chat.
- Start a new ChatGPT 4 session.
- Attach your CircuitPython interpreter 'micropython'. Click the 'paperclip' to upload it.
- Use this command:
Please make /mnt/data/micropython executable.
- Use this code to check that the interpreter is working:
import sys
print("Version:", sys.version)
print("Platform:", sys.platform)
- Flex some Python skills:
Use introspection to list the features in the <os> module
How it Works
The code interpreter is like a Jupyter Notebook that GPT controls. When you ask it to run some code it writes the script and executes it as a notebook 'cell'. It adds an icon that allows you to view the script and the output.
For the CircuitPython interpreter it will embed the CircuitPython code in the cell as a string, then use the shell to execute the CircuitPython interpreter with the embedded script and capture the output. It can then interpret the output or return it to you directly.
It's pretty code-inceptiony and may take a while to figure out. Be sure to click on the icons to view the code and output for each 'cell'.
Observations
I'm still in awe that it even works. You will have to use some prompting to make sure it is running the interpreter and not just calculating the responses in some other way. It can use introspection to figure out which features are available and which are not present.
This is still very experimental at this point!
Conclusions
Building a CircuitPython interpreter that can run within GPT is an interesting and at times mind-bending exercise.
Code Interpreter could be a useful tool for testing different builds or even different versions of the CircuitPython core.
Future Work
- Figure out how to add library packages to Code Interpreter.
- Implement some hardware-in-software modules (fake temperature and humidity sensor, fake I2C and SPI peripherals, fake
board
with a few pins etc.) - Make the interaction traceable so we know if the interpreter is used or not.
- Wrap it in a GPT application.
- Figure out how to call it via the GPT API.
Hug Reports
- Dan Halbert for writing the guide to building CircuitPython
- Simon Willison for his experiments with GPT Code Interpreter: