GenAI L12 Accessing Ollama in Python (Jupyter Notebook)

Duration: 16 min

This video lesson is available to enrolled students.

Return to /learn/CAPGEMINI-PREPARATION/ai-literacy/ai-ml-by-arun-sir/ollama/asset-genai-l12-accessing-ollama-in-python-jupyter-notebook after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. English Communication: Communication Assessment, English Writing Ability, Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions)
  2. Artificial Intelligence (AI Literacy): AI & ML by Arun Sir
  3. Programming, Logic & Problem Solving (Technical Assessment): Language Design, Pseudo Codes
  4. DSA (Technical Assessment): Introduction to DS, Array, Stack, Queue, Linked List
  5. Software Engineering (Technical Assessment): Fundamentals of Software Engineering, Software Requirements and Quality Assurance, Software Design, Estimation and Metrics, Software Testing, Software Maintenance and Configuration Management
  6. OOPs (Technical Assessment): Object Oriented Programming (OOPS)
  7. DBMS (Technical Assessment): Basics of DBMS, ER Diagram, Relational Model & Functional Dependencies, Keys & Integrity Constraints, Normalization (1NF - BCNF), Decomposition Properties & 4NF, File Organization & Indexing, Relational Algebra, Relational Calculus, Transaction Management, Concurrency Control, Database Recovery, ORDBMS, Database Security & Authorization, Query Processing & Optimization, Enhanced Data Models, Data Warehousing & Mining, Big Data Systems, NoSQL
  8. SQL (Technical Assessment): SQL
  9. Networking (Technical Assessment): Introduction to CN, Data Communication, DLL: Access Control, DLL: Flow Control, DLL: Error Control, DLL: Framing, Data Link Layer - Ethernet, Net Layer: IPv4 & Proto, Net Layer: IP Addressing, Net Layer:Routing Protocol, Transport Layer Services, TL: Congestion & UDP, Application Layer, Hardware basics, Network Security, Mobile Technology
  10. Modern Engg Awareness (Technical Assessment): Emerging Tech Trends, Cyber and Info Security
  11. Debugging Assessment: Coding Questions by Yash Sir, C Programming by Yash Sir, Programming in C++, Java
  12. Cognitive Assessment: Gamified / Game Based Aptitude, Seating Arrangements, Puzzle Solving (Puzzles), Data Arrangement, Blood Relations, Directions (Direction Test), Coding Decoding, Series (Number and Letter Series) (Numerical Relations and Reasoning), Logical Sequence Of Words, Ranking, Order and Sequence, Data Sufficiency, Inequality, Venn Diagram, Syllogisms, Cubes & Dices, Calendar, Clock, Analytical Reasoning (Counting Figures Reasoning), Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Selection Decision Table (Decision Making), Behavioral Assessment (Behavioral Competency Profiling) (Psychometric Test)
  13. HR Interview: MR & HR Interview 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…