@revenium/openai 1.0.10 → 1.0.11
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/CHANGELOG.md +78 -0
- package/LICENSE +21 -21
- package/README.md +1231 -1152
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/types/openai-augmentation.js +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/types/openai-augmentation.js +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/types/openai-augmentation.d.ts +1 -1
- package/examples/README.md +361 -0
- package/examples/azure-basic.ts +194 -0
- package/examples/azure-responses-basic.ts +204 -0
- package/examples/azure-responses-streaming.ts +226 -0
- package/examples/azure-streaming.ts +188 -0
- package/examples/openai-basic.ts +125 -0
- package/examples/openai-responses-basic.ts +183 -0
- package/examples/openai-responses-streaming.ts +203 -0
- package/examples/openai-streaming.ts +161 -0
- package/package.json +87 -84
package/dist/cjs/index.js
CHANGED
|
@@ -11,14 +11,14 @@
|
|
|
11
11
|
* OPENAI_API_KEY=sk_your_openai_key
|
|
12
12
|
*
|
|
13
13
|
* Simple Usage (auto-initialization):
|
|
14
|
-
* import { patchOpenAIInstance } from 'revenium
|
|
14
|
+
* import { patchOpenAIInstance } from '@revenium/openai';
|
|
15
15
|
* import OpenAI from 'openai';
|
|
16
16
|
*
|
|
17
17
|
* const openai = patchOpenAIInstance(new OpenAI());
|
|
18
18
|
* // Auto-initializes from environment variables
|
|
19
19
|
*
|
|
20
20
|
* Advanced Usage (explicit initialization):
|
|
21
|
-
* import { initializeReveniumFromEnv, patchOpenAIInstance } from 'revenium
|
|
21
|
+
* import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
22
22
|
* import OpenAI from 'openai';
|
|
23
23
|
*
|
|
24
24
|
* const result = initializeReveniumFromEnv();
|
package/dist/esm/index.js
CHANGED
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
* OPENAI_API_KEY=sk_your_openai_key
|
|
11
11
|
*
|
|
12
12
|
* Simple Usage (auto-initialization):
|
|
13
|
-
* import { patchOpenAIInstance } from 'revenium
|
|
13
|
+
* import { patchOpenAIInstance } from '@revenium/openai';
|
|
14
14
|
* import OpenAI from 'openai';
|
|
15
15
|
*
|
|
16
16
|
* const openai = patchOpenAIInstance(new OpenAI());
|
|
17
17
|
* // Auto-initializes from environment variables
|
|
18
18
|
*
|
|
19
19
|
* Advanced Usage (explicit initialization):
|
|
20
|
-
* import { initializeReveniumFromEnv, patchOpenAIInstance } from 'revenium
|
|
20
|
+
* import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
21
21
|
* import OpenAI from 'openai';
|
|
22
22
|
*
|
|
23
23
|
* const result = initializeReveniumFromEnv();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
* OPENAI_API_KEY=sk_your_openai_key
|
|
11
11
|
*
|
|
12
12
|
* Simple Usage (auto-initialization):
|
|
13
|
-
* import { patchOpenAIInstance } from 'revenium
|
|
13
|
+
* import { patchOpenAIInstance } from '@revenium/openai';
|
|
14
14
|
* import OpenAI from 'openai';
|
|
15
15
|
*
|
|
16
16
|
* const openai = patchOpenAIInstance(new OpenAI());
|
|
17
17
|
* // Auto-initializes from environment variables
|
|
18
18
|
*
|
|
19
19
|
* Advanced Usage (explicit initialization):
|
|
20
|
-
* import { initializeReveniumFromEnv, patchOpenAIInstance } from 'revenium
|
|
20
|
+
* import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
21
21
|
* import OpenAI from 'openai';
|
|
22
22
|
*
|
|
23
23
|
* const result = initializeReveniumFromEnv();
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# Revenium OpenAI Middleware - TypeScript Examples
|
|
2
|
+
|
|
3
|
+
**TypeScript-first** examples demonstrating how to use the Revenium OpenAI middleware with full type safety and IntelliSense support.
|
|
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
|
|
16
|
+
|
|
17
|
+
### 1. Install Dependencies
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @revenium/openai openai@^5.8.0
|
|
21
|
+
npm install -D typescript tsx @types/node # For TypeScript development
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 2. TypeScript Configuration
|
|
25
|
+
|
|
26
|
+
Ensure your `tsconfig.json` includes:
|
|
27
|
+
|
|
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
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 3. Environment Setup
|
|
43
|
+
|
|
44
|
+
Create a `.env` file in your project root:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# Required - OpenAI API key
|
|
48
|
+
OPENAI_API_KEY=sk-your_openai_api_key_here
|
|
49
|
+
|
|
50
|
+
# Required - Revenium API key
|
|
51
|
+
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
|
|
52
|
+
|
|
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
|
|
56
|
+
|
|
57
|
+
# Optional - For Azure OpenAI examples
|
|
58
|
+
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
|
|
59
|
+
AZURE_OPENAI_API_KEY=your-azure-api-key
|
|
60
|
+
AZURE_OPENAI_DEPLOYMENT=your-deployment-name
|
|
61
|
+
AZURE_OPENAI_API_VERSION=2024-12-01-preview
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 4. Run TypeScript Examples
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# OpenAI examples
|
|
68
|
+
npx tsx examples/openai-basic.ts
|
|
69
|
+
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
|
+
```
|
|
79
|
+
|
|
80
|
+
## Getting Started - Step by Step
|
|
81
|
+
|
|
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.
|
|
83
|
+
|
|
84
|
+
### Step 1: Create Your First Test
|
|
85
|
+
|
|
86
|
+
#### TypeScript Test
|
|
87
|
+
|
|
88
|
+
Create `test-openai.ts`:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// test-openai.ts
|
|
92
|
+
import "dotenv/config";
|
|
93
|
+
import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
|
|
94
|
+
import OpenAI from "openai";
|
|
95
|
+
|
|
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
|
+
}
|
|
125
|
+
|
|
126
|
+
testOpenAI();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### JavaScript Test
|
|
130
|
+
|
|
131
|
+
Create `test-openai.js`:
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
// test-openai.js
|
|
135
|
+
require("dotenv").config();
|
|
136
|
+
const { initializeReveniumFromEnv, patchOpenAIInstance } = require("@revenium/openai");
|
|
137
|
+
const OpenAI = require("openai");
|
|
138
|
+
|
|
139
|
+
async function testOpenAI() {
|
|
140
|
+
try {
|
|
141
|
+
// Initialize Revenium middleware
|
|
142
|
+
initializeReveniumFromEnv();
|
|
143
|
+
|
|
144
|
+
// Create and patch OpenAI client (returns patched instance)
|
|
145
|
+
const openai = patchOpenAIInstance(new OpenAI());
|
|
146
|
+
|
|
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
|
+
});
|
|
153
|
+
|
|
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
|
+
}
|
|
160
|
+
|
|
161
|
+
testOpenAI();
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Step 2: Update package.json
|
|
165
|
+
|
|
166
|
+
Add a test script to easily run your example:
|
|
167
|
+
|
|
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
|
+
```
|
|
185
|
+
|
|
186
|
+
### Step 3: Run Your Tests
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
# TypeScript
|
|
190
|
+
npm run test
|
|
191
|
+
|
|
192
|
+
# Or directly
|
|
193
|
+
npx tsx test-openai.ts
|
|
194
|
+
|
|
195
|
+
# JavaScript
|
|
196
|
+
node test-openai.js
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Step 4: Create Advanced Examples
|
|
200
|
+
|
|
201
|
+
Explore our comprehensive examples for different use cases:
|
|
202
|
+
|
|
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
|
|
208
|
+
|
|
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
|
|
214
|
+
|
|
215
|
+
### Step 5: Project Structure
|
|
216
|
+
|
|
217
|
+
Here's a recommended project structure:
|
|
218
|
+
|
|
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
|
+
```
|
|
234
|
+
|
|
235
|
+
## TypeScript Integration Patterns
|
|
236
|
+
|
|
237
|
+
### Pattern A: Auto-Initialization (Recommended)
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import { patchOpenAIInstance } from "@revenium/openai";
|
|
241
|
+
import OpenAI from "openai";
|
|
242
|
+
|
|
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
|
+
```
|
|
247
|
+
|
|
248
|
+
### Pattern B: Explicit Initialization
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
|
|
252
|
+
import OpenAI from "openai";
|
|
253
|
+
|
|
254
|
+
// Explicit initialization with error handling
|
|
255
|
+
const result = initializeReveniumFromEnv();
|
|
256
|
+
if (!result.success) {
|
|
257
|
+
throw new Error(result.message);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const openai = patchOpenAIInstance(new OpenAI());
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Pattern C: Manual Configuration
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import { patchOpenAIInstance } from "@revenium/openai";
|
|
267
|
+
import OpenAI from "openai";
|
|
268
|
+
|
|
269
|
+
const openai = patchOpenAIInstance(new OpenAI(), {
|
|
270
|
+
meteringApiKey: "hak_your_key",
|
|
271
|
+
meteringBaseUrl: "https://api.revenium.io/meter",
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Available TypeScript Examples
|
|
276
|
+
|
|
277
|
+
### OpenAI Examples
|
|
278
|
+
|
|
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 |
|
|
285
|
+
|
|
286
|
+
### Azure OpenAI Examples
|
|
287
|
+
|
|
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 |
|
|
294
|
+
|
|
295
|
+
## TypeScript Requirements
|
|
296
|
+
|
|
297
|
+
**Minimum versions:**
|
|
298
|
+
- TypeScript: 4.5+
|
|
299
|
+
- Node.js: 16.0+
|
|
300
|
+
- OpenAI SDK: 5.0+ (5.8+ for Responses API)
|
|
301
|
+
|
|
302
|
+
**TypeScript compiler options:**
|
|
303
|
+
```json
|
|
304
|
+
{
|
|
305
|
+
"esModuleInterop": true,
|
|
306
|
+
"allowSyntheticDefaultImports": true,
|
|
307
|
+
"strict": true,
|
|
308
|
+
"skipLibCheck": true
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## TypeScript Troubleshooting
|
|
313
|
+
|
|
314
|
+
### Issue: `usageMetadata` property not recognized
|
|
315
|
+
|
|
316
|
+
**Solution**: Ensure you import the middleware **before** OpenAI:
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
// Correct order
|
|
320
|
+
import "@revenium/openai";
|
|
321
|
+
import OpenAI from "openai";
|
|
322
|
+
|
|
323
|
+
// Wrong order
|
|
324
|
+
import OpenAI from "openai";
|
|
325
|
+
import "@revenium/openai";
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Issue: Type errors with streaming responses
|
|
329
|
+
|
|
330
|
+
**Solution**: The middleware extends OpenAI types automatically. If you see errors:
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
// Ensure you're using the patched client (correct pattern)
|
|
334
|
+
import { patchOpenAIInstance } from "@revenium/openai";
|
|
335
|
+
import OpenAI from "openai";
|
|
336
|
+
|
|
337
|
+
const openai = patchOpenAIInstance(new OpenAI()); // Returns patched instance
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### Issue: Examples not found after npm install
|
|
341
|
+
|
|
342
|
+
**Solution**: Examples are included in the package:
|
|
343
|
+
|
|
344
|
+
```bash
|
|
345
|
+
# Check examples are present
|
|
346
|
+
ls node_modules/@revenium/openai/examples/
|
|
347
|
+
|
|
348
|
+
# Copy to your project
|
|
349
|
+
cp -r node_modules/@revenium/openai/examples/ ./examples/
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Getting Help
|
|
353
|
+
|
|
354
|
+
- **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)
|
|
356
|
+
- **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)
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ️ Azure OpenAI Basic Example
|
|
3
|
+
*
|
|
4
|
+
* Shows how to use Revenium middleware with Azure OpenAI chat completions and embeddings.
|
|
5
|
+
* Demonstrates seamless metadata integration with Azure - all metadata fields are optional!
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import 'dotenv/config';
|
|
9
|
+
import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
10
|
+
import { AzureOpenAI } from 'openai';
|
|
11
|
+
|
|
12
|
+
async function azureBasicExample() {
|
|
13
|
+
console.log('️ Azure OpenAI Basic Usage with Seamless Metadata Integration\n');
|
|
14
|
+
|
|
15
|
+
// Initialize Revenium middleware
|
|
16
|
+
const initResult = initializeReveniumFromEnv();
|
|
17
|
+
if (!initResult.success) {
|
|
18
|
+
console.error(' Failed to initialize Revenium:', initResult.message);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Create Azure OpenAI instance and patch it
|
|
23
|
+
const azure = patchOpenAIInstance(
|
|
24
|
+
new AzureOpenAI({
|
|
25
|
+
endpoint: process.env.AZURE_OPENAI_ENDPOINT,
|
|
26
|
+
apiKey: process.env.AZURE_OPENAI_API_KEY,
|
|
27
|
+
apiVersion: process.env.AZURE_OPENAI_API_VERSION || '2024-12-01-preview',
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
console.log(' Azure OpenAI client configured and patched');
|
|
32
|
+
console.log(' Endpoint:', process.env.AZURE_OPENAI_ENDPOINT);
|
|
33
|
+
console.log(' API Version:', process.env.AZURE_OPENAI_API_VERSION || '2024-12-01-preview');
|
|
34
|
+
console.log();
|
|
35
|
+
|
|
36
|
+
// Check if we have a chat model configured
|
|
37
|
+
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT;
|
|
38
|
+
const isChatModel = deployment && !deployment.includes('embedding');
|
|
39
|
+
|
|
40
|
+
if (!isChatModel) {
|
|
41
|
+
console.log('️ Note: Current Azure deployment appears to be for embeddings.');
|
|
42
|
+
console.log(' To test chat completions, update .env to use a chat model:');
|
|
43
|
+
console.log(' - Comment out the embeddings section');
|
|
44
|
+
console.log(' - Uncomment the chat testing section');
|
|
45
|
+
console.log(' - Set AZURE_OPENAI_DEPLOYMENT=gpt-4o');
|
|
46
|
+
console.log('\n Skipping chat examples and testing embeddings instead...\n');
|
|
47
|
+
} else {
|
|
48
|
+
// Example 1: Basic Azure chat completion (no metadata)
|
|
49
|
+
console.log(' Example 1: Basic Azure chat completion (automatic tracking)');
|
|
50
|
+
|
|
51
|
+
const basicResponse = await azure.chat.completions.create({
|
|
52
|
+
model: deployment,
|
|
53
|
+
messages: [{ role: 'user', content: 'What are the benefits of using Azure OpenAI?' }],
|
|
54
|
+
// No usageMetadata - still automatically tracked with Azure provider info!
|
|
55
|
+
// No max_tokens - let response complete naturally
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
console.log(' Response:', basicResponse.choices[0]?.message?.content);
|
|
59
|
+
console.log(' Usage:', basicResponse.usage);
|
|
60
|
+
console.log(' Automatically tracked to Revenium with Azure provider metadata\n');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (isChatModel) {
|
|
64
|
+
// Example 2: Azure chat completion with rich metadata (all optional!)
|
|
65
|
+
console.log(' Example 2: Azure chat completion with rich metadata');
|
|
66
|
+
|
|
67
|
+
const metadataResponse = await azure.chat.completions.create({
|
|
68
|
+
model: deployment,
|
|
69
|
+
messages: [
|
|
70
|
+
{
|
|
71
|
+
role: 'user',
|
|
72
|
+
content: 'Explain how Azure OpenAI differs from standard OpenAI in 3 points.',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
|
|
76
|
+
// All metadata fields are optional - add what you need for Azure analytics!
|
|
77
|
+
// Note: Nested subscriber structure for consistency across language implementations
|
|
78
|
+
usageMetadata: {
|
|
79
|
+
// User tracking (optional) - nested subscriber object
|
|
80
|
+
subscriber: {
|
|
81
|
+
id: 'azure-user-789',
|
|
82
|
+
email: 'azure-dev@company.com',
|
|
83
|
+
credential: {
|
|
84
|
+
name: 'azure-key',
|
|
85
|
+
value: 'azure789',
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// Business context (optional)
|
|
90
|
+
organizationId: 'enterprise-corp',
|
|
91
|
+
productId: 'azure-ai-platform',
|
|
92
|
+
|
|
93
|
+
// Task classification (optional)
|
|
94
|
+
taskType: 'azure-comparison',
|
|
95
|
+
traceId: `azure-${Date.now()}`,
|
|
96
|
+
|
|
97
|
+
// Azure-specific fields (optional)
|
|
98
|
+
agent: 'azure-basic-chat-node',
|
|
99
|
+
responseQualityScore: 0.92,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
console.log(' Response:', metadataResponse.choices[0]?.message?.content);
|
|
104
|
+
console.log(' Usage:', metadataResponse.usage);
|
|
105
|
+
console.log(' Tracked with Azure provider + rich metadata for enterprise analytics\n');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Example 3: Azure embeddings (requires embeddings model)
|
|
109
|
+
console.log(' Example 3: Azure embeddings');
|
|
110
|
+
|
|
111
|
+
if (isChatModel) {
|
|
112
|
+
console.log('️ Note: Current deployment is a chat model (gpt-4o).');
|
|
113
|
+
console.log(' Embeddings require an embedding model like text-embedding-3-large.');
|
|
114
|
+
console.log(' To test embeddings, switch .env to embeddings configuration.');
|
|
115
|
+
console.log(' Skipping embeddings examples.\n');
|
|
116
|
+
} else {
|
|
117
|
+
console.log(' Example 3a: Basic Azure embeddings (automatic tracking)');
|
|
118
|
+
|
|
119
|
+
const basicEmbedding = await azure.embeddings.create({
|
|
120
|
+
model: deployment || 'text-embedding-3-large',
|
|
121
|
+
input:
|
|
122
|
+
'Azure OpenAI provides enterprise-grade AI capabilities with enhanced security and compliance',
|
|
123
|
+
// No usageMetadata - still automatically tracked with Azure provider info!
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
console.log(' Model:', basicEmbedding.model);
|
|
127
|
+
console.log(' Usage:', basicEmbedding.usage);
|
|
128
|
+
console.log(' Embedding dimensions:', basicEmbedding.data[0]?.embedding.length);
|
|
129
|
+
console.log(' Azure embeddings automatically tracked without metadata\n');
|
|
130
|
+
|
|
131
|
+
// Example 4: Azure embeddings with metadata (all optional!)
|
|
132
|
+
console.log(' Example 3b: Azure embeddings with rich metadata');
|
|
133
|
+
|
|
134
|
+
const metadataEmbedding = await azure.embeddings.create({
|
|
135
|
+
model: deployment || 'text-embedding-3-large',
|
|
136
|
+
input:
|
|
137
|
+
'Enterprise document processing with Azure OpenAI embeddings and comprehensive tracking',
|
|
138
|
+
|
|
139
|
+
// All metadata fields are optional - perfect for Azure enterprise use cases!
|
|
140
|
+
// Note: Nested subscriber structure matches Python middleware for consistency
|
|
141
|
+
usageMetadata: {
|
|
142
|
+
subscriber: {
|
|
143
|
+
id: 'azure-embed-user-456',
|
|
144
|
+
email: 'embeddings@enterprise-corp.com',
|
|
145
|
+
credential: {
|
|
146
|
+
name: 'azure-embed-key',
|
|
147
|
+
value: 'embed456',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
organizationId: 'enterprise-corp',
|
|
151
|
+
taskType: 'enterprise-document-embedding',
|
|
152
|
+
productId: 'azure-search-platform',
|
|
153
|
+
traceId: `azure-embed-${Date.now()}`,
|
|
154
|
+
agent: 'azure-basic-embeddings-node',
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
console.log(' Model:', metadataEmbedding.model);
|
|
159
|
+
console.log(' Usage:', metadataEmbedding.usage);
|
|
160
|
+
console.log(' Embedding dimensions:', metadataEmbedding.data[0]?.embedding.length);
|
|
161
|
+
console.log(' Azure embeddings tracked with metadata for enterprise analytics\n');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Summary
|
|
165
|
+
console.log(' Azure OpenAI Summary:');
|
|
166
|
+
console.log(' Azure OpenAI automatically detected and tracked');
|
|
167
|
+
console.log(' Model name resolution for accurate pricing');
|
|
168
|
+
console.log(' Provider metadata includes "Azure" for analytics');
|
|
169
|
+
if (isChatModel) {
|
|
170
|
+
console.log(' Chat completions work with or without metadata');
|
|
171
|
+
} else {
|
|
172
|
+
console.log(' Embeddings work with or without metadata');
|
|
173
|
+
}
|
|
174
|
+
console.log(' All metadata fields are optional');
|
|
175
|
+
console.log(' No type casting required - native TypeScript support');
|
|
176
|
+
console.log(' Enterprise-grade tracking with Azure compliance');
|
|
177
|
+
|
|
178
|
+
if (!isChatModel) {
|
|
179
|
+
console.log('\n To test Azure chat completions:');
|
|
180
|
+
console.log(' 1. Edit .env file');
|
|
181
|
+
console.log(' 2. Comment out embeddings section');
|
|
182
|
+
console.log(' 3. Uncomment chat section');
|
|
183
|
+
console.log(' 4. Run this example again');
|
|
184
|
+
} else {
|
|
185
|
+
console.log('\n To test Azure embeddings:');
|
|
186
|
+
console.log(' 1. Edit .env file');
|
|
187
|
+
console.log(' 2. Comment out chat section');
|
|
188
|
+
console.log(' 3. Uncomment embeddings section');
|
|
189
|
+
console.log(' 4. Run this example again');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Run the example
|
|
194
|
+
azureBasicExample().catch(console.error);
|