Jupyter AI — A generative AI extension for JupyterLab

Marc Bolle
5 min readJul 13, 2023

--

Discover Jupyter AI, a new Jupyter Lab extension that adds a generative AI-powered conversational assistant!

Jupyter AI was developed by David L. Qiu. It enhances your JupyterLab experience by providing a user-friendly interface for exploring generative AI models in your notebooks.

Jupyter AI supports a variety of language models, including OpenAI, Hugging Face, SageMaker, and Cohere.

This article provides a practical introduction to the chat interface of Jupyter AI

Table of Contents

◦ Installing Jupyter AI
◦ Setting up the Chat Interface
◦ Chatting
◦ Generating code
◦ Asking about something in your notebook
◦ Generating a new notebook
◦ Learning about local data

Installing Jupyter AI

In order to unleash the power of Jupyter AI, the first step is to install this extension into your JupyterLab environment. In this section, I will guide you through the step-by-step installation process.

Prerequisites

Before we proceed with the installation of Jupyter AI, let’s ensure that you have all the prerequisites in place:

  1. Python version from 3.8 to 3.10 (Python 3.11 is not supported yet)
  2. JupyterLab 3 installed (JupyterLab 4 is not supported yet)
  3. Jupyter Server 2.x

Jupyter AI installation

We are now ready to install Jupyter AI. You can install it seamlessly with PIP:

pip install jupyter_ai

Setting up the Chat Interface

The chat interface is the simplest method to use Jupyter AI. You can engage with a generative AI model from within Jupyter Lab.

After you’ve launched JupyterLab, open the chat interface by clicking the new “chat” icon in the left side panel.

The first time you open the chat interface, Jupyter AI will ask you which models you wish to use as a language model and an embedding model. Jupyter AI supports the following models:

After you’ve made your choices, the UI shows fields for authenticating yourself with API keys. In the following example, I pick OpenAI’s gpt-3.5-turbo as language model, and OpenAI’s text-embedding-ada-002 as embedding model:

Once you’ve configured all of the required keys, click the “back” (left arrow) button in the upper-left corner of the Jupyter AI side panel. The chat interface appears and you can ask anything.

⚠️ The chat interface sends data to the third-party model hosted you have selected. Read the terms of use of this third-party hoster to find out what types of data are transmitted and stored.

Chatting

You can chat with Jupyternaut, the Jupyter AI chatbot. Once you have sent a message, Jupyternaut should respond.

The last two exchanges in your conversation are remembered by the chat backend and passed to the language model. Thus, you can ask follow-up questions.

Generating code

You can ask the Jupyternaut to generate code. In the following example, I ask the model to generate a code that reverses the words of a string:

Asking about something in your notebook

The chat interface of Jupyter AI can incorporate a section of your notebook in your prompt and send it to the language model.

To do that, you need to hightlight a portion of your notebook and click the “Include selection” box in the chat window. Then, you can enter your message and send it to the model.

In the following example, I include a cell and ask the model to explain what does it do:

⚠️ Adding code from your notebook to your prompt will rise the number of tokens, causing your request to cost more money. Review your model provider’s cost policy to avoid undesired bill.

Generating a new notebook

Here is one of the most interesting features of Jupyter AI: the generation of new notebooks from scratch. You can use the command /generate to ask Jupyternaut to write a new notebook.

It may take several minutes to generate your notebook. In the meantime, you can continue to use JupterLab and Jupyter AI as usual.

When Jupyter AI has finished creating your notebook, it will send you another message containing the location of the freshly created notebook.

In the following example, I ask the model the create a new notebook that demonstrates how to use Matplotlib:

And here is the notebook, which contains a very-well introduction to Matplotlib with an example:

⚠️ Generated notebooks may contain errors and may have unintended side effects when you run the code in them. Before running any generated code, review it thoroughly.

⚠️ This type of request might considerably rise the number of tokens too, review your model provider’s cost policy to avoid undesired bill.

Learning about local data

You can teach Jupyter AI about local data so that it can be used to answer your questions.
This local data is embedded using the embedding model you selected in the settings panel.

You have two options:
- The /learn command to teach Jupyter AI about a file,
- The /lean docs/ command to teach Jupyter AI about a folder full of documentation.

You can then use /ask command to ask a question specifically about the data that you taught Jupyter AI.

You can make Jupyter AI forget all it learn with the /learn -d command. This will erase the local vector database.

⚠️This functionality may send data to the third-party model provider that host the embedding model you selected. Review carefully their policies before using this functionality.

That’s it. We have seen how to use the chat interface of Jupyter AI to enhance our experience in Jupyter Lab. For more info about Jupyter AI, visit the official documentation.

--

--