Ollama Python Tutorial: Run Local LLMs with Chat, Streaming, Structured Outputs, Tools and Embeddings (2026) Python sqlite3 Tutorial: Tables, Transactions, JSON and Upserts with the Built-in Database (2026) Convert Python to EXE with PyInstaller: One-File Apps, Data Files and Hidden Imports (2026) Two-Factor Authentication in Python with pyotp: TOTP, QR Codes and FastAPI (2026) Hash Passwords in Python with Argon2: pwdlib, argon2-cffi and bcrypt Migration (2026) Encrypt and Decrypt in Python with Fernet: Strings, Files and Passwords (2026) psutil Tutorial: Monitor CPU, Memory, Disk and Processes in Python (2026) PyMuPDF Tutorial: PDF to Image, Extract Text and Images in Python (2026) MoviePy 2 Tutorial: Edit Videos with Python (Trim, Text, Merge, Audio) 2026 FastAPI Dependency Injection with Depends: Annotated, Yield and Overrides (2026) HTML to PDF in Python with WeasyPrint and Jinja2: Invoices and Reports (2026) Extract Tables from PDF in Python with pdfplumber: CSV, Excel and pandas (2026) OCR in Python: Extract Text from Images and Scanned PDFs with RapidOCR (2026) Text to Speech in Python with Piper: Offline Natural Voices, WAV Files and a TTS API (2026) Speech to Text in Python with faster-whisper: Transcribe Audio and Make Subtitles (2026) Send Email with Python: smtplib, HTML, Attachments and Bulk Mail (2026) Python QR Code Generator: Styled QR Codes with qrcode (2026) Pillow 13: Resize, Crop and Watermark Images in Python (2026) Parallel Programming with Dask in Python 2026: Intro Guide MarkItDown: Convert Files to Markdown for LLMs in Python (2026) Plotly Express: Interactive Charts in Python (2026) pandera: Validate pandas DataFrames in Python (2026) jsonschema: Validate JSON Data in Python (2026) Faker: Generate Fake Test Data in Python (2026) RapidFuzz: Fuzzy String Matching in Python (2026) sqlglot: Parse and Transpile SQL in Python (2026) structlog: Structured Logging in Python (2026) python-pptx in 2026: Automate PowerPoint Reports with Python python-docx: Create and Edit Word Documents with Python (2026) pypdf: Merge, Split and Edit PDFs with Python (2026) View all in this category →

Build an AI Chatbot in Python in 10 Minutes (Free, Local, No API Key) – Source Code

Build a free AI chatbot that runs entirely on your own computer. Ollama serves a small local model (qwen2.5:1.5b), so there is no API key, no paid service and your chats stay private. You write a terminal chat loop, give it memory and streaming answers, then wrap it in a web chat UI with Gradio, all in about 40 lines of Python.

Duration
11:26 (5 chapters)
Tools
Python 3, Ollama, qwen2.5:1.5b, ollama 0.6.3, Gradio 6.30.0, VS Code

Watch the video

Watch on YouTube

Chapters

  1. 0:00:0000. The finished chatbot
  2. 0:00:2801. Setup: Ollama, a local model and Python
  3. 0:02:5102. The chat loop
  4. 0:04:5903. Memory and streaming answers
  5. 0:07:3404. A web UI with Gradio, demo and wrap-up

What's in the zip

  • chat.py – terminal chatbot with memory and streaming answers (ch 2-3)
  • app.py – web chatbot with Gradio (ch 4)
  • requirements.txt – pinned pip packages
  • README.md, LICENSE

How to run

  1. Install Ollama from ollama.com.
  2. Download the local model (about 1 GB).
  3. Create a virtual environment and install the requirements (ollama==0.6.3, gradio==6.30.0).
  4. Run the terminal or web chatbot.
ollama pull qwen2.5:1.5b
python -m venv .venv
# Windows: .venv\Scripts\activate    macOS/Linux: source .venv/bin/activate
pip install -r requirements.txt
python chat.py      # terminal chatbot, type quit to stop
python app.py       # web chatbot at http://127.0.0.1:7860

Ollama must be running in the background. If you switch to a paid cloud API later, never paste the key into your code: read it from an environment variable.

Download source code

File: ai-chatbot-code.zip

Size: 2.3 KB (2,318 bytes)

SHA-256:
24ef2e8b182d8f7481d83e2c93ba7dbc1a110a390226b2ce57df42f583015f65

Licence: code is released under the MIT licence, copyright PyInns. See LICENSE and README inside the zip. Free learning code, not production code.