@revenium/openai 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +10 -15
- package/CHANGELOG.md +59 -0
- package/CODE_OF_CONDUCT.md +57 -0
- package/CONTRIBUTING.md +38 -0
- package/README.md +109 -515
- package/SECURITY.md +34 -0
- package/dist/cjs/core/client/index.js +14 -0
- package/dist/cjs/core/client/index.js.map +1 -0
- package/dist/cjs/core/client/manager.js +109 -0
- package/dist/cjs/core/client/manager.js.map +1 -0
- package/dist/cjs/core/config/azure-config.js +5 -17
- package/dist/cjs/core/config/azure-config.js.map +1 -1
- package/dist/cjs/core/config/index.js +2 -2
- package/dist/cjs/core/config/index.js.map +1 -1
- package/dist/cjs/core/config/loader.js +34 -14
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/config/manager.js +12 -5
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/config/validator.js +3 -45
- package/dist/cjs/core/config/validator.js.map +1 -1
- package/dist/cjs/core/middleware/index.js +17 -0
- package/dist/cjs/core/middleware/index.js.map +1 -0
- package/dist/cjs/core/middleware/interfaces.js +361 -0
- package/dist/cjs/core/middleware/interfaces.js.map +1 -0
- package/dist/cjs/core/middleware/revenium-client.js +142 -0
- package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
- package/dist/cjs/core/providers/detector.js +45 -23
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/providers/index.js +2 -1
- package/dist/cjs/core/providers/index.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +14 -13
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +30 -35
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/core/tracking/usage-tracker.js +22 -18
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -1
- package/dist/cjs/index.js +26 -174
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js +0 -8
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/openai-augmentation.js +0 -49
- package/dist/cjs/types/openai-augmentation.js.map +1 -1
- package/dist/cjs/utils/constants.js +17 -20
- package/dist/cjs/utils/constants.js.map +1 -1
- package/dist/cjs/utils/error-handler.js +18 -14
- package/dist/cjs/utils/error-handler.js.map +1 -1
- package/dist/cjs/utils/metadata-builder.js +27 -19
- package/dist/cjs/utils/metadata-builder.js.map +1 -1
- package/dist/cjs/utils/provider-detection.js +25 -28
- package/dist/cjs/utils/provider-detection.js.map +1 -1
- package/dist/cjs/utils/stop-reason-mapper.js +4 -0
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
- package/dist/cjs/utils/url-builder.js +3 -3
- package/dist/esm/core/client/index.js +6 -0
- package/dist/esm/core/client/index.js.map +1 -0
- package/dist/esm/core/client/manager.js +102 -0
- package/dist/esm/core/client/manager.js.map +1 -0
- package/dist/esm/core/config/azure-config.js +6 -18
- package/dist/esm/core/config/azure-config.js.map +1 -1
- package/dist/esm/core/config/index.js +5 -4
- package/dist/esm/core/config/index.js.map +1 -1
- package/dist/esm/core/config/loader.js +33 -13
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/config/manager.js +14 -7
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/config/validator.js +3 -44
- package/dist/esm/core/config/validator.js.map +1 -1
- package/dist/esm/core/middleware/index.js +8 -0
- package/dist/esm/core/middleware/index.js.map +1 -0
- package/dist/esm/core/middleware/interfaces.js +353 -0
- package/dist/esm/core/middleware/interfaces.js.map +1 -0
- package/dist/esm/core/middleware/revenium-client.js +105 -0
- package/dist/esm/core/middleware/revenium-client.js.map +1 -0
- package/dist/esm/core/providers/detector.js +43 -22
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/providers/index.js +2 -2
- package/dist/esm/core/providers/index.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +13 -12
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/core/tracking/payload-builder.js +31 -36
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/core/tracking/usage-tracker.js +24 -20
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
- package/dist/esm/index.js +9 -157
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js +2 -10
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/openai-augmentation.js +0 -49
- package/dist/esm/types/openai-augmentation.js.map +1 -1
- package/dist/esm/utils/constants.js +16 -19
- package/dist/esm/utils/constants.js.map +1 -1
- package/dist/esm/utils/error-handler.js +19 -15
- package/dist/esm/utils/error-handler.js.map +1 -1
- package/dist/esm/utils/metadata-builder.js +27 -19
- package/dist/esm/utils/metadata-builder.js.map +1 -1
- package/dist/esm/utils/provider-detection.js +26 -29
- package/dist/esm/utils/provider-detection.js.map +1 -1
- package/dist/esm/utils/stop-reason-mapper.js +4 -0
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
- package/dist/esm/utils/url-builder.js +3 -3
- package/dist/types/core/client/index.d.ts +6 -0
- package/dist/types/core/client/index.d.ts.map +1 -0
- package/dist/types/core/client/manager.d.ts +32 -0
- package/dist/types/core/client/manager.d.ts.map +1 -0
- package/dist/types/core/config/azure-config.d.ts +2 -2
- package/dist/types/core/config/azure-config.d.ts.map +1 -1
- package/dist/types/core/config/index.d.ts +4 -4
- package/dist/types/core/config/index.d.ts.map +1 -1
- package/dist/types/core/config/loader.d.ts +3 -1
- package/dist/types/core/config/loader.d.ts.map +1 -1
- package/dist/types/core/config/manager.d.ts +1 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/config/validator.d.ts +1 -12
- package/dist/types/core/config/validator.d.ts.map +1 -1
- package/dist/types/core/middleware/index.d.ts +8 -0
- package/dist/types/core/middleware/index.d.ts.map +1 -0
- package/dist/types/core/middleware/interfaces.d.ts +74 -0
- package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
- package/dist/types/core/middleware/revenium-client.d.ts +58 -0
- package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
- package/dist/types/core/providers/detector.d.ts +9 -2
- package/dist/types/core/providers/detector.d.ts.map +1 -1
- package/dist/types/core/providers/index.d.ts +2 -2
- package/dist/types/core/providers/index.d.ts.map +1 -1
- package/dist/types/core/tracking/api-client.d.ts +1 -1
- package/dist/types/core/tracking/api-client.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts +3 -3
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/core/tracking/usage-tracker.d.ts +2 -2
- package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -1
- package/dist/types/index.d.ts +11 -114
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/function-parameters.d.ts +2 -23
- package/dist/types/types/function-parameters.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +17 -115
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +4 -139
- package/dist/types/types/openai-augmentation.d.ts.map +1 -1
- package/dist/types/utils/constants.d.ts +7 -1
- package/dist/types/utils/constants.d.ts.map +1 -1
- package/dist/types/utils/error-handler.d.ts +2 -2
- package/dist/types/utils/error-handler.d.ts.map +1 -1
- package/dist/types/utils/metadata-builder.d.ts +4 -3
- package/dist/types/utils/metadata-builder.d.ts.map +1 -1
- package/dist/types/utils/provider-detection.d.ts +3 -3
- package/dist/types/utils/provider-detection.d.ts.map +1 -1
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
- package/dist/types/utils/url-builder.d.ts +3 -3
- package/examples/README.md +270 -224
- package/examples/azure/basic.ts +62 -0
- package/examples/azure/responses-basic.ts +45 -0
- package/examples/azure/responses-stream.ts +61 -0
- package/examples/azure/stream.ts +56 -0
- package/examples/getting_started.ts +31 -43
- package/examples/openai/basic.ts +45 -0
- package/examples/openai/metadata.ts +67 -0
- package/examples/openai/responses-basic.ts +44 -0
- package/examples/openai/responses-embed.ts +34 -0
- package/examples/openai/responses-streaming.ts +63 -0
- package/examples/openai/streaming.ts +59 -0
- package/package.json +20 -13
- package/dist/cjs/core/wrapper/index.js +0 -15
- package/dist/cjs/core/wrapper/index.js.map +0 -1
- package/dist/cjs/core/wrapper/instance-patcher.js +0 -202
- package/dist/cjs/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/cjs/core/wrapper/request-handler.js +0 -317
- package/dist/cjs/core/wrapper/request-handler.js.map +0 -1
- package/dist/cjs/core/wrapper/stream-wrapper.js +0 -82
- package/dist/cjs/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/cjs/utils/azure-model-resolver.js +0 -211
- package/dist/cjs/utils/azure-model-resolver.js.map +0 -1
- package/dist/cjs/utils/request-handler-factory.js +0 -185
- package/dist/cjs/utils/request-handler-factory.js.map +0 -1
- package/dist/esm/core/wrapper/index.js +0 -9
- package/dist/esm/core/wrapper/index.js.map +0 -1
- package/dist/esm/core/wrapper/instance-patcher.js +0 -199
- package/dist/esm/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/esm/core/wrapper/request-handler.js +0 -310
- package/dist/esm/core/wrapper/request-handler.js.map +0 -1
- package/dist/esm/core/wrapper/stream-wrapper.js +0 -79
- package/dist/esm/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/esm/utils/azure-model-resolver.js +0 -204
- package/dist/esm/utils/azure-model-resolver.js.map +0 -1
- package/dist/esm/utils/request-handler-factory.js +0 -146
- package/dist/esm/utils/request-handler-factory.js.map +0 -1
- package/dist/types/core/wrapper/index.d.ts +0 -8
- package/dist/types/core/wrapper/index.d.ts.map +0 -1
- package/dist/types/core/wrapper/instance-patcher.d.ts +0 -33
- package/dist/types/core/wrapper/instance-patcher.d.ts.map +0 -1
- package/dist/types/core/wrapper/request-handler.d.ts +0 -29
- package/dist/types/core/wrapper/request-handler.d.ts.map +0 -1
- package/dist/types/core/wrapper/stream-wrapper.d.ts +0 -13
- package/dist/types/core/wrapper/stream-wrapper.d.ts.map +0 -1
- package/dist/types/utils/azure-model-resolver.d.ts +0 -41
- package/dist/types/utils/azure-model-resolver.d.ts.map +0 -1
- package/dist/types/utils/request-handler-factory.d.ts +0 -81
- package/dist/types/utils/request-handler-factory.d.ts.map +0 -1
- package/examples/azure-basic.ts +0 -206
- package/examples/azure-responses-basic.ts +0 -233
- package/examples/azure-responses-streaming.ts +0 -255
- package/examples/azure-streaming.ts +0 -209
- package/examples/openai-basic.ts +0 -147
- package/examples/openai-function-calling.ts +0 -259
- package/examples/openai-responses-basic.ts +0 -212
- package/examples/openai-responses-streaming.ts +0 -232
- package/examples/openai-streaming.ts +0 -172
- package/examples/openai-vision.ts +0 -289
- package/src/core/config/azure-config.ts +0 -72
- package/src/core/config/index.ts +0 -23
- package/src/core/config/loader.ts +0 -66
- package/src/core/config/manager.ts +0 -94
- package/src/core/config/validator.ts +0 -89
- package/src/core/providers/detector.ts +0 -159
- package/src/core/providers/index.ts +0 -16
- package/src/core/tracking/api-client.ts +0 -78
- package/src/core/tracking/index.ts +0 -21
- package/src/core/tracking/payload-builder.ts +0 -132
- package/src/core/tracking/usage-tracker.ts +0 -189
- package/src/core/wrapper/index.ts +0 -9
- package/src/core/wrapper/instance-patcher.ts +0 -288
- package/src/core/wrapper/request-handler.ts +0 -423
- package/src/core/wrapper/stream-wrapper.ts +0 -100
- package/src/index.ts +0 -336
- package/src/types/function-parameters.ts +0 -251
- package/src/types/index.ts +0 -313
- package/src/types/openai-augmentation.ts +0 -233
- package/src/types/responses-api.ts +0 -308
- package/src/utils/azure-model-resolver.ts +0 -220
- package/src/utils/constants.ts +0 -21
- package/src/utils/error-handler.ts +0 -251
- package/src/utils/metadata-builder.ts +0 -219
- package/src/utils/provider-detection.ts +0 -257
- package/src/utils/request-handler-factory.ts +0 -285
- package/src/utils/stop-reason-mapper.ts +0 -74
- package/src/utils/type-guards.ts +0 -202
- package/src/utils/url-builder.ts +0 -68
package/examples/README.md
CHANGED
|
@@ -1,341 +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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
### 1. Getting Started
|
|
42
|
+
|
|
43
|
+
**File:** `getting_started.ts`
|
|
44
|
+
|
|
45
|
+
The simplest example to get you started with Revenium tracking:
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
- Initialize the middleware
|
|
48
|
+
- Create a basic chat completion
|
|
49
|
+
- Display response and usage metrics
|
|
46
50
|
|
|
47
|
-
**
|
|
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:**
|
|
57
60
|
|
|
58
|
-
|
|
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
|
|
69
|
+
|
|
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
|
|
84
103
|
|
|
85
|
-
**
|
|
104
|
+
**Run:**
|
|
86
105
|
|
|
87
|
-
|
|
106
|
+
```bash
|
|
107
|
+
npm run example:openai-metadata
|
|
108
|
+
# or
|
|
109
|
+
npx tsx examples/openai/metadata.ts
|
|
110
|
+
```
|
|
88
111
|
|
|
89
|
-
|
|
112
|
+
**What it does:**
|
|
90
113
|
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
114
|
+
- Shows all available metadata fields
|
|
115
|
+
- Demonstrates subscriber tracking
|
|
116
|
+
- Includes organization and product tracking
|
|
94
117
|
|
|
95
|
-
**
|
|
118
|
+
**Metadata fields supported:**
|
|
96
119
|
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
-
|
|
100
|
-
-
|
|
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`)
|
|
101
128
|
|
|
102
|
-
|
|
129
|
+
---
|
|
103
130
|
|
|
104
|
-
|
|
131
|
+
### 4. OpenAI Streaming
|
|
105
132
|
|
|
106
|
-
|
|
133
|
+
**File:** `openai/streaming.ts`
|
|
107
134
|
|
|
108
|
-
Demonstrates streaming responses
|
|
135
|
+
Demonstrates streaming responses:
|
|
109
136
|
|
|
110
|
-
-
|
|
111
|
-
-
|
|
112
|
-
-
|
|
137
|
+
- Real-time token streaming
|
|
138
|
+
- Accumulating responses
|
|
139
|
+
- Streaming metrics
|
|
113
140
|
|
|
114
|
-
**
|
|
141
|
+
**Run:**
|
|
115
142
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
143
|
+
```bash
|
|
144
|
+
npm run example:openai-stream
|
|
145
|
+
# or
|
|
146
|
+
npx tsx examples/openai/streaming.ts
|
|
147
|
+
```
|
|
120
148
|
|
|
121
|
-
**
|
|
149
|
+
**What it does:**
|
|
122
150
|
|
|
123
|
-
|
|
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
|
|
124
155
|
|
|
125
|
-
|
|
156
|
+
---
|
|
126
157
|
|
|
127
|
-
|
|
158
|
+
### 5. OpenAI Responses API (Basic)
|
|
128
159
|
|
|
129
|
-
|
|
130
|
-
- **Function execution** - Handle tool calls and return results
|
|
131
|
-
- **Multi-turn conversations** - Manage conversation flow with function results
|
|
160
|
+
**File:** `openai/responses-basic.ts`
|
|
132
161
|
|
|
133
|
-
|
|
162
|
+
Demonstrates OpenAI's Responses API:
|
|
134
163
|
|
|
135
|
-
-
|
|
136
|
-
-
|
|
137
|
-
- Automatic tracking
|
|
138
|
-
- Multi-turn conversation handling
|
|
164
|
+
- Simplified interface with `input` parameter
|
|
165
|
+
- Stateful API features
|
|
166
|
+
- Automatic tracking
|
|
139
167
|
|
|
140
|
-
**
|
|
168
|
+
**Run:**
|
|
141
169
|
|
|
142
|
-
|
|
170
|
+
```bash
|
|
171
|
+
npm run example:openai-res-basic
|
|
172
|
+
# or
|
|
173
|
+
npx tsx examples/openai/responses-basic.ts
|
|
174
|
+
```
|
|
143
175
|
|
|
144
|
-
|
|
176
|
+
**What it does:**
|
|
145
177
|
|
|
146
|
-
|
|
178
|
+
- Uses the new Responses API
|
|
179
|
+
- Creates responses with metadata
|
|
180
|
+
- Demonstrates simplified interface
|
|
147
181
|
|
|
148
|
-
|
|
149
|
-
- **Base64 images** - Process local images as base64
|
|
150
|
-
- **Multi-image analysis** - Compare multiple images in one request
|
|
182
|
+
---
|
|
151
183
|
|
|
152
|
-
|
|
184
|
+
### 6. OpenAI Responses API (Embeddings)
|
|
153
185
|
|
|
154
|
-
|
|
155
|
-
- Multiple image input methods
|
|
156
|
-
- Detailed image descriptions
|
|
157
|
-
- Usage tracking for vision API calls
|
|
186
|
+
**File:** `openai/responses-embed.ts`
|
|
158
187
|
|
|
159
|
-
|
|
188
|
+
Demonstrates embeddings with Responses API:
|
|
160
189
|
|
|
161
|
-
|
|
190
|
+
- Embeddings generation
|
|
191
|
+
- Metadata tracking
|
|
192
|
+
- Usage metrics
|
|
162
193
|
|
|
163
|
-
|
|
194
|
+
**Run:**
|
|
164
195
|
|
|
165
|
-
|
|
196
|
+
```bash
|
|
197
|
+
npm run example:openai-res-embed
|
|
198
|
+
# or
|
|
199
|
+
npx tsx examples/openai/responses-embed.ts
|
|
200
|
+
```
|
|
166
201
|
|
|
167
|
-
|
|
168
|
-
- **Stateful API** - Enhanced capabilities for agent-like applications
|
|
169
|
-
- **Unified experience** - Combines chat completions and assistants features
|
|
202
|
+
**What it does:**
|
|
170
203
|
|
|
171
|
-
|
|
204
|
+
- Generates text embeddings
|
|
205
|
+
- Tracks embedding usage
|
|
206
|
+
- Demonstrates metadata with embeddings
|
|
172
207
|
|
|
173
|
-
|
|
174
|
-
- Automatic tracking with new API
|
|
175
|
-
- Metadata support
|
|
176
|
-
- Backward compatibility notes
|
|
208
|
+
---
|
|
177
209
|
|
|
178
|
-
|
|
210
|
+
### 7. OpenAI Responses API (Streaming)
|
|
179
211
|
|
|
180
|
-
**
|
|
212
|
+
**File:** `openai/responses-streaming.ts`
|
|
181
213
|
|
|
182
|
-
|
|
214
|
+
Demonstrates streaming with Responses API:
|
|
183
215
|
|
|
184
|
-
|
|
216
|
+
- Real-time streaming responses
|
|
217
|
+
- Event handling
|
|
218
|
+
- Streaming metrics
|
|
185
219
|
|
|
186
|
-
|
|
187
|
-
- **Event handling** - Process response events as they arrive
|
|
188
|
-
- **Usage tracking** - Automatic tracking for streaming responses
|
|
220
|
+
**Run:**
|
|
189
221
|
|
|
190
|
-
|
|
222
|
+
```bash
|
|
223
|
+
npm run example:openai-res-stream
|
|
224
|
+
# or
|
|
225
|
+
npx tsx examples/openai/responses-streaming.ts
|
|
226
|
+
```
|
|
191
227
|
|
|
192
|
-
|
|
193
|
-
- Type-safe event processing
|
|
194
|
-
- Automatic usage metrics
|
|
195
|
-
- Stream completion tracking
|
|
228
|
+
**What it does:**
|
|
196
229
|
|
|
197
|
-
|
|
230
|
+
- Creates streaming responses
|
|
231
|
+
- Processes response events in real-time
|
|
232
|
+
- Tracks streaming usage metrics
|
|
198
233
|
|
|
199
|
-
|
|
234
|
+
---
|
|
200
235
|
|
|
201
|
-
### Azure OpenAI
|
|
236
|
+
### 8. Azure OpenAI (Basic)
|
|
202
237
|
|
|
203
|
-
|
|
238
|
+
**File:** `azure/basic.ts`
|
|
204
239
|
|
|
205
|
-
Demonstrates Azure OpenAI integration
|
|
240
|
+
Demonstrates Azure OpenAI integration:
|
|
206
241
|
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
242
|
+
- Automatic Azure detection
|
|
243
|
+
- Azure-specific configuration
|
|
244
|
+
- Chat completions with Azure
|
|
210
245
|
|
|
211
|
-
**
|
|
246
|
+
**Prerequisites:**
|
|
212
247
|
|
|
213
|
-
|
|
214
|
-
- Model name resolution for Azure
|
|
215
|
-
- Accurate Azure pricing
|
|
216
|
-
- Metadata tracking with Azure
|
|
248
|
+
To run this example with Azure OpenAI, you need to configure Azure credentials in `.env`:
|
|
217
249
|
|
|
218
|
-
|
|
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
|
+
```
|
|
219
255
|
|
|
220
|
-
**
|
|
256
|
+
**Run:**
|
|
221
257
|
|
|
222
|
-
|
|
258
|
+
```bash
|
|
259
|
+
npm run example:azure-basic
|
|
260
|
+
# or
|
|
261
|
+
npx tsx examples/azure/basic.ts
|
|
262
|
+
```
|
|
223
263
|
|
|
224
|
-
|
|
264
|
+
**What it does:**
|
|
265
|
+
|
|
266
|
+
- Automatically detects Azure configuration
|
|
267
|
+
- Uses Azure OpenAI for chat completions
|
|
268
|
+
- Sends metering data with provider='AZURE_OPENAI'
|
|
225
269
|
|
|
226
|
-
|
|
227
|
-
- **Deployment resolution** - Automatic Azure deployment name handling
|
|
228
|
-
- **Usage tracking** - Azure-specific metrics and pricing
|
|
270
|
+
**Important for Azure:**
|
|
229
271
|
|
|
230
|
-
**
|
|
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.
|
|
231
273
|
|
|
232
|
-
|
|
274
|
+
---
|
|
233
275
|
|
|
234
|
-
|
|
276
|
+
### 9. Azure OpenAI (Streaming)
|
|
277
|
+
|
|
278
|
+
**File:** `azure/stream.ts`
|
|
279
|
+
|
|
280
|
+
Demonstrates streaming with Azure OpenAI:
|
|
235
281
|
|
|
236
|
-
|
|
282
|
+
- Real-time streaming from Azure
|
|
283
|
+
- Azure deployment handling
|
|
284
|
+
- Streaming metrics
|
|
237
285
|
|
|
238
|
-
|
|
239
|
-
- **Unified interface** - Same Responses API patterns on Azure
|
|
240
|
-
- **Automatic tracking** - Azure-aware usage tracking
|
|
286
|
+
**Run:**
|
|
241
287
|
|
|
242
|
-
|
|
288
|
+
```bash
|
|
289
|
+
npm run example:azure-stream
|
|
290
|
+
# or
|
|
291
|
+
npx tsx examples/azure/stream.ts
|
|
292
|
+
```
|
|
243
293
|
|
|
244
|
-
**
|
|
294
|
+
**What it does:**
|
|
245
295
|
|
|
246
|
-
|
|
296
|
+
- Creates streaming chat completions with Azure
|
|
297
|
+
- Displays tokens in real-time
|
|
298
|
+
- Tracks Azure streaming metrics
|
|
247
299
|
|
|
248
|
-
|
|
300
|
+
---
|
|
249
301
|
|
|
250
|
-
|
|
251
|
-
- **Event handling** - Process Azure response events
|
|
252
|
-
- **Complete tracking** - Azure metrics with new API
|
|
302
|
+
### 10. Azure Responses API (Basic)
|
|
253
303
|
|
|
254
|
-
**
|
|
304
|
+
**File:** `azure/responses-basic.ts`
|
|
255
305
|
|
|
256
|
-
|
|
306
|
+
Demonstrates Responses API with Azure OpenAI:
|
|
257
307
|
|
|
258
|
-
|
|
308
|
+
- Azure + Responses API
|
|
309
|
+
- Simplified interface on Azure
|
|
310
|
+
- Automatic tracking
|
|
259
311
|
|
|
260
|
-
|
|
312
|
+
**Run:**
|
|
261
313
|
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
"module": "ESNext",
|
|
267
|
-
"moduleResolution": "node",
|
|
268
|
-
"esModuleInterop": true,
|
|
269
|
-
"allowSyntheticDefaultImports": true,
|
|
270
|
-
"strict": true,
|
|
271
|
-
"skipLibCheck": true
|
|
272
|
-
}
|
|
273
|
-
}
|
|
314
|
+
```bash
|
|
315
|
+
npm run example:azure-res-basic
|
|
316
|
+
# or
|
|
317
|
+
npx tsx examples/azure/responses-basic.ts
|
|
274
318
|
```
|
|
275
319
|
|
|
276
|
-
|
|
320
|
+
**What it does:**
|
|
321
|
+
|
|
322
|
+
- Uses Responses API with Azure OpenAI
|
|
323
|
+
- Creates responses with Azure deployment
|
|
324
|
+
- Tracks Azure usage metrics
|
|
277
325
|
|
|
278
|
-
|
|
279
|
-
- **TypeScript 4.5+** for module augmentation features
|
|
280
|
-
- **Valid Revenium API key** (starts with `hak_`)
|
|
281
|
-
- **Valid OpenAI API key** (starts with `sk-`) or Azure OpenAI credentials
|
|
282
|
-
- **OpenAI SDK 5.0+** (5.8+ for Responses API)
|
|
326
|
+
---
|
|
283
327
|
|
|
284
|
-
|
|
328
|
+
### 11. Azure Responses API (Streaming)
|
|
285
329
|
|
|
286
|
-
|
|
330
|
+
**File:** `azure/responses-stream.ts`
|
|
287
331
|
|
|
288
|
-
|
|
332
|
+
Demonstrates Responses API streaming with Azure:
|
|
289
333
|
|
|
290
|
-
|
|
334
|
+
- Real-time Responses API on Azure
|
|
335
|
+
- Event handling
|
|
336
|
+
- Azure streaming metrics
|
|
291
337
|
|
|
292
|
-
|
|
293
|
-
// ❌ Wrong - missing module augmentation import
|
|
294
|
-
import { initializeReveniumFromEnv } from "@revenium/openai";
|
|
338
|
+
**Run:**
|
|
295
339
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
340
|
+
```bash
|
|
341
|
+
npm run example:azure-res-stream
|
|
342
|
+
# or
|
|
343
|
+
npx tsx examples/azure/responses-stream.ts
|
|
299
344
|
```
|
|
300
345
|
|
|
301
|
-
|
|
346
|
+
**What it does:**
|
|
302
347
|
|
|
303
|
-
|
|
348
|
+
- Creates streaming responses with Azure
|
|
349
|
+
- Processes response events in real-time
|
|
350
|
+
- Tracks Azure streaming usage
|
|
304
351
|
|
|
305
|
-
|
|
352
|
+
---
|
|
306
353
|
|
|
307
|
-
|
|
308
|
-
- Check variable names match exactly
|
|
309
|
-
- Verify you're importing `dotenv/config` before the middleware
|
|
310
|
-
- Check API keys have correct prefixes (`hak_` for Revenium, `sk-` for OpenAI)
|
|
354
|
+
## Common Issues
|
|
311
355
|
|
|
312
|
-
###
|
|
356
|
+
### "Client not initialized" error
|
|
313
357
|
|
|
314
|
-
**
|
|
358
|
+
**Solution:** Make sure to call `Initialize()` before using `GetClient()`.
|
|
315
359
|
|
|
316
|
-
|
|
360
|
+
### "REVENIUM_METERING_API_KEY is required" error
|
|
317
361
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
"moduleResolution": "node",
|
|
322
|
-
"esModuleInterop": true,
|
|
323
|
-
"allowSyntheticDefaultImports": true,
|
|
324
|
-
"strict": true
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
```
|
|
362
|
+
**Solution:** Set the `REVENIUM_METERING_API_KEY` environment variable in your `.env` file.
|
|
363
|
+
|
|
364
|
+
### "invalid Revenium API key format" error
|
|
328
365
|
|
|
329
|
-
|
|
366
|
+
**Solution:** Revenium API keys should start with `hak_`. Check your API key format.
|
|
330
367
|
|
|
331
|
-
|
|
368
|
+
### Environment variables not loading
|
|
332
369
|
|
|
333
|
-
**
|
|
370
|
+
**Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
|
|
334
371
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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`
|
|
339
379
|
|
|
340
380
|
### Debug Mode
|
|
341
381
|
|
|
@@ -346,12 +386,18 @@ Enable detailed logging to troubleshoot issues:
|
|
|
346
386
|
REVENIUM_DEBUG=true
|
|
347
387
|
|
|
348
388
|
# Then run examples
|
|
349
|
-
|
|
389
|
+
npm run example:getting-started
|
|
350
390
|
```
|
|
351
391
|
|
|
352
|
-
##
|
|
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:
|
|
353
401
|
|
|
354
|
-
-
|
|
355
|
-
-
|
|
356
|
-
- **OpenAI Documentation**: [OpenAI API Reference](https://platform.openai.com/docs)
|
|
357
|
-
- **Issues**: [Report bugs](https://github.com/revenium/revenium-middleware-openai-node/issues)
|
|
402
|
+
- Documentation: https://docs.revenium.io
|
|
403
|
+
- Email: support@revenium.io
|