AI Code Assistant at NERSC


May 28, 2024

If you are used to Github Copilot on VS Code or on Google Colaboratory as I am, coding without an AI assistant is so slow!

In this tutorial we will see how to activate Google AI Gemini on Jupyter@NERSC, this will work as well in any other Jupyter environment. I choose Google Gemini because it has a generous free tier allowance, ChatGPT instead requires to buy credits for using it through the API.

The good news is that we do not need to be in control of the environment running JupyterHub, we can successfully install what is necessary in a Jupyter kernel we control.

For example we could use our own conda environment at NERSC.

This web page was generated from a Notebook, from the sidebar you can directly download the source Notebook, upload it to Jupyter@NERSC and directly execute it there, no need to copy-paste.

Install packages

First make sure you are running the right Jupyter Kernel. Jupyter AI supports many model providers, each with different required packages:

%pip install jupyter-ai langchain-google-genai

Configure Google API key

Next we need a Google API key to authenticate, go to:

create a new secret key, and paste it below:

%env GOOGLE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
env: GOOGLE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Load and test Jupyter AI

Finally we can test it

%load_ext jupyter_ai
/global/common/software/cmb/zonca/conda/pycmb/lib/python3.10/site-packages/tqdm/ TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See
  from .autonotebook import tqdm as notebook_tqdm
%ai list gemini
Provider Environment variable Set? Models
%%ai gemini:gemini-pro -f code

find indices of the largest 10 values in a numpy array
AI generated code inserted below ⬇️
import numpy as np

def find_indices_of_largest_10_values(array):
  """Finds the indices of the largest 10 values in a numpy array.

    array: A numpy array.

    A list of the indices of the largest 10 values in the array.

  # Find the indices of the largest 10 values in the array.
  indices = np.argsort(array)[-10:]

  # Return the list of indices.
  return indices