Skip to main content

A comprehensive Python client library for the Ollama API

Project description

Ollama Toolkit Python Client

PyPI version License: MIT Python 3.8+

A comprehensive Python client library and command-line tools for interacting with the Ollama API. This package provides easy access to all Ollama Toolkit endpoints with intuitive interfaces, complete type hints, and detailed documentation.

Features

  • ๐Ÿš€ Complete API Coverage: Support for all Ollama Toolkit endpoints
  • ๐Ÿ”„ Async Support: Both synchronous and asynchronous interfaces
  • ๐Ÿ”ง Built-in CLI: Powerful command-line tools for Ollama interaction
  • ๐Ÿ”Œ Auto-Installation: Can automatically install and start Ollama if needed
  • ๐Ÿ’ช Robust Error Handling: Comprehensive error types and fallback mechanisms
  • ๐Ÿ“Š Embeddings Support: Easy creation and manipulation of embeddings
  • ๐Ÿงช Well-Tested: Comprehensive test suite for reliability

EIDOSIAN Excellence

Every part of this toolkit embraces Eidosian Principles, ensuring:

  • Precision for style, humor for clarity
  • A seamless flow in usage
  • Self-awareness to continually refine your process

Installation

Prerequisites

  1. Ensure you have Python 3.8+ installed
  2. Install Ollama on your system
  3. Start the Ollama service by running ollama serve in a terminal

Install from PyPI

pip install ollama-api

Install from source

git clone https://github.com/lloydhd/ollama_toolkit.git
cd ollama_toolkit
pip install -e .
  • From PyPI:
    pip install ollama-toolkit
    
  • For development:
    pip install -e /path/to/ollama_toolkit
    

Verifying Installation

After installation, you can verify everything is working by running:

# Run all tests
python -m pytest ollama_toolkit/tests

# Run specific test modules
python -m pytest ollama_toolkit/tests/test_client.py

You can also run a quick import test to ensure the package is accessible:

python -c "import ollama_toolkit; print(f'Ollama Toolkit version: {ollama_toolkit.__version__}')"

Quick Start

from ollama_toolkit import OllamaClient

# Initialize the client
client = OllamaClient()

# Get Ollama version
version = client.get_version()
print(f"Ollama version: {version['version']}")

# Generate text (non-streaming)
response = client.generate(
    model="llama2",
    prompt="Explain quantum computing in simple terms",
    options={"temperature": 0.7}
)

print(response["response"])

# Generate text (streaming)
for chunk in client.generate(
    model="llama2", 
    prompt="Write a short poem about AI", 
    stream=True
):
    if "response" in chunk:
        print(chunk["response"], end="", flush=True)

Async Support

The library also supports async operations:

import asyncio
from ollama_toolkit import OllamaClient

async def main():
    client = OllamaClient()
    
    # Async generation
    response = await client.agenerate(
        model="llama2",
        prompt="Explain how neural networks work"
    )
    print(response["response"])
    
    # Async streaming
    async for chunk in client.agenerate(
        model="llama2",
        prompt="Write a haiku about programming",
        stream=True
    ):
        if "response" in chunk:
            print(chunk["response"], end="", flush=True)

asyncio.run(main())

Automatic Ollama Installation

This package can automatically check for Ollama installation and help you install it:

from ollama_toolkit.utils.common import ensure_ollama_running

# Check and optionally install/start Ollama
is_running, message = ensure_ollama_running()
if is_running:
    print(f"Ollama is ready: {message}")
else:
    print(f"Ollama setup failed: {message}")

You can also use the provided CLI tool:

# Check if Ollama is installed and running, install if needed
python -m ollama_toolkit.tools.install_ollama

# Check only, don't install or start
python -m ollama_toolkit.tools.install_ollama --check

# Install Ollama if not already installed
python -m ollama_toolkit.tools.install_ollama --install

# Start Ollama if not already running
python -m ollama_toolkit.tools.install_ollama --start

# Restart Ollama server
python -m ollama_toolkit.tools.install_ollama --restart

Command-Line Interface

The package includes a comprehensive CLI:

# Main CLI command with subcommands
python -m ollama_toolkit.cli --help

# List available models
python -m ollama_toolkit.cli list-models

