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
Chapters
- 0:00:0000. The finished chatbot
- 0:00:2801. Setup: Ollama, a local model and Python
- 0:02:5102. The chat loop
- 0:04:5903. Memory and streaming answers
- 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 packagesREADME.md,LICENSE
How to run
- Install Ollama from ollama.com.
- Download the local model (about 1 GB).
- Create a virtual environment and install the requirements (
ollama==0.6.3,gradio==6.30.0). - 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:7860Ollama 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.
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.