{ "cells": [ { "cell_type": "raw", "id": "b0fa7a7f", "metadata": {}, "source": [ "---\n", "layout: post\n", "toc: true\n", "format:\n", " html: default\n", " ipynb: default\n", "date: '2024-05-28'\n", "categories:\n", "- python\n", "- nersc\n", "---" ] }, { "cell_type": "markdown", "id": "dec8a6e5", "metadata": {}, "source": [ "# AI Code Assistant at NERSC\n", "\n", "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!\n", "\n", "In this tutorial we will see how to activate Google AI Gemini on [Jupyter@NERSC](https://jupyter.nersc.gov/), 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.\n", "\n", "Notice that using the Gemini API you are sending your interactions with AI to Google, see the [Gemini API privacy policy](https://support.google.com/gemini/answer/13594961) for more details.\n", "\n", "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.\n", "\n", "For example we could use our own [conda environment at NERSC](./2024-05-02-python-nersc-conda.md).\n", "\n", "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.\n", "\n", "## Install packages\n", "\n", "First make sure you are running the right Jupyter Kernel, which should be running in your own conda environment, see above on instructions on how to create it.\n", "You need to install the packages just once, either from this notebook or from the command line after having activated the environment. Once the packages are installed, no need to run this cell anymore.\n", "\n", "Jupyter AI supports many model providers, each with different required packages:" ] }, { "cell_type": "code", "execution_count": null, "id": "c38ec60f", "metadata": {}, "outputs": [], "source": [ "%pip install jupyter-ai langchain-google-genai" ] }, { "cell_type": "markdown", "id": "ec95eae3", "metadata": {}, "source": [ "## Configure Google API key\n", "\n", "Next we need a Google API key to authenticate, go to:\n", "\n", "\n", "\n", "create a new secret key, and paste it below:" ] }, { "cell_type": "code", "execution_count": 9, "id": "31f2e432-d414-4354-ace1-182b0d2735d4", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: GOOGLE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" ] } ], "source": [ "%env GOOGLE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ] }, { "cell_type": "markdown", "id": "2a11f9fe", "metadata": {}, "source": [ "## Load and test Jupyter AI\n", "\n", "Finally we can test it" ] }, { "cell_type": "code", "execution_count": 3, "id": "ee6a0457-333e-4fdd-b11f-e635cf4e9e6b", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/global/common/software/cmb/zonca/conda/pycmb/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "%load_ext jupyter_ai" ] }, { "cell_type": "code", "execution_count": 4, "id": "6682d616-b67f-41fe-884c-1f19c9b75586", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/markdown": [ "| Provider | Environment variable | Set? | Models |\n", "|----------|----------------------|------|--------|\n", "| `gemini` | `GOOGLE_API_KEY` | | |\n" ], "text/plain": [ "gemini\n", "Requires environment variable: GOOGLE_API_KEY (set)\n", "* gemini:gemini-1.0-pro\n", "* gemini:gemini-1.0-pro-001\n", "* gemini:gemini-1.0-pro-latest\n", "* gemini:gemini-1.0-pro-vision-latest\n", "* gemini:gemini-pro\n", "* gemini:gemini-pro-vision\n", "\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%ai list gemini" ] }, { "cell_type": "code", "execution_count": 5, "id": "d62d0ae9-68c5-4717-a21d-145c9ca5fff2", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "AI generated code inserted below ⬇️" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": { "text/html": { "jupyter_ai": { "model_id": "gemini-pro", "provider_id": "gemini" } } }, "output_type": "execute_result" } ], "source": [ "%%ai gemini:gemini-pro -f code\n", "\n", "find indices of the largest 10 values in a numpy array" ] }, { "cell_type": "code", "execution_count": 7, "id": "c7ec34b2", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def find_indices_of_largest_10_values(array):\n", " \"\"\"Finds the indices of the largest 10 values in a numpy array.\n", "\n", " Args:\n", " array: A numpy array.\n", "\n", " Returns:\n", " A list of the indices of the largest 10 values in the array.\n", " \"\"\"\n", "\n", " # Find the indices of the largest 10 values in the array.\n", " indices = np.argsort(array)[-10:]\n", "\n", " # Return the list of indices.\n", " return indices" ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "pycmb", "language": "python", "name": "pycmb" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 5 }