Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Session 1.2: AI Assistant in Jupyter

DSC ScaDS.AI, Leipzig University

JupyterLab / Jupyter Notebooks offers several options for integrating and using AI assistants that can help us with programming and data analysis tasks.

For our training sessions, we want to use the AI assistant bia-bob, which is provided via a Python package.

In this notebook, we will look at the general use of the AI assistant - from importing and initializing to assisting with difficult tasks.

API Access

As explained in the slides, we want to use an LLM provider API that our AI assistant can connect to. For this, we need to provide the URL for the LLM endpoint (API), and an according API key. Both will be provided as so-called environment variables / env vars (a constant key-value pair available in memory).

To check what env vars are set and available right now, we can use the following commands in a code cell:

  • !printenv (bash/zsh terminal on Linux OS)

  • !set or !ls env: (command prompt or PowerShell on Windows OS)

  • %env (IPython Magic Command for Notebooks, OS agnostic)

You can also check for a specific env var, e.g. ENDPOINT_URL:

  • %env ENDPOINT_URL

# ToDo: Check the available env vars on your system

We now want to provide the URL for the LLM endpoint API and the API key as env vars.

To do this for JuypterLab in a virtual environment managed by uv:

  • Create a file secrets in the environments’ directory

  • Add the line: ENDPOINT_URL=url (replace url with the actual URL to the API)

  • Add the line: OPENAI_API_KEY=your-api-key (replace your-api-key with the actual key)

  • Stop JupyterLab

  • Restart JupyterLab with the content of this secrets file via:

uv run --env-file path/to/secrets jupyter lab
# Running this cell should now print the endpoint URL
%env ENDPOINT_URL
# Running this cell should now print your API key
%env OPENAI_API_KEY

Import and Initialize the AI Assistant

# Import os to read env vars
import os
# Import bia-bob as bob and additional helper functions
from bia_bob import bob, available_models, fix, doc

To intialize bob with any LLM provider, we can set the according URL to its API via the parameter endpoint:

bob.initialize(endpoint=os.getenv('ENDPOINT_URL'))

We can now print all available LLMs for this endpoint:

models = available_models(endpoint=os.getenv('ENDPOINT_URL'))
# Show some of the available models
display(models[10:20])
['glm-5.1', 'gpt-oss-120b', 'kimi-k2.5', 'kimi-k2.6', 'llama-4-scout-17b-16e-instruct', 'mini', 'mistral-medium-3.5', 'multilingual-e5-large-instruct', 'mxbai-embed-large:latest', 'nomic-embed-text-v1.5']

We can now initialize bob to use this endpoint and a specific LLM:

bob.initialize(
    endpoint=os.getenv('ENDPOINT_URL'),
    model="mini",
)

Use the AI Assistant

Prompting

The AI assistant can then be used in a code cell via IPython’s so-called Magic Commands (%, %%).

Either via:

%bob Single-line prompt. All is written into one line

...or via:

%%bob
Multi-line prompt.
With more details.
And more context.
%%bob 
Hello. 
Are you familiar with Machine Learning ? 
Please explain briefly. 
Just a short text output, no code nor code cell !
Loading...

Prompt augmentation

To inject the content of existing variables into prompts, we can augment a prompt by using {variable_name}.

Here we need to consider the size of the variable content, since LLMs only have a limited context window (number of tokens in a prompt and conversation)

%%bob
Read the names of LLMs provided in this list {models}.
For four of these models of your choice, explain what each model is capable of and when it should be used.
Loading...

Code generation and explanation

However, another useful application of the AI assistant is in generating and explaning code, e.g. for data analysis or exploration tasks. As a general rule, keep in mind that the more specific your prompt is, the better the generated results will be.

You can ask the AI assistant to explain code

  • Add the magic command for a multi-line prompt on top of the affected code cell, e.g.: %%bob Explain the following code in detail for beginners

You can also ask the AI assistant to add proper documentation to the code

  • Import the according function from bia-bob: from bia_bob import doc

  • Add the magic command %%doc on top of the according code cell

Let’s try it out

Our assistant will use libraries which are not available in your venv yet, .e.g. pandas. This will result in an error. You can use uv to install (add) it:

#!uv add pandas
%%bob
Generate Python code to do the following:
- Create a list of 10 random floats
- Compute the mean and the standard deviation
- Print the results
- Make use of the packages numpy and pandas, if necessary
- Just the code, NO comments at all
import numpy as np
import pandas as pd
rand_floats = np.random.rand(10).tolist()
series = pd.Series(rand_floats)
mean_new = series.mean()
std_new = series.std()

print(rand_floats)
print(mean_new)
print(std_new)

Fixing code errors

The AI assistant can also be used to fix erroneous code:

  • Import the according function from bia-bob: from bia_bob import fix

  • Add the magic command %%fix on top of the affected code cell

import numpy as np
import pandas as pd

rand_floats = np.random.rand(10).tolist()
series = pd.Series(rand_flo)
mean_new = series.mean()
std_new = series.std()

print(rand_floats)
print(mean_new)
print(std_new)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[60], line 5
      1 import numpy as np
      2 import pandas as pd
      3 
      4 rand_floats = np.random.rand(10).tolist()
----> 5 series = pd.Series(rand_flo)
      6 mean_new = series.mean()
      7 std_new = series.std()
      8 

NameError: name 'rand_flo' is not defined