@revenium/openai 1.0.12 → 1.0.14
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.
- package/.env.example +10 -15
- package/CHANGELOG.md +59 -0
- package/CODE_OF_CONDUCT.md +57 -0
- package/CONTRIBUTING.md +38 -0
- package/README.md +109 -515
- package/SECURITY.md +34 -0
- package/dist/cjs/core/client/index.js +14 -0
- package/dist/cjs/core/client/index.js.map +1 -0
- package/dist/cjs/core/client/manager.js +109 -0
- package/dist/cjs/core/client/manager.js.map +1 -0
- package/dist/cjs/core/config/azure-config.js +5 -17
- package/dist/cjs/core/config/azure-config.js.map +1 -1
- package/dist/cjs/core/config/index.js +2 -2
- package/dist/cjs/core/config/index.js.map +1 -1
- package/dist/cjs/core/config/loader.js +34 -14
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/config/manager.js +12 -5
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/config/validator.js +3 -45
- package/dist/cjs/core/config/validator.js.map +1 -1
- package/dist/cjs/core/middleware/index.js +17 -0
- package/dist/cjs/core/middleware/index.js.map +1 -0
- package/dist/cjs/core/middleware/interfaces.js +361 -0
- package/dist/cjs/core/middleware/interfaces.js.map +1 -0
- package/dist/cjs/core/middleware/revenium-client.js +142 -0
- package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
- package/dist/cjs/core/providers/detector.js +45 -23
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/providers/index.js +2 -1
- package/dist/cjs/core/providers/index.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +14 -13
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +30 -35
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/core/tracking/usage-tracker.js +22 -18
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -1
- package/dist/cjs/index.js +26 -174
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js +0 -8
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/openai-augmentation.js +0 -49
- package/dist/cjs/types/openai-augmentation.js.map +1 -1
- package/dist/cjs/utils/constants.js +17 -20
- package/dist/cjs/utils/constants.js.map +1 -1
- package/dist/cjs/utils/error-handler.js +18 -14
- package/dist/cjs/utils/error-handler.js.map +1 -1
- package/dist/cjs/utils/metadata-builder.js +27 -19
- package/dist/cjs/utils/metadata-builder.js.map +1 -1
- package/dist/cjs/utils/provider-detection.js +25 -28
- package/dist/cjs/utils/provider-detection.js.map +1 -1
- package/dist/cjs/utils/stop-reason-mapper.js +4 -0
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
- package/dist/cjs/utils/url-builder.js +3 -3
- package/dist/esm/core/client/index.js +6 -0
- package/dist/esm/core/client/index.js.map +1 -0
- package/dist/esm/core/client/manager.js +102 -0
- package/dist/esm/core/client/manager.js.map +1 -0
- package/dist/esm/core/config/azure-config.js +6 -18
- package/dist/esm/core/config/azure-config.js.map +1 -1
- package/dist/esm/core/config/index.js +5 -4
- package/dist/esm/core/config/index.js.map +1 -1
- package/dist/esm/core/config/loader.js +33 -13
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/config/manager.js +14 -7
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/config/validator.js +3 -44
- package/dist/esm/core/config/validator.js.map +1 -1
- package/dist/esm/core/middleware/index.js +8 -0
- package/dist/esm/core/middleware/index.js.map +1 -0
- package/dist/esm/core/middleware/interfaces.js +353 -0
- package/dist/esm/core/middleware/interfaces.js.map +1 -0
- package/dist/esm/core/middleware/revenium-client.js +105 -0
- package/dist/esm/core/middleware/revenium-client.js.map +1 -0
- package/dist/esm/core/providers/detector.js +43 -22
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/providers/index.js +2 -2
- package/dist/esm/core/providers/index.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +13 -12
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/core/tracking/payload-builder.js +31 -36
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/core/tracking/usage-tracker.js +24 -20
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
- package/dist/esm/index.js +9 -157
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js +2 -10
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/openai-augmentation.js +0 -49
- package/dist/esm/types/openai-augmentation.js.map +1 -1
- package/dist/esm/utils/constants.js +16 -19
- package/dist/esm/utils/constants.js.map +1 -1
- package/dist/esm/utils/error-handler.js +19 -15
- package/dist/esm/utils/error-handler.js.map +1 -1
- package/dist/esm/utils/metadata-builder.js +27 -19
- package/dist/esm/utils/metadata-builder.js.map +1 -1
- package/dist/esm/utils/provider-detection.js +26 -29
- package/dist/esm/utils/provider-detection.js.map +1 -1
- package/dist/esm/utils/stop-reason-mapper.js +4 -0
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
- package/dist/esm/utils/url-builder.js +3 -3
- package/dist/types/core/client/index.d.ts +6 -0
- package/dist/types/core/client/index.d.ts.map +1 -0
- package/dist/types/core/client/manager.d.ts +32 -0
- package/dist/types/core/client/manager.d.ts.map +1 -0
- package/dist/types/core/config/azure-config.d.ts +2 -2
- package/dist/types/core/config/azure-config.d.ts.map +1 -1
- package/dist/types/core/config/index.d.ts +4 -4
- package/dist/types/core/config/index.d.ts.map +1 -1
- package/dist/types/core/config/loader.d.ts +3 -1
- package/dist/types/core/config/loader.d.ts.map +1 -1
- package/dist/types/core/config/manager.d.ts +1 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/config/validator.d.ts +1 -12
- package/dist/types/core/config/validator.d.ts.map +1 -1
- package/dist/types/core/middleware/index.d.ts +8 -0
- package/dist/types/core/middleware/index.d.ts.map +1 -0
- package/dist/types/core/middleware/interfaces.d.ts +74 -0
- package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
- package/dist/types/core/middleware/revenium-client.d.ts +58 -0
- package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
- package/dist/types/core/providers/detector.d.ts +9 -2
- package/dist/types/core/providers/detector.d.ts.map +1 -1
- package/dist/types/core/providers/index.d.ts +2 -2
- package/dist/types/core/providers/index.d.ts.map +1 -1
- package/dist/types/core/tracking/api-client.d.ts +1 -1
- package/dist/types/core/tracking/api-client.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts +3 -3
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/core/tracking/usage-tracker.d.ts +2 -2
- package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -1
- package/dist/types/index.d.ts +11 -114
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/function-parameters.d.ts +2 -23
- package/dist/types/types/function-parameters.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +17 -115
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +4 -139
- package/dist/types/types/openai-augmentation.d.ts.map +1 -1
- package/dist/types/utils/constants.d.ts +7 -1
- package/dist/types/utils/constants.d.ts.map +1 -1
- package/dist/types/utils/error-handler.d.ts +2 -2
- package/dist/types/utils/error-handler.d.ts.map +1 -1
- package/dist/types/utils/metadata-builder.d.ts +4 -3
- package/dist/types/utils/metadata-builder.d.ts.map +1 -1
- package/dist/types/utils/provider-detection.d.ts +3 -3
- package/dist/types/utils/provider-detection.d.ts.map +1 -1
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
- package/dist/types/utils/url-builder.d.ts +3 -3
- package/examples/README.md +270 -224
- package/examples/azure/basic.ts +62 -0
- package/examples/azure/responses-basic.ts +45 -0
- package/examples/azure/responses-stream.ts +61 -0
- package/examples/azure/stream.ts +56 -0
- package/examples/getting_started.ts +31 -43
- package/examples/openai/basic.ts +45 -0
- package/examples/openai/metadata.ts +67 -0
- package/examples/openai/responses-basic.ts +44 -0
- package/examples/openai/responses-embed.ts +34 -0
- package/examples/openai/responses-streaming.ts +63 -0
- package/examples/openai/streaming.ts +59 -0
- package/package.json +20 -13
- package/dist/cjs/core/wrapper/index.js +0 -15
- package/dist/cjs/core/wrapper/index.js.map +0 -1
- package/dist/cjs/core/wrapper/instance-patcher.js +0 -202
- package/dist/cjs/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/cjs/core/wrapper/request-handler.js +0 -317
- package/dist/cjs/core/wrapper/request-handler.js.map +0 -1
- package/dist/cjs/core/wrapper/stream-wrapper.js +0 -82
- package/dist/cjs/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/cjs/utils/azure-model-resolver.js +0 -211
- package/dist/cjs/utils/azure-model-resolver.js.map +0 -1
- package/dist/cjs/utils/request-handler-factory.js +0 -185
- package/dist/cjs/utils/request-handler-factory.js.map +0 -1
- package/dist/esm/core/wrapper/index.js +0 -9
- package/dist/esm/core/wrapper/index.js.map +0 -1
- package/dist/esm/core/wrapper/instance-patcher.js +0 -199
- package/dist/esm/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/esm/core/wrapper/request-handler.js +0 -310
- package/dist/esm/core/wrapper/request-handler.js.map +0 -1
- package/dist/esm/core/wrapper/stream-wrapper.js +0 -79
- package/dist/esm/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/esm/utils/azure-model-resolver.js +0 -204
- package/dist/esm/utils/azure-model-resolver.js.map +0 -1
- package/dist/esm/utils/request-handler-factory.js +0 -146
- package/dist/esm/utils/request-handler-factory.js.map +0 -1
- package/dist/types/core/wrapper/index.d.ts +0 -8
- package/dist/types/core/wrapper/index.d.ts.map +0 -1
- package/dist/types/core/wrapper/instance-patcher.d.ts +0 -33
- package/dist/types/core/wrapper/instance-patcher.d.ts.map +0 -1
- package/dist/types/core/wrapper/request-handler.d.ts +0 -29
- package/dist/types/core/wrapper/request-handler.d.ts.map +0 -1
- package/dist/types/core/wrapper/stream-wrapper.d.ts +0 -13
- package/dist/types/core/wrapper/stream-wrapper.d.ts.map +0 -1
- package/dist/types/utils/azure-model-resolver.d.ts +0 -41
- package/dist/types/utils/azure-model-resolver.d.ts.map +0 -1
- package/dist/types/utils/request-handler-factory.d.ts +0 -81
- package/dist/types/utils/request-handler-factory.d.ts.map +0 -1
- package/examples/azure-basic.ts +0 -206
- package/examples/azure-responses-basic.ts +0 -233
- package/examples/azure-responses-streaming.ts +0 -255
- package/examples/azure-streaming.ts +0 -209
- package/examples/openai-basic.ts +0 -147
- package/examples/openai-function-calling.ts +0 -259
- package/examples/openai-responses-basic.ts +0 -212
- package/examples/openai-responses-streaming.ts +0 -232
- package/examples/openai-streaming.ts +0 -172
- package/examples/openai-vision.ts +0 -289
- package/src/core/config/azure-config.ts +0 -72
- package/src/core/config/index.ts +0 -23
- package/src/core/config/loader.ts +0 -66
- package/src/core/config/manager.ts +0 -94
- package/src/core/config/validator.ts +0 -89
- package/src/core/providers/detector.ts +0 -159
- package/src/core/providers/index.ts +0 -16
- package/src/core/tracking/api-client.ts +0 -78
- package/src/core/tracking/index.ts +0 -21
- package/src/core/tracking/payload-builder.ts +0 -132
- package/src/core/tracking/usage-tracker.ts +0 -189
- package/src/core/wrapper/index.ts +0 -9
- package/src/core/wrapper/instance-patcher.ts +0 -288
- package/src/core/wrapper/request-handler.ts +0 -423
- package/src/core/wrapper/stream-wrapper.ts +0 -100
- package/src/index.ts +0 -336
- package/src/types/function-parameters.ts +0 -251
- package/src/types/index.ts +0 -313
- package/src/types/openai-augmentation.ts +0 -233
- package/src/types/responses-api.ts +0 -308
- package/src/utils/azure-model-resolver.ts +0 -220
- package/src/utils/constants.ts +0 -21
- package/src/utils/error-handler.ts +0 -251
- package/src/utils/metadata-builder.ts +0 -219
- package/src/utils/provider-detection.ts +0 -257
- package/src/utils/request-handler-factory.ts +0 -285
- package/src/utils/stop-reason-mapper.ts +0 -74
- package/src/utils/type-guards.ts +0 -202
- package/src/utils/url-builder.ts +0 -68
package/README.md
CHANGED
|
@@ -3,22 +3,26 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@revenium/openai)
|
|
4
4
|
[](https://nodejs.org/)
|
|
5
5
|
[](https://docs.revenium.io)
|
|
6
|
+
[](https://www.revenium.ai)
|
|
6
7
|
[](https://opensource.org/licenses/MIT)
|
|
7
8
|
|
|
8
9
|
**Transparent TypeScript middleware for automatic Revenium usage tracking with OpenAI**
|
|
9
10
|
|
|
10
|
-
A professional-grade Node.js middleware that seamlessly integrates with OpenAI and Azure OpenAI to provide automatic usage tracking, billing analytics, and comprehensive metadata collection. Features native TypeScript support with zero type casting required and supports both
|
|
11
|
+
A professional-grade Node.js middleware that seamlessly integrates with OpenAI and Azure OpenAI to provide automatic usage tracking, billing analytics, and comprehensive metadata collection. Features native TypeScript support with zero type casting required and supports both Chat Completions API, Embeddings API, and Responses API.
|
|
12
|
+
|
|
13
|
+
**Go-aligned API for consistent cross-language development!**
|
|
11
14
|
|
|
12
15
|
## Features
|
|
13
16
|
|
|
17
|
+
- **Go-Aligned API** - Same `Initialize()`/`GetClient()` pattern as Go implementation
|
|
14
18
|
- **Seamless Integration** - Native TypeScript support, no type casting required
|
|
15
|
-
- **Optional Metadata** - Track users, organizations, and
|
|
16
|
-
- **
|
|
19
|
+
- **Optional Metadata** - Track users, organizations, and business context (all fields optional)
|
|
20
|
+
- **Multiple API Support** - Chat Completions, Embeddings, and Responses API
|
|
17
21
|
- **Azure OpenAI Support** - Full Azure OpenAI integration with automatic detection
|
|
18
22
|
- **Type Safety** - Complete TypeScript support with IntelliSense
|
|
19
23
|
- **Streaming Support** - Handles regular and streaming requests seamlessly
|
|
20
24
|
- **Fire-and-Forget** - Never blocks your application flow
|
|
21
|
-
- **
|
|
25
|
+
- **Automatic .env Loading** - Loads environment variables automatically
|
|
22
26
|
|
|
23
27
|
## Getting Started
|
|
24
28
|
|
|
@@ -39,42 +43,28 @@ npm install --save-dev typescript @types/node
|
|
|
39
43
|
|
|
40
44
|
### 2. Configure Environment Variables
|
|
41
45
|
|
|
42
|
-
Create a `.env` file
|
|
46
|
+
Create a `.env` file in your project root. See [`.env.example`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/.env.example) for all available configuration options.
|
|
43
47
|
|
|
44
|
-
**
|
|
48
|
+
**Minimum required configuration:**
|
|
45
49
|
|
|
46
50
|
```env
|
|
47
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
48
51
|
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key_here
|
|
52
|
+
REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
49
53
|
OPENAI_API_KEY=sk_your_openai_api_key_here
|
|
50
54
|
```
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
Run the [getting started example](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/getting_started.ts):
|
|
55
|
-
|
|
56
|
-
```bash
|
|
57
|
-
npx tsx node_modules/@revenium/openai/examples/getting_started.ts
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Or with debug logging:
|
|
56
|
+
**NOTE: Replace the placeholder values with your actual API keys.**
|
|
61
57
|
|
|
62
|
-
|
|
63
|
-
# Linux/macOS
|
|
64
|
-
REVENIUM_DEBUG=true npx tsx node_modules/@revenium/openai/examples/getting_started.ts
|
|
65
|
-
|
|
66
|
-
# Windows (PowerShell)
|
|
67
|
-
$env:REVENIUM_DEBUG="true"; npx tsx node_modules/@revenium/openai/examples/getting_started.ts
|
|
68
|
-
```
|
|
58
|
+
### 3. Run Your First Example
|
|
69
59
|
|
|
70
|
-
**For
|
|
60
|
+
**For complete examples and usage patterns, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md).**
|
|
71
61
|
|
|
72
62
|
---
|
|
73
63
|
|
|
74
64
|
## Requirements
|
|
75
65
|
|
|
76
66
|
- Node.js 16+
|
|
77
|
-
- OpenAI package
|
|
67
|
+
- OpenAI package v5.0.0 or later
|
|
78
68
|
- TypeScript 5.0+ (for TypeScript projects)
|
|
79
69
|
|
|
80
70
|
---
|
|
@@ -95,7 +85,7 @@ The middleware automatically captures comprehensive usage data:
|
|
|
95
85
|
- **User Tracking** - Subscriber ID, email, credentials
|
|
96
86
|
- **Organization Data** - Organization ID, subscription ID, product ID
|
|
97
87
|
- **Task Classification** - Task type, agent identifier, trace ID
|
|
98
|
-
- **Quality Metrics** - Response quality scores,
|
|
88
|
+
- **Quality Metrics** - Response quality scores, task identifiers
|
|
99
89
|
|
|
100
90
|
### **Technical Details**
|
|
101
91
|
|
|
@@ -104,545 +94,152 @@ The middleware automatically captures comprehensive usage data:
|
|
|
104
94
|
- **Error Tracking** - Failed requests, error types, retry attempts
|
|
105
95
|
- **Environment Info** - Development vs production usage
|
|
106
96
|
|
|
107
|
-
##
|
|
108
|
-
|
|
109
|
-
This middleware includes **full support** for OpenAI's new Responses API, which is designed to replace the traditional Chat Completions API with enhanced capabilities for agent-like applications.
|
|
110
|
-
|
|
111
|
-
### What is the Responses API?
|
|
112
|
-
|
|
113
|
-
The Responses API is OpenAI's new stateful API that:
|
|
114
|
-
|
|
115
|
-
- Uses `input` instead of `messages` parameter for simplified interaction
|
|
116
|
-
- Provides unified experience combining chat completions and assistants capabilities
|
|
117
|
-
- Supports advanced features like background tasks, function calling, and code interpreter
|
|
118
|
-
- Offers better streaming and real-time response generation
|
|
119
|
-
- Works with GPT-5 and other advanced models
|
|
120
|
-
|
|
121
|
-
### API Comparison
|
|
122
|
-
|
|
123
|
-
**Traditional Chat Completions:**
|
|
124
|
-
|
|
125
|
-
```javascript
|
|
126
|
-
const response = await openai.chat.completions.create({
|
|
127
|
-
model: 'gpt-4o',
|
|
128
|
-
messages: [{ role: 'user', content: 'Hello' }],
|
|
129
|
-
});
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
**New Responses API:**
|
|
133
|
-
|
|
134
|
-
```javascript
|
|
135
|
-
const response = await openai.responses.create({
|
|
136
|
-
model: 'gpt-5',
|
|
137
|
-
input: 'Hello', // Simplified input parameter
|
|
138
|
-
});
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Key Differences
|
|
142
|
-
|
|
143
|
-
| Feature | Chat Completions | Responses API |
|
|
144
|
-
| ---------------------- | ---------------------------- | ----------------------------------- |
|
|
145
|
-
| **Input Format** | `messages: [...]` | `input: "string"` or `input: [...]` |
|
|
146
|
-
| **Models** | GPT-4, GPT-4o, etc. | GPT-5, GPT-4o, etc. |
|
|
147
|
-
| **Response Structure** | `choices[0].message.content` | `output_text` |
|
|
148
|
-
| **Stateful** | No | Yes (with `store: true`) |
|
|
149
|
-
| **Advanced Features** | Limited | Built-in tools, reasoning, etc. |
|
|
150
|
-
| **Temperature** | Supported | Not supported with GPT-5 |
|
|
151
|
-
|
|
152
|
-
### Requirements & Installation
|
|
153
|
-
|
|
154
|
-
**OpenAI SDK Version:**
|
|
155
|
-
|
|
156
|
-
- **Minimum:** `5.8.0` (when Responses API was officially released)
|
|
157
|
-
- **Recommended:** `5.8.2` or later (tested and verified)
|
|
158
|
-
- **Current:** `6.2.0` (latest available)
|
|
159
|
-
|
|
160
|
-
**Installation:**
|
|
161
|
-
|
|
162
|
-
```bash
|
|
163
|
-
# Install latest version with Responses API support
|
|
164
|
-
npm install openai@^5.8.0
|
|
165
|
-
|
|
166
|
-
# Or install specific tested version
|
|
167
|
-
npm install openai@5.8.2
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
### Current Status
|
|
171
|
-
|
|
172
|
-
**The Responses API is officially available in OpenAI SDK 5.8+**
|
|
173
|
-
|
|
174
|
-
**Official Release:**
|
|
175
|
-
|
|
176
|
-
- Released by OpenAI in SDK version 5.8.0
|
|
177
|
-
- Fully documented in official OpenAI documentation
|
|
178
|
-
- Production-ready with GPT-5 and other supported models
|
|
179
|
-
- Complete middleware support with Revenium integration
|
|
180
|
-
|
|
181
|
-
**Middleware Features:**
|
|
182
|
-
|
|
183
|
-
- Full Responses API support (streaming & non-streaming)
|
|
184
|
-
- Seamless metadata tracking identical to Chat Completions
|
|
185
|
-
- Type-safe TypeScript integration
|
|
186
|
-
- Complete token tracking including reasoning tokens
|
|
187
|
-
- Azure OpenAI compatibility
|
|
97
|
+
## API Overview
|
|
188
98
|
|
|
189
|
-
|
|
99
|
+
The middleware provides a Go-aligned API with the following main functions:
|
|
190
100
|
|
|
191
|
-
-
|
|
192
|
-
-
|
|
101
|
+
- **`Initialize(config?)`** - Initialize the middleware (from environment or explicit config)
|
|
102
|
+
- **`GetClient()`** - Get the global Revenium client instance
|
|
103
|
+
- **`Configure(config)`** - Alias for `Initialize()` for programmatic configuration
|
|
104
|
+
- **`IsInitialized()`** - Check if the middleware is initialized
|
|
105
|
+
- **`Reset()`** - Reset the global client (useful for testing)
|
|
193
106
|
|
|
194
|
-
|
|
107
|
+
**For complete API documentation and usage examples, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md).**
|
|
195
108
|
|
|
196
|
-
|
|
109
|
+
## Metadata Fields
|
|
197
110
|
|
|
198
|
-
|
|
111
|
+
The middleware supports the following optional metadata fields for tracking:
|
|
199
112
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
113
|
+
| Field | Type | Description |
|
|
114
|
+
| ----------------------- | ------ | ----------------------------------------------------------- |
|
|
115
|
+
| `traceId` | string | Unique identifier for session or conversation tracking |
|
|
116
|
+
| `taskType` | string | Type of AI task being performed (e.g., "chat", "embedding") |
|
|
117
|
+
| `agent` | string | AI agent or bot identifier |
|
|
118
|
+
| `organizationId` | string | Organization or company identifier |
|
|
119
|
+
| `productId` | string | Your product or feature identifier |
|
|
120
|
+
| `subscriptionId` | string | Subscription plan identifier |
|
|
121
|
+
| `responseQualityScore` | number | Custom quality rating (0.0-1.0) |
|
|
122
|
+
| `subscriber.id` | string | Unique user identifier |
|
|
123
|
+
| `subscriber.email` | string | User email address |
|
|
124
|
+
| `subscriber.credential` | object | Authentication credential (`name` and `value` fields) |
|
|
203
125
|
|
|
204
|
-
**
|
|
205
|
-
- `openai-responses-basic.ts` - New Responses API with string input
|
|
206
|
-
- `openai-responses-streaming.ts` - Streaming with Responses API
|
|
126
|
+
**All metadata fields are optional.** For complete metadata documentation and usage examples, see:
|
|
207
127
|
|
|
208
|
-
|
|
209
|
-
-
|
|
210
|
-
- `azure-streaming.ts` - Azure streaming responses
|
|
211
|
-
- `azure-responses-basic.ts` - Azure Responses API
|
|
212
|
-
- `azure-responses-streaming.ts` - Azure streaming Responses API
|
|
128
|
+
- [`examples/README.md`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md) - All usage examples
|
|
129
|
+
- [Revenium API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete API documentation
|
|
213
130
|
|
|
214
|
-
|
|
215
|
-
- `examples/README.md` - Complete setup guide with TypeScript and JavaScript patterns
|
|
216
|
-
|
|
217
|
-
#### Running Examples
|
|
218
|
-
|
|
219
|
-
**Installed via npm?**
|
|
220
|
-
```bash
|
|
221
|
-
# Try these in order:
|
|
222
|
-
npx tsx node_modules/@revenium/openai/examples/openai-basic.ts
|
|
223
|
-
npx tsx node_modules/@revenium/openai/examples/openai-streaming.ts
|
|
224
|
-
npx tsx node_modules/@revenium/openai/examples/openai-responses-basic.ts
|
|
225
|
-
|
|
226
|
-
# View all examples:
|
|
227
|
-
ls node_modules/@revenium/openai/examples/
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
**Cloned from GitHub?**
|
|
231
|
-
```bash
|
|
232
|
-
npm install
|
|
233
|
-
npm run example:openai-basic
|
|
234
|
-
npm run example:openai-streaming
|
|
235
|
-
npm run example:openai-responses-basic
|
|
236
|
-
|
|
237
|
-
# See all example scripts:
|
|
238
|
-
npm run
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
**Browse online:** [`examples/` directory on GitHub](https://github.com/revenium/revenium-middleware-openai-node/tree/HEAD/examples)
|
|
242
|
-
|
|
243
|
-
### Temporarily Disabling Tracking
|
|
244
|
-
|
|
245
|
-
If you need to disable Revenium tracking temporarily, you can unpatch the OpenAI client:
|
|
246
|
-
|
|
247
|
-
```javascript
|
|
248
|
-
import { unpatchOpenAI, patchOpenAI } from '@revenium/openai-middleware';
|
|
249
|
-
|
|
250
|
-
// Disable tracking
|
|
251
|
-
unpatchOpenAI();
|
|
252
|
-
|
|
253
|
-
// Your OpenAI calls now bypass Revenium tracking
|
|
254
|
-
await openai.chat.completions.create({...});
|
|
255
|
-
|
|
256
|
-
// Re-enable tracking
|
|
257
|
-
patchOpenAI();
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
**Common use cases:**
|
|
261
|
-
|
|
262
|
-
- **Debugging**: Isolate whether issues are caused by the middleware
|
|
263
|
-
- **Testing**: Compare behavior with/without tracking
|
|
264
|
-
- **Conditional tracking**: Enable/disable based on environment
|
|
265
|
-
- **Troubleshooting**: Temporary bypass during incident response
|
|
266
|
-
|
|
267
|
-
**Note**: This affects all OpenAI instances globally since we patch the prototype methods.
|
|
131
|
+
## Configuration Options
|
|
268
132
|
|
|
269
|
-
|
|
133
|
+
### Environment Variables
|
|
270
134
|
|
|
271
|
-
|
|
135
|
+
For a complete list of all available environment variables with examples, see [`.env.example`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/.env.example).
|
|
272
136
|
|
|
273
|
-
|
|
137
|
+
## Examples
|
|
274
138
|
|
|
275
|
-
|
|
139
|
+
The package includes comprehensive examples in the [`examples/`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/) directory.
|
|
276
140
|
|
|
277
|
-
|
|
278
|
-
- `examples/azure-basic.ts` - Azure chat completions with environment variable setup
|
|
279
|
-
- `examples/azure-streaming.ts` - Azure streaming responses
|
|
280
|
-
- `examples/azure-responses-basic.ts` - Azure Responses API integration
|
|
141
|
+
### Getting Started
|
|
281
142
|
|
|
282
|
-
**Environment variables needed:**
|
|
283
143
|
```bash
|
|
284
|
-
|
|
285
|
-
AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
|
|
286
|
-
AZURE_OPENAI_API_KEY="your-azure-api-key"
|
|
287
|
-
AZURE_OPENAI_DEPLOYMENT="gpt-4o"
|
|
288
|
-
AZURE_OPENAI_API_VERSION="2024-12-01-preview"
|
|
289
|
-
|
|
290
|
-
# Revenium configuration
|
|
291
|
-
REVENIUM_METERING_API_KEY="hak_your_revenium_api_key"
|
|
292
|
-
REVENIUM_METERING_BASE_URL="https://api.revenium.io"
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### Azure Features
|
|
296
|
-
|
|
297
|
-
- **Automatic Detection**: Detects Azure OpenAI clients automatically
|
|
298
|
-
- **Model Name Resolution**: Maps Azure deployment names to standard model names for accurate pricing
|
|
299
|
-
- **Provider Metadata**: Correctly tags requests with `provider: "Azure"` and `modelSource: "OPENAI"`
|
|
300
|
-
- **Deployment Support**: Works with any Azure deployment name (simple or complex)
|
|
301
|
-
- **Endpoint Flexibility**: Supports all Azure OpenAI endpoint formats
|
|
302
|
-
- **Zero Code Changes**: Existing Azure OpenAI code works without modification
|
|
303
|
-
|
|
304
|
-
### Azure Environment Variables
|
|
305
|
-
|
|
306
|
-
| Variable | Required | Description | Example |
|
|
307
|
-
| -------------------------- | -------- | ---------------------------------------------- | ------------------------------------ |
|
|
308
|
-
| `AZURE_OPENAI_ENDPOINT` | Yes | Your Azure OpenAI endpoint URL | `https://acme.openai.azure.com/` |
|
|
309
|
-
| `AZURE_OPENAI_API_KEY` | Yes | Your Azure OpenAI API key | `abc123...` |
|
|
310
|
-
| `AZURE_OPENAI_DEPLOYMENT` | No | Default deployment name | `gpt-4o` or `text-embedding-3-large` |
|
|
311
|
-
| `AZURE_OPENAI_API_VERSION` | No | API version (defaults to `2024-12-01-preview`) | `2024-12-01-preview` |
|
|
312
|
-
|
|
313
|
-
### Azure Model Name Resolution
|
|
314
|
-
|
|
315
|
-
The middleware automatically maps Azure deployment names to standard model names for accurate pricing:
|
|
316
|
-
|
|
317
|
-
```typescript
|
|
318
|
-
// Azure deployment names → Standard model names for pricing
|
|
319
|
-
"gpt-4o-2024-11-20" → "gpt-4o"
|
|
320
|
-
"gpt4o-prod" → "gpt-4o"
|
|
321
|
-
"o4-mini" → "gpt-4o-mini"
|
|
322
|
-
"gpt-35-turbo-dev" → "gpt-3.5-turbo"
|
|
323
|
-
"text-embedding-3-large" → "text-embedding-3-large" // Direct match
|
|
324
|
-
"embedding-3-large" → "text-embedding-3-large"
|
|
325
|
-
```
|
|
326
|
-
|
|
327
|
-
## Advanced Usage
|
|
328
|
-
|
|
329
|
-
### Initialization Options
|
|
330
|
-
|
|
331
|
-
The middleware supports three initialization patterns:
|
|
332
|
-
|
|
333
|
-
**Automatic (Recommended)** - Import and patch OpenAI instance:
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
import { patchOpenAIInstance } from '@revenium/openai';
|
|
337
|
-
import OpenAI from 'openai';
|
|
338
|
-
|
|
339
|
-
const openai = patchOpenAIInstance(new OpenAI());
|
|
340
|
-
// Tracking works automatically if env vars are set
|
|
144
|
+
npm run example:getting-started
|
|
341
145
|
```
|
|
342
146
|
|
|
343
|
-
|
|
147
|
+
### OpenAI Examples
|
|
344
148
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
149
|
+
| Example | Command | Description |
|
|
150
|
+
| ------------------------------- | ----------------------------------- | --------------------------------- |
|
|
151
|
+
| `openai/basic.ts` | `npm run example:openai-basic` | Chat completions and embeddings |
|
|
152
|
+
| `openai/metadata.ts` | `npm run example:openai-metadata` | All metadata fields demonstration |
|
|
153
|
+
| `openai/streaming.ts` | `npm run example:openai-stream` | Streaming chat completions |
|
|
154
|
+
| `openai/responses-basic.ts` | `npm run example:openai-res-basic` | Responses API usage |
|
|
155
|
+
| `openai/responses-embed.ts` | `npm run example:openai-res-embed` | Embeddings with Responses API |
|
|
156
|
+
| `openai/responses-streaming.ts` | `npm run example:openai-res-stream` | Streaming Responses API |
|
|
348
157
|
|
|
349
|
-
|
|
350
|
-
if (!result.success) {
|
|
351
|
-
console.error('Failed to initialize:', result.message);
|
|
352
|
-
process.exit(1);
|
|
353
|
-
}
|
|
158
|
+
### Azure OpenAI Examples
|
|
354
159
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
### Streaming Responses
|
|
363
|
-
|
|
364
|
-
Streaming is fully supported with real-time token tracking and time-to-first-token metrics. The middleware automatically tracks streaming responses without any additional configuration.
|
|
365
|
-
|
|
366
|
-
See [`examples/openai-streaming.ts`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/openai-streaming.ts) and [`examples/azure-streaming.ts`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/azure-streaming.ts) for working streaming examples.
|
|
367
|
-
|
|
368
|
-
### Custom Metadata Tracking
|
|
369
|
-
|
|
370
|
-
Add business context to track usage by organization, user, task type, or custom fields. Pass a `usageMetadata` object with any of these optional fields:
|
|
371
|
-
|
|
372
|
-
| Field | Description | Use Case |
|
|
373
|
-
|-------|-------------|----------|
|
|
374
|
-
| `traceId` | Unique identifier for session or conversation tracking | Link multiple API calls together for debugging, user session analytics, or distributed tracing across services |
|
|
375
|
-
| `taskType` | Type of AI task being performed | Categorize usage by workload (e.g., "chat", "code-generation", "doc-summary") for cost analysis and optimization |
|
|
376
|
-
| `subscriber.id` | Unique user identifier | Track individual user consumption for billing, rate limiting, or user analytics |
|
|
377
|
-
| `subscriber.email` | User email address | Identify users for support, compliance, or usage reports |
|
|
378
|
-
| `subscriber.credential.name` | Authentication credential name | Track which API key or service account made the request |
|
|
379
|
-
| `subscriber.credential.value` | Authentication credential value | Associate usage with specific credentials for security auditing |
|
|
380
|
-
| `organizationId` | Organization or company identifier | Multi-tenant cost allocation, usage quotas per organization |
|
|
381
|
-
| `subscriptionId` | Subscription plan identifier | Track usage against subscription limits, identify plan upgrade opportunities |
|
|
382
|
-
| `productId` | Your product or feature identifier | Attribute AI costs to specific features in your application (e.g., "chatbot", "email-assistant") |
|
|
383
|
-
| `agent` | AI agent or bot identifier | Distinguish between multiple AI agents or automation workflows in your system |
|
|
384
|
-
| `responseQualityScore` | Custom quality rating (0.0-1.0) | Track user satisfaction or automated quality metrics for model performance analysis |
|
|
385
|
-
|
|
386
|
-
**Resources:**
|
|
387
|
-
- [API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete metadata field documentation
|
|
388
|
-
|
|
389
|
-
### OpenAI Responses API
|
|
390
|
-
**Use case:** Using OpenAI's new Responses API with string inputs and simplified interface (SDK 5.8+).
|
|
391
|
-
|
|
392
|
-
See working examples:
|
|
393
|
-
- `examples/openai-responses-basic.ts` - Basic Responses API usage
|
|
394
|
-
- `examples/openai-responses-streaming.ts` - Streaming with Responses API
|
|
395
|
-
|
|
396
|
-
### Azure OpenAI Integration
|
|
397
|
-
**Use case:** Automatic Azure OpenAI detection with deployment name resolution and accurate pricing.
|
|
398
|
-
|
|
399
|
-
See working examples:
|
|
400
|
-
- `examples/azure-basic.ts` - Azure chat completions and embeddings
|
|
401
|
-
- `examples/azure-responses-basic.ts` - Azure Responses API integration
|
|
402
|
-
|
|
403
|
-
### Embeddings with Metadata
|
|
404
|
-
**Use case:** Track embeddings usage for search engines, RAG systems, and document processing.
|
|
405
|
-
|
|
406
|
-
Embeddings examples are included in:
|
|
407
|
-
- `examples/openai-basic.ts` - Text embeddings with metadata
|
|
408
|
-
- `examples/openai-streaming.ts` - Batch embeddings processing
|
|
409
|
-
|
|
410
|
-
### Manual Configuration
|
|
411
|
-
|
|
412
|
-
For advanced use cases, configure the middleware manually:
|
|
413
|
-
|
|
414
|
-
```typescript
|
|
415
|
-
import { configure } from '@revenium/openai';
|
|
160
|
+
| Example | Command | Description |
|
|
161
|
+
| --------------------------- | ---------------------------------- | ----------------------------- |
|
|
162
|
+
| `azure/basic.ts` | `npm run example:azure-basic` | Azure chat completions |
|
|
163
|
+
| `azure/stream.ts` | `npm run example:azure-stream` | Azure streaming |
|
|
164
|
+
| `azure/responses-basic.ts` | `npm run example:azure-res-basic` | Azure Responses API |
|
|
165
|
+
| `azure/responses-stream.ts` | `npm run example:azure-res-stream` | Azure Responses API streaming |
|
|
416
166
|
|
|
417
|
-
|
|
418
|
-
reveniumApiKey: 'hak_your_api_key',
|
|
419
|
-
reveniumBaseUrl: 'https://api.revenium.io',
|
|
420
|
-
apiTimeout: 5000,
|
|
421
|
-
failSilent: true,
|
|
422
|
-
maxRetries: 3,
|
|
423
|
-
});
|
|
424
|
-
```
|
|
425
|
-
|
|
426
|
-
## Configuration Options
|
|
427
|
-
|
|
428
|
-
### Environment Variables
|
|
429
|
-
|
|
430
|
-
| Variable | Required | Default | Description |
|
|
431
|
-
| ------------------------------ | -------- | ------------------------------- | ---------------------------------------------- |
|
|
432
|
-
| `REVENIUM_METERING_API_KEY` | true | - | Your Revenium API key (starts with `hak_`) |
|
|
433
|
-
| `OPENAI_API_KEY` | true | - | Your OpenAI API key (starts with `sk-`) |
|
|
434
|
-
| `REVENIUM_METERING_BASE_URL` | false | `https://api.revenium.io` | Revenium metering API base URL |
|
|
435
|
-
| `REVENIUM_DEBUG` | false | `false` | Enable debug logging (`true`/`false`) |
|
|
436
|
-
| `AZURE_OPENAI_ENDPOINT` | false | - | Azure OpenAI endpoint URL (for Azure testing) |
|
|
437
|
-
| `AZURE_OPENAI_API_KEY` | false | - | Azure OpenAI API key (for Azure testing) |
|
|
438
|
-
| `AZURE_OPENAI_DEPLOYMENT` | false | - | Azure OpenAI deployment name (for Azure) |
|
|
439
|
-
| `AZURE_OPENAI_API_VERSION` | false | `2024-12-01-preview` | Azure OpenAI API version (for Azure) |
|
|
440
|
-
|
|
441
|
-
**Important Note about `REVENIUM_METERING_BASE_URL`:**
|
|
442
|
-
|
|
443
|
-
- This variable is **optional** and defaults to the production URL (`https://api.revenium.io`)
|
|
444
|
-
- If you don't set it explicitly, the middleware will use the default production endpoint
|
|
445
|
-
- However, you may see console warnings or errors if the middleware cannot determine the correct environment
|
|
446
|
-
- **Best practice:** Always set this variable explicitly to match your environment:
|
|
447
|
-
|
|
448
|
-
```bash
|
|
449
|
-
# Default production URL (recommended)
|
|
450
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
451
|
-
```
|
|
452
|
-
|
|
453
|
-
- **Remember:** Your `REVENIUM_METERING_API_KEY` must match your base URL environment
|
|
454
|
-
|
|
455
|
-
### Usage Metadata Options
|
|
456
|
-
|
|
457
|
-
All metadata fields are optional and help provide better analytics:
|
|
458
|
-
|
|
459
|
-
```typescript
|
|
460
|
-
interface UsageMetadata {
|
|
461
|
-
traceId?: string; // Session or conversation ID
|
|
462
|
-
taskType?: string; // Type of AI task (e.g., "chat", "summary")
|
|
463
|
-
subscriber?: {
|
|
464
|
-
// User information (nested structure)
|
|
465
|
-
id?: string; // User ID from your system
|
|
466
|
-
email?: string; // User's email address
|
|
467
|
-
credential?: {
|
|
468
|
-
// User credentials
|
|
469
|
-
name?: string; // Credential name
|
|
470
|
-
value?: string; // Credential value
|
|
471
|
-
};
|
|
472
|
-
};
|
|
473
|
-
organizationId?: string; // Organization/company ID
|
|
474
|
-
subscriptionId?: string; // Billing plan ID
|
|
475
|
-
productId?: string; // Your product/feature ID
|
|
476
|
-
agent?: string; // AI agent identifier
|
|
477
|
-
responseQualityScore?: number; // Quality score (0-1)
|
|
478
|
-
}
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
## Included Examples
|
|
482
|
-
|
|
483
|
-
The package includes 8 comprehensive example files in your installation:
|
|
484
|
-
|
|
485
|
-
**OpenAI Examples:**
|
|
486
|
-
- **openai-basic.ts**: Basic chat completions with metadata tracking
|
|
487
|
-
- **openai-streaming.ts**: Streaming responses with real-time output
|
|
488
|
-
- **openai-responses-basic.ts**: New Responses API usage (OpenAI SDK 5.8+)
|
|
489
|
-
- **openai-responses-streaming.ts**: Streaming with Responses API
|
|
490
|
-
|
|
491
|
-
**Azure OpenAI Examples:**
|
|
492
|
-
- **azure-basic.ts**: Azure OpenAI chat completions
|
|
493
|
-
- **azure-streaming.ts**: Azure streaming responses
|
|
494
|
-
- **azure-responses-basic.ts**: Azure Responses API
|
|
495
|
-
- **azure-responses-streaming.ts**: Azure streaming Responses API
|
|
496
|
-
|
|
497
|
-
**For npm users:** Examples are installed in `node_modules/@revenium/openai/examples/`
|
|
498
|
-
|
|
499
|
-
**For GitHub users:** Examples are in the repository's `examples/` directory
|
|
500
|
-
|
|
501
|
-
For detailed setup instructions and usage patterns, see [examples/README.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md).
|
|
167
|
+
**For complete example documentation, setup instructions, and usage patterns, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md).**
|
|
502
168
|
|
|
503
169
|
## How It Works
|
|
504
170
|
|
|
505
|
-
1. **
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
- `embeddings.create` (Embeddings API)
|
|
509
|
-
2. **Request Interception**: All OpenAI requests are intercepted to extract metadata
|
|
510
|
-
3. **Usage Extraction**: Token counts, model info, and timing data are captured
|
|
171
|
+
1. **Initialize**: Call `Initialize()` to set up the middleware with your configuration
|
|
172
|
+
2. **Get Client**: Call `GetClient()` to get a wrapped OpenAI client instance
|
|
173
|
+
3. **Make Requests**: Use the client normally - all requests are automatically tracked
|
|
511
174
|
4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
|
|
512
175
|
5. **Transparent Response**: Original OpenAI responses are returned unchanged
|
|
513
176
|
|
|
514
177
|
The middleware never blocks your application - if Revenium tracking fails, your OpenAI requests continue normally.
|
|
515
178
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
### Common Issues
|
|
519
|
-
|
|
520
|
-
#### 1. **No tracking data in dashboard**
|
|
521
|
-
|
|
522
|
-
**Symptoms**: OpenAI calls work but no data appears in Revenium dashboard
|
|
179
|
+
**Supported APIs:**
|
|
523
180
|
|
|
524
|
-
|
|
181
|
+
- Chat Completions API (`client.chat().completions().create()`)
|
|
182
|
+
- Embeddings API (`client.embeddings().create()`)
|
|
183
|
+
- Responses API (`client.responses().create()` and `client.responses().createStreaming()`)
|
|
525
184
|
|
|
526
|
-
|
|
527
|
-
export REVENIUM_DEBUG=true
|
|
528
|
-
```
|
|
529
|
-
|
|
530
|
-
**Expected output for successful tracking**:
|
|
531
|
-
|
|
532
|
-
```bash
|
|
533
|
-
[Revenium Debug] OpenAI chat.completions.create intercepted
|
|
534
|
-
[Revenium Debug] Revenium tracking successful
|
|
535
|
-
|
|
536
|
-
# For Responses API:
|
|
537
|
-
[Revenium Debug] OpenAI responses.create intercepted
|
|
538
|
-
[Revenium Debug] Revenium tracking successful
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
#### 2. **Environment mismatch errors**
|
|
542
|
-
|
|
543
|
-
**Symptoms**: Authentication errors or 401/403 responses
|
|
544
|
-
|
|
545
|
-
**Solution**: Ensure your API key matches your base URL environment:
|
|
546
|
-
|
|
547
|
-
```bash
|
|
548
|
-
# Correct - Key and URL from same environment
|
|
549
|
-
REVENIUM_METERING_API_KEY=hak_your_api_key_here
|
|
550
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
551
|
-
|
|
552
|
-
# Wrong - Key and URL from different environments
|
|
553
|
-
REVENIUM_METERING_API_KEY=hak_wrong_environment_key
|
|
554
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
555
|
-
```
|
|
556
|
-
|
|
557
|
-
#### 3. **TypeScript type errors**
|
|
558
|
-
|
|
559
|
-
**Symptoms**: TypeScript errors about `usageMetadata` property
|
|
560
|
-
|
|
561
|
-
**Solution**: Ensure you're importing the middleware before OpenAI:
|
|
562
|
-
|
|
563
|
-
```typescript
|
|
564
|
-
// Correct order
|
|
565
|
-
import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
566
|
-
import OpenAI from 'openai';
|
|
567
|
-
|
|
568
|
-
// Wrong order
|
|
569
|
-
import OpenAI from 'openai';
|
|
570
|
-
import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
571
|
-
```
|
|
572
|
-
|
|
573
|
-
#### 4. **Azure OpenAI not working**
|
|
574
|
-
|
|
575
|
-
**Symptoms**: Azure OpenAI calls not being tracked
|
|
576
|
-
|
|
577
|
-
**Solution**: Ensure you're using `patchOpenAIInstance()` with your Azure client:
|
|
185
|
+
## Troubleshooting
|
|
578
186
|
|
|
579
|
-
|
|
580
|
-
import { AzureOpenAI } from 'openai';
|
|
581
|
-
import { patchOpenAIInstance } from '@revenium/openai';
|
|
187
|
+
### Common Issues
|
|
582
188
|
|
|
583
|
-
|
|
584
|
-
const azure = patchOpenAIInstance(new AzureOpenAI({...}));
|
|
189
|
+
**No tracking data appears:**
|
|
585
190
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
191
|
+
1. Verify environment variables are set correctly in `.env`
|
|
192
|
+
2. Enable debug logging by setting `REVENIUM_DEBUG=true` in `.env`
|
|
193
|
+
3. Check console for `[Revenium]` log messages
|
|
194
|
+
4. Verify your `REVENIUM_METERING_API_KEY` is valid
|
|
589
195
|
|
|
590
|
-
|
|
196
|
+
**Client not initialized error:**
|
|
591
197
|
|
|
592
|
-
|
|
198
|
+
- Make sure you call `Initialize()` before `GetClient()`
|
|
199
|
+
- Check that your `.env` file is in the project root
|
|
200
|
+
- Verify `REVENIUM_METERING_API_KEY` is set
|
|
593
201
|
|
|
594
|
-
**
|
|
202
|
+
**Azure OpenAI not working:**
|
|
595
203
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
204
|
+
- Verify all Azure environment variables are set (see `.env.example`)
|
|
205
|
+
- Check that `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_API_KEY` are correct
|
|
206
|
+
- Ensure you're using a valid deployment name in the `model` parameter
|
|
599
207
|
|
|
600
208
|
### Debug Mode
|
|
601
209
|
|
|
602
|
-
Enable
|
|
210
|
+
Enable detailed logging by adding to your `.env`:
|
|
603
211
|
|
|
604
|
-
```
|
|
605
|
-
|
|
212
|
+
```env
|
|
213
|
+
REVENIUM_DEBUG=true
|
|
606
214
|
```
|
|
607
215
|
|
|
608
|
-
This will show:
|
|
609
|
-
|
|
610
|
-
- Middleware initialization status
|
|
611
|
-
- Request interception confirmations
|
|
612
|
-
- Metadata extraction details
|
|
613
|
-
- Tracking success/failure messages
|
|
614
|
-
- Error details and stack traces
|
|
615
|
-
|
|
616
216
|
### Getting Help
|
|
617
217
|
|
|
618
|
-
If
|
|
619
|
-
|
|
620
|
-
1. **Check the logs** with `REVENIUM_DEBUG=true`
|
|
621
|
-
2. **Verify environment variables** are set correctly
|
|
622
|
-
3. **Test with minimal example** from our documentation
|
|
623
|
-
4. **Contact support** with debug logs and error details
|
|
218
|
+
If issues persist:
|
|
624
219
|
|
|
625
|
-
|
|
220
|
+
1. Enable debug logging (`REVENIUM_DEBUG=true`)
|
|
221
|
+
2. Check the [`examples/`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/) directory for working examples
|
|
222
|
+
3. Review [`examples/README.md`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md) for detailed setup instructions
|
|
223
|
+
4. Contact support@revenium.io with debug logs
|
|
626
224
|
|
|
627
225
|
## Supported Models
|
|
628
226
|
|
|
629
|
-
This middleware works with
|
|
227
|
+
This middleware works with any OpenAI model. For the complete model list, see the [OpenAI Models Documentation](https://platform.openai.com/docs/models).
|
|
630
228
|
|
|
631
|
-
|
|
632
|
-
- [Revenium AI Models API](https://revenium.readme.io/v2.0.0/reference/get_ai_model)
|
|
229
|
+
### API Support Matrix
|
|
633
230
|
|
|
634
|
-
|
|
231
|
+
The following table shows what has been tested and verified with working examples:
|
|
635
232
|
|
|
636
|
-
|
|
233
|
+
| Feature | Chat Completions | Embeddings | Responses API |
|
|
234
|
+
| --------------------- | ---------------- | ---------- | ------------- |
|
|
235
|
+
| **OpenAI Basic** | Yes | Yes | Yes |
|
|
236
|
+
| **OpenAI Streaming** | Yes | No | Yes |
|
|
237
|
+
| **Azure Basic** | Yes | No | Yes |
|
|
238
|
+
| **Azure Streaming** | Yes | No | Yes |
|
|
239
|
+
| **Metadata Tracking** | Yes | Yes | Yes |
|
|
240
|
+
| **Token Counting** | Yes | Yes | Yes |
|
|
637
241
|
|
|
638
|
-
|
|
639
|
-
| --------------------- | -------------------- | ------------- | -------------- |
|
|
640
|
-
| **Basic Requests** | Yes | Yes | Yes |
|
|
641
|
-
| **Streaming** | Yes | Yes | No |
|
|
642
|
-
| **Metadata Tracking** | Yes | Yes | Yes |
|
|
643
|
-
| **Azure OpenAI** | Yes | Yes | Yes |
|
|
644
|
-
| **Cost Calculation** | Yes | Yes | Yes |
|
|
645
|
-
| **Token Counting** | Yes | Yes | Yes |
|
|
242
|
+
**Note:** "Yes" = Tested with working examples in [`examples/`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/) directory
|
|
646
243
|
|
|
647
244
|
## Documentation
|
|
648
245
|
|
|
@@ -668,14 +265,11 @@ This project is licensed under the MIT License - see the [LICENSE](https://githu
|
|
|
668
265
|
|
|
669
266
|
For issues, feature requests, or contributions:
|
|
670
267
|
|
|
268
|
+
- **Website**: [www.revenium.ai](https://www.revenium.ai)
|
|
671
269
|
- **GitHub Repository**: [revenium/revenium-middleware-openai-node](https://github.com/revenium/revenium-middleware-openai-node)
|
|
672
270
|
- **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-openai-node/issues)
|
|
673
271
|
- **Documentation**: [docs.revenium.io](https://docs.revenium.io)
|
|
674
|
-
- **
|
|
675
|
-
|
|
676
|
-
## Development
|
|
677
|
-
|
|
678
|
-
For development and testing instructions, see [DEVELOPMENT.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/DEVELOPMENT.md).
|
|
272
|
+
- **Email**: support@revenium.io
|
|
679
273
|
|
|
680
274
|
---
|
|
681
275
|
|