Home Artificial Intelligence Jupyter AI: The AI Extension for Jupyter Lab

Jupyter AI: The AI Extension for Jupyter Lab

1
Jupyter AI: The AI Extension for Jupyter Lab

It adds a native chat UI, magic commands to send prompts to ChatGPT, and generates entire notebooks from a text prompt.

Photo by Artin Bakhan on Unsplash

Previously we’ve seen methods to add ChatGPT functionalities to Jupyter Notebook and VSCode through extensions and now it’s time for Jupyter Lab!

Why do you wish it?

Well, unlike the previous extensions I showed you, this one offers a chat UI that permits us to talk with the Jupyter AI conversational assistant. Also, it supports a big selection of generative model providers resembling OpenAI, Anthropic, Cohere, Hugging Face, and more.

Here’s every little thing that you must learn about Jupyter AI.

In the event you don’t feel like reading, you’ll be able to watch my video below.

Establishing Jupyter AI

To put in jupyter_ai that you must have Python 3.8 to three.10 and JupyterLab 3 installed. You’ll be able to install jupyter_ai with each conda and pip, but should you’re an Apple Silicon-based Mac user (M1, M1 Pro, M2, etc.) like me, you must use conda.

Let’s create a recent virtual environment with Python 3.10 and install jupyter_ai.

$ conda create -n jupyter-ai python=3.10
$ conda activate jupyter-ai
$ pip install jupyter_ai

Note that Apple Silicon-based Mac users have to uninstall the pip provided version of grpcio and install the version provided by conda as an alternative.

$ pip uninstall grpcio; conda install grpcio

Then we are able to launch Jupyter Lab

jupyter lab

If every little thing was installed successfully, you must see the brand new “chat” icon within the left side panel on Jupyter Lab.

Click on “Start Here” to pick out the model and introduce your API key.

After saving changes, click the “back” button within the upper-left corner. Now you must see the chat interface below where you’ll be able to start a chat.

We’ll see some cool things you’ll be able to do from the chat interface later. Now let’s concentrate on the notebook.

Working with Jupyter AI

First, we’d like to load the jupyter_ai extension by running the code below in a notebook cell.

%load_ext jupyter_ai

Then we are able to list all of the providers and models available using the code below.

%ai list

Listed here are the providers and models you need to use.

Note that to make use of Jupyter AI with a selected provider, you should install its Python packages and set its API key in your environment or within the chat interface.

Say I would like to make use of OpenAI’s ChatGPT model. To accomplish that, I actually have to put in the openai package.

!pip install openai

And set the API key in my environment (to generate your OpenAI API key click here)

import os
os.environ[“OPENAI_API_KEY”]=your-api-key

Now we are able to start working with ChatGPT inside Jupyter Lab

Magic commands

Let’s generate code using the %%ai magic command.

%%ai chatgpt
A function that computes the bottom common multiples of two integers, and
a function that runs 5 test cases of the bottom common multiple function

In the event you run the command above, you’ll get the next output.

As you’ll be able to see, by default, the output of an %%ai command can be formatted as markdown by default. You’ll be able to change the format of the output to code, image, markdown, math, HTML, JSON, and text by utilizing the -f argument of the magic command.

Let’s set the format to code now.

%%ai chatgpt -f code
A function that computes the bottom common multiples of two integers, and
a function that runs 5 test cases of the bottom common multiple function

Now the output is inserted in one other cell.

You may also generate math formulas in markdown format.

In, Out, and Err

If you wish to execute a prompt using code that the kernel knows about, nevertheless it’s not in the present cell, you need to use the curly brace syntax to incorporate variables and other Python expressions in your prompt.

This is very useful when you wish to explain code positioned elsewhere in a Jupyter notebook. Say I actually have the code below with its input positioned to In[12].

for i in range(1,9):
print(i)

Now I can discuss with In[12] in an %%ai magic command to get an evidence.

Besides In, other special lists with interpolation syntax are Out and Err that could be helpful each time you wish to work with the output or error you get.

Here’s an example of the Err.

Using the chat interface

The chat interface positioned on the side panel can provide help to do similar things that we previously have seen with the %%ai magic command.

In case you wish to include a portion of your notebook in your prompt, just select the portion after which check the box “Include selection.”

In the instance below, I ask for an evidence of the code in my notebook.

One cool feature the chat interface has is to generate a complete notebook from a text prompt. You simply have to send a message starting with /generate like in the instance below.

/generate a demo of methods to use the pandas library

After a couple of minutes, you must get the message below. In the event you go to the files section on the left panel, you’ll see a recent .ipynb file generated.

That’s it! For more information about Jupyter AI, visit the official doc.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here