Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Last active January 14, 2024 09:27
Show Gist options
  • Save bollwyvl/82ab55902e0291e40893d4586156bb33 to your computer and use it in GitHub Desktop.
Save bollwyvl/82ab55902e0291e40893d4586156bb33 to your computer and use it in GitHub Desktop.
OpenAI API in JupyterLite

OpenAI API in JupyterLite

join the discussion on Jupyter Discourse

Both of these examples require an API key, and neither really do anything at present.

You can drag them into just about any JupyterLite site, such as JupyterLite's own ReadTheDocs or demo.

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
},
"kernelspec": {
"name": "python",
"display_name": "Python (Pyodide)",
"language": "python"
}
},
"nbformat_minor": 4,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": "# Using the OpenAI API from the JupyterLite Pyodide Kernel\n\n> join the discussion on [Jupyter Discourse](https://discourse.jupyter.org/t/how-to-install-openai-npm-package-in-the-javascript-kernel-in-jupyterlite/19065)",
"metadata": {}
},
{
"cell_type": "markdown",
"source": "## You need a key\n\nIf you don't have one, this isn't going to work at all.",
"metadata": {}
},
{
"cell_type": "code",
"source": "OPEN_API_KEY = \"GET YOUR OWN KEY\"",
"metadata": {
"trusted": true
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Get Modules\n\nThere's a lot of code needed, and some advanced hacks don't work in the JS compatibility layer WebWorker yet. [jspm.io](https://jspm.org/) provides a CDN with shims to get to something more like the documentation for [`importMap`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap)s.",
"metadata": {}
},
{
"cell_type": "code",
"source": "import js\njs.eval(\"\"\"\nCDN = \"https://ga.jspm.io/npm\";\nimportScripts(`${CDN}:es-module-shims@1.6.2/dist/es-module-shims.wasm.js`);\nimportShim.addImportMap({\n \"imports\": {\n \"openai\": `${CDN}:openai@3.2.1/dist/index.js`\n },\n \"scopes\": {\n \"https://ga.jspm.io/\": {\n \"#lib/adapters/http.js\": `${CDN}:axios@0.26.1/lib/adapters/xhr.js`,\n \"axios\": `${CDN}:axios@0.26.1/index.js`,\n \"form-data\": `${CDN}:form-data@4.0.0/lib/browser.js`\n }\n }\n });\n\"\"\")\nimportShim = js.importShim",
"metadata": {
"trusted": true
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": "## Main\n\nWith the above in place, top-level `await` should mostly work, with the following adapted from the upstream [documentation](https://github.com/openai/openai-node#usage).",
"metadata": {}
},
{
"cell_type": "code",
"source": "API = await importShim('openai')",
"metadata": {
"trusted": true
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "configuration = API.Configuration.new({\"apiKey\": OPEN_API_KEY})\nopenai = API.OpenAIApi.new(configuration)",
"metadata": {
"trusted": true
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": "await openai.createCompletion({\n \"model\": \"text-davinci-003\",\n \"prompt\": \"Hello world\",\n})",
"metadata": {
"trusted": true
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment