AI Assitant in Jupyter#

Jupyter Notebook offers options of integrating and using AI assistants that can support us in programming and data analysis tasks.

For our training school we want to make use of the AI assistant bia-bob, which is provided via a Python library.

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, DEFAULT_SYSTEM_PROMPT
# 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
# 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 basic behavior of an AI assistant (the underlying LLM) can be controlled via a so-called system prompt.

A system prompt is a specific instruction to the AI assistant to control its behavior, tone, or style.

# Show the default system prompt of bia-bob
print(DEFAULT_SYSTEM_PROMPT[:640])
You are a extremely talented bioimage analyst and you use Python to solve your tasks unless stated otherwise.
If there are several ways to solve the task, chose the option with the least amount of code.    

## Python specific instructions

When writing python code, you can only use those libraries: {libraries}.
If you create images, show the results and save them in variables for later reuse.
{reusable_variables}
NEVER overwrite the values of the variables and functions that are available.

## Python specific code snippets

If the user asks for those simple tasks, use these code snippets.

{additional_snippets}
{builtin_snippets}

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 image analysis methods for medical imagery ? 
Briefly explain how they work and what they are used for.

Image analysis methods for medical imagery are used to extract features and information from medical images, such as MRI, CT, or X-ray scans. These methods are crucial in medical diagnosis, treatment planning, and research. Image analysis techniques can include segmentation, feature extraction, classification, and registration. For example, segmentation involves dividing an image into different regions or objects, which can help identify and isolate important structures in an image. Feature extraction involves identifying and quantifying characteristics of the image, such as edges, textures, or shapes. Classification involves assigning images or regions to specific categories, such as normal versus abnormal, based on their features. Registration is used to align multiple images from different modalities or different time points to enable comparison and analysis. These methods are often used in conjunction with machine learning algorithms to improve accuracy and efficiency.

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 the conda environment via the conda configuration file day0_preparation/conda-ai4med-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-ai4med-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?

Now, we want to use bob to solve the undone exercises from the Python basics session. Ask bob to generate according code, guide it with according prompts

%%bob

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.

In this example, we will use the experimental LLM infrastructure of Uni Leipzig and a Llama-3 model.

from bia_bob import bob
bob.initialize(
    endpoint='https://kiara.sc.uni-leipzig.de/api', 
    model='vllm-meta-llama-llama-3-3-70b-instruct', 
    api_key='sk-...')
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
%%bob
Are you familiar with the Python programming language?
Give me a short code example on Python list handling.

I am familiar with the Python programming language and can provide examples of Python list handling.

# Create a sample list
new_list = [1, 3, 2, 5, 4]

# Print the original list
print("Original list:", new_list)

# Indexing
print("First element:", new_list[0])

# Append an element
new_list.append(6)
print("List after appending:", new_list)

# Sort the list
new_list.sort()
print("Sorted list:", new_list)
Original list: [1, 3, 2, 5, 4]
First element: 1
List after appending: [1, 3, 2, 5, 4, 6]
Sorted list: [1, 2, 3, 4, 5, 6]