GenAI L12 Accessing Ollama in Python (Jupyter Notebook)

Duration: 16 min

This video lesson is available to enrolled students.

Enroll to watch — CDAC C-CAT Preparation

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This lecture introduces students to accessing the Ollama local AI model library using Python within a Jupyter Notebook environment. The session begins with foundational setup procedures, guiding learners through the installation of Python on a Windows operating system. Key steps include downloading the official Python installer, configuring system settings for long path support, and verifying the installation via command line. The instructor then addresses common environment configuration issues, specifically troubleshooting 'jupyter' command not found errors by utilizing the python -m pip method. Once the environment is established, the lesson transitions to practical application by demonstrating how to import the Ollama library and execute chat functions. Students learn to define system prompts, query local models like 'gemma2:2b', and handle connection errors such as localhost refused to connect. The video emphasizes the workflow from raw environment setup to successful API interaction, providing a complete end-to-end guide for local AI integration.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video opens with an introduction to the session's core principles, focusing on the foundational concepts required for accessing Ollama in Python. The instructor sets the stage by outlining the necessary prerequisites and the general workflow for local AI integration. Visual demonstrations of key ideas are presented to establish context before diving into technical setup.

  2. 2:00 5:00 02:00-05:00

    This segment details the installation of Python on Windows. The instructor navigates to python.org/downloads and clicks 'Download Python install manager'. Evidence includes the on-screen text 'python-manager-25.2.msix' and the browser history showing the download completion. The user then opens File Explorer to locate the executable. During installation, a configuration helper window appears stating 'Windows is not configured to allow paths longer than 260 characters', prompting the user to update settings. The progress bar reaches 'Installing 99%...' before completion.

  3. 5:00 10:00 05:00-10:00

    The instructor verifies the Python installation by checking the version in PowerShell, displaying 'Python 3.14.2' and 'pip 25.3'. A troubleshooting phase follows where the 'jupyter notebook' command fails with a CommandNotFoundException error. To resolve this, the instructor runs 'python -m pip install notebook' to ensure the correct package manager is used. The terminal output shows 'Collecting argon2-cffi-bindings' and other dependencies being installed. This section highlights the importance of verifying environment variables and using specific pip flags to avoid path resolution issues.

  4. 10:00 15:00 10:00-15:00

    The focus shifts to Jupyter Notebook usage. The user renames 'Untitled.ipynb' to 'myfirstnb'. A cell is executed containing the code print("hello python"). The instructor then imports the Ollama library using 'import ollama' and lists available models with 'ollama list'. A chat function is initiated where the model is set to 'gemma2:2b' and a user prompt asks, "what is machine learning in 50 words". The code structure includes defining a system role and content to guide the AI assistant.

  5. 15:00 15:59 15:00-15:59

    In the final segment, the video demonstrates running a Python script to interact with the Ollama API. The code defines a chat session using 'stream = ollama.chat' with specific system and user prompts. A connection error occurs, displaying 'This site can't be reached' and 'localhost refused to connect', indicating the Ollama service was not running. The instructor re-runs the code successfully, generating the output: 'Machine learning is a field of computer science where algorithms learn from data'. This concludes the session by showing how to handle API connection issues and retrieve streaming responses.

The lecture provides a comprehensive, step-by-step guide for setting up a Python environment to interact with local Large Language Models via Ollama. The progression moves logically from system-level configuration to application-level coding. Key technical takeaways include the necessity of configuring Windows long path settings during Python installation, as indicated by the '260 characters' warning. The troubleshooting of Jupyter installation demonstrates a critical best practice: using 'python -m pip' instead of just 'pip' to ensure packages are installed in the correct environment. The practical coding section illustrates the standard API call structure for Ollama, specifically using the 'ollama.chat' method with a dictionary containing 'role' and 'content'. Students learn that local models require the Ollama service to be running, evidenced by the 'localhost refused to connect' error. The successful output confirms that once connected, the library can stream responses from models like 'gemma2:2b'. This workflow is essential for students building local AI applications without relying on cloud APIs.

Loading lesson…