Building Tool-Calling Local AI Agents with Python

Introduction

This article outlines the process of building a local AI agent in Python utilizing Ollama and Qwen3. The purpose of this agent is to perform tasks such as searching, reading, and remembering information stored in a local project folder without the need for online services.

What You Will Learn

The article will cover the following key topics:

  • Setting up Ollama with a tool-capable model.
  • Understanding the tool-calling exchange mechanism.
  • Writing efficient and safe Python functions as tools.
  • Building the agent loop and integrating short-term and long-term memory.

Setup and Installation

To create the local AI agent, firstly, you need to install Ollama. This can be done via the official download page. It is recommended to have Python 3.10 or newer installed. After installation, download the Qwen3 model, which supports tool calling and is about 2.5 GB in size.

Next, create a project directory and set up a virtual environment to keep dependencies organized:

mkdir local-agent && cd local-agent
python -m venv .venv
source .venv/bin/activate
pip install ollama

Understanding Tool-Calling

The AI model does not execute tasks independently; it generates structured requests for specific actions. The interaction involves four roles: system, user, assistant, and tool. Each tool call consists of a name and an arguments dictionary, which your code executes, returning the result to the AI model as a new message.

Designing the Tools

For effective operation, the agent will employ five specific tools:

  • list_files to display the contents of a folder.
  • search_files to locate lines containing a specific term.
  • read_file to read specified lines from a file.
  • recent_files to identify files modified within a certain timeframe.
  • remember to store information for future sessions.

Writing the Tools

Using Ollama’s Python library, each tool is built as a plain function. The libraries automatically generate the tool schema, meaning that comprehensive documentation within the code is critical. The attributes include return types and argument descriptions, ensuring the model can understand their functionalities.

Building the Agent Loop

The agent loop connects the user interface to the model, processes tool requests, and combines the outcomes. This loop consists of functions to handle input, call tools, and manage results, ensuring each interaction flows logically until a final output is obtained.

Adding Memory

The AI agent utilizes both short-term and long-term memory mechanisms. Short-term memory involves retaining the last few interactions in a message list, while long-term memory is achieved through a JSON file that stores facts across sessions. This allows the model to refer back to previously stored information during subsequent interactions.

Running and Testing the Assistant

After setting everything up, run the assistant by pointing it to a project folder. You can use the following command:

python assistant.py ~/my-project

Engage with the AI agent by issuing various prompts to evaluate its functionality against the tools implemented, ensuring that it operates as expected.

Conclusion

The completion of this project results in a functional local AI agent capable of efficiently searching, reading, and remembering information. Users are encouraged to test the assistant across different project structures and refine its capabilities based on observations from the tool log found in the output.

Source: machinelearningmastery.com

Related Articles

The surge of public records requests, particularly driven by artificial intelligence (AI), is straining America’s open records systems. Companies such...
Overview of FigureLabs FigureLabs is an AI-powered tool designed to assist researchers in creating clear and informative...
Introduction Setting up light and dark mode in a Next.js project using the shadcn/ui framework is largely automated. This guide walks...