@revenium/openai 1.0.13 → 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 +44 -11
- package/CODE_OF_CONDUCT.md +57 -0
- package/CONTRIBUTING.md +38 -0
- package/README.md +104 -216
- 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 +11 -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 +15 -25
- 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 -195
- 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 +17 -16
- 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/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 +13 -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 +16 -26
- 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 -177
- 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 +17 -16
- 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/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 -135
- 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 +11 -105
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +4 -138
- 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 +2 -2
- 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/examples/README.md +282 -198
- 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 -95
- 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 -137
- 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 -360
- package/src/types/function-parameters.ts +0 -251
- package/src/types/index.ts +0 -310
- package/src/types/openai-augmentation.ts +0 -232
- 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 -228
- 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 -78
- package/src/utils/type-guards.ts +0 -202
- package/src/utils/url-builder.ts +0 -68
package/examples/README.md
CHANGED
|
@@ -1,303 +1,381 @@
|
|
|
1
1
|
# Revenium OpenAI Middleware - Examples
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This directory contains examples demonstrating how to use the Revenium OpenAI middleware.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Prerequisites
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Before running the examples, make sure you have:
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
1. **Node.js 16 or later** installed
|
|
10
|
+
2. **Revenium API Key** - Get one from [Revenium Dashboard](https://app.revenium.ai)
|
|
11
|
+
3. **OpenAI API Key** - Get one from [OpenAI Platform](https://platform.openai.com)
|
|
12
|
+
4. **(Optional) Azure OpenAI credentials** - For Azure examples
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
npm init -y
|
|
16
|
-
```
|
|
14
|
+
## Setup
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
1. **Clone the repository** (if you haven't already):
|
|
19
17
|
|
|
20
|
-
```bash
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
```
|
|
18
|
+
```bash
|
|
19
|
+
git clone https://github.com/revenium/revenium-middleware-openai-node.git
|
|
20
|
+
cd revenium-middleware-openai-node
|
|
21
|
+
```
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
2. **Install dependencies**:
|
|
26
24
|
|
|
27
|
-
|
|
25
|
+
```bash
|
|
26
|
+
npm install
|
|
27
|
+
```
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
# Optional - For Azure OpenAI examples
|
|
39
|
-
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
|
|
40
|
-
AZURE_OPENAI_API_KEY=your-azure-api-key
|
|
41
|
-
AZURE_OPENAI_DEPLOYMENT=your-deployment-name
|
|
42
|
-
AZURE_OPENAI_API_VERSION=2024-12-01-preview
|
|
43
|
-
```
|
|
29
|
+
3. **Configure environment variables**:
|
|
30
|
+
|
|
31
|
+
Copy the `.env.example` file to `.env` and edit it with your API keys:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cp .env.example .env
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
See [.env.example](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/.env.example) for all available configuration options.
|
|
44
38
|
|
|
45
|
-
|
|
39
|
+
## Examples
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
### 1. Getting Started
|
|
42
|
+
|
|
43
|
+
**File:** `getting_started.ts`
|
|
44
|
+
|
|
45
|
+
The simplest example to get you started with Revenium tracking:
|
|
46
|
+
|
|
47
|
+
- Initialize the middleware
|
|
48
|
+
- Create a basic chat completion
|
|
49
|
+
- Display response and usage metrics
|
|
50
|
+
|
|
51
|
+
**Run:**
|
|
48
52
|
|
|
49
53
|
```bash
|
|
50
|
-
|
|
54
|
+
npm run example:getting-started
|
|
55
|
+
# or
|
|
51
56
|
npx tsx examples/getting_started.ts
|
|
52
|
-
npx tsx examples/openai-basic.ts
|
|
53
|
-
npx tsx examples/openai-streaming.ts
|
|
54
57
|
```
|
|
55
58
|
|
|
56
|
-
**
|
|
59
|
+
**What it does:**
|
|
60
|
+
|
|
61
|
+
- Loads configuration from environment variables
|
|
62
|
+
- Creates a simple chat completion request
|
|
63
|
+
- Automatically sends metering data to Revenium API
|
|
64
|
+
- Displays the response
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### 2. OpenAI Basic
|
|
57
69
|
|
|
58
|
-
|
|
70
|
+
**File:** `openai/basic.ts`
|
|
71
|
+
|
|
72
|
+
Demonstrates standard OpenAI API usage:
|
|
73
|
+
|
|
74
|
+
- Chat completions with metadata
|
|
75
|
+
- Embeddings generation
|
|
76
|
+
- Multiple API calls
|
|
77
|
+
|
|
78
|
+
**Run:**
|
|
59
79
|
|
|
60
80
|
```bash
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
npx tsx
|
|
81
|
+
npm run example:openai-basic
|
|
82
|
+
# or
|
|
83
|
+
npx tsx examples/openai/basic.ts
|
|
64
84
|
```
|
|
65
85
|
|
|
66
|
-
|
|
86
|
+
**What it does:**
|
|
67
87
|
|
|
68
|
-
|
|
88
|
+
- Creates chat completions with metadata tracking
|
|
89
|
+
- Generates text embeddings
|
|
90
|
+
- Demonstrates metadata usage for tracking
|
|
69
91
|
|
|
70
|
-
|
|
92
|
+
---
|
|
71
93
|
|
|
72
|
-
|
|
73
|
-
- **Complete metadata example** - Shows all 11 optional metadata fields in comments
|
|
74
|
-
- **Ready to customize** - Uncomment the metadata section to add tracking context
|
|
94
|
+
### 3. OpenAI Metadata
|
|
75
95
|
|
|
76
|
-
**
|
|
96
|
+
**File:** `openai/metadata.ts`
|
|
77
97
|
|
|
78
|
-
|
|
79
|
-
- Native `usageMetadata` support via module augmentation
|
|
80
|
-
- All metadata fields documented with examples
|
|
81
|
-
- Single API call demonstration
|
|
98
|
+
Demonstrates all available metadata fields:
|
|
82
99
|
|
|
83
|
-
|
|
100
|
+
- Complete metadata structure
|
|
101
|
+
- All optional fields documented
|
|
102
|
+
- Subscriber information
|
|
103
|
+
|
|
104
|
+
**Run:**
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
npm run example:openai-metadata
|
|
108
|
+
# or
|
|
109
|
+
npx tsx examples/openai/metadata.ts
|
|
110
|
+
```
|
|
84
111
|
|
|
85
|
-
**
|
|
112
|
+
**What it does:**
|
|
86
113
|
|
|
87
|
-
|
|
114
|
+
- Shows all available metadata fields
|
|
115
|
+
- Demonstrates subscriber tracking
|
|
116
|
+
- Includes organization and product tracking
|
|
88
117
|
|
|
89
|
-
|
|
118
|
+
**Metadata fields supported:**
|
|
90
119
|
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
120
|
+
- `traceId` - Session or conversation tracking identifier
|
|
121
|
+
- `taskType` - Type of AI task being performed
|
|
122
|
+
- `agent` - AI agent or bot identifier
|
|
123
|
+
- `organizationId` - Organization identifier
|
|
124
|
+
- `productId` - Product or service identifier
|
|
125
|
+
- `subscriptionId` - Subscription tier identifier
|
|
126
|
+
- `responseQualityScore` - Quality rating (0.0-1.0)
|
|
127
|
+
- `subscriber` - Nested subscriber object with `id`, `email`, `credential` (with `name` and `value`)
|
|
94
128
|
|
|
95
|
-
|
|
129
|
+
---
|
|
96
130
|
|
|
97
|
-
|
|
98
|
-
- Full type safety with IntelliSense
|
|
99
|
-
- Comprehensive metadata examples
|
|
100
|
-
- Error handling patterns
|
|
131
|
+
### 4. OpenAI Streaming
|
|
101
132
|
|
|
102
|
-
**
|
|
133
|
+
**File:** `openai/streaming.ts`
|
|
103
134
|
|
|
104
|
-
|
|
135
|
+
Demonstrates streaming responses:
|
|
105
136
|
|
|
106
|
-
|
|
137
|
+
- Real-time token streaming
|
|
138
|
+
- Accumulating responses
|
|
139
|
+
- Streaming metrics
|
|
107
140
|
|
|
108
|
-
|
|
141
|
+
**Run:**
|
|
109
142
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
143
|
+
```bash
|
|
144
|
+
npm run example:openai-stream
|
|
145
|
+
# or
|
|
146
|
+
npx tsx examples/openai/streaming.ts
|
|
147
|
+
```
|
|
113
148
|
|
|
114
|
-
**
|
|
149
|
+
**What it does:**
|
|
115
150
|
|
|
116
|
-
-
|
|
117
|
-
-
|
|
118
|
-
-
|
|
119
|
-
-
|
|
151
|
+
- Creates a streaming chat completion
|
|
152
|
+
- Displays tokens as they arrive in real-time
|
|
153
|
+
- Tracks streaming metrics
|
|
154
|
+
- Sends metering data after stream completes
|
|
120
155
|
|
|
121
|
-
|
|
156
|
+
---
|
|
122
157
|
|
|
123
|
-
|
|
158
|
+
### 5. OpenAI Responses API (Basic)
|
|
124
159
|
|
|
125
|
-
|
|
160
|
+
**File:** `openai/responses-basic.ts`
|
|
126
161
|
|
|
127
162
|
Demonstrates OpenAI's Responses API:
|
|
128
163
|
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
164
|
+
- Simplified interface with `input` parameter
|
|
165
|
+
- Stateful API features
|
|
166
|
+
- Automatic tracking
|
|
132
167
|
|
|
133
|
-
**
|
|
168
|
+
**Run:**
|
|
134
169
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
170
|
+
```bash
|
|
171
|
+
npm run example:openai-res-basic
|
|
172
|
+
# or
|
|
173
|
+
npx tsx examples/openai/responses-basic.ts
|
|
174
|
+
```
|
|
139
175
|
|
|
140
|
-
**
|
|
176
|
+
**What it does:**
|
|
141
177
|
|
|
142
|
-
|
|
178
|
+
- Uses the new Responses API
|
|
179
|
+
- Creates responses with metadata
|
|
180
|
+
- Demonstrates simplified interface
|
|
143
181
|
|
|
144
|
-
|
|
182
|
+
---
|
|
145
183
|
|
|
146
|
-
|
|
184
|
+
### 6. OpenAI Responses API (Embeddings)
|
|
147
185
|
|
|
148
|
-
|
|
149
|
-
- **Event handling** - Process response events as they arrive
|
|
150
|
-
- **Usage tracking** - Automatic tracking for streaming responses
|
|
186
|
+
**File:** `openai/responses-embed.ts`
|
|
151
187
|
|
|
152
|
-
|
|
188
|
+
Demonstrates embeddings with Responses API:
|
|
153
189
|
|
|
154
|
-
-
|
|
155
|
-
-
|
|
156
|
-
-
|
|
157
|
-
- Stream completion tracking
|
|
190
|
+
- Embeddings generation
|
|
191
|
+
- Metadata tracking
|
|
192
|
+
- Usage metrics
|
|
158
193
|
|
|
159
|
-
**
|
|
194
|
+
**Run:**
|
|
160
195
|
|
|
161
|
-
|
|
196
|
+
```bash
|
|
197
|
+
npm run example:openai-res-embed
|
|
198
|
+
# or
|
|
199
|
+
npx tsx examples/openai/responses-embed.ts
|
|
200
|
+
```
|
|
162
201
|
|
|
163
|
-
|
|
202
|
+
**What it does:**
|
|
164
203
|
|
|
165
|
-
|
|
204
|
+
- Generates text embeddings
|
|
205
|
+
- Tracks embedding usage
|
|
206
|
+
- Demonstrates metadata with embeddings
|
|
166
207
|
|
|
167
|
-
|
|
208
|
+
---
|
|
168
209
|
|
|
169
|
-
|
|
170
|
-
- **Chat completions** - Azure-hosted models with tracking
|
|
171
|
-
- **Automatic detection** - Middleware detects Azure vs OpenAI automatically
|
|
210
|
+
### 7. OpenAI Responses API (Streaming)
|
|
172
211
|
|
|
173
|
-
**
|
|
212
|
+
**File:** `openai/responses-streaming.ts`
|
|
174
213
|
|
|
175
|
-
|
|
176
|
-
- Model name resolution for Azure
|
|
177
|
-
- Accurate Azure pricing
|
|
178
|
-
- Metadata tracking with Azure
|
|
214
|
+
Demonstrates streaming with Responses API:
|
|
179
215
|
|
|
180
|
-
|
|
216
|
+
- Real-time streaming responses
|
|
217
|
+
- Event handling
|
|
218
|
+
- Streaming metrics
|
|
181
219
|
|
|
182
|
-
**
|
|
220
|
+
**Run:**
|
|
183
221
|
|
|
184
|
-
|
|
222
|
+
```bash
|
|
223
|
+
npm run example:openai-res-stream
|
|
224
|
+
# or
|
|
225
|
+
npx tsx examples/openai/responses-streaming.ts
|
|
226
|
+
```
|
|
185
227
|
|
|
186
|
-
|
|
228
|
+
**What it does:**
|
|
229
|
+
|
|
230
|
+
- Creates streaming responses
|
|
231
|
+
- Processes response events in real-time
|
|
232
|
+
- Tracks streaming usage metrics
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
### 8. Azure OpenAI (Basic)
|
|
187
237
|
|
|
188
|
-
|
|
189
|
-
- **Deployment resolution** - Automatic Azure deployment name handling
|
|
190
|
-
- **Usage tracking** - Azure-specific metrics and pricing
|
|
238
|
+
**File:** `azure/basic.ts`
|
|
191
239
|
|
|
192
|
-
|
|
240
|
+
Demonstrates Azure OpenAI integration:
|
|
193
241
|
|
|
194
|
-
|
|
242
|
+
- Automatic Azure detection
|
|
243
|
+
- Azure-specific configuration
|
|
244
|
+
- Chat completions with Azure
|
|
245
|
+
|
|
246
|
+
**Prerequisites:**
|
|
247
|
+
|
|
248
|
+
To run this example with Azure OpenAI, you need to configure Azure credentials in `.env`:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
AZURE_OPENAI_API_KEY=your_azure_key
|
|
252
|
+
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
|
|
253
|
+
AZURE_OPENAI_API_VERSION=your_azure_openai_api_version
|
|
254
|
+
```
|
|
195
255
|
|
|
196
|
-
|
|
256
|
+
**Run:**
|
|
197
257
|
|
|
198
|
-
|
|
258
|
+
```bash
|
|
259
|
+
npm run example:azure-basic
|
|
260
|
+
# or
|
|
261
|
+
npx tsx examples/azure/basic.ts
|
|
262
|
+
```
|
|
199
263
|
|
|
200
|
-
|
|
201
|
-
- **Unified interface** - Same Responses API patterns on Azure
|
|
202
|
-
- **Automatic tracking** - Azure-aware usage tracking
|
|
264
|
+
**What it does:**
|
|
203
265
|
|
|
204
|
-
|
|
266
|
+
- Automatically detects Azure configuration
|
|
267
|
+
- Uses Azure OpenAI for chat completions
|
|
268
|
+
- Sends metering data with provider='AZURE_OPENAI'
|
|
205
269
|
|
|
206
|
-
**
|
|
270
|
+
**Important for Azure:**
|
|
207
271
|
|
|
208
|
-
|
|
272
|
+
When using Azure OpenAI, you must pass your **deployment name** (not the OpenAI model name) in the `model` parameter. The deployment name is what you configured in Azure Portal.
|
|
209
273
|
|
|
210
|
-
|
|
274
|
+
---
|
|
211
275
|
|
|
212
|
-
|
|
213
|
-
- **Event handling** - Process Azure response events
|
|
214
|
-
- **Complete tracking** - Azure metrics with new API
|
|
276
|
+
### 9. Azure OpenAI (Streaming)
|
|
215
277
|
|
|
216
|
-
**
|
|
278
|
+
**File:** `azure/stream.ts`
|
|
217
279
|
|
|
218
|
-
|
|
280
|
+
Demonstrates streaming with Azure OpenAI:
|
|
219
281
|
|
|
220
|
-
|
|
282
|
+
- Real-time streaming from Azure
|
|
283
|
+
- Azure deployment handling
|
|
284
|
+
- Streaming metrics
|
|
221
285
|
|
|
222
|
-
|
|
286
|
+
**Run:**
|
|
223
287
|
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
"module": "ESNext",
|
|
229
|
-
"moduleResolution": "node",
|
|
230
|
-
"esModuleInterop": true,
|
|
231
|
-
"allowSyntheticDefaultImports": true,
|
|
232
|
-
"strict": true,
|
|
233
|
-
"skipLibCheck": true
|
|
234
|
-
}
|
|
235
|
-
}
|
|
288
|
+
```bash
|
|
289
|
+
npm run example:azure-stream
|
|
290
|
+
# or
|
|
291
|
+
npx tsx examples/azure/stream.ts
|
|
236
292
|
```
|
|
237
293
|
|
|
238
|
-
|
|
294
|
+
**What it does:**
|
|
295
|
+
|
|
296
|
+
- Creates streaming chat completions with Azure
|
|
297
|
+
- Displays tokens in real-time
|
|
298
|
+
- Tracks Azure streaming metrics
|
|
239
299
|
|
|
240
|
-
|
|
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)
|
|
300
|
+
---
|
|
245
301
|
|
|
246
|
-
|
|
302
|
+
### 10. Azure Responses API (Basic)
|
|
247
303
|
|
|
248
|
-
|
|
304
|
+
**File:** `azure/responses-basic.ts`
|
|
249
305
|
|
|
250
|
-
|
|
306
|
+
Demonstrates Responses API with Azure OpenAI:
|
|
251
307
|
|
|
252
|
-
|
|
308
|
+
- Azure + Responses API
|
|
309
|
+
- Simplified interface on Azure
|
|
310
|
+
- Automatic tracking
|
|
253
311
|
|
|
254
|
-
|
|
255
|
-
// ❌ Wrong - missing module augmentation import
|
|
256
|
-
import { initializeReveniumFromEnv } from "@revenium/openai";
|
|
312
|
+
**Run:**
|
|
257
313
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
314
|
+
```bash
|
|
315
|
+
npm run example:azure-res-basic
|
|
316
|
+
# or
|
|
317
|
+
npx tsx examples/azure/responses-basic.ts
|
|
261
318
|
```
|
|
262
319
|
|
|
263
|
-
|
|
320
|
+
**What it does:**
|
|
321
|
+
|
|
322
|
+
- Uses Responses API with Azure OpenAI
|
|
323
|
+
- Creates responses with Azure deployment
|
|
324
|
+
- Tracks Azure usage metrics
|
|
264
325
|
|
|
265
|
-
|
|
326
|
+
---
|
|
266
327
|
|
|
267
|
-
|
|
328
|
+
### 11. Azure Responses API (Streaming)
|
|
268
329
|
|
|
269
|
-
|
|
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)
|
|
330
|
+
**File:** `azure/responses-stream.ts`
|
|
273
331
|
|
|
274
|
-
|
|
332
|
+
Demonstrates Responses API streaming with Azure:
|
|
275
333
|
|
|
276
|
-
|
|
334
|
+
- Real-time Responses API on Azure
|
|
335
|
+
- Event handling
|
|
336
|
+
- Azure streaming metrics
|
|
277
337
|
|
|
278
|
-
**
|
|
338
|
+
**Run:**
|
|
279
339
|
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
"esModuleInterop": true,
|
|
285
|
-
"allowSyntheticDefaultImports": true,
|
|
286
|
-
"strict": true
|
|
287
|
-
}
|
|
288
|
-
}
|
|
340
|
+
```bash
|
|
341
|
+
npm run example:azure-res-stream
|
|
342
|
+
# or
|
|
343
|
+
npx tsx examples/azure/responses-stream.ts
|
|
289
344
|
```
|
|
290
345
|
|
|
291
|
-
|
|
346
|
+
**What it does:**
|
|
347
|
+
|
|
348
|
+
- Creates streaming responses with Azure
|
|
349
|
+
- Processes response events in real-time
|
|
350
|
+
- Tracks Azure streaming usage
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## Common Issues
|
|
355
|
+
|
|
356
|
+
### "Client not initialized" error
|
|
357
|
+
|
|
358
|
+
**Solution:** Make sure to call `Initialize()` before using `GetClient()`.
|
|
359
|
+
|
|
360
|
+
### "REVENIUM_METERING_API_KEY is required" error
|
|
361
|
+
|
|
362
|
+
**Solution:** Set the `REVENIUM_METERING_API_KEY` environment variable in your `.env` file.
|
|
292
363
|
|
|
293
|
-
|
|
364
|
+
### "invalid Revenium API key format" error
|
|
294
365
|
|
|
295
|
-
**
|
|
366
|
+
**Solution:** Revenium API keys should start with `hak_`. Check your API key format.
|
|
296
367
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
368
|
+
### Environment variables not loading
|
|
369
|
+
|
|
370
|
+
**Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
|
|
371
|
+
|
|
372
|
+
### Azure example not working
|
|
373
|
+
|
|
374
|
+
**Solution:** Make sure you have set all required Azure environment variables in your `.env` file:
|
|
375
|
+
|
|
376
|
+
- `AZURE_OPENAI_API_KEY`
|
|
377
|
+
- `AZURE_OPENAI_ENDPOINT`
|
|
378
|
+
- `AZURE_OPENAI_API_VERSION`
|
|
301
379
|
|
|
302
380
|
### Debug Mode
|
|
303
381
|
|
|
@@ -308,12 +386,18 @@ Enable detailed logging to troubleshoot issues:
|
|
|
308
386
|
REVENIUM_DEBUG=true
|
|
309
387
|
|
|
310
388
|
# Then run examples
|
|
311
|
-
|
|
389
|
+
npm run example:getting-started
|
|
312
390
|
```
|
|
313
391
|
|
|
314
|
-
##
|
|
392
|
+
## Next Steps
|
|
393
|
+
|
|
394
|
+
- Check the [main README](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/README.md) for detailed documentation
|
|
395
|
+
- Visit the [Revenium Dashboard](https://app.revenium.ai) to view your metering data
|
|
396
|
+
- See [.env.example](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/.env.example) for all configuration options
|
|
397
|
+
|
|
398
|
+
## Support
|
|
399
|
+
|
|
400
|
+
For issues or questions:
|
|
315
401
|
|
|
316
|
-
-
|
|
317
|
-
-
|
|
318
|
-
- **OpenAI Documentation**: [OpenAI API Reference](https://platform.openai.com/docs)
|
|
319
|
-
- **Issues**: [Report bugs](https://github.com/revenium/revenium-middleware-openai-node/issues)
|
|
402
|
+
- Documentation: https://docs.revenium.io
|
|
403
|
+
- Email: support@revenium.io
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Azure OpenAI Example
|
|
3
|
+
* Demonstrates usage with Azure OpenAI.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Initialize, GetClient, UsageMetadata } from "@revenium/openai";
|
|
7
|
+
|
|
8
|
+
async function main() {
|
|
9
|
+
// Initialize the middleware
|
|
10
|
+
Initialize();
|
|
11
|
+
|
|
12
|
+
// Get the client
|
|
13
|
+
const client = GetClient();
|
|
14
|
+
|
|
15
|
+
// Create a metadata
|
|
16
|
+
const metadata: UsageMetadata = {
|
|
17
|
+
organizationId: "org-azure-demo",
|
|
18
|
+
productId: "prod-azure-integration",
|
|
19
|
+
taskType: "question-answering",
|
|
20
|
+
agent: "azure-assistant",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Create a chat completion
|
|
24
|
+
// IMPORTANT: For Azure OpenAI, the Model parameter must be your DEPLOYMENT NAME
|
|
25
|
+
// This is the name you gave to your deployment in Azure Portal, NOT the OpenAI model name
|
|
26
|
+
// Example: If you created a deployment called "gpt-5-mini-2" in Azure, use that name here
|
|
27
|
+
//
|
|
28
|
+
|
|
29
|
+
const response = await client
|
|
30
|
+
.chat()
|
|
31
|
+
.completions()
|
|
32
|
+
.create(
|
|
33
|
+
{
|
|
34
|
+
// NOTE: Make sure REVENIUM_AZURE_DISABLE=0 in your .env file to enable Azure
|
|
35
|
+
// If Azure is disabled, this will fail because "gpt-5-mini-2" is not a valid OpenAI model name
|
|
36
|
+
model: "gpt-5-mini-2", // Your Azure deployment name (requires REVENIUM_AZURE_DISABLE=0)
|
|
37
|
+
messages: [
|
|
38
|
+
{
|
|
39
|
+
role: "system" as const,
|
|
40
|
+
content:
|
|
41
|
+
"You are a helpful assistant that explains cloud computing concepts.",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
role: "user" as const,
|
|
45
|
+
content:
|
|
46
|
+
"Explain the difference between Azure OpenAI and OpenAI native API in 2-3 sentences.",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
max_completion_tokens: 2000, // Increased for reasoning models (gpt-5-mini uses reasoning tokens)
|
|
50
|
+
// Note: Reasoning models like gpt-5-mini only support default temperature (1.0)
|
|
51
|
+
},
|
|
52
|
+
metadata
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// Display the response
|
|
56
|
+
if (response.choices.length > 0) {
|
|
57
|
+
console.log(`Assistant: ${response.choices[0].message.content}`);
|
|
58
|
+
}
|
|
59
|
+
console.log("\nUsage data sent to Revenium! Check your dashboard");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
main().catch(console.error);
|