Ollama Utils
A Python library providing convenient utilities for integrating Ollama with Streamlit and Python applications. This package offers a simple, pythonic interface to Ollama's API with built-in Streamlit components for rapid prototyping of LLM applications.
Features
- Full HTTP API Integration - No CLI dependencies
- =� Streaming Support - Real-time response streaming
- <� Parameter Control - Fine-tune model behavior (temperature, top_p, etc.)
- =� Streamlit Components - Ready-to-use UI components
- = Model Management - List, pull, delete, and inspect models
- =� Chat Interface - Multi-turn conversations
- <� Simple API - Easy to use, well-documented functions
Installation
Using uv (recommended)
uv add ollama-utils
Using pip
pip install ollama-utils
Prerequisites
-
Install Ollama: Download from ollama.com
-
Start Ollama server:
ollama serve -
Pull a model:
ollama pull llama3.2:latest
Quick Start
Basic Text Generation
from ollama_utils import generate_with_model
# Simple generation
response = generate_with_model("llama3.2:latest", "Write a haiku about Python")
print(response)
# With custom parameters
response = generate_with_model(
"llama3.2:latest",
"Explain quantum computing",
temperature=0.7,
num_predict=500
)
Chat Conversations
from ollama_utils import chat_with_model
messages = [
{"role": "user", "content": "Hello! What's the weather like?"},
{"role": "assistant", "content": "I don't have access to real-time weather data, but I can help you with weather-related questions!"},
{"role": "user", "content": "What should I wear in 70�F weather?"}
]
response = chat_with_model("llama3.2:latest", messages)
print(response)
Streaming Responses
from ollama_utils import generate_with_model
# Stream generation
for chunk in generate_with_model("llama3.2:latest", "Tell me a story", stream=True):
print(chunk, end="", flush=True)
# Stream chat
for chunk in chat_with_model("llama3.2:latest", messages, stream=True):
print(chunk, end="", flush=True)
Model Management
from ollama_utils import list_models, pull_model, delete_model, show_model
# List available models
models = list_models()
for model in models:
print(f"Model: {model['name']}, Size: {model['size']}")
# Pull a new model
result = pull_model("mistral:latest")
if result["success"]:
print("Model pulled successfully!")
# Get model info
info = show_model("llama3.2:latest")
print(info)
Streamlit Integration
Quick Chat Interface
import streamlit as st
from ollama_utils.streamlit_helpers import chat_ui
st.title("My LLM Chat App")
# This creates a complete chat interface!
chat_ui()
Custom Streamlit App
import streamlit as st
from ollama_utils.streamlit_helpers import model_selector
from ollama_utils import generate_with_model
st.title("LLM Text Generator")
# Model selection dropdown
model = model_selector()
# Text input
prompt = st.text_area("Enter your prompt:")
if st.button("Generate"):
if model and prompt:
# Generate with streaming
response_placeholder = st.empty()
full_response = ""
for chunk in generate_with_model(model, prompt, stream=True):
full_response += chunk
response_placeholder.markdown(full_response + "�")
response_placeholder.markdown(full_response)
API Reference
Core Functions
generate_with_model(model_name, prompt, stream=False, **kwargs)
Generate text using the /api/generate endpoint.
Parameters:
model_name(str): Name of the model (e.g., "llama3.2:latest")prompt(str): Input promptstream(bool): Enable streaming responses**kwargs: Additional parameters (temperature, top_p, num_predict, etc.)
Returns:
- If
stream=False: Complete response as string - If
stream=True: Generator yielding response chunks
chat_with_model(model_name, messages, stream=False, **kwargs)
Multi-turn chat using the /api/chat endpoint.
Parameters:
model_name(str): Name of the modelmessages(List[dict]): List of messages with "role" and "content" keysstream(bool): Enable streaming responses**kwargs: Additional parameters
Returns:
- If
stream=False: Complete response as string - If
stream=True: Generator yielding response chunks
list_models()
List all locally installed models.
Returns:
- List of model dictionaries with metadata
pull_model(model_name)
Download a model from Ollama registry.
Parameters:
model_name(str): Name of the model to pull
Returns:
- Dictionary with "success" and "output"/"error" keys
delete_model(model_name)
Remove a model from local cache.
Parameters:
model_name(str): Name of the model to delete
Returns:
- Dictionary with "success" and "output"/"error" keys
show_model(model_name)
Display detailed information about a model.
Parameters:
model_name(str): Name of the model
Returns:
- Formatted string with model information
is_model_installed(model_name)
Check if a model is installed locally.
Parameters:
model_name(str): Name of the model
Returns:
- Boolean indicating if the model is installed
Streamlit Helpers
model_selector(label="Select a local model", sidebar=True)
Create a dropdown selector for available models.
Parameters:
label(str): Label for the selectorsidebar(bool): Whether to place in sidebar
Returns:
- Selected model name or None
chat_ui(model_name=None, streaming=True)
Complete chat interface with history and controls.
Parameters:
model_name(str, optional): Model to use (if None, shows selector)streaming(bool): Enable streaming responses
Advanced Usage
Custom Parameters
# Fine-tune model behavior
response = generate_with_model(
"llama3.2:latest",
"Explain machine learning",
temperature=0.8, # Creativity (0.0-2.0)
top_p=0.9, # Nucleus sampling (0.0-1.0)
top_k=40, # Top-k sampling (1-100)
repeat_penalty=1.1, # Repetition penalty (0.0-2.0)
num_predict=1000, # Max tokens to generate
)
Error Handling
from ollama_utils import chat_with_model
try:
response = chat_with_model("nonexistent-model", messages)
if response.startswith("Chat error"):
print(f"Error occurred: {response}")
else:
print(f"Response: {response}")
except Exception as e:
print(f"Unexpected error: {e}")
Demo Application
Run the included demo to test all features:
git clone https://github.com/malpasocodes/ollama-utils.git
cd ollama-utils
uv sync
uv run streamlit run demo_app.py
The demo includes:
- Model management interface
- Text generation testing
- Chat interface
- API parameter testing
- Full chat UI demonstration
Requirements
- Python 3.8+
- Ollama installed and running
requestslibrarystreamlitlibrary (for Streamlit helpers)
Contributing
- Fork the repository
- Clone and set up development environment:
git clone https://github.com/your-username/ollama-utils.git cd ollama-utils uv sync
- Create a feature branch (
git checkout -b feature/amazing-feature) - Run tests:
uv run pytest - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: API Reference
Changelog
0.1.0
- Initial release
- Full HTTP API integration
- Streaming support
- Streamlit helpers
- Model management functions
- Chat and generation capabilities
Metadata
Release files for ollama-utils 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ollama_utils-0.1.0.tar.gz | 18.1 kB | Without provenance | Details for ollama_utils-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ollama_utils-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ollama_utils-0.1.0-py3-none-any.whl |
Total release size: 27.5 kB
Release files / ollama_utils-0.1.0.tar.gz
| Download URL | ollama_utils-0.1.0.tar.gz |
|---|---|
| Size | 18.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
48dc1d402f88461f25fa0379bb0a9848a6868ab428ebf1715aab0f6504890b69
|
|
BLAKE2b-256 checksum How to use checksums |
7dc2e241fc0c0abd876bc7c546660e5402ba933ca7f39e9f383061ad9ca15550
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Release files / ollama_utils-0.1.0-py3-none-any.whl
| Download URL | ollama_utils-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c19d1a7a20a1e0f0c8f80f42263597aa34c0ca9e5acd8411174d2bdf669bd05
|
|
BLAKE2b-256 checksum How to use checksums |
cdaa0b4ebda0a9594605ba2f63cb25d629d2bb45ef5247717737f6682995791b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Security for ollama-utils 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.