revenium-python-sdk 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- revenium_python_sdk-0.1.0/LICENSE +21 -0
- revenium_python_sdk-0.1.0/PKG-INFO +252 -0
- revenium_python_sdk-0.1.0/README.md +168 -0
- revenium_python_sdk-0.1.0/pyproject.toml +120 -0
- revenium_python_sdk-0.1.0/revenium_middleware/__init__.py +184 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/__init__.py +65 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/config.py +165 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/context.py +109 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/decorators.py +202 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/metering.py +207 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/prompt_extraction.py +55 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/subscriber.py +51 -0
- revenium_python_sdk-0.1.0/revenium_middleware/_core/trace_fields.py +265 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/__init__.py +108 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/bedrock_adapter.py +753 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/config.py +29 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/middleware.py +1070 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/prompt_extractor.py +178 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/provider.py +141 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/summary_printer.py +286 -0
- revenium_python_sdk-0.1.0/revenium_middleware/anthropic/trace_fields.py +158 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/__init__.py +114 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/__init__.py +127 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/exceptions.py +137 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/protocols.py +192 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/summary_printer.py +271 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/trace_fields.py +205 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/types.py +208 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/common/utils.py +1111 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/config.py +64 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/google_ai/__init__.py +53 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/google_ai/middleware.py +667 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/google_ai/provider.py +135 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/prompt_extractor.py +396 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/vertex_ai/__init__.py +56 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/vertex_ai/middleware.py +1162 -0
- revenium_python_sdk-0.1.0/revenium_middleware/google/vertex_ai/provider.py +99 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/__init__.py +25 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/__init__.py +81 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/config.py +53 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/context.py +198 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/decorators.py +912 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/hooks.py +192 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/integrations/__init__.py +26 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/integrations/crewai.py +446 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/middleware.py +321 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/summary_printer.py +314 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/trace_fields.py +51 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/client/validation.py +207 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/proxy/__init__.py +25 -0
- revenium_python_sdk-0.1.0/revenium_middleware/litellm/proxy/middleware.py +217 -0
- revenium_python_sdk-0.1.0/revenium_middleware/ollama/__init__.py +28 -0
- revenium_python_sdk-0.1.0/revenium_middleware/ollama/middleware.py +569 -0
- revenium_python_sdk-0.1.0/revenium_middleware/ollama/trace_fields.py +63 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/__init__.py +23 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/azure_config.py +169 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/azure_model_resolver.py +219 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/config.py +45 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/exceptions.py +115 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/langchain/__init__.py +114 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/langchain/_utils.py +129 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/langchain/unified_handler.py +526 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/middleware.py +1451 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/prompt_extractor.py +173 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/provider.py +170 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/summary_printer.py +292 -0
- revenium_python_sdk-0.1.0/revenium_middleware/openai/trace_fields.py +98 -0
- revenium_python_sdk-0.1.0/revenium_middleware/perplexity/__init__.py +97 -0
- revenium_python_sdk-0.1.0/revenium_middleware/perplexity/middleware.py +379 -0
- revenium_python_sdk-0.1.0/revenium_middleware/perplexity/perplexity_sdk.py +256 -0
- revenium_python_sdk-0.1.0/revenium_middleware/perplexity/provider.py +84 -0
- revenium_python_sdk-0.1.0/revenium_middleware/perplexity/trace_fields.py +25 -0
- revenium_python_sdk-0.1.0/revenium_python_sdk.egg-info/PKG-INFO +252 -0
- revenium_python_sdk-0.1.0/revenium_python_sdk.egg-info/SOURCES.txt +77 -0
- revenium_python_sdk-0.1.0/revenium_python_sdk.egg-info/dependency_links.txt +1 -0
- revenium_python_sdk-0.1.0/revenium_python_sdk.egg-info/requires.txt +71 -0
- revenium_python_sdk-0.1.0/revenium_python_sdk.egg-info/top_level.txt +1 -0
- revenium_python_sdk-0.1.0/setup.cfg +4 -0
- revenium_python_sdk-0.1.0/tests/test_metering.py +76 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Revenium, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: revenium-python-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The official Revenium Python SDK — unified AI metering middleware for OpenAI, Anthropic, Google, Ollama, LiteLLM, and Perplexity.
|
|
5
|
+
Author-email: Revenium <support@revenium.io>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/revenium/revenium-python-sdk
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/revenium/revenium-python-sdk/issues
|
|
9
|
+
Project-URL: Documentation, https://docs.revenium.io
|
|
10
|
+
Keywords: sdk,ai,llm,middleware,metering,revenium,openai,anthropic,google,ollama,litellm,perplexity
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Intended Audience :: Developers
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: revenium_metering>=6.8.2
|
|
26
|
+
Provides-Extra: openai
|
|
27
|
+
Requires-Dist: wrapt; extra == "openai"
|
|
28
|
+
Requires-Dist: openai>=1.0.0; extra == "openai"
|
|
29
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "openai"
|
|
30
|
+
Provides-Extra: langchain
|
|
31
|
+
Requires-Dist: langchain>=0.1.16; extra == "langchain"
|
|
32
|
+
Requires-Dist: langchain-openai>=0.1.0; extra == "langchain"
|
|
33
|
+
Requires-Dist: langchain-core>=0.1.0; extra == "langchain"
|
|
34
|
+
Provides-Extra: perplexity
|
|
35
|
+
Requires-Dist: wrapt; extra == "perplexity"
|
|
36
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "perplexity"
|
|
37
|
+
Provides-Extra: perplexity-openai
|
|
38
|
+
Requires-Dist: wrapt; extra == "perplexity-openai"
|
|
39
|
+
Requires-Dist: openai>=1.0.0; extra == "perplexity-openai"
|
|
40
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "perplexity-openai"
|
|
41
|
+
Provides-Extra: perplexity-native
|
|
42
|
+
Requires-Dist: wrapt; extra == "perplexity-native"
|
|
43
|
+
Requires-Dist: perplexityai>=0.1.0; extra == "perplexity-native"
|
|
44
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "perplexity-native"
|
|
45
|
+
Provides-Extra: google
|
|
46
|
+
Requires-Dist: wrapt; extra == "google"
|
|
47
|
+
Provides-Extra: google-genai
|
|
48
|
+
Requires-Dist: wrapt; extra == "google-genai"
|
|
49
|
+
Requires-Dist: google-genai>=0.1.0; extra == "google-genai"
|
|
50
|
+
Requires-Dist: python-dotenv; extra == "google-genai"
|
|
51
|
+
Provides-Extra: google-vertex
|
|
52
|
+
Requires-Dist: wrapt; extra == "google-vertex"
|
|
53
|
+
Requires-Dist: vertexai>=1.0.0; extra == "google-vertex"
|
|
54
|
+
Requires-Dist: python-dotenv; extra == "google-vertex"
|
|
55
|
+
Provides-Extra: anthropic
|
|
56
|
+
Requires-Dist: wrapt; extra == "anthropic"
|
|
57
|
+
Requires-Dist: anthropic; extra == "anthropic"
|
|
58
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "anthropic"
|
|
59
|
+
Provides-Extra: ollama
|
|
60
|
+
Requires-Dist: wrapt; extra == "ollama"
|
|
61
|
+
Requires-Dist: ollama; extra == "ollama"
|
|
62
|
+
Provides-Extra: litellm
|
|
63
|
+
Requires-Dist: wrapt; extra == "litellm"
|
|
64
|
+
Requires-Dist: litellm; extra == "litellm"
|
|
65
|
+
Provides-Extra: litellm-proxy
|
|
66
|
+
Requires-Dist: wrapt; extra == "litellm-proxy"
|
|
67
|
+
Requires-Dist: litellm[proxy]; extra == "litellm-proxy"
|
|
68
|
+
Provides-Extra: dev
|
|
69
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
70
|
+
Requires-Dist: pytest-asyncio; extra == "dev"
|
|
71
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
72
|
+
Requires-Dist: flake8; extra == "dev"
|
|
73
|
+
Requires-Dist: black; extra == "dev"
|
|
74
|
+
Requires-Dist: mypy; extra == "dev"
|
|
75
|
+
Requires-Dist: freezegun; extra == "dev"
|
|
76
|
+
Requires-Dist: openai-responses>=0.12.0; extra == "dev"
|
|
77
|
+
Requires-Dist: requests; extra == "dev"
|
|
78
|
+
Requires-Dist: wrapt; extra == "dev"
|
|
79
|
+
Requires-Dist: python-dotenv>=0.19.0; extra == "dev"
|
|
80
|
+
Requires-Dist: anthropic; extra == "dev"
|
|
81
|
+
Requires-Dist: boto3; extra == "dev"
|
|
82
|
+
Requires-Dist: ollama; extra == "dev"
|
|
83
|
+
Dynamic: license-file
|
|
84
|
+
|
|
85
|
+
# Revenium Python SDK
|
|
86
|
+
|
|
87
|
+
[](https://pypi.org/project/revenium-python-sdk/)
|
|
88
|
+
[](https://pypi.org/project/revenium-python-sdk/)
|
|
89
|
+
[](https://docs.revenium.io)
|
|
90
|
+
[](https://opensource.org/licenses/MIT)
|
|
91
|
+
|
|
92
|
+
The official Revenium Python SDK — unified AI metering middleware for deeply attributed AI usage metrics. Supports OpenAI, Anthropic, Google (Gemini/Vertex AI), Ollama, LiteLLM, and Perplexity.
|
|
93
|
+
|
|
94
|
+
## Features
|
|
95
|
+
|
|
96
|
+
- **Unified SDK**: Single package with middleware for all major AI providers
|
|
97
|
+
- **Asynchronous Processing**: Background thread management for non-blocking metering operations
|
|
98
|
+
- **Graceful Shutdown**: Ensures all metering data is properly sent even during application shutdown
|
|
99
|
+
- **Decorator Support**: `@revenium_meter` and `@revenium_metadata` for easy integration
|
|
100
|
+
- **Tool Metering**: Meter arbitrary tool/function calls alongside LLM API metering
|
|
101
|
+
|
|
102
|
+
## Supported Providers
|
|
103
|
+
|
|
104
|
+
| Provider | Extra | Install Command |
|
|
105
|
+
|----------|-------|----------------|
|
|
106
|
+
| OpenAI | `openai` | `pip install revenium-python-sdk[openai]` |
|
|
107
|
+
| Anthropic | `anthropic` | `pip install revenium-python-sdk[anthropic]` |
|
|
108
|
+
| Google Gemini | `google-genai` | `pip install revenium-python-sdk[google-genai]` |
|
|
109
|
+
| Google Vertex AI | `google-vertex` | `pip install revenium-python-sdk[google-vertex]` |
|
|
110
|
+
| Ollama | `ollama` | `pip install revenium-python-sdk[ollama]` |
|
|
111
|
+
| LiteLLM | `litellm` | `pip install revenium-python-sdk[litellm]` |
|
|
112
|
+
| LiteLLM Proxy | `litellm-proxy` | `pip install revenium-python-sdk[litellm-proxy]` |
|
|
113
|
+
| Perplexity (OpenAI) | `perplexity-openai` | `pip install revenium-python-sdk[perplexity-openai]` |
|
|
114
|
+
| Perplexity (Native) | `perplexity-native` | `pip install revenium-python-sdk[perplexity-native]` |
|
|
115
|
+
| LangChain | `langchain` | `pip install revenium-python-sdk[langchain]` |
|
|
116
|
+
|
|
117
|
+
## Installation
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# Core SDK
|
|
121
|
+
pip install revenium-python-sdk
|
|
122
|
+
|
|
123
|
+
# With a specific provider
|
|
124
|
+
pip install revenium-python-sdk[openai]
|
|
125
|
+
|
|
126
|
+
# Multiple providers
|
|
127
|
+
pip install revenium-python-sdk[openai,anthropic,ollama]
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Quick Start
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from revenium_middleware import client, run_async_in_thread, shutdown_event
|
|
134
|
+
|
|
135
|
+
# Record usage directly
|
|
136
|
+
client.record_usage(
|
|
137
|
+
model="gpt-4o",
|
|
138
|
+
prompt_tokens=500,
|
|
139
|
+
completion_tokens=200,
|
|
140
|
+
user_id="user123",
|
|
141
|
+
session_id="session456"
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
# Run async metering tasks in background threads
|
|
145
|
+
async def async_metering_task():
|
|
146
|
+
await client.async_record_usage(
|
|
147
|
+
model="gpt-3.5-turbo",
|
|
148
|
+
prompt_tokens=300,
|
|
149
|
+
completion_tokens=150,
|
|
150
|
+
user_id="user789"
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
thread = run_async_in_thread(async_metering_task())
|
|
154
|
+
|
|
155
|
+
# Application continues while metering happens in background
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Provider-Specific Usage
|
|
159
|
+
|
|
160
|
+
Each provider has its own middleware module. See the `examples/` directory for detailed usage:
|
|
161
|
+
|
|
162
|
+
- `examples/openai/` — OpenAI and Azure OpenAI examples
|
|
163
|
+
- `examples/anthropic/` — Anthropic and Bedrock examples
|
|
164
|
+
- `examples/google/` — Google AI and Vertex AI examples
|
|
165
|
+
- `examples/ollama/` — Ollama examples
|
|
166
|
+
- `examples/litellm/` — LiteLLM client and proxy examples
|
|
167
|
+
- `examples/perplexity/` — Perplexity examples
|
|
168
|
+
|
|
169
|
+
## Tool Metering
|
|
170
|
+
|
|
171
|
+
The `meter_tool` decorator lets you meter arbitrary tool/function calls (web scrapers, image generators, database lookups, etc.) alongside your LLM API metering. This is available via `revenium_metering` v6.8.2+.
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from revenium_middleware import meter_tool, configure
|
|
175
|
+
|
|
176
|
+
# Configure the metering client
|
|
177
|
+
configure(
|
|
178
|
+
metering_url="https://api.revenium.io/meter",
|
|
179
|
+
api_key="your-api-key",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Decorate any tool function to automatically meter it
|
|
183
|
+
@meter_tool("my-web-scraper", operation="scrape")
|
|
184
|
+
def scrape_website(url):
|
|
185
|
+
# Your scraping logic here
|
|
186
|
+
return {"pages": 5, "data_mb": 2.3}
|
|
187
|
+
|
|
188
|
+
# The decorator captures timing, success/failure, and reports to Revenium
|
|
189
|
+
result = scrape_website("https://example.com")
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
You can also report tool calls manually:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from revenium_middleware import report_tool_call
|
|
196
|
+
|
|
197
|
+
report_tool_call(
|
|
198
|
+
tool_id="my-tool",
|
|
199
|
+
operation="fetch",
|
|
200
|
+
duration_ms=1234,
|
|
201
|
+
success=True,
|
|
202
|
+
usage_metadata={"records": 42},
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Compatibility
|
|
207
|
+
|
|
208
|
+
- Python 3.8+
|
|
209
|
+
- Compatible with all supported AI providers
|
|
210
|
+
|
|
211
|
+
## Logging
|
|
212
|
+
|
|
213
|
+
This module uses Python's standard logging system. You can control the log level by setting the `REVENIUM_LOG_LEVEL` environment variable:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
# Enable debug logging
|
|
217
|
+
export REVENIUM_LOG_LEVEL=DEBUG
|
|
218
|
+
|
|
219
|
+
# Or when running your script
|
|
220
|
+
REVENIUM_LOG_LEVEL=DEBUG python your_script.py
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Available log levels:
|
|
224
|
+
- `DEBUG`: Detailed debugging information
|
|
225
|
+
- `INFO`: General information (default)
|
|
226
|
+
- `WARNING`: Warning messages only
|
|
227
|
+
- `ERROR`: Error messages only
|
|
228
|
+
- `CRITICAL`: Critical error messages only
|
|
229
|
+
|
|
230
|
+
## Documentation
|
|
231
|
+
|
|
232
|
+
For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
|
|
233
|
+
|
|
234
|
+
## Contributing
|
|
235
|
+
|
|
236
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
|
237
|
+
|
|
238
|
+
## Code of Conduct
|
|
239
|
+
|
|
240
|
+
See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md)
|
|
241
|
+
|
|
242
|
+
## Security
|
|
243
|
+
|
|
244
|
+
See [SECURITY.md](./SECURITY.md)
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
249
|
+
|
|
250
|
+
## Acknowledgments
|
|
251
|
+
|
|
252
|
+
- Built by the Revenium team
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Revenium Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/revenium-python-sdk/)
|
|
4
|
+
[](https://pypi.org/project/revenium-python-sdk/)
|
|
5
|
+
[](https://docs.revenium.io)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
The official Revenium Python SDK — unified AI metering middleware for deeply attributed AI usage metrics. Supports OpenAI, Anthropic, Google (Gemini/Vertex AI), Ollama, LiteLLM, and Perplexity.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Unified SDK**: Single package with middleware for all major AI providers
|
|
13
|
+
- **Asynchronous Processing**: Background thread management for non-blocking metering operations
|
|
14
|
+
- **Graceful Shutdown**: Ensures all metering data is properly sent even during application shutdown
|
|
15
|
+
- **Decorator Support**: `@revenium_meter` and `@revenium_metadata` for easy integration
|
|
16
|
+
- **Tool Metering**: Meter arbitrary tool/function calls alongside LLM API metering
|
|
17
|
+
|
|
18
|
+
## Supported Providers
|
|
19
|
+
|
|
20
|
+
| Provider | Extra | Install Command |
|
|
21
|
+
|----------|-------|----------------|
|
|
22
|
+
| OpenAI | `openai` | `pip install revenium-python-sdk[openai]` |
|
|
23
|
+
| Anthropic | `anthropic` | `pip install revenium-python-sdk[anthropic]` |
|
|
24
|
+
| Google Gemini | `google-genai` | `pip install revenium-python-sdk[google-genai]` |
|
|
25
|
+
| Google Vertex AI | `google-vertex` | `pip install revenium-python-sdk[google-vertex]` |
|
|
26
|
+
| Ollama | `ollama` | `pip install revenium-python-sdk[ollama]` |
|
|
27
|
+
| LiteLLM | `litellm` | `pip install revenium-python-sdk[litellm]` |
|
|
28
|
+
| LiteLLM Proxy | `litellm-proxy` | `pip install revenium-python-sdk[litellm-proxy]` |
|
|
29
|
+
| Perplexity (OpenAI) | `perplexity-openai` | `pip install revenium-python-sdk[perplexity-openai]` |
|
|
30
|
+
| Perplexity (Native) | `perplexity-native` | `pip install revenium-python-sdk[perplexity-native]` |
|
|
31
|
+
| LangChain | `langchain` | `pip install revenium-python-sdk[langchain]` |
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# Core SDK
|
|
37
|
+
pip install revenium-python-sdk
|
|
38
|
+
|
|
39
|
+
# With a specific provider
|
|
40
|
+
pip install revenium-python-sdk[openai]
|
|
41
|
+
|
|
42
|
+
# Multiple providers
|
|
43
|
+
pip install revenium-python-sdk[openai,anthropic,ollama]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from revenium_middleware import client, run_async_in_thread, shutdown_event
|
|
50
|
+
|
|
51
|
+
# Record usage directly
|
|
52
|
+
client.record_usage(
|
|
53
|
+
model="gpt-4o",
|
|
54
|
+
prompt_tokens=500,
|
|
55
|
+
completion_tokens=200,
|
|
56
|
+
user_id="user123",
|
|
57
|
+
session_id="session456"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# Run async metering tasks in background threads
|
|
61
|
+
async def async_metering_task():
|
|
62
|
+
await client.async_record_usage(
|
|
63
|
+
model="gpt-3.5-turbo",
|
|
64
|
+
prompt_tokens=300,
|
|
65
|
+
completion_tokens=150,
|
|
66
|
+
user_id="user789"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
thread = run_async_in_thread(async_metering_task())
|
|
70
|
+
|
|
71
|
+
# Application continues while metering happens in background
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Provider-Specific Usage
|
|
75
|
+
|
|
76
|
+
Each provider has its own middleware module. See the `examples/` directory for detailed usage:
|
|
77
|
+
|
|
78
|
+
- `examples/openai/` — OpenAI and Azure OpenAI examples
|
|
79
|
+
- `examples/anthropic/` — Anthropic and Bedrock examples
|
|
80
|
+
- `examples/google/` — Google AI and Vertex AI examples
|
|
81
|
+
- `examples/ollama/` — Ollama examples
|
|
82
|
+
- `examples/litellm/` — LiteLLM client and proxy examples
|
|
83
|
+
- `examples/perplexity/` — Perplexity examples
|
|
84
|
+
|
|
85
|
+
## Tool Metering
|
|
86
|
+
|
|
87
|
+
The `meter_tool` decorator lets you meter arbitrary tool/function calls (web scrapers, image generators, database lookups, etc.) alongside your LLM API metering. This is available via `revenium_metering` v6.8.2+.
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from revenium_middleware import meter_tool, configure
|
|
91
|
+
|
|
92
|
+
# Configure the metering client
|
|
93
|
+
configure(
|
|
94
|
+
metering_url="https://api.revenium.io/meter",
|
|
95
|
+
api_key="your-api-key",
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Decorate any tool function to automatically meter it
|
|
99
|
+
@meter_tool("my-web-scraper", operation="scrape")
|
|
100
|
+
def scrape_website(url):
|
|
101
|
+
# Your scraping logic here
|
|
102
|
+
return {"pages": 5, "data_mb": 2.3}
|
|
103
|
+
|
|
104
|
+
# The decorator captures timing, success/failure, and reports to Revenium
|
|
105
|
+
result = scrape_website("https://example.com")
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
You can also report tool calls manually:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from revenium_middleware import report_tool_call
|
|
112
|
+
|
|
113
|
+
report_tool_call(
|
|
114
|
+
tool_id="my-tool",
|
|
115
|
+
operation="fetch",
|
|
116
|
+
duration_ms=1234,
|
|
117
|
+
success=True,
|
|
118
|
+
usage_metadata={"records": 42},
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Compatibility
|
|
123
|
+
|
|
124
|
+
- Python 3.8+
|
|
125
|
+
- Compatible with all supported AI providers
|
|
126
|
+
|
|
127
|
+
## Logging
|
|
128
|
+
|
|
129
|
+
This module uses Python's standard logging system. You can control the log level by setting the `REVENIUM_LOG_LEVEL` environment variable:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# Enable debug logging
|
|
133
|
+
export REVENIUM_LOG_LEVEL=DEBUG
|
|
134
|
+
|
|
135
|
+
# Or when running your script
|
|
136
|
+
REVENIUM_LOG_LEVEL=DEBUG python your_script.py
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Available log levels:
|
|
140
|
+
- `DEBUG`: Detailed debugging information
|
|
141
|
+
- `INFO`: General information (default)
|
|
142
|
+
- `WARNING`: Warning messages only
|
|
143
|
+
- `ERROR`: Error messages only
|
|
144
|
+
- `CRITICAL`: Critical error messages only
|
|
145
|
+
|
|
146
|
+
## Documentation
|
|
147
|
+
|
|
148
|
+
For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
|
|
149
|
+
|
|
150
|
+
## Contributing
|
|
151
|
+
|
|
152
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
|
153
|
+
|
|
154
|
+
## Code of Conduct
|
|
155
|
+
|
|
156
|
+
See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md)
|
|
157
|
+
|
|
158
|
+
## Security
|
|
159
|
+
|
|
160
|
+
See [SECURITY.md](./SECURITY.md)
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
165
|
+
|
|
166
|
+
## Acknowledgments
|
|
167
|
+
|
|
168
|
+
- Built by the Revenium team
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "revenium-python-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "The official Revenium Python SDK — unified AI metering middleware for OpenAI, Anthropic, Google, Ollama, LiteLLM, and Perplexity."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Revenium", email = "support@revenium.io" }
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"revenium_metering>=6.8.2"
|
|
17
|
+
]
|
|
18
|
+
keywords = ["sdk", "ai", "llm", "middleware", "metering", "revenium", "openai", "anthropic", "google", "ollama", "litellm", "perplexity"]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.8",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Intended Audience :: Developers",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
"Homepage" = "https://github.com/revenium/revenium-python-sdk"
|
|
35
|
+
"Bug Tracker" = "https://github.com/revenium/revenium-python-sdk/issues"
|
|
36
|
+
"Documentation" = "https://docs.revenium.io"
|
|
37
|
+
|
|
38
|
+
[project.optional-dependencies]
|
|
39
|
+
openai = [
|
|
40
|
+
"wrapt",
|
|
41
|
+
"openai>=1.0.0",
|
|
42
|
+
"python-dotenv>=0.19.0",
|
|
43
|
+
]
|
|
44
|
+
langchain = [
|
|
45
|
+
"langchain>=0.1.16",
|
|
46
|
+
"langchain-openai>=0.1.0",
|
|
47
|
+
"langchain-core>=0.1.0",
|
|
48
|
+
]
|
|
49
|
+
perplexity = [
|
|
50
|
+
"wrapt",
|
|
51
|
+
"python-dotenv>=0.19.0",
|
|
52
|
+
]
|
|
53
|
+
perplexity-openai = [
|
|
54
|
+
"wrapt",
|
|
55
|
+
"openai>=1.0.0",
|
|
56
|
+
"python-dotenv>=0.19.0",
|
|
57
|
+
]
|
|
58
|
+
perplexity-native = [
|
|
59
|
+
"wrapt",
|
|
60
|
+
"perplexityai>=0.1.0",
|
|
61
|
+
"python-dotenv>=0.19.0",
|
|
62
|
+
]
|
|
63
|
+
google = [
|
|
64
|
+
"wrapt",
|
|
65
|
+
]
|
|
66
|
+
google-genai = [
|
|
67
|
+
"wrapt",
|
|
68
|
+
"google-genai>=0.1.0",
|
|
69
|
+
"python-dotenv",
|
|
70
|
+
]
|
|
71
|
+
google-vertex = [
|
|
72
|
+
"wrapt",
|
|
73
|
+
"vertexai>=1.0.0",
|
|
74
|
+
"python-dotenv",
|
|
75
|
+
]
|
|
76
|
+
anthropic = [
|
|
77
|
+
"wrapt",
|
|
78
|
+
"anthropic",
|
|
79
|
+
"python-dotenv>=0.19.0",
|
|
80
|
+
]
|
|
81
|
+
ollama = [
|
|
82
|
+
"wrapt",
|
|
83
|
+
"ollama",
|
|
84
|
+
]
|
|
85
|
+
litellm = [
|
|
86
|
+
"wrapt",
|
|
87
|
+
"litellm",
|
|
88
|
+
]
|
|
89
|
+
litellm-proxy = [
|
|
90
|
+
"wrapt",
|
|
91
|
+
"litellm[proxy]",
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
dev = [
|
|
95
|
+
"pytest>=7.0.0",
|
|
96
|
+
"pytest-asyncio",
|
|
97
|
+
"pytest-cov",
|
|
98
|
+
"flake8",
|
|
99
|
+
"black",
|
|
100
|
+
"mypy",
|
|
101
|
+
"freezegun",
|
|
102
|
+
"openai-responses>=0.12.0",
|
|
103
|
+
"requests",
|
|
104
|
+
"wrapt",
|
|
105
|
+
"python-dotenv>=0.19.0",
|
|
106
|
+
"anthropic",
|
|
107
|
+
"boto3",
|
|
108
|
+
"ollama",
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
[tool.pytest.ini_options]
|
|
112
|
+
markers = [
|
|
113
|
+
"unit: Unit tests (fast, no external dependencies)",
|
|
114
|
+
"e2e: end-to-end tests requiring live services",
|
|
115
|
+
"integration: integration tests requiring live services",
|
|
116
|
+
]
|
|
117
|
+
addopts = "-m 'not e2e and not integration'"
|
|
118
|
+
|
|
119
|
+
[tool.setuptools.packages.find]
|
|
120
|
+
include = ["revenium_middleware*"]
|