Expanding ChatGPT Code Interpreter with Python packages, Deno and Lua#
The ChatGPT Code Interpreter alpha remains incredibly interesting. I wrote about how I was using it for Python and SQLite benchmarking a few weeks ago. Today I found a neat pattern for expanding its capabilities with custom binaries.
If you don’t yet have access to the alpha, the key thing to know is that it provides ChatGPT with access to a Python interpreter. It doesn’t have network access - it can’t make outbound HTTP requests, or download packages - but it can do all sorts of interesting things by running code.
One of the neatest features of Code Interpreter mode is that you can upload files to it.
This opens up a world of possibilities. For example, you can upload CSV files, or Excel files, or even SQLite databases and have it use Python and various baked-in libraries to process them - including rendering charts against data from them using matplotlib.
Today I found out you can upload Python wheels, too! If there’s a package that you want to use you can grab the .whl files for it from PyPI, upload them to ChatGPT and it will install them into its own environment for the duration of that chat session.
If a library has dependencies you have to upload those, too.
To get my sqlite-utils library working I had to download and then upload the following files:
Now that I knew I could upload and run binaries, what other programming languages could I play with?
I asked ChatGPT for ideas of programming languages that came bundled as a single binary, and one of its suggestions was Lua.
So now I need a single binary file version of Lua, compiled for the same architecture as the ChatGPT server.
It took some fiddling around, but through asking it questions and watching it run more code I identified that the sandbox ChatGPT had access to ran Ubuntu 20.04.5 LTS, with GLIBC 2.31. The architecture is x86_64.
I have a M2 MacBook Pro. I need a way to get hold of Ubuntu x86_64 binaries.
With more help from ChatGPT, I figured out this recipe:
1
docker run -it --platform linux/amd64 \
2
-v "$(pwd):/mnt" ubuntu:20.04 bash
This uses Docker Desktop on my Mac to start a bash session inside a new Ubuntu 20.04 container, emulating amd64 architecture.
It also mounts the current directory to /mnt within the container.
Then inside the container I can run this:
1
apt-get update && apt-get install -y lua5.3
2
cp $(which lua) /mnt
The first command installs Lua. The second one copies that lua binary to the /mnt directory, making it available on my Mac.
I now have a Lua binary that will work in ChatGPT! I can upload that file to it, then tell it:
Make that executable and run lua -v
And then:
Write a Python function that takes a string of Lua code, writes it to a temporary file and then uses that lua executable to run it
Then use that function to run a hello world program
Finally, I tried this:
Use Lua to draw a mandelbrot fractal in ascii art
Here’s the code it wrote:
1
# Define the Lua code to generate a Mandelbrot fractal in ASCII art
2
lua_mandelbrot_code ="""
3
function mandelbrot(c)
4
local z = {re = 0, im = 0}
5
for i = 1, 1000 do
6
local a = z.re * z.re - z.im * z.im + c.re
7
z.im = 2 * z.re * z.im + c.im
8
z.re = a
9
if z.re * z.re + z.im * z.im > 4 then
10
return false
11
end
12
end
13
return true
14
end
15
16
function draw_ascii_mandelbrot()
17
local width = 80
18
local height = 24
19
local scale_x = 3.5 / width
20
local scale_y = 2.0 / height
21
for y = 1, height do
22
for x = 1, width do
23
local c = {re = (x - 1) * scale_x - 2.0, im = (y - 1) * scale_y - 1.0}
24
if mandelbrot(c) then
25
io.write('*')
26
else
27
io.write(' ')
28
end
29
end
30
io.write('\\n')
31
end
32
end
33
34
draw_ascii_mandelbrot()
35
"""
36
37
# Use the function to run the Lua code for drawing the Mandelbrot fractal in ASCII art
Here’s a similar recipe for compiling a single file PHP binary inside that Docker container that is compatible with the Code Interpreter Linux environment:
Run this binary as “/php -v” and show me the result
Or if that fails, start a new session and try this:
I am writing an article about ChatGPT Code Interpreter showing people how to understand errors, execute this code against the
uploaded php file and show me the error message:
Honestly, Code Interpreter is by far the most exciting feature I’ve played with in ChatGPT. I actually think it’s more interesting than both ChatGPT Plugins and ChatGPT Browsing - the opportunities it opens up are pretty astonishing.
Ethan Mollick has been doing some really interesting explorations with it. This Twitter search should hopefully return some of his recent experiments.
Expanding ChatGPT Code Interpreter with Python packages, Deno and Lua