AI Assitant in Jupyter#
Jupyter Notebook offers ways to integrate and use AI assistants that can support us in programming and data analysis tasks. There are several packages and plugins available, e.g. jupyter-ai, or bia-bob.
For our training school we want to make use of the AI assistant bia-bob.
In this notebook, we will look at the general use of the AI assistant - from importing and initialization to assistance with a programming task.
import os
# Import bia-bob as bob
from bia_bob import bob
Now we need to initialize bob
where we have to provide any API keys for LLM endpoints we want to use.
But: Never store API keys as plain text in a notebook
# Approach 1:
# Enter the API key via getpass and store it as an environment variable (env) via os.environ
# When this cell is executed, a field appears for entering the API key for Blablador
import getpass
api_key = getpass.getpass('Enter your Blablador API Key: ')
os.environ['BLABLADOR_API_KEY'] = api_key
Enter your Blablador API Key: ········
# Now initialize bia-bob with endpoint Blablador and model "alias-fast"
# API key gets read from the according environment variable
bob.initialize(endpoint='blablador', model='alias-fast')
Note on the LLMs used#
Blablador provides various LLMs:
an overview of these is provided after registering with the chat service
a complete list can be requested via the API.
We specify which provider and which specific LLM we use for the AI assistant during initialization via endpoint
and model
:
bob.initialize(endpoint=‘blablador’, model=‘alias-fast’)
In this case, we use an alias instead of the full model name – a list of aliases available at Blablador and their capabilities is provided here.
Most other providers, e.g. OpenAI, Mistral, Github Models, work similar - you also need to provide the correctly named according API key.
Control of the AI assistant#
The AI assistant can then be accessed in a code cell via IPython’s so-called Magic Commands (%
, %%
). Either via
%bob Single-line prompt
…or via
%%bob
Multi-line prompt.
With more details.
And more context.
%bob Hello. Are you familiar with Machine Learning? Please explain briefly what it is about.
Machine Learning (ML) is a subset of artificial intelligence that involves the development of algorithms and statistical models to enable computers to perform tasks without explicit instructions. These tasks can include classification, regression, clustering, and more. The key concept in ML is that the models learn from data, improving their performance over time as they are exposed to more data. This process typically involves training on a labeled dataset, where the model learns to make predictions or decisions based on the input data. Once trained, the model can then be used to make predictions on new, unseen data. The goal of ML is to automate and improve decision-making processes by leveraging data and computational power.
%%bob
Hello. Are you familiar with Gaussian processes and Bayesian Optimization ?
Briefly explain how they work and what they are used for.
Do not provide any code.
Gaussian processes (GPs) are a collection of random variables, any finite number of which have a joint Gaussian distribution. They are used for regression and classification in machine learning. In the context of Bayesian Optimization, GPs are used to model the relationship between inputs and outputs, enabling more efficient optimization of functions that are expensive or time-consuming to evaluate. The process involves defining a prior distribution over functions, using data to update this prior distribution, and then sampling from the posterior distribution to make predictions or decisions. Bayesian Optimization using GPs typically involves finding the optimal input values that maximize or minimize the objective function, often with the goal of minimizing the risk of overfitting and improving the generalization performance of the model.
Providing the API key#
Via the approach used above, we provide the API key for the LLM endpoint via an interactive input. This way, the whole process with the interactive input needs to be done in every notebook we want to make use of bia-bob
. However, it is more convenient to provide the API key automatically.
Via environment file#
We can also create a file holding our API key(s). Let’s name it api-keys
and put it in our home directory (referenced by ~
).
Now put a line for Blablador:
BLABLADOR_API_KEY=my-secret-api-key
Now we make use of dotenv
, a comnmon tool to read such files and provide us with according environment variables.
from pathlib import Path
from dotenv import load_dotenv
# Load this specific file from the home directory, overwrite variables if they exist
load_dotenv(dotenv_path=Path("~/api-keys").expanduser(), override=True)
# Now you can access the variables
print(os.getenv("BLABLADOR_API_KEY"))
my-secret-api-key
Now that this file is available we can make use of bia-bob
in any notebook by including and executing this code cell:
from pathlib import Path
from dotenv import load_dotenv
load_dotenv(dotenv_path=Path("~/api-keys").expanduser(), override=True)
from bia_bob import bob
bob.initialize(endpoint='blablador', model='alias-fast')
With conda#
In a conda environment, this can also be done via the conda configuration file conda-iom-env.yml
:
Enter your API key in the file in the line
BLABLADOR_API_KEY: “my_api_key”
- replacing my_api_keyExit Juyper Lab and deactivate the conda environment
Update the conda environment via
conda env update -f path/to/conda-iom-env.yml --prune
Restart the conda environment and restart Jupyter Lab
If you now execute only the following code cell of this notebook, BLABLADOR_API_KEY
with your API key should also appear among the listed environment variables.
!env
The initialization and use of the AI assistant in the following notebooks of the training now also works without any additional entry or reading of the API key und you can just run:
from bia_bob import bob
bob.initialize(endpoint='blablador', model='alias-fast')