@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.
- package/.env.example +20 -0
- package/CHANGELOG.md +47 -47
- package/README.md +121 -964
- package/dist/cjs/core/config/loader.js +1 -1
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/config/manager.js +2 -1
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/providers/detector.js +3 -3
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +1 -1
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +17 -12
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/index.js +23 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/utils/metadata-builder.js +12 -5
- package/dist/cjs/utils/metadata-builder.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 +32 -7
- package/dist/cjs/utils/url-builder.js.map +1 -1
- package/dist/esm/core/config/loader.js +1 -1
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/config/manager.js +2 -1
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/providers/detector.js +3 -3
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +1 -1
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/core/tracking/payload-builder.js +17 -12
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/index.js +22 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/utils/metadata-builder.js +12 -5
- package/dist/esm/utils/metadata-builder.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 +32 -7
- package/dist/esm/utils/url-builder.js.map +1 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/index.d.ts +23 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +9 -13
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +1 -2
- package/dist/types/types/openai-augmentation.d.ts.map +1 -1
- package/dist/types/utils/metadata-builder.d.ts +2 -1
- package/dist/types/utils/metadata-builder.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 +11 -3
- package/dist/types/utils/url-builder.d.ts.map +1 -1
- package/examples/README.md +213 -255
- package/examples/azure-basic.ts +26 -14
- package/examples/azure-responses-basic.ts +39 -10
- package/examples/azure-responses-streaming.ts +39 -10
- package/examples/azure-streaming.ts +41 -20
- package/examples/getting_started.ts +54 -0
- package/examples/openai-basic.ts +39 -17
- package/examples/openai-function-calling.ts +259 -0
- package/examples/openai-responses-basic.ts +38 -9
- package/examples/openai-responses-streaming.ts +38 -9
- package/examples/openai-streaming.ts +24 -13
- package/examples/openai-vision.ts +289 -0
- package/package.json +3 -9
- package/src/core/config/azure-config.ts +72 -0
- package/src/core/config/index.ts +23 -0
- package/src/core/config/loader.ts +66 -0
- package/src/core/config/manager.ts +95 -0
- package/src/core/config/validator.ts +89 -0
- package/src/core/providers/detector.ts +159 -0
- package/src/core/providers/index.ts +16 -0
- package/src/core/tracking/api-client.ts +78 -0
- package/src/core/tracking/index.ts +21 -0
- package/src/core/tracking/payload-builder.ts +137 -0
- package/src/core/tracking/usage-tracker.ts +189 -0
- package/src/core/wrapper/index.ts +9 -0
- package/src/core/wrapper/instance-patcher.ts +288 -0
- package/src/core/wrapper/request-handler.ts +423 -0
- package/src/core/wrapper/stream-wrapper.ts +100 -0
- package/src/index.ts +360 -0
- package/src/types/function-parameters.ts +251 -0
- package/src/types/index.ts +310 -0
- package/src/types/openai-augmentation.ts +232 -0
- package/src/types/responses-api.ts +308 -0
- package/src/utils/azure-model-resolver.ts +220 -0
- package/src/utils/constants.ts +21 -0
- package/src/utils/error-handler.ts +251 -0
- package/src/utils/metadata-builder.ts +228 -0
- package/src/utils/provider-detection.ts +257 -0
- package/src/utils/request-handler-factory.ts +285 -0
- package/src/utils/stop-reason-mapper.ts +78 -0
- package/src/utils/type-guards.ts +202 -0
- package/src/utils/url-builder.ts +68 -0
package/examples/README.md
CHANGED
|
@@ -1,42 +1,25 @@
|
|
|
1
|
-
# Revenium OpenAI Middleware -
|
|
1
|
+
# Revenium OpenAI Middleware - Examples
|
|
2
2
|
|
|
3
|
-
**TypeScript-first** examples demonstrating
|
|
3
|
+
**TypeScript-first** examples demonstrating automatic Revenium usage tracking with the OpenAI SDK.
|
|
4
4
|
|
|
5
|
-
##
|
|
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.
|
|
7
|
+
### 1. Create Your Project
|
|
18
8
|
|
|
19
9
|
```bash
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
# Create project directory
|
|
11
|
+
mkdir my-openai-project
|
|
12
|
+
cd my-openai-project
|
|
23
13
|
|
|
24
|
-
|
|
14
|
+
# Initialize Node.js project
|
|
15
|
+
npm init -y
|
|
16
|
+
```
|
|
25
17
|
|
|
26
|
-
|
|
18
|
+
### 2. Install Dependencies
|
|
27
19
|
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
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
|
|
54
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.
|
|
55
|
-
REVENIUM_DEBUG=false
|
|
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
|
|
45
|
+
### 4. Run Examples
|
|
46
|
+
|
|
47
|
+
**If you cloned from GitHub:**
|
|
65
48
|
|
|
66
49
|
```bash
|
|
67
|
-
#
|
|
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
|
-
|
|
56
|
+
**If you installed via npm:**
|
|
81
57
|
|
|
82
|
-
|
|
58
|
+
Examples are included in your `node_modules/@revenium/openai/examples/` directory:
|
|
83
59
|
|
|
84
|
-
|
|
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
|
-
|
|
66
|
+
## Available Examples
|
|
87
67
|
|
|
88
|
-
|
|
68
|
+
### `getting_started.ts` - Simple Entry Point
|
|
89
69
|
|
|
90
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
127
|
-
```
|
|
76
|
+
**Key Features:**
|
|
128
77
|
|
|
129
|
-
|
|
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
|
-
|
|
83
|
+
**Perfect for:** First-time users, quick validation, understanding metadata structure
|
|
132
84
|
|
|
133
|
-
|
|
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
|
-
|
|
140
|
-
try {
|
|
141
|
-
// Initialize Revenium middleware
|
|
142
|
-
initializeReveniumFromEnv();
|
|
87
|
+
### `openai-basic.ts` - Chat Completions and Embeddings
|
|
143
88
|
|
|
144
|
-
|
|
145
|
-
const openai = patchOpenAIInstance(new OpenAI());
|
|
89
|
+
Demonstrates standard OpenAI API usage with automatic tracking:
|
|
146
90
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
102
|
+
**Perfect for:** Understanding basic OpenAI API patterns with tracking
|
|
165
103
|
|
|
166
|
-
|
|
104
|
+
**See the file for complete code examples.**
|
|
167
105
|
|
|
168
|
-
|
|
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
|
-
|
|
108
|
+
Demonstrates streaming responses with automatic token tracking:
|
|
187
109
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
193
|
-
npx tsx test-openai.ts
|
|
114
|
+
**Key Features:**
|
|
194
115
|
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
121
|
+
**Perfect for:** Real-time applications, chatbots, interactive AI assistants
|
|
200
122
|
|
|
201
|
-
|
|
123
|
+
**See the file for complete code examples.**
|
|
202
124
|
|
|
203
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
133
|
+
**Key Features:**
|
|
218
134
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
140
|
+
**Perfect for:** Applications using OpenAI's latest API features
|
|
236
141
|
|
|
237
|
-
|
|
142
|
+
**See the file for complete code examples.**
|
|
238
143
|
|
|
239
|
-
|
|
240
|
-
import { patchOpenAIInstance } from "@revenium/openai";
|
|
241
|
-
import OpenAI from "openai";
|
|
144
|
+
### `openai-responses-streaming.ts` - Responses API Streaming
|
|
242
145
|
|
|
243
|
-
|
|
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
|
-
|
|
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
|
-
|
|
251
|
-
import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
|
|
252
|
-
import OpenAI from "openai";
|
|
152
|
+
**Key Features:**
|
|
253
153
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
154
|
+
- Responses API streaming patterns
|
|
155
|
+
- Type-safe event processing
|
|
156
|
+
- Automatic usage metrics
|
|
157
|
+
- Stream completion tracking
|
|
259
158
|
|
|
260
|
-
|
|
261
|
-
```
|
|
159
|
+
**Perfect for:** Real-time applications using the new Responses API
|
|
262
160
|
|
|
263
|
-
|
|
161
|
+
**See the file for complete code examples.**
|
|
264
162
|
|
|
265
|
-
|
|
266
|
-
import { patchOpenAIInstance } from "@revenium/openai";
|
|
267
|
-
import OpenAI from "openai";
|
|
163
|
+
### Azure OpenAI Examples
|
|
268
164
|
|
|
269
|
-
|
|
270
|
-
meteringApiKey: "hak_your_key",
|
|
271
|
-
meteringBaseUrl: "https://api.revenium.io/meter",
|
|
272
|
-
});
|
|
273
|
-
```
|
|
165
|
+
#### `azure-basic.ts` - Azure Chat Completions
|
|
274
166
|
|
|
275
|
-
|
|
167
|
+
Demonstrates Azure OpenAI integration with automatic detection:
|
|
276
168
|
|
|
277
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
210
|
+
Demonstrates Responses API streaming with Azure OpenAI:
|
|
296
211
|
|
|
297
|
-
**
|
|
298
|
-
-
|
|
299
|
-
-
|
|
300
|
-
|
|
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
|
-
"
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
250
|
+
**Problem:** TypeScript doesn't recognize `usageMetadata` in OpenAI SDK calls
|
|
315
251
|
|
|
316
|
-
**Solution
|
|
252
|
+
**Solution:**
|
|
317
253
|
|
|
318
254
|
```typescript
|
|
319
|
-
//
|
|
320
|
-
import "@revenium/openai";
|
|
321
|
-
import OpenAI from "openai";
|
|
255
|
+
// ❌ Wrong - missing module augmentation import
|
|
256
|
+
import { initializeReveniumFromEnv } from "@revenium/openai";
|
|
322
257
|
|
|
323
|
-
//
|
|
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
|
-
###
|
|
263
|
+
### Environment Variables Not Loading
|
|
329
264
|
|
|
330
|
-
**
|
|
265
|
+
**Problem:** `REVENIUM_METERING_API_KEY` or `OPENAI_API_KEY` not found
|
|
331
266
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
291
|
+
### Azure Configuration Issues
|
|
341
292
|
|
|
342
|
-
**
|
|
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
|
-
#
|
|
346
|
-
|
|
307
|
+
# In .env file
|
|
308
|
+
REVENIUM_DEBUG=true
|
|
347
309
|
|
|
348
|
-
#
|
|
349
|
-
|
|
310
|
+
# Then run examples
|
|
311
|
+
npx tsx examples/getting_started.ts
|
|
350
312
|
```
|
|
351
313
|
|
|
352
|
-
##
|
|
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
|
-
- **
|
|
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)
|
package/examples/azure-basic.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
-
//
|
|
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
|
|
87
|
+
// User identification
|
|
80
88
|
subscriber: {
|
|
81
89
|
id: 'azure-user-789',
|
|
82
90
|
email: 'azure-dev@company.com',
|
|
83
91
|
credential: {
|
|
84
|
-
name: '
|
|
85
|
-
value: '
|
|
92
|
+
name: 'api-key-prod',
|
|
93
|
+
value: 'key-jkl-012',
|
|
86
94
|
},
|
|
87
95
|
},
|
|
88
96
|
|
|
89
|
-
//
|
|
97
|
+
// Organization & billing
|
|
90
98
|
organizationId: 'enterprise-corp',
|
|
91
|
-
|
|
99
|
+
subscriptionId: 'plan-azure-enterprise-2024',
|
|
92
100
|
|
|
93
|
-
//
|
|
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
|
-
|
|
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
|
},
|