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 difficult tasks.

import os
# Import bia-bob as bob
from bia_bob import bob
# Import library to prompt for a password
import getpass
# Never store API keys as plain text in a notebook
# Better: 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
api_key = getpass.getpass('Enter your Blablador API Key: ')
os.environ['BLABLADOR_API_KEY'] = api_key
Enter your Blablador API Key:  ········
# Initialize bia-bob with Blablador and API key from environment variable
bob.initialize(endpoint='blablador', model='alias-fast')
This notebook may contain text, code and images generated by artificial intelligence. Used model: alias-fast, vision model: None, endpoint: https://helmholtz-blablador.fz-juelich.de:8000/v1, bia-bob version: 0.34.3.. Do not enter sensitive or private information and verify generated contents according to good scientific practice. Read more: https://github.com/haesleinhuepf/bia-bob#disclaimer

Note on the LLMs used#

Blablador provides various LLMs – an overview of these is provided after registering with the chat service, and 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 (AI) that involves training algorithms to make predictions or decisions based on data, without being explicitly programmed. It leverages statistical techniques, mathematical models, and computational algorithms to enable systems to learn from and make inferences on data. The goal of ML is to develop models that can automatically improve their performance over time as they are exposed to more data. There are three main types of ML: supervised learning, unsupervised learning, and reinforcement learning.

%%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.

Yes, I am familiar with Gaussian processes and Bayesian Optimization.

Gaussian processes (GPs) are a type of stochastic process that are defined by a mean function and a covariance function. They are used for various tasks, such as regression, classification, and function approximation. The mean function determines the average value of the process, while the covariance function determines how the values in different parts of the input space are related to each other.

Bayesian Optimization is a sequential design method for global optimization of black-box functions that do not have a standard optimization algorithm that is guaranteed to find the global optimum. It is used to find the minimum or maximum of a function that may be noisy and has no closed-form solution. Bayesian Optimization works by modeling the function using a GP and using this model to select the next point to evaluate. The goal is to find the optimal point that minimizes the expected loss of the function.

Gaussian processes and Bayesian Optimization are often used in machine learning and optimization tasks to efficiently and effectively find the optimal solution to a problem.

Providing the API key#

In this notebook, we provide the API key for Blablador via an interactive input in the first code cell. However, it is more convenient to provide the API key as a permanent environment variable.

In a conda environment, this can 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_key

  • Exit Juyper Lab and deactivate the conda environment

  • Update the conda environment via conda env update -f 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 the separate entry of the API key like in the first cell. Let’s try:

# Import bia-bob as bob
from bia_bob import bob
# Initialize bia-bob with Blablador and API key from environment variable
bob.initialize(endpoint='blablador', model='alias-fast')
This notebook may contain text, code and images generated by artificial intelligence. Used model: alias-fast, vision model: None, endpoint: https://helmholtz-blablador.fz-juelich.de:8000/v1, bia-bob version: 0.32.0.. Do not enter sensitive or private information and verify generated contents according to good scientific practice. Read more: https://github.com/haesleinhuepf/bia-bob#disclaimer
%bob Hi - who are you ?

Hello! I’m an AI assistant designed to help with a variety of tasks and answer questions to the best of my ability. I’m here to assist you with information, explanations, suggestions, and more. How can I assist you today?

Custom Endpoints (Providers)#

Beside keyword-based endpoints like blablador, ollama, etc., bia-bob also allows to define custom endpoints (providers).

Just provide the providers URL to the API, the name of the model you want to use, and the API key.

from bia_bob import bob
bob.initialize(
    endpoint='https://url-to-llm-provider/api', 
    model='model-name', 
    api_key='api-key-for-provider')
This notebook may contain text, code and images generated by artificial intelligence. Used model: vllm-meta-llama-llama-3-3-70b-instruct, vision model: None, endpoint: https://kiara.sc.uni-leipzig.de/api, bia-bob version: 0.34.3.. Do not enter sensitive or private information and verify generated contents according to good scientific practice. Read more: https://github.com/haesleinhuepf/bia-bob#disclaimer