# Generate text
python -m ollama_toolkit.cli generate llama2 "Explain quantum computing"

# Chat with a model
python -m ollama_toolkit.cli chat llama2 "Tell me a joke" --system "You are a comedian"

# Create embeddings
python -m ollama_toolkit.cli embedding llama2 "This is a test sentence"

# Model management
python -m ollama_toolkit.cli pull llama2
python -m ollama_toolkit.cli model-info llama2
python -m ollama_toolkit.cli copy llama2 llama2-backup
python -m ollama_toolkit.cli delete llama2-backup

# Get Ollama version
python -m ollama_toolkit.cli version

API Documentation

Core Client Methods

  • generate(model, prompt, options=None, stream=False) - Generate text completions
  • chat(model, messages, options=None, stream=False) - Generate chat completions
  • list_models() - Get list of available models
  • get_model_info(model) - Get detailed model information
  • pull_model(model, stream=False) - Pull a model from Ollama library
  • delete_model(model) - Delete a model
  • copy_model(source, destination) - Copy a model
  • get_version() - Get Ollama version
  • create_embedding(model, prompt) - Generate embeddings
  • batch_embeddings(model, prompts) - Generate multiple embeddings efficiently

All methods have async equivalents prefixed with 'a' (e.g., agenerate, achat).

Error Handling

The package provides specific exception types for better error handling:

from ollama_toolkit import ModelNotFoundError, OllamaAPIError

try:
    client.generate(model="non-existent-model", prompt="Hello")
except ModelNotFoundError as e:
    print(f"Model not found: {e}")
except OllamaAPIError as e:
    print(f"API error: {e}")

Development

This project uses a central virtual environment located at /home/lloyd/Development/eidos_venv. To set up your development environment:

# Initialize the development environment (creates and activates venv, installs dependencies)
source ./development.sh

# Format code
black ollama_toolkit
isort ollama_toolkit

# Run type checking
mypy ollama_toolkit

# Run tests
pytest ollama_toolkit/tests

For repository setup:

# Initialize as its own Git repository
./init_repo.sh

# Update parent repository .gitignore to exclude this package
./update_parent_gitignore.sh

Examples

The package includes several example scripts to help you get started:

  • basic_usage.py - Basic client usage with model listing and generation
  • version_example.py - How to check the Ollama version
  • generate_example.py - Text generation with both streaming and non-streaming modes
  • chat_example.py - Chat completion with message history management
  • embedding_example.py - Creating and comparing text embeddings

Run the examples directly from the examples directory:

python -m ollama_toolkit.examples.basic_usage

Use the scripts in the examples folder:

python -m ollama_toolkit.examples.quickstart
python -m ollama_toolkit.examples.basic_usage
python -m ollama_toolkit.examples.generate_example
python -m ollama_toolkit.examples.chat_example
python -m ollama_toolkit.examples.embedding_example

Review each example for the latest usage patterns, including fallback mechanisms, streaming modes, and updated model names (e.g. deepseek-r1:1.5b).

Project Structure

