A Python library that meters Ollama usage to Revenium.
Project description
🤖 Revenium Middleware for Ollama
A middleware library for metering and monitoring Ollama API usage in Python applications. 🐍✨
✨ Features
- 📊 Precise Usage Tracking: Monitor tokens, costs, and request counts across all Ollama API endpoints
- 🔌 Seamless Integration: Drop-in middleware that works with minimal code changes
- ⚙️ Flexible Configuration: Customize metering behavior to suit your application needs
📥 Installation
pip install revenium-middleware-ollama
🔧 Usage
🔄 Zero-Config Integration
Simply export your REVENIUM_METERING_API_KEY and import the middleware. Your Ollama calls will be metered automatically:
import ollama
import revenium_middleware_ollama
# Ensure REVENIUM_METERING_API_KEY environment variable is set
response: ollama.ChatResponse = ollama.chat(
model='qwen2.5:0.5b', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
])
print(response['message']['content'])
The middleware automatically intercepts Ollama API calls and sends metering data to Revenium without requiring any
changes to your existing code. Make sure to set the REVENIUM_METERING_API_KEY environment variable for authentication
with the Revenium service.
📈 Enhanced Tracking with Metadata
For more granular usage tracking and detailed reporting, add the usage_metadata parameter:
import ollama
import revenium_middleware_ollama
response = ollama.chat(
model='qwen2.5:0.5b', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
},
],
usage_metadata={
"trace_id": "conv-28a7e9d4-1c3b-4e5f-8a9b-7d1e3f2c1b4a",
"task_id": "chat-summary-af23c910",
"task_type": "text-classification",
"subscriber_identity": "customer-email@example.com",
"organization_id": "acme-corporation-12345",
"subscription_id": "startup-plan-quarterly-2025-Q1",
"product_id": "intelligent-document-processor-v3",
"source_id": "mobile-app-ios-v4.2",
"ai_provider_key_name": "ollama-production-key1",
"agent": "customer-support-assistant-v2",
},
)
print(response.choices[0].message.content)
🏷️ Metadata Fields
The usage_metadata parameter supports the following fields:
| Field | Description | Use Case |
|---|---|---|
trace_id |
Unique identifier for a conversation or session | Track multi-turn conversations |
task_id |
Identifier for a specific AI task | Group related API calls for a single task |
task_type |
Classification of the AI operation | Categorize usage by purpose (e.g., classification, summarization) |
subscriber_identity |
End-user identifier | Track usage by individual users |
organization_id |
Customer or department identifier | Allocate costs to business units |
subscription_id |
Reference to a billing plan | Associate usage with specific subscriptions |
product_id |
The product or feature using AI | Track usage across different products |
source_id |
Origin of the request | Monitor usage by platform or app version |
ai_provider_key_name |
Identifier for the API key used | Track usage by different API keys |
agent |
Identifier for the specific AI agent | Compare performance across different AI agents |
All metadata fields are optional. Adding them enables more detailed reporting and analytics in Revenium.
🔄 Compatibility
- 🐍 Python 3.8+
- 🤖 Ollama Python SDK 1.0.0+
- 🌐 Works with all Ollama models
📚 Documentation
Full documentation is available at https://revenium-middleware-ollama.readthedocs.io/
👥 Contributing
Contributions are welcome! Please check out our contributing guidelines for details.
- 🍴 Fork the repository
- 🌿 Create your feature branch (
git checkout -b feature/amazing-feature) - 💾 Commit your changes (
git commit -m 'Add some amazing feature') - 🚀 Push to the branch (
git push origin feature/amazing-feature) - 🔍 Open a Pull Request
📄 License
This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0) - see the LICENSE file for details.
🙏 Acknowledgments
- 🔥 Thanks to the Ollama team for creating an excellent API
- 💖 Built with ❤️ by the Revenium team
Project details
Release history Release notifications | RSS feed
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 revenium_middleware_ollama-0.1.0.tar.gz.
File metadata
- Download URL: revenium_middleware_ollama-0.1.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36051b8d6beb9977a38a25864f901afc80edb618647efdf0d83adc684b576b6c
|
|
| MD5 |
a7ecc7ffc591561578adcb7b1aaf2c9a
|
|
| BLAKE2b-256 |
e31b4d0743bb37b558a6268591bc7032d88bef848e5cd24161dc7d967f8267ee
|
File details
Details for the file revenium_middleware_ollama-0.1.0-py3-none-any.whl.
File metadata
- Download URL: revenium_middleware_ollama-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b80b13b134518e5a540813b3529edcbd7aca95b04c738b5843ca8f987438507
|
|
| MD5 |
5aee5f8f8464409ad05b0b90788a2ce0
|
|
| BLAKE2b-256 |
e92ec4b4541066169fdb27bc1d3967e52a1fe79a4fcfbf7bb631d9da146b895b
|