@revenium/openai 1.0.11 → 1.0.13

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.
Files changed (96) hide show
  1. package/.env.example +20 -0
  2. package/CHANGELOG.md +47 -47
  3. package/README.md +121 -964
  4. package/dist/cjs/core/config/loader.js +1 -1
  5. package/dist/cjs/core/config/loader.js.map +1 -1
  6. package/dist/cjs/core/config/manager.js +2 -1
  7. package/dist/cjs/core/config/manager.js.map +1 -1
  8. package/dist/cjs/core/providers/detector.js +3 -3
  9. package/dist/cjs/core/providers/detector.js.map +1 -1
  10. package/dist/cjs/core/tracking/api-client.js +1 -1
  11. package/dist/cjs/core/tracking/api-client.js.map +1 -1
  12. package/dist/cjs/core/tracking/payload-builder.js +17 -12
  13. package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
  14. package/dist/cjs/index.js +23 -2
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/types/index.js.map +1 -1
  17. package/dist/cjs/utils/metadata-builder.js +12 -5
  18. package/dist/cjs/utils/metadata-builder.js.map +1 -1
  19. package/dist/cjs/utils/stop-reason-mapper.js +4 -0
  20. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
  21. package/dist/cjs/utils/url-builder.js +32 -7
  22. package/dist/cjs/utils/url-builder.js.map +1 -1
  23. package/dist/esm/core/config/loader.js +1 -1
  24. package/dist/esm/core/config/loader.js.map +1 -1
  25. package/dist/esm/core/config/manager.js +2 -1
  26. package/dist/esm/core/config/manager.js.map +1 -1
  27. package/dist/esm/core/providers/detector.js +3 -3
  28. package/dist/esm/core/providers/detector.js.map +1 -1
  29. package/dist/esm/core/tracking/api-client.js +1 -1
  30. package/dist/esm/core/tracking/api-client.js.map +1 -1
  31. package/dist/esm/core/tracking/payload-builder.js +17 -12
  32. package/dist/esm/core/tracking/payload-builder.js.map +1 -1
  33. package/dist/esm/index.js +22 -2
  34. package/dist/esm/index.js.map +1 -1
  35. package/dist/esm/types/index.js.map +1 -1
  36. package/dist/esm/utils/metadata-builder.js +12 -5
  37. package/dist/esm/utils/metadata-builder.js.map +1 -1
  38. package/dist/esm/utils/stop-reason-mapper.js +4 -0
  39. package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
  40. package/dist/esm/utils/url-builder.js +32 -7
  41. package/dist/esm/utils/url-builder.js.map +1 -1
  42. package/dist/types/core/config/manager.d.ts.map +1 -1
  43. package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
  44. package/dist/types/index.d.ts +23 -2
  45. package/dist/types/index.d.ts.map +1 -1
  46. package/dist/types/types/index.d.ts +9 -13
  47. package/dist/types/types/index.d.ts.map +1 -1
  48. package/dist/types/types/openai-augmentation.d.ts +1 -2
  49. package/dist/types/types/openai-augmentation.d.ts.map +1 -1
  50. package/dist/types/utils/metadata-builder.d.ts +2 -1
  51. package/dist/types/utils/metadata-builder.d.ts.map +1 -1
  52. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
  53. package/dist/types/utils/url-builder.d.ts +11 -3
  54. package/dist/types/utils/url-builder.d.ts.map +1 -1
  55. package/examples/README.md +213 -255
  56. package/examples/azure-basic.ts +26 -14
  57. package/examples/azure-responses-basic.ts +39 -10
  58. package/examples/azure-responses-streaming.ts +39 -10
  59. package/examples/azure-streaming.ts +41 -20
  60. package/examples/getting_started.ts +54 -0
  61. package/examples/openai-basic.ts +39 -17
  62. package/examples/openai-function-calling.ts +259 -0
  63. package/examples/openai-responses-basic.ts +38 -9
  64. package/examples/openai-responses-streaming.ts +38 -9
  65. package/examples/openai-streaming.ts +24 -13
  66. package/examples/openai-vision.ts +289 -0
  67. package/package.json +3 -9
  68. package/src/core/config/azure-config.ts +72 -0
  69. package/src/core/config/index.ts +23 -0
  70. package/src/core/config/loader.ts +66 -0
  71. package/src/core/config/manager.ts +95 -0
  72. package/src/core/config/validator.ts +89 -0
  73. package/src/core/providers/detector.ts +159 -0
  74. package/src/core/providers/index.ts +16 -0
  75. package/src/core/tracking/api-client.ts +78 -0
  76. package/src/core/tracking/index.ts +21 -0
  77. package/src/core/tracking/payload-builder.ts +137 -0
  78. package/src/core/tracking/usage-tracker.ts +189 -0
  79. package/src/core/wrapper/index.ts +9 -0
  80. package/src/core/wrapper/instance-patcher.ts +288 -0
  81. package/src/core/wrapper/request-handler.ts +423 -0
  82. package/src/core/wrapper/stream-wrapper.ts +100 -0
  83. package/src/index.ts +360 -0
  84. package/src/types/function-parameters.ts +251 -0
  85. package/src/types/index.ts +310 -0
  86. package/src/types/openai-augmentation.ts +232 -0
  87. package/src/types/responses-api.ts +308 -0
  88. package/src/utils/azure-model-resolver.ts +220 -0
  89. package/src/utils/constants.ts +21 -0
  90. package/src/utils/error-handler.ts +251 -0
  91. package/src/utils/metadata-builder.ts +228 -0
  92. package/src/utils/provider-detection.ts +257 -0
  93. package/src/utils/request-handler-factory.ts +285 -0
  94. package/src/utils/stop-reason-mapper.ts +78 -0
  95. package/src/utils/type-guards.ts +202 -0
  96. package/src/utils/url-builder.ts +68 -0