ollama_toolkit/
โ”œโ”€โ”€ __init__.py                  # Package initialization and exports
โ”œโ”€โ”€ client.py                    # Main OllamaClient implementation
โ”œโ”€โ”€ cli.py                       # Command-line interface
โ”œโ”€โ”€ exceptions.py                # Custom exceptions
โ”œโ”€โ”€ docs/                        # Documentation files
โ”‚   โ”œโ”€โ”€ index.md                 # Main documentation index
โ”‚   โ”œโ”€โ”€ api_reference.md         # API documentation
โ”‚   โ””โ”€โ”€ examples.md              # Example usage documentation
โ”œโ”€โ”€ examples/                    # Example usage scripts
โ”‚   โ”œโ”€โ”€ __init__.py              # Package marker
โ”‚   โ”œโ”€โ”€ basic_usage.py           # Basic client usage example
โ”‚   โ”œโ”€โ”€ chat_example.py          # Chat API example
โ”‚   โ”œโ”€โ”€ embedding_example.py     # Embedding API example
โ”‚   โ”œโ”€โ”€ generate_example.py      # Generate API example
โ”‚   โ””โ”€โ”€ version_example.py       # Version API example
โ”œโ”€โ”€ tests/                       # Test suite
โ”‚   โ”œโ”€โ”€ __init__.py              # Package marker
โ”‚   โ”œโ”€โ”€ test_client.py           # Client tests
โ”‚   โ”œโ”€โ”€ test_nexus.py            # Test runner utility
โ”‚   โ””โ”€โ”€ test_utils.py            # Utility tests
โ”œโ”€โ”€ tools/                       # Development tools
โ”‚   โ”œโ”€โ”€ __init__.py              # Package marker
โ”‚   โ””โ”€โ”€ install_ollama.py        # Ollama installation tool
โ”œโ”€โ”€ utils/                       # Utility functions
โ”‚   โ”œโ”€โ”€ __init__.py              # Package marker
โ”‚   โ”œโ”€โ”€ common.py                # Common utilities
โ”‚   โ””โ”€โ”€ model_constants.py       # Model name constants
โ””โ”€โ”€ wheelhouse/                  # Build artifacts
โ”œโ”€โ”€ LICENSE                      # License file
โ”œโ”€โ”€ pyproject.toml               # Project configuration
โ”œโ”€โ”€ setup.py                     # Legacy setup script
โ”œโ”€โ”€ README.md                    # Project documentation
โ””โ”€โ”€ publish.py                   # Script for publishing to PyPI

Project Setup

Initializing as a Standalone Repository

If you're working within a larger repository and want to initialize ollama_toolkit as its own Git repository:

# Navigate to the ollama_toolkit directory
cd /path/to/ollama_toolkit

# Initialize a new Git repository
git init

# Add all files
git add .

# Create an initial commit
git commit -m "Initial commit of ollama_toolkit"

# Add a remote repository (replace with your repository URL)
git remote add origin https://github.com/Ace1928/ollama_toolkit.git

# Push to your repository
git push -u origin main

To avoid tracking this directory in the parent repository, add it to the parent's .gitignore file:

Overview

The ollama_toolkit package provides a convenient interface to interact with the Ollama Toolkit. It includes:

  • A high-level client (OllamaClient) for making API requests
  • Command-line interface for interacting with Ollama models
  • Utility functions for common operations
  • Comprehensive error handling
  • Detailed examples and documentation

Automated Documentation

To build the documentation locally:

  1. Install or update documentation tools:
    pip install sphinx sphinx-autobuild sphinx-rtd-theme
    
  2. Navigate into the docs folder (or project root if you have a docs/ directory there).
  3. Build the docs (for example with Sphinx):
    sphinx-build -b html . _build/html
    
  4. Open the generated HTML files under _build/html in your browser.

Our docstrings follow a Google-style or reStructuredText format (compatible with autodoc systems). For more advanced usage, you can integrate with other popular doc generators (MkDocs, pdoc, etc.).

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Format your code (black ollama_toolkit && isort ollama_toolkit)
  4. Commit your changes (git commit -m 'Add some amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

Contact

Project Link: https://github.com/Ace1928/ollama_toolkit

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

ollama_toolkit-0.1.9.tar.gz (155.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ollama_toolkit-0.1.9-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

File details

Details for the file ollama_toolkit-0.1.9.tar.gz.

File metadata

  • Download URL: ollama_toolkit-0.1.9.tar.gz
  • Upload date:
  • Size: 155.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ollama_toolkit-0.1.9.tar.gz
Algorithm Hash digest
SHA256 3ac27a4ba99dec1b13d9b0655f7cd7608e21e3f85f1f9e83a134f5684c82a9c8
MD5 922654a089f77cba595c3479390caf0f
BLAKE2b-256 9d3acdcbed51040c44c0dbd7a655e735440dee543f5c3001dc6e0a67c6c6fc07

See more details on using hashes here.

File details

Details for the file ollama_toolkit-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: ollama_toolkit-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 35.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ollama_toolkit-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5faff556bd2345e0dd2cb2770412bee2db7ac5526d2ff766806d22a9b4d811bd
MD5 27ece7319be30dfc0e39145b96462872
BLAKE2b-256 4b42777b4ad31a7dee5785f6e85c56cd5c83d5cb70612616ab9ac5ff0e7aa985

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page