When LLM met Pyodide
LLMs are getting better and better at generating code, can we give them a “secure” environment where they can execute any arbitrary code without affecting the server or the users system?
Obviously we must not give access to the user file-system, a database or a backend with a client facing chat interface that wouldn’t be great.
We should have a completely client side appication which can run a code generated by an LLM.
First thought
let’s start with JavaScript
The model will generate some JS code that we can put in an iframe and show it to the user, adding some popular packages with <scripts> or asking the LLM to do so, would be a great addition to add things like ThreeJS for 3D, TailwindCSS for consistency, or DaisyUI, Alpine or HTMX for backend integartion would be possible too…
But browsers are not just capable of running JS we also have WASM.
I found WASM
WebAssembly is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
Pyodide and Jupyter Lite
Jupyter runs on the browser, but it requires a server running on the background when you run it locally or even when you are using kaggle or colab. But then I found a project called jupyter-lite.
JupyterLite is a JupyterLab distribution that runs entirely in the browser built from the ground-up using JupyterLab components and extensions.
How does this thing work?
Answer: Pyodide and Xeus-Python
Pyodide is a Python distribution for the browser and Node.js based on WebAssembly.
Xeus is a Jupyter kernel for Python based on the native implementation of the Jupyter protocol xeus.
Security
Browsers have beed evolved over decades to become a secure environment, that is the reason why in this experiment I went ahead with building the application with nothing but just client side next.js
Working
Important packages like numpy, matplotlib etc. are fetched when you load the page, some other packages are lazy loaded only when called like plotly, scikit learn and then there is micropip to install other python-based packages.
Try today! Your contributions would be appreciated Charmer