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",
"task_type": "summarize-customer-issue",
"subscriber": {
"id": "subscriberid-1234567890",
"email": "user@example.com",
"credential": {
"name": "engineering-api-key",
"value": "sk-1234567890abcdef"
}
},
"organization_id": "acme-corp",
"subscription_id": "startup-plan-Q1",
"product_id": "saas-app-gold-tier",
"agent": "support-agent",
},
)
print(response['message']['content'])
🚀 OpenAI Compatibility Mode
The middleware can also be used with Ollama's OpenAI compatibility mode.
import openai
import revenium_middleware_openai
openai.api_key = 'ollama'
openai.base_url = 'http://localhost:11434/v1/'
question = "Why is the sky blue?"
response = openai.chat.completions.create(
model="gemma3:12b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": question}
],
usage_metadata={
"trace_id": "conv-28a7e9d4",
"task_type": "summarize-customer-issue",
"subscriber": {
"id": "subscriberid-1234567890",
"email": "user@example.com",
"credential": {
"name": "engineering-api-key",
"value": "sk-1234567890abcdef"
}
},
"organization_id": "acme-corp",
"subscription_id": "startup-plan-Q1",
"product_id": "saas-app-gold-tier",
"agent": "support-agent",
}
)
print(response)
🏷️ Metadata Fields
The usage_metadata parameter supports the following fields:
| Field | Description | Use Case |
|---|---|---|
trace_id |
Unique identifier for a conversation or session | Group multi-turn conversations into single event for performance & cost tracking |
task_type |
Classification of the AI operation by type of work | Track cost & performance by purpose (e.g., classification, summarization) |
subscriber |
Object containing subscriber information | Track cost & performance by individual users or API keys |
subscriber.id |
The id of the subscriber from non-Revenium systems | Track cost & performance by individual users (if customers are anonymous or tracking by emails is not desired) |
subscriber.email |
The email address of the subscriber | Track cost & performance by individual users (if customer e-mail addresses are known) |
subscriber.credential |
Object containing credential information | Track cost & performance by API key |
subscriber.credential.name |
An alias for an API key used by one or more users | Track cost & performance by individual API keys |
subscriber.credential.value |
The key value associated with the subscriber (i.e an API key) | Track cost & performance by API key value (normally used when the only identifier for a user is an API key) |
organization_id |
Customer or department ID from non-Revenium systems | Track cost & performance by customers or business units |
subscription_id |
Reference to a billing plan in non-Revenium systems | Track cost & performance by a specific subscription |
product_id |
Your product or feature making the AI call | Track cost & performance across different products |
agent |
Identifier for the specific AI agent | Track cost & performance performance by AI agent |
response_quality_score |
The quality of the AI response (0..1) | Track AI response quality |
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
🔍 Logging
This module uses Python's standard logging system. You can control the log level by setting the REVENIUM_LOG_LEVEL environment variable:
# Enable debug logging
export REVENIUM_LOG_LEVEL=DEBUG
# Or when running your script
REVENIUM_LOG_LEVEL=DEBUG python your_script.py
Available log levels:
DEBUG: Detailed debugging informationINFO: General information (default)WARNING: Warning messages onlyERROR: Error messages onlyCRITICAL: Critical error messages only
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- 💖 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.16.tar.gz.
File metadata
- Download URL: revenium_middleware_ollama-0.1.16.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a47d582270aef7d0c8b2d18f1e7baf66c1fa18d52cb99945b766c2120487ba61
|
|
| MD5 |
792923c1bb82ef32e05f7d2d07a38ac3
|
|
| BLAKE2b-256 |
fe7491cddfd55221a6c4b3671e97d3d0dd6e8a07a9420ea0ea39eee8a135263e
|
File details
Details for the file revenium_middleware_ollama-0.1.16-py3-none-any.whl.
File metadata
- Download URL: revenium_middleware_ollama-0.1.16-py3-none-any.whl
- Upload date:
- Size: 7.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 |
f1e08a8038c86c3ffad371c68d031651aa70d05bff9c526212c55d38a57f4aba
|
|
| MD5 |
1a52dca97e20a5a1ad89ee7b0aed6843
|
|
| BLAKE2b-256 |
a5877c03283ee15f8013478ed584a0684f2fcf2cc4c5867f91e079ee7b4d5d02
|