Skip to main content

A Python library that provides core functionality to send AI metering data to Revenium.

Project description

🔄 Revenium Core Middleware

PyPI version Python Versions License: Apache 2.0

A foundational library that provides core metering functionality shared across all Revenium AI provider-specific middleware implementations (OpenAI, Anthropic, Ollama, etc). 🐍✨

✨ Features

  • 🧠 Shared Core Functionality: Provides the essential metering infrastructure used by all Revenium middleware implementations
  • 🔄 Asynchronous Processing: Background thread management for non-blocking metering operations
  • 🛑 Graceful Shutdown: Ensures all metering data is properly sent even during application shutdown
  • 🔌 Provider Agnostic: Designed to work with any AI provider through specific middleware implementations

📥 Installation

pip install revenium-middleware

🔧 Usage

🔄 Direct Usage

While this package is primarily intended as a dependency for provider-specific middleware, you can use it directly:

from revenium_middleware import client, run_async_in_thread, shutdown_event

# Record usage directly
client.record_usage(
    model="gpt-4o",
    prompt_tokens=500,
    completion_tokens=200,
    user_id="user123",
    session_id="session456"
)

# Run async metering tasks in background threads
async def async_metering_task():
    await client.async_record_usage(
        model="gpt-3.5-turbo",
        prompt_tokens=300,
        completion_tokens=150,
        user_id="user789"
    )

thread = run_async_in_thread(async_metering_task())

# Application continues while metering happens in background

🏗️ Building Provider-Specific Middleware

This library is designed to be extended by provider-specific middleware implementations:

from revenium_middleware import client, run_async_in_thread

# Example of how a provider-specific middleware might use the core
def record_provider_usage(response_data, metadata):
    # Extract token counts from provider-specific response format
    prompt_tokens = response_data.usage.prompt_tokens
    completion_tokens = response_data.usage.completion_tokens
    
    # Use the core client to record the usage
    run_async_in_thread(
        client.async_record_usage(
            model=response_data.model,
            prompt_tokens=prompt_tokens,
            completion_tokens=completion_tokens,
            **metadata
        )
    )

🔄 Compatibility

  • 🐍 Python 3.8+
  • 🤝 Compatible with all Revenium provider-specific middleware implementations

🔍 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 information
  • INFO: General information (default)
  • WARNING: Warning messages only
  • ERROR: Error messages only
  • CRITICAL: Critical error messages only

📚 Documentation

For more detailed documentation, please refer to the docstrings in the code or visit our GitHub repository.

👥 Contributing

Contributions are welcome! Please check out our contributing guidelines for details.

  1. 🍴 Fork the repository
  2. 🌿 Create your feature branch (git checkout -b feature/amazing-feature)
  3. 💾 Commit your changes (git commit -m 'Add some amazing feature')
  4. 🚀 Push to the branch (git push origin feature/amazing-feature)
  5. 🔍 Open a Pull Request

📄 License

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

🙏 Acknowledgments

  • 💖 Built with ❤️ by the Revenium team

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

revenium_middleware-0.3.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

revenium_middleware-0.3.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file revenium_middleware-0.3.1.tar.gz.

File metadata

  • Download URL: revenium_middleware-0.3.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for revenium_middleware-0.3.1.tar.gz
Algorithm Hash digest
SHA256 c2d86fdb0ccbd8f2147cfc2c2a567e48ec7221bcb33c2ce5af64d52ede2264d1
MD5 be7cdd18ddc78611d44c1379604b47c3
BLAKE2b-256 2e0bf07b1ae519f1fe1e5d10140e6184b2cad3a353059f8898a722d0029a8a02

See more details on using hashes here.

File details

Details for the file revenium_middleware-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for revenium_middleware-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11050396054fa6650d6c31e405c5789f3f5cc9b4beeb7137a80228b5a4d3ff82
MD5 bd4507778042905a55f142b4ef71012d
BLAKE2b-256 abc6dc844dab8b97b1c95279c0088c4f1539ece99042e92c05df08b31eb24b3e

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