python-ai-agent-ollama

agent
Security Audit
Pass
Health Pass
  • License — License: MIT
  • Description — Repository has a description
  • Active repo — Last push 0 days ago
  • Community trust — 25 GitHub stars
Code Pass
  • Code scan — Scanned 5 files during light audit, no dangerous patterns found
Permissions Pass
  • Permissions — No dangerous permissions requested

No AI report is available for this listing yet.

SUMMARY

A local AI agent built from scratch in Python with Ollama: conversation memory, system prompts, and multi-step tool calling.

README.md
Typing SVG



Python
Ollama
OpenAI API
Qwen3
License


Playlist What it covers
YouTube AI Fundamentals Tokens, embeddings, vector databases and RAG. Watch this first if the basics are new to you
YouTube Advanced AI This project, built step by step (videos 1 to 10 in the table above)

🚀 What is this?

A local AI agent built from scratch in Python. It talks to a model running on Ollama through the OpenAI-compatible API, so there is no paid key and no framework hiding the logic.

It remembers the conversation, follows a system prompt, and can use tools in several steps. For example, it first finds your city, then fetches the weather for that city.

🎬 Built as part of the Advanced AI series on codestorywithMIK.

📚 New to AI? Start with the AI Fundamentals playlist (tokens, embeddings, vector databases, RAG), then follow the Advanced AI playlist where this project is built step by step.

[!NOTE]
🌱 Work in progress: This project is in its early phase. As the Advanced AI playlist moves ahead, it will keep evolving, and we will add things like RAG, persistent memory and more tools.



🎥 See it in action

Screenshot 2026-10-20 at 1 54 28 AM
The grey lines are the debug trace: you can watch the agent think.

✨ Features

Feature Details
🧠 Conversation memory The full history is sent on every call, because the model itself has no memory
🎭 System prompt Sets the agent's behavior and style
🔧 Tool calling Tools are described with JSON schema and run by our own code
🔗 Chained tool calls A loop with a step limit (MAX_STEPS = 5) so it can never run forever
📝 Required parameters get_city_weather needs a city, declared in the schema
🛡️ Safe errors Tool failures go back to the model as text, so the program does not crash
🪲 Debug trace Grey step-by-step logs, switched on with DEBUG=True

🧩 How it works

The model never runs code. It only asks for a tool. Our Python code runs it, sends the result back, and the loop repeats until the model answers in plain text.

flowchart LR
    U([You]) --> M[simple_ai_assistant.py]
    M -->|history and tool list| L{{LLM on Ollama}}
    L -->|tool request| R[tools.run_tool]
    R --> T1[get_current_time]
    R --> T2[get_city_weather]
    R --> T3[get_user_city]
    T1 -->|result as text| M
    T2 -->|result as text| M
    T3 -->|result as text| M
    L -->|final answer| A([Assistant])
📖 The 4-step loop in words (click to expand)
  1. We send the conversation plus the list of tools to the model.
  2. The model replies with a request such as get_city_weather(city="Bangalore"), not an answer.
  3. Our code runs the real function and sends the result back with the tool role.
  4. The model reads the result and either asks for another tool or writes the final answer.

This repeats up to MAX_STEPS times per question.


🛠️ Available tools

Tool What it does
get_current_time Returns the current local date and time
get_city_weather(city) One-line weather from the free wttr.in service
get_user_city Returns the user's city (hard-coded to Bangalore as a demo value)

⚡ Quick start

Prerequisites: Python 3.10+ and Ollama installed and running.

# 1. Download the model
ollama pull qwen3:4b

# 2. Get the code
git clone https://github.com/MAZHARMIK/python-ai-agent-ollama.git
cd python-ai-agent-ollama

# 3. Create and activate a virtual environment
python -m venv .venv
# Windows:    .venv\Scripts\activate
# Mac/Linux:  source .venv/bin/activate

# 4. Install dependencies
pip install -r requirements.txt

# 5. Create your settings file
cp .env.example .env        # Windows: copy .env.example .env

# 6. Run
python simple_ai_assistant.py

Type exit, quit, bye or goodbye to stop.

Screenshot 2026-10-20 at 1 57 07 AM

⚙️ Configuration

Settings live in .env (see .env.example):

Name Meaning
BASE_URL OpenAI-compatible endpoint. For Ollama: http://127.0.0.1:11434/v1
API_KEY Required by the client, ignored by Ollama. Any dummy value works
MODEL Exact model name from ollama list. It must support tool calling
DEBUG Set to True (capital T) to show the grey debug trace

💡 Rules for .env: NAME=value, no spaces around =, no quotes. Restart the program after editing it.


📸 More screenshots

Screenshot 2026-10-20 at 1 58 20 AM
Screenshot 2026-10-20 at 2 02 02 AM

➕ Add your own tool

Three changes in tools.py, and the chat loop stays untouched:

  1. ✍️ Write the Python function.
  2. 📋 Add its description to the TOOLS list (name, description, parameters, required).
  3. 🗺️ Register it in FUNCTION_NAME_TO_FUNCTION_MAP.

The description matters: the model reads it to decide when to use the tool.


📁 Project structure

python-ai-agent-ollama/
├── simple_ai_assistant.py    chat loop, memory and the agent loop
├── tools.py                  tool functions, TOOLS schema, run_tool()
├── Utilities/
│   └── utilities.py          grey debug log() helper
├── .env.example              settings template
├── requirements.txt          dependencies
└── LICENSE                   MIT

⚠️ Honest limitations

  • Memory is kept in RAM and is lost when the program exits.
  • The history is not trimmed, so very long chats can exceed the model's context window.
  • Small local models can pick the wrong tool or skip arguments. A larger model is more reliable.
  • get_user_city is a hard-coded demo value. In a real app it would come from a profile or database.
  • The weather tool needs internet, and wttr.in is a free service that can be slow or down.
  • Tool output is not treated as untrusted, which is why the tools are read-only.

🙌 If this helped you, a ⭐ goes a long way. Thank you!

YouTube
WhatsApp
Instagram
LinkedIn

Licensed under MIT


☕ Support codestorywithMIK (No Pressure)

Education here is 100% free and always will be. 💙
If the content helped you and you'd like to support the channel with a coffee:

UPI: imammazhar4@ybl


Reviews (0)

No results found