@@ -1,42 +1,25 @@
1
- # Revenium OpenAI Middleware - TypeScript Examples
1
+ # Revenium OpenAI Middleware - Examples
2
2
 
3
- **TypeScript-first** examples demonstrating how to use the Revenium OpenAI middleware with full type safety and IntelliSense support.
3
+ **TypeScript-first** examples demonstrating automatic Revenium usage tracking with the OpenAI SDK.
4
4
 
5
- ## TypeScript-First Approach
6
-
7
- This middleware is designed with **TypeScript developers in mind**:
8
-
9
- - **Full Type Safety**: All interfaces are strongly typed with comprehensive JSDoc
10
- - **IntelliSense Support**: Rich auto-completion and inline documentation
11
- - **Module Augmentation**: Native `usageMetadata` support in OpenAI SDK calls
12
- - **Dual Package Exports**: Works with both CommonJS and ES Modules
13
- - **Zero Configuration**: Auto-initialization with environment variables
14
-
15
- ## Installation & Setup
5
+ ## Getting Started - Step by Step
16
6
 
17
- ### 1. Install Dependencies
7
+ ### 1. Create Your Project
18
8
 
19
9
  ```bash
20
- npm install @revenium/openai openai@^5.8.0
21
- npm install -D typescript tsx @types/node # For TypeScript development
22
- ```
10
+ # Create project directory
11
+ mkdir my-openai-project
12
+ cd my-openai-project
23
13
 
24
- ### 2. TypeScript Configuration
14
+ # Initialize Node.js project
15
+ npm init -y
16
+ ```
25
17
 
26
- Ensure your `tsconfig.json` includes:
18
+ ### 2. Install Dependencies
27
19
 
28
- ```json
29
- {
30
- "compilerOptions": {
31
- "target": "ES2020",
32
- "module": "ESNext",
33
- "moduleResolution": "node",
34
- "esModuleInterop": true,
35
- "allowSyntheticDefaultImports": true,
36
- "strict": true,
37
- "skipLibCheck": true
38
- }
39
- }
20
+ ```bash
21
+ npm install @revenium/openai openai dotenv
22
+ npm install -D typescript tsx @types/node # For TypeScript
40
23
  ```
41
24
 
42
25
  ### 3. Environment Setup
@@ -44,15 +27,13 @@ Ensure your `tsconfig.json` includes:
44
27
  Create a `.env` file in your project root:
45
28
 
46
29
  ```bash
47
- # Required - OpenAI API key
48
- OPENAI_API_KEY=sk-your_openai_api_key_here
49
-
50
- # Required - Revenium API key
30
+ # Required
51
31
  REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
32
+ OPENAI_API_KEY=sk_your_openai_api_key
52
33
 
53
- # Optional (uses defaults if not set)
54
- REVENIUM_METERING_BASE_URL=https://api.revenium.io/meter
55
- REVENIUM_DEBUG=false # Set to true for detailed logging
34
+ # Optional
35
+ REVENIUM_METERING_BASE_URL=https://api.revenium.ai
36
+ REVENIUM_DEBUG=false
56
37
 
57
38
  # Optional - For Azure OpenAI examples
58
39
  AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
@@ -61,301 +42,278 @@ AZURE_OPENAI_DEPLOYMENT=your-deployment-name
61
42
  AZURE_OPENAI_API_VERSION=2024-12-01-preview
62
43
  ```
63
44
 
64
- ### 4. Run TypeScript Examples
45
+ ### 4. Run Examples
46
+
47
+ **If you cloned from GitHub:**
65
48
 
66
49
  ```bash
67
- # OpenAI examples
50
+ # Run examples directly
51
+ npx tsx examples/getting_started.ts
68
52
  npx tsx examples/openai-basic.ts
69
53
  npx tsx examples/openai-streaming.ts
70
- npx tsx examples/openai-responses-basic.ts
71
- npx tsx examples/openai-responses-streaming.ts
72
-
73
- # Azure OpenAI examples
74
- npx tsx examples/azure-basic.ts
75
- npx tsx examples/azure-streaming.ts
76
- npx tsx examples/azure-responses-basic.ts
77
- npx tsx examples/azure-responses-streaming.ts
78
54
  ```
79
55
 
80
- ## Getting Started - Step by Step
56
+ **If you installed via npm:**
81
57
 
82
- This guide walks you through creating a complete project from scratch. For GitHub users who cloned this repository, you can run the included examples directly. For npm users, these examples are also available in your `node_modules/@revenium/openai/examples/` directory.
58
+ Examples are included in your `node_modules/@revenium/openai/examples/` directory:
83
59
 
84
- ### Step 1: Create Your First Test
60
+ ```bash
61
+ npx tsx node_modules/@revenium/openai/examples/getting_started.ts
62
+ npx tsx node_modules/@revenium/openai/examples/openai-basic.ts
63
+ npx tsx node_modules/@revenium/openai/examples/openai-streaming.ts
64
+ ```
85
65
 
86
- #### TypeScript Test
66
+ ## Available Examples
87
67
 
88
- Create `test-openai.ts`:
68
+ ### `getting_started.ts` - Simple Entry Point
89
69
 
90
- ```typescript
91
- // test-openai.ts
92
- import "dotenv/config";
93
- import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
94
- import OpenAI from "openai";
70
+ The simplest example to get you started with Revenium tracking:
95
71
 
96
- async function testOpenAI() {
97
- try {
98
- // Initialize Revenium middleware
99
- initializeReveniumFromEnv();
100
-
101
- // Create and patch OpenAI client (returns patched instance)
102
- const openai = patchOpenAIInstance(new OpenAI());
103
-
104
- // Make API call with optional metadata
105
- const response = await openai.chat.completions.create({
106
- model: "gpt-4o",
107
- messages: [{ role: "user", content: "What is artificial intelligence?" }],
108
- max_tokens: 100,
109
- usageMetadata: {
110
- subscriber: {
111
- id: "test-user",
112
- email: "test@example.com",
113
- },
114
- organizationId: "test-org",
115
- taskType: "test-query",
116
- },
117
- });
118
-
119
- console.log("Response:", response.choices[0].message.content);
120
- // Success! Response and metering data sent to Revenium
121
- } catch (error) {
122
- console.error("Error:", error);
123
- }
124
- }
72
+ - **Minimal setup** - Just import, configure, and start tracking
73
+ - **Complete metadata example** - Shows all 11 optional metadata fields in comments
74
+ - **Ready to customize** - Uncomment the metadata section to add tracking context
125
75
 
126
- testOpenAI();
127
- ```
76
+ **Key Features:**
128
77
 
129
- #### JavaScript Test
78
+ - Auto-initialization from environment variables
79
+ - Native `usageMetadata` support via module augmentation
80
+ - All metadata fields documented with examples
81
+ - Single API call demonstration
130
82
 
131
- Create `test-openai.js`:
83
+ **Perfect for:** First-time users, quick validation, understanding metadata structure
132
84
 
133
- ```javascript
134
- // test-openai.js
135
- require("dotenv").config();
136
- const { initializeReveniumFromEnv, patchOpenAIInstance } = require("@revenium/openai");
137
- const OpenAI = require("openai");
85
+ **See the file for complete code examples.**
138
86
 
139
- async function testOpenAI() {
140
- try {
141
- // Initialize Revenium middleware
142
- initializeReveniumFromEnv();
87
+ ### `openai-basic.ts` - Chat Completions and Embeddings
143
88
 
144
- // Create and patch OpenAI client (returns patched instance)
145
- const openai = patchOpenAIInstance(new OpenAI());
89
+ Demonstrates standard OpenAI API usage with automatic tracking:
146
90
 
147
- // Make API call (metadata optional)
148
- const response = await openai.chat.completions.create({
149
- model: "gpt-4o-mini",
150
- messages: [{ role: "user", content: "What is artificial intelligence?" }],
151
- max_tokens: 100,
152
- });
91
+ - **Chat completions** - Basic chat API with metadata tracking
92
+ - **Embeddings** - Text embedding generation with usage tracking
93
+ - **Multiple API calls** - Batch operations with consistent metadata
153
94
 
154
- console.log("Response:", response.choices[0].message.content);
155
- // Success! Response and metering data sent to Revenium
156
- } catch (error) {
157
- console.error("Error:", error);
158
- }
159
- }
95
+ **Key Features:**
160
96
 
161
- testOpenAI();
162
- ```
97
+ - TypeScript module augmentation for native `usageMetadata` support
98
+ - Full type safety with IntelliSense
99
+ - Comprehensive metadata examples
100
+ - Error handling patterns
163
101
 
164
- ### Step 2: Update package.json
102
+ **Perfect for:** Understanding basic OpenAI API patterns with tracking
165
103
 
166
- Add a test script to easily run your example:
104
+ **See the file for complete code examples.**
167
105
 
168
- ```json
169
- {
170
- "scripts": {
171
- "test": "tsx test-openai.ts"
172
- },
173
- "dependencies": {
174
- "@revenium/openai": "^1.0.10",
175
- "openai": "^5.8.0",
176
- "dotenv": "^16.0.0"
177
- },
178
- "devDependencies": {
179
- "typescript": "^5.0.0",
180
- "tsx": "^4.0.0",
181
- "@types/node": "^20.0.0"
182
- }
183
- }
184
- ```
106
+ ### `openai-streaming.ts` - Real-time Streaming
185
107
 
186
- ### Step 3: Run Your Tests
108
+ Demonstrates streaming responses with automatic token tracking:
187
109
 
188
- ```bash
189
- # TypeScript
190
- npm run test
110
+ - **Streaming chat completions** - Real-time token streaming with metadata
111
+ - **Batch embeddings** - Multiple embedding requests efficiently
112
+ - **Stream processing** - Type-safe event handling
191
113
 
192
- # Or directly
193
- npx tsx test-openai.ts
114
+ **Key Features:**
194
115
 
195
- # JavaScript
196
- node test-openai.js
197
- ```
116
+ - Automatic tracking when stream completes
117
+ - Real-time token counting
118
+ - Time-to-first-token metrics
119
+ - Stream error handling
198
120
 
199
- ### Step 4: Create Advanced Examples
121
+ **Perfect for:** Real-time applications, chatbots, interactive AI assistants
200
122
 
201
- Explore our comprehensive examples for different use cases:
123
+ **See the file for complete code examples.**
202
124
 
203
- **Standard Chat Completions API:**
204
- - `openai-basic.ts` - Chat completions with metadata
205
- - `openai-streaming.ts` - Streaming responses
206
- - `azure-basic.ts` - Azure OpenAI chat completions
207
- - `azure-streaming.ts` - Azure OpenAI streaming
125
+ ### `openai-responses-basic.ts` - Responses API
208
126
 
209
- **Responses API (OpenAI SDK 5.8+):**
210
- - `openai-responses-basic.ts` - New Responses API
211
- - `openai-responses-streaming.ts` - Responses API streaming
212
- - `azure-responses-basic.ts` - Azure Responses API
213
- - `azure-responses-streaming.ts` - Azure Responses API streaming
127
+ Demonstrates OpenAI's Responses API:
214
128
 
215
- ### Step 5: Project Structure
129
+ - **Simplified interface** - Uses `input` instead of `messages` parameter
130
+ - **Stateful API** - Enhanced capabilities for agent-like applications
131
+ - **Unified experience** - Combines chat completions and assistants features
216
132
 
217
- Here's a recommended project structure:
133
+ **Key Features:**
218
134
 
219
- ```
220
- my-openai-project/
221
- ├── .env # Environment variables (never commit!)
222
- ├── .gitignore # Include .env in here
223
- ├── package.json # Dependencies and scripts
224
- ├── tsconfig.json # TypeScript configuration
225
- ├── src/
226
- │ └── index.ts # Your application code
227
- ├── examples/ # Copy examples here for reference
228
- │ ├── openai-basic.ts
229
- │ └── ...
230
- └── node_modules/
231
- └── @revenium/openai/
232
- └── examples/ # npm users: examples available here too
233
- ```
135
+ - New Responses API patterns
136
+ - Automatic tracking with new API
137
+ - Metadata support
138
+ - Backward compatibility notes
234
139
 
235
- ## TypeScript Integration Patterns
140
+ **Perfect for:** Applications using OpenAI's latest API features
236
141
 
237
- ### Pattern A: Auto-Initialization (Recommended)
142
+ **See the file for complete code examples.**
238
143
 
239
- ```typescript
240
- import { patchOpenAIInstance } from "@revenium/openai";
241
- import OpenAI from "openai";
144
+ ### `openai-responses-streaming.ts` - Responses API Streaming
242
145
 
243
- // Auto-initializes from environment variables
244
- const openai = patchOpenAIInstance(new OpenAI());
245
- // Already tracked! No explicit init needed if env vars are set
246
- ```
146
+ Demonstrates streaming with the new Responses API:
247
147
 
248
- ### Pattern B: Explicit Initialization
148
+ - **Streaming responses** - Real-time responses with new API
149
+ - **Event handling** - Process response events as they arrive
150
+ - **Usage tracking** - Automatic tracking for streaming responses
249
151
 
250
- ```typescript
251
- import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
252
- import OpenAI from "openai";
152
+ **Key Features:**
253
153
 
254
- // Explicit initialization with error handling
255
- const result = initializeReveniumFromEnv();
256
- if (!result.success) {
257
- throw new Error(result.message);
258
- }
154
+ - Responses API streaming patterns
155
+ - Type-safe event processing
156
+ - Automatic usage metrics
157
+ - Stream completion tracking
259
158
 
260
- const openai = patchOpenAIInstance(new OpenAI());
261
- ```
159
+ **Perfect for:** Real-time applications using the new Responses API
262
160
 
263
- ### Pattern C: Manual Configuration
161
+ **See the file for complete code examples.**
264
162
 
265
- ```typescript
266
- import { patchOpenAIInstance } from "@revenium/openai";
267
- import OpenAI from "openai";
163
+ ### Azure OpenAI Examples
268
164
 
269
- const openai = patchOpenAIInstance(new OpenAI(), {
270
- meteringApiKey: "hak_your_key",
271
- meteringBaseUrl: "https://api.revenium.io/meter",
272
- });
273
- ```
165
+ #### `azure-basic.ts` - Azure Chat Completions
274
166
 
275
- ## Available TypeScript Examples
167
+ Demonstrates Azure OpenAI integration with automatic detection:
276
168
 
277
- ### OpenAI Examples
169
+ - **Azure configuration** - Environment-based Azure setup
170
+ - **Chat completions** - Azure-hosted models with tracking
171
+ - **Automatic detection** - Middleware detects Azure vs OpenAI automatically
278
172
 
279
- | File | Description | Features |
280
- |------|-------------|----------|
281
- | `openai-basic.ts` | Basic chat completions | Chat, embeddings, metadata tracking |
282
- | `openai-streaming.ts` | Streaming responses | Real-time token streaming |
283
- | `openai-responses-basic.ts` | Responses API (new) | New response format (SDK 5.8+) |
284
- | `openai-responses-streaming.ts` | Responses API streaming | Streaming with new API |
173
+ **Key Features:**
285
174
 
286
- ### Azure OpenAI Examples
175
+ - Azure OpenAI deployment configuration
176
+ - Model name resolution for Azure
177
+ - Accurate Azure pricing
178
+ - Metadata tracking with Azure
179
+
180
+ **Perfect for:** Enterprise applications using Azure OpenAI
181
+
182
+ **See the file for complete code examples.**
183
+
184
+ #### `azure-streaming.ts` - Azure Streaming
185
+
186
+ Demonstrates streaming with Azure OpenAI:
187
+
188
+ - **Azure streaming** - Real-time responses from Azure-hosted models
189
+ - **Deployment resolution** - Automatic Azure deployment name handling
190
+ - **Usage tracking** - Azure-specific metrics and pricing
191
+
192
+ **Perfect for:** Real-time Azure OpenAI applications
193
+
194
+ **See the file for complete code examples.**
195
+
196
+ #### `azure-responses-basic.ts` - Azure Responses API
197
+
198
+ Demonstrates new Responses API with Azure OpenAI:
199
+
200
+ - **Azure + Responses API** - Combine Azure hosting with new API
201
+ - **Unified interface** - Same Responses API patterns on Azure
202
+ - **Automatic tracking** - Azure-aware usage tracking
203
+
204
+ **Perfect for:** Azure applications using latest OpenAI features
205
+
206
+ **See the file for complete code examples.**
287
207
 
288
- | File | Description | Features |
289
- |------|-------------|----------|
290
- | `azure-basic.ts` | Azure chat completions | Azure integration, auto-detection |
291
- | `azure-streaming.ts` | Azure streaming | Real-time Azure responses |
292
- | `azure-responses-basic.ts` | Azure Responses API | New API with Azure |
293
- | `azure-responses-streaming.ts` | Azure Responses streaming | Streaming with Azure Responses API |
208
+ #### `azure-responses-streaming.ts` - Azure Responses Streaming
294
209
 
295
- ## TypeScript Requirements
210
+ Demonstrates Responses API streaming with Azure OpenAI:
296
211
 
297
- **Minimum versions:**
298
- - TypeScript: 4.5+
299
- - Node.js: 16.0+
300
- - OpenAI SDK: 5.0+ (5.8+ for Responses API)
212
+ - **Azure streaming** - Real-time Responses API on Azure
213
+ - **Event handling** - Process Azure response events
214
+ - **Complete tracking** - Azure metrics with new API
215
+
216
+ **Perfect for:** Real-time Azure applications with Responses API
217
+
218
+ **See the file for complete code examples.**
219
+
220
+ ## TypeScript Configuration
221
+
222
+ Ensure your `tsconfig.json` includes:
301
223
 
302
- **TypeScript compiler options:**
303
224
  ```json
304
225
  {
305
- "esModuleInterop": true,
306
- "allowSyntheticDefaultImports": true,
307
- "strict": true,
308
- "skipLibCheck": true
226
+ "compilerOptions": {
227
+ "target": "ES2020",
228
+ "module": "ESNext",
229
+ "moduleResolution": "node",
230
+ "esModuleInterop": true,
231
+ "allowSyntheticDefaultImports": true,
232
+ "strict": true,
233
+ "skipLibCheck": true
234
+ }
309
235
  }
310
236
  ```
311
237
 
312
- ## TypeScript Troubleshooting
238
+ ## Requirements
239
+
240
+ - **Node.js 16+** with TypeScript support
241
+ - **TypeScript 4.5+** for module augmentation features
242
+ - **Valid Revenium API key** (starts with `hak_`)
243
+ - **Valid OpenAI API key** (starts with `sk-`) or Azure OpenAI credentials
244
+ - **OpenAI SDK 5.0+** (5.8+ for Responses API)
245
+
246
+ ## Troubleshooting
247
+
248
+ ### Module Augmentation Not Working
313
249
 
314
- ### Issue: `usageMetadata` property not recognized
250
+ **Problem:** TypeScript doesn't recognize `usageMetadata` in OpenAI SDK calls
315
251
 
316
- **Solution**: Ensure you import the middleware **before** OpenAI:
252
+ **Solution:**
317
253
 
318
254
  ```typescript
319
- // Correct order
320
- import "@revenium/openai";
321
- import OpenAI from "openai";
255
+ // ❌ Wrong - missing module augmentation import
256
+ import { initializeReveniumFromEnv } from "@revenium/openai";
322
257
 
323
- // Wrong order
258
+ // ✅ Correct - import for module augmentation
259
+ import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
324
260
  import OpenAI from "openai";
325
- import "@revenium/openai";
326
261
  ```
327
262
 
328
- ### Issue: Type errors with streaming responses
263
+ ### Environment Variables Not Loading
329
264
 
330
- **Solution**: The middleware extends OpenAI types automatically. If you see errors:
265
+ **Problem:** `REVENIUM_METERING_API_KEY` or `OPENAI_API_KEY` not found
331
266
 
332
- ```typescript
333
- // Ensure you're using the patched client (correct pattern)
334
- import { patchOpenAIInstance } from "@revenium/openai";
335
- import OpenAI from "openai";
267
+ **Solutions:**
268
+
269
+ - Ensure `.env` file is in project root
270
+ - Check variable names match exactly
271
+ - Verify you're importing `dotenv/config` before the middleware
272
+ - Check API keys have correct prefixes (`hak_` for Revenium, `sk-` for OpenAI)
273
+
274
+ ### TypeScript Compilation Errors
336
275
 
337
- const openai = patchOpenAIInstance(new OpenAI()); // Returns patched instance
276
+ **Problem:** Module resolution or import errors
277
+
278
+ **Solution:** Verify your `tsconfig.json` settings:
279
+
280
+ ```json
281
+ {
282
+ "compilerOptions": {
283
+ "moduleResolution": "node",
284
+ "esModuleInterop": true,
285
+ "allowSyntheticDefaultImports": true,
286
+ "strict": true
287
+ }
288
+ }
338
289
  ```
339
290
 
340
- ### Issue: Examples not found after npm install
291
+ ### Azure Configuration Issues
341
292
 
342
- **Solution**: Examples are included in the package:
293
+ **Problem:** Azure OpenAI not working or incorrect pricing
294
+
295
+ **Solutions:**
296
+
297
+ - Verify all Azure environment variables are set (`AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_DEPLOYMENT`)
298
+ - Check deployment name matches your Azure resource
299
+ - Ensure API version is compatible (`2024-12-01-preview` or later recommended)
300
+ - Verify endpoint URL format: `https://your-resource-name.openai.azure.com/`
301
+
302
+ ### Debug Mode
303
+
304
+ Enable detailed logging to troubleshoot issues:
343
305
 
