How to install OpenAI npm package in the Javascript kernel in JupyterLite?

OpenAI’s ChatGPT is very hot. I am willing to try it in JupyterLite with OpenAI’s APIs. So I can access its service in browsers easily, even on mobile.
But I couldn’t find a way to install it in the Javascript kernel in JupyterLite.
Any ideas?
Thank you in advance,

It kinda takes a lot: moved the long post to this gist.

In a nutshell:

  • patch imports and importMaps with jspm
  • do some async skullduggery
  • put your key in
  • greet AI overlords, if that’s what you’re into (i didn’t bother trying)

As a bonus, a second notebook shows how to use the Pyodide JS compatibility layer to basically do the same thing in python, and it’s actually not too bad to read.

3 Likes

It works. Thank you very much.

To be honest, it is more complicated than I expected. So is there a way we can pre-build the OpenAI npm package into the JupyterLite deployment? Maybe it is easier for downstream developers.

downstream developers

Sure, specifically as JS developers comfortable with npm, etc, they can do whatever they want with a custom kernel, with the packages compiled in at build time, and then release these in a way that works with the core jupyter lite build system for a (theoretically) reproducible system.

It’s also theoretically possible to do some of this from a “plain” labextension, but this wouldn’t work much at all in a stock JupyterLab, so likely better to handle in the “server”.

More generally, importMaps could be made to work in the core kernel, but at present, the JS ecosystem is not yet ready for the general case of modules (safari only added this last month), and adding a hardcoded dependency on a specific CDN has been avoided thus far.

As for a dependency on a specific, for-pay SaaS with no particular self-hostable alternative… definitely not going in core.

Hi bollwyvl,
We are just working on jupyterhub. And we had a question when we started to work. We just want to restrict Jupiter kernal for some users. If there any steps please let me know.
Thanks

Regards
Saran