Tutorial
Accessing IBM Granite LLMs in Jupyter Notebook through Ollama
Experiment with LLMs in your local environmentIBM Granite is IBM's family of open, performant, and trusted large language models (LLMs), built to deliver AI-driven insights and capabilities.
For developers or data scientists who are interested in experimenting with the Granite models, Ollama provides a way to run the models locally. By using Jupyter Notebook and Python scripts, you can easily invoke the Granite models through Ollama.
This tutorial walks you through the step-by-step process to access one of the IBM Granite models in a Jupyter Notebook using Ollama. The same setup can also be applied to access other LLMs.
The following diagram shows the architecture of this setup.

Prerequisite
You need to install Python 3.x on to your system.
Steps
Step 1. Set up Ollama
Ollama is a tool designed to run and interact with large language models (LLMs) locally on your computer rather than in the cloud. It provides a streamlined interface for deploying and managing models directly on local hardware.
Start the Ollama app.
ollama serveDownload any Granite model to your system. In this tutorial, I chose
granite3-dense:2b.ollama pull granite3-dense:2bVerify that the model downloaded successfully.
ollama listYou should get the below output.
NAME ID SIZE MODIFIED granite3-dense:2b a9c7deef7ab8 1.6 GB 2 hours ago
Ollama is now ready to serve content from the downloaded LLM.
Step 2. Set up JupyterLab
Next, you need to install JupyterLab in a Python virtual environment.
Go to a folder where you want to install JupyterLab. For example, this is my folder:
cd /Users/gandhi/GandhiMain/998-workCreate a virtual environment called
my-jupyter-envand activate it by using these commands:python -m venv my-jupyter-env source my-jupyter-env/bin/activateInstall JupyterLab.
pip install jupyterlabRun JupyterLab.
jupyter labJupyterLab will start and open in a new browser window at the URL
http://localhost:8888/lab/workspaces/auto-U.
Step 3. Create a Jupyter Notebook
Let's create a Jupyter Notebook with a Python script to access the downloaded IBM Granite model.
In the JupyterLab window, click the + button in the top.

In the Notebook section of the navigator window, choose the "Python 3 (ipykernel)" tile.

Click the Save button to save the notebook.

Enter a name for the notebook, then click the Rename button.

The notebook is saved in the given name.

Step 4. Insert scripts into the notebook
Next, you need to update the notebook to access and use the downloaded LLM.
Create four separate cells in the notebook. Then, copy these four scripts into those four separate cells.
Cell 1:
!pip install langchain-ollamaCell 2:
model_id = "granite3-dense:2b"Cell 3:
from langchain_ollama.llms import OllamaLLM model = OllamaLLM(model=model_id)Cell 4:
prompt = "What is Kubernetes" response = model.invoke(prompt) print(response)Verify that your notebook looks like this.

These scripts, including MD content surrounding the them, are available in the sample.ipynb notebook here. You can copy and paste this into your notebook and execute it.

Step 5. Execute the Jupyter Notebook
Let's execute the notebook, to see the IBM Granite model in action.
Ensure the kernel is pointing to
Python 3 (ipykernel)in the top right corner.
Run all four cells to see the output.

We have successfully executed the notebook and called the IBM Granite model via ollama.
Step 6. Close the apps in our local environment
First, close JupyterLab by choosing File > Shutdown from the menu.

Deactivate the virtual environment.
deactivateGo to the terminal window where Ollama is running, and press Ctrl+C.
Conclusion
As demonstrated in this tutorial, by using Ollama with JupyterLab, you can experiment with the LLMs in your local environment.