344
306
  ```bash
345
- # Check examples are present
346
- ls node_modules/@revenium/openai/examples/
307
+ # In .env file
308
+ REVENIUM_DEBUG=true
347
309
 
348
- # Copy to your project
349
- cp -r node_modules/@revenium/openai/examples/ ./examples/
310
+ # Then run examples
311
+ npx tsx examples/getting_started.ts
350
312
  ```
351
313
 
352
- ## Getting Help
314
+ ## Additional Resources
353
315
 
354
316
  - **Main Documentation**: See root [README.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/README.md)
355
- - **Troubleshooting**: See [TROUBLESHOOTING.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/TROUBLESHOOTING.md)
317
+ - **API Reference**: [Revenium Metadata Fields](https://revenium.readme.io/reference/meter_ai_completion)
318
+ - **OpenAI Documentation**: [OpenAI API Reference](https://platform.openai.com/docs)
356
319
  - **Issues**: [Report bugs](https://github.com/revenium/revenium-middleware-openai-node/issues)
357
- - **Support**: support@revenium.io
358
-
359
- ---
360
-
361
- **Built by Revenium** | [Documentation](https://docs.revenium.io) | [npm Package](https://www.npmjs.com/package/@revenium/openai)
@@ -1,8 +1,17 @@
1
1
  /**
2
- * Azure OpenAI Basic Example
2
+ * Azure OpenAI Basic Example
3
3
  *
4
4
  * Shows how to use Revenium middleware with Azure OpenAI chat completions and embeddings.
5
5
  * Demonstrates seamless metadata integration with Azure - all metadata fields are optional!
6
+ *
7
+ * Metadata Options:
8
+ * - Start with basic usage (no metadata) - tracking works automatically
9
+ * - Add subscriber info for user tracking
10
+ * - Include organization/product IDs for business analytics
11
+ * - Use task type and trace ID for detailed analysis
12
+ *
13
+ * For complete metadata field reference, see:
14
+ * https://revenium.readme.io/reference/meter_ai_completion
6
15
  */
7
16
 
8
17
  import 'dotenv/config';
@@ -73,30 +82,32 @@ async function azureBasicExample() {
73
82
  },
74
83
  ],
75
84
 
76
- // All metadata fields are optional - add what you need for Azure analytics!
77
- // Note: Nested subscriber structure for consistency across language implementations
85
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
78
86
  usageMetadata: {
79
- // User tracking (optional) - nested subscriber object
87
+ // User identification
80
88
  subscriber: {
81
89
  id: 'azure-user-789',
82
90
  email: 'azure-dev@company.com',
83
91
  credential: {
84
- name: 'azure-key',
85
- value: 'azure789',
92
+ name: 'api-key-prod',
93
+ value: 'key-jkl-012',
86
94
  },
87
95
  },
88
96
 
89
- // Business context (optional)
97
+ // Organization & billing
90
98
  organizationId: 'enterprise-corp',
91
- productId: 'azure-ai-platform',
99
+ subscriptionId: 'plan-azure-enterprise-2024',
92
100
 
93
- // Task classification (optional)
101
+ // Product & task tracking
102
+ productId: 'azure-ai-platform',
94
103
  taskType: 'azure-comparison',
95
- traceId: `azure-${Date.now()}`,
96
-
97
- // Azure-specific fields (optional)
98
104
  agent: 'azure-basic-chat-node',
99
- responseQualityScore: 0.92,
105
+
106
+ // Session tracking
107
+ traceId: 'azure-' + Date.now(),
108
+
109
+ // Quality metrics
110
+ responseQualityScore: 0.92, // 0.0-1.0 scale
100
111
  },
101
112
  });
102
113
 
@@ -148,8 +159,9 @@ async function azureBasicExample() {
148
159
  },
149
160
  },
150
161
  organizationId: 'enterprise-corp',
151
- taskType: 'enterprise-document-embedding',
152
162
  productId: 'azure-search-platform',
163
+ subscriptionId: 'sub-azure-premium-999',
164
+ taskType: 'enterprise-document-embedding',
153
165
  traceId: `azure-embed-${Date.now()}`,
154
166
  agent: 'azure-basic-embeddings-node',
155
167
  },