A lightweight AI agent library powered by Ollama with built-in tools and custom tool support
Project description
Ollama Agent
A lightweight AI agent library powered by Ollama with built-in tools and custom tool support.
Features
- Simple, intuitive API
- 10 built-in tools (web search, calculator, weather, system info, etc.)
- Easy custom tool registration via decorators or functions
- Configurable via environment variables or constructor parameters
- Optional user approval for dangerous operations
- Conversation history management
Installation
pip install ollama-agent
Or install from source:
git clone https://github.com/aashish-thapa/ollama-agent.git
cd ollama-agent-lib
pip install -e .
Prerequisites
- Python 3.10+
- Ollama installed and running locally
- A model pulled (e.g.,
ollama pull llama3.2)
Quick Start
from ollama_agent import OllamaAgent
# Create an agent
agent = OllamaAgent()
# Run a query
response = agent.run("What's the current time?")
print(response)
# Run another query (conversation history is maintained)
response = agent.run("And what's the weather in New York?")
print(response)
# Reset conversation when needed
agent.reset()
Custom Tools
Using Decorators
from ollama_agent import OllamaAgent, register_tool
@register_tool("greet", description="Greet someone by name. Input: name")
def greet(name: str) -> str:
return f"Hello, {name}! Nice to meet you."
@register_tool("add_numbers", description="Add two numbers. Input: two numbers separated by comma")
def add_numbers(input_str: str) -> str:
a, b = map(float, input_str.split(","))
return f"Result: {a + b}"
agent = OllamaAgent()
response = agent.run("Greet Alice")
Using Function Registration
from ollama_agent import OllamaAgent, register_tool_func
def my_tool(input_str: str) -> str:
return f"Processed: {input_str}"
register_tool_func(
name="my_tool",
func=my_tool,
description="Process some input. Input: text to process"
)
agent = OllamaAgent()
Instance-specific Tools
from ollama_agent import OllamaAgent
agent = OllamaAgent()
# Add tool to this agent only
agent.add_tool(
name="uppercase",
func=lambda text: text.upper(),
description="Convert text to uppercase. Input: text"
)
response = agent.run("Convert 'hello' to uppercase")
# Remove when done
agent.remove_tool("uppercase")
Configuration
Via Constructor
from ollama_agent import OllamaAgent
agent = OllamaAgent(
model="llama3.2", # Ollama model name
base_url="http://localhost:11434", # Ollama API URL
temperature=0.7, # Model temperature (0-1)
max_iterations=10, # Max tool calls per query
approval_callback=my_callback, # Optional approval function
)
Via Environment Variables
export OLLAMA_MODEL=llama3.2
export OLLAMA_BASE_URL=http://localhost:11434
export TEMPERATURE=0.7
export MAX_ITERATIONS=10
export MAX_SEARCH_RESULTS=5
export REQUIRE_APPROVAL_COMMANDS=true
export REQUIRE_APPROVAL_FILES=false
Approval Callback
For dangerous operations (like shell commands), you can require user approval:
from ollama_agent import OllamaAgent
def approval_callback(tool_name: str, tool_input: str) -> bool:
"""Return True to allow, False to deny."""
print(f"Tool: {tool_name}")
print(f"Input: {tool_input}")
return input("Allow? (y/n): ").lower() == "y"
agent = OllamaAgent(approval_callback=approval_callback)
response = agent.run("List files in my home directory")
Built-in Tools
| Tool | Description |
|---|---|
web_search |
Search the web using DuckDuckGo |
get_current_time |
Get current date and time |
run_command |
Run shell commands (requires approval by default) |
system_info |
Get CPU, memory, disk, uptime info |
weather |
Get current weather |
calculator |
Evaluate math expressions |
read_file |
Read file contents |
list_directory |
List directory contents |
wikipedia |
Search Wikipedia |
ip_info |
Get public IP and location |
API Reference
OllamaAgent
class OllamaAgent:
def __init__(
self,
model: str = None,
base_url: str = None,
temperature: float = None,
max_iterations: int = None,
approval_callback: Callable[[str, str], bool] = None,
tools: dict = None,
config: Config = None,
): ...
def run(self, query: str, verbose: bool = False) -> str: ...
def reset(self) -> None: ...
def add_tool(self, name: str, func: Callable, description: str, requires_approval: str = None) -> None: ...
def remove_tool(self, name: str) -> None: ...
def get_history(self) -> list[dict]: ...
Tool Registration
# Decorator
@register_tool(name: str, description: str, requires_approval: str = None)
def my_tool(input: str) -> str: ...
# Function
register_tool_func(name: str, func: Callable, description: str, requires_approval: str = None)
# Unregister
unregister_tool(name: str)
# Query
get_all_tools() -> dict
list_tools() -> list[str]
get_tool(name: str) -> dict
License
MIT License - see LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ollama_agent-0.1.2.tar.gz.
File metadata
- Download URL: ollama_agent-0.1.2.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
298f76f649e62c37cf5c8a79f47d57649806767226f409aa0761ed551ecd629c
|
|
| MD5 |
cdfe0e598f1cb86021ee62597325fe44
|
|
| BLAKE2b-256 |
fd9f5514d5bcf14928b69d21c2d420cef7fcaeb5ba654e2f4488a7dabfd02c24
|
File details
Details for the file ollama_agent-0.1.2-py3-none-any.whl.
File metadata
- Download URL: ollama_agent-0.1.2-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
083dfbc0b7e5b88124520804e2fc0c7bed6dd7d72aef71e454e8d517b294a2b4
|
|
| MD5 |
a12cb8d680533401de6341ed3bae477a
|
|
| BLAKE2b-256 |
9eef492cc224cb0ead063dc107ecd93591522a35f6705ad69ba1a7db7370d8ec
|