@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.
Files changed (228) hide show
  1. package/.env.example +10 -15
  2. package/CHANGELOG.md +44 -11
  3. package/CODE_OF_CONDUCT.md +57 -0
  4. package/CONTRIBUTING.md +38 -0
  5. package/README.md +104 -216
  6. package/SECURITY.md +34 -0
  7. package/dist/cjs/core/client/index.js +14 -0
  8. package/dist/cjs/core/client/index.js.map +1 -0
  9. package/dist/cjs/core/client/manager.js +109 -0
  10. package/dist/cjs/core/client/manager.js.map +1 -0
  11. package/dist/cjs/core/config/azure-config.js +5 -17
  12. package/dist/cjs/core/config/azure-config.js.map +1 -1
  13. package/dist/cjs/core/config/index.js +2 -2
  14. package/dist/cjs/core/config/index.js.map +1 -1
  15. package/dist/cjs/core/config/loader.js +34 -14
  16. package/dist/cjs/core/config/loader.js.map +1 -1
  17. package/dist/cjs/core/config/manager.js +11 -5
  18. package/dist/cjs/core/config/manager.js.map +1 -1
  19. package/dist/cjs/core/config/validator.js +3 -45
  20. package/dist/cjs/core/config/validator.js.map +1 -1
  21. package/dist/cjs/core/middleware/index.js +17 -0
  22. package/dist/cjs/core/middleware/index.js.map +1 -0
  23. package/dist/cjs/core/middleware/interfaces.js +361 -0
  24. package/dist/cjs/core/middleware/interfaces.js.map +1 -0
  25. package/dist/cjs/core/middleware/revenium-client.js +142 -0
  26. package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
  27. package/dist/cjs/core/providers/detector.js +45 -23
  28. package/dist/cjs/core/providers/detector.js.map +1 -1
  29. package/dist/cjs/core/providers/index.js +2 -1
  30. package/dist/cjs/core/providers/index.js.map +1 -1
  31. package/dist/cjs/core/tracking/api-client.js +14 -13
  32. package/dist/cjs/core/tracking/api-client.js.map +1 -1
  33. package/dist/cjs/core/tracking/payload-builder.js +15 -25
  34. package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
  35. package/dist/cjs/core/tracking/usage-tracker.js +22 -18
  36. package/dist/cjs/core/tracking/usage-tracker.js.map +1 -1
  37. package/dist/cjs/index.js +26 -195
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/types/index.js +0 -8
  40. package/dist/cjs/types/index.js.map +1 -1
  41. package/dist/cjs/types/openai-augmentation.js +0 -49
  42. package/dist/cjs/types/openai-augmentation.js.map +1 -1
  43. package/dist/cjs/utils/constants.js +17 -20
  44. package/dist/cjs/utils/constants.js.map +1 -1
  45. package/dist/cjs/utils/error-handler.js +18 -14
  46. package/dist/cjs/utils/error-handler.js.map +1 -1
  47. package/dist/cjs/utils/metadata-builder.js +17 -16
  48. package/dist/cjs/utils/metadata-builder.js.map +1 -1
  49. package/dist/cjs/utils/provider-detection.js +25 -28
  50. package/dist/cjs/utils/provider-detection.js.map +1 -1
  51. package/dist/esm/core/client/index.js +6 -0
  52. package/dist/esm/core/client/index.js.map +1 -0
  53. package/dist/esm/core/client/manager.js +102 -0
  54. package/dist/esm/core/client/manager.js.map +1 -0
  55. package/dist/esm/core/config/azure-config.js +6 -18
  56. package/dist/esm/core/config/azure-config.js.map +1 -1
  57. package/dist/esm/core/config/index.js +5 -4
  58. package/dist/esm/core/config/index.js.map +1 -1
  59. package/dist/esm/core/config/loader.js +33 -13
  60. package/dist/esm/core/config/loader.js.map +1 -1
  61. package/dist/esm/core/config/manager.js +13 -7
  62. package/dist/esm/core/config/manager.js.map +1 -1
  63. package/dist/esm/core/config/validator.js +3 -44
  64. package/dist/esm/core/config/validator.js.map +1 -1
  65. package/dist/esm/core/middleware/index.js +8 -0
  66. package/dist/esm/core/middleware/index.js.map +1 -0
  67. package/dist/esm/core/middleware/interfaces.js +353 -0
  68. package/dist/esm/core/middleware/interfaces.js.map +1 -0
  69. package/dist/esm/core/middleware/revenium-client.js +105 -0
  70. package/dist/esm/core/middleware/revenium-client.js.map +1 -0
  71. package/dist/esm/core/providers/detector.js +43 -22
  72. package/dist/esm/core/providers/detector.js.map +1 -1
  73. package/dist/esm/core/providers/index.js +2 -2
  74. package/dist/esm/core/providers/index.js.map +1 -1
  75. package/dist/esm/core/tracking/api-client.js +13 -12
  76. package/dist/esm/core/tracking/api-client.js.map +1 -1
  77. package/dist/esm/core/tracking/payload-builder.js +16 -26
  78. package/dist/esm/core/tracking/payload-builder.js.map +1 -1
  79. package/dist/esm/core/tracking/usage-tracker.js +24 -20
  80. package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
  81. package/dist/esm/index.js +9 -177
  82. package/dist/esm/index.js.map +1 -1
  83. package/dist/esm/types/index.js +2 -10
  84. package/dist/esm/types/index.js.map +1 -1
  85. package/dist/esm/types/openai-augmentation.js +0 -49
  86. package/dist/esm/types/openai-augmentation.js.map +1 -1
  87. package/dist/esm/utils/constants.js +16 -19
  88. package/dist/esm/utils/constants.js.map +1 -1
  89. package/dist/esm/utils/error-handler.js +19 -15
  90. package/dist/esm/utils/error-handler.js.map +1 -1
  91. package/dist/esm/utils/metadata-builder.js +17 -16
  92. package/dist/esm/utils/metadata-builder.js.map +1 -1
  93. package/dist/esm/utils/provider-detection.js +26 -29
  94. package/dist/esm/utils/provider-detection.js.map +1 -1
  95. package/dist/types/core/client/index.d.ts +6 -0
  96. package/dist/types/core/client/index.d.ts.map +1 -0
  97. package/dist/types/core/client/manager.d.ts +32 -0
  98. package/dist/types/core/client/manager.d.ts.map +1 -0
  99. package/dist/types/core/config/azure-config.d.ts +2 -2
  100. package/dist/types/core/config/azure-config.d.ts.map +1 -1
  101. package/dist/types/core/config/index.d.ts +4 -4
  102. package/dist/types/core/config/index.d.ts.map +1 -1
  103. package/dist/types/core/config/loader.d.ts +3 -1
  104. package/dist/types/core/config/loader.d.ts.map +1 -1
  105. package/dist/types/core/config/manager.d.ts +1 -1
  106. package/dist/types/core/config/manager.d.ts.map +1 -1
  107. package/dist/types/core/config/validator.d.ts +1 -12
  108. package/dist/types/core/config/validator.d.ts.map +1 -1
  109. package/dist/types/core/middleware/index.d.ts +8 -0
  110. package/dist/types/core/middleware/index.d.ts.map +1 -0
  111. package/dist/types/core/middleware/interfaces.d.ts +74 -0
  112. package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
  113. package/dist/types/core/middleware/revenium-client.d.ts +58 -0
  114. package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
  115. package/dist/types/core/providers/detector.d.ts +9 -2
  116. package/dist/types/core/providers/detector.d.ts.map +1 -1
  117. package/dist/types/core/providers/index.d.ts +2 -2
  118. package/dist/types/core/providers/index.d.ts.map +1 -1
  119. package/dist/types/core/tracking/api-client.d.ts +1 -1
  120. package/dist/types/core/tracking/api-client.d.ts.map +1 -1
  121. package/dist/types/core/tracking/payload-builder.d.ts +3 -3
  122. package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
  123. package/dist/types/core/tracking/usage-tracker.d.ts +2 -2
  124. package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -1
  125. package/dist/types/index.d.ts +11 -135
  126. package/dist/types/index.d.ts.map +1 -1
  127. package/dist/types/types/function-parameters.d.ts +2 -23
  128. package/dist/types/types/function-parameters.d.ts.map +1 -1
  129. package/dist/types/types/index.d.ts +11 -105
  130. package/dist/types/types/index.d.ts.map +1 -1
  131. package/dist/types/types/openai-augmentation.d.ts +4 -138
  132. package/dist/types/types/openai-augmentation.d.ts.map +1 -1
  133. package/dist/types/utils/constants.d.ts +7 -1
  134. package/dist/types/utils/constants.d.ts.map +1 -1
  135. package/dist/types/utils/error-handler.d.ts +2 -2
  136. package/dist/types/utils/error-handler.d.ts.map +1 -1
  137. package/dist/types/utils/metadata-builder.d.ts +2 -2
  138. package/dist/types/utils/metadata-builder.d.ts.map +1 -1
  139. package/dist/types/utils/provider-detection.d.ts +3 -3
  140. package/dist/types/utils/provider-detection.d.ts.map +1 -1
  141. package/examples/README.md +282 -198
  142. package/examples/azure/basic.ts +62 -0
  143. package/examples/azure/responses-basic.ts +45 -0
  144. package/examples/azure/responses-stream.ts +61 -0
  145. package/examples/azure/stream.ts +56 -0
  146. package/examples/getting_started.ts +31 -43
  147. package/examples/openai/basic.ts +45 -0
  148. package/examples/openai/metadata.ts +67 -0
  149. package/examples/openai/responses-basic.ts +44 -0
  150. package/examples/openai/responses-embed.ts +34 -0
  151. package/examples/openai/responses-streaming.ts +63 -0
  152. package/examples/openai/streaming.ts +59 -0
  153. package/package.json +20 -13
  154. package/dist/cjs/core/wrapper/index.js +0 -15
  155. package/dist/cjs/core/wrapper/index.js.map +0 -1
  156. package/dist/cjs/core/wrapper/instance-patcher.js +0 -202
  157. package/dist/cjs/core/wrapper/instance-patcher.js.map +0 -1
  158. package/dist/cjs/core/wrapper/request-handler.js +0 -317
  159. package/dist/cjs/core/wrapper/request-handler.js.map +0 -1
  160. package/dist/cjs/core/wrapper/stream-wrapper.js +0 -82
  161. package/dist/cjs/core/wrapper/stream-wrapper.js.map +0 -1
  162. package/dist/cjs/utils/azure-model-resolver.js +0 -211
  163. package/dist/cjs/utils/azure-model-resolver.js.map +0 -1
  164. package/dist/cjs/utils/request-handler-factory.js +0 -185
  165. package/dist/cjs/utils/request-handler-factory.js.map +0 -1
  166. package/dist/esm/core/wrapper/index.js +0 -9
  167. package/dist/esm/core/wrapper/index.js.map +0 -1
  168. package/dist/esm/core/wrapper/instance-patcher.js +0 -199
  169. package/dist/esm/core/wrapper/instance-patcher.js.map +0 -1
  170. package/dist/esm/core/wrapper/request-handler.js +0 -310
  171. package/dist/esm/core/wrapper/request-handler.js.map +0 -1
  172. package/dist/esm/core/wrapper/stream-wrapper.js +0 -79
  173. package/dist/esm/core/wrapper/stream-wrapper.js.map +0 -1
  174. package/dist/esm/utils/azure-model-resolver.js +0 -204
  175. package/dist/esm/utils/azure-model-resolver.js.map +0 -1
  176. package/dist/esm/utils/request-handler-factory.js +0 -146
  177. package/dist/esm/utils/request-handler-factory.js.map +0 -1
  178. package/dist/types/core/wrapper/index.d.ts +0 -8
  179. package/dist/types/core/wrapper/index.d.ts.map +0 -1
  180. package/dist/types/core/wrapper/instance-patcher.d.ts +0 -33
  181. package/dist/types/core/wrapper/instance-patcher.d.ts.map +0 -1
  182. package/dist/types/core/wrapper/request-handler.d.ts +0 -29
  183. package/dist/types/core/wrapper/request-handler.d.ts.map +0 -1
  184. package/dist/types/core/wrapper/stream-wrapper.d.ts +0 -13
  185. package/dist/types/core/wrapper/stream-wrapper.d.ts.map +0 -1
  186. package/dist/types/utils/azure-model-resolver.d.ts +0 -41
  187. package/dist/types/utils/azure-model-resolver.d.ts.map +0 -1
  188. package/dist/types/utils/request-handler-factory.d.ts +0 -81
  189. package/dist/types/utils/request-handler-factory.d.ts.map +0 -1
  190. package/examples/azure-basic.ts +0 -206
  191. package/examples/azure-responses-basic.ts +0 -233
  192. package/examples/azure-responses-streaming.ts +0 -255
  193. package/examples/azure-streaming.ts +0 -209
  194. package/examples/openai-basic.ts +0 -147
  195. package/examples/openai-function-calling.ts +0 -259
  196. package/examples/openai-responses-basic.ts +0 -212
  197. package/examples/openai-responses-streaming.ts +0 -232
  198. package/examples/openai-streaming.ts +0 -172
  199. package/examples/openai-vision.ts +0 -289
  200. package/src/core/config/azure-config.ts +0 -72
  201. package/src/core/config/index.ts +0 -23
  202. package/src/core/config/loader.ts +0 -66
  203. package/src/core/config/manager.ts +0 -95
  204. package/src/core/config/validator.ts +0 -89
  205. package/src/core/providers/detector.ts +0 -159
  206. package/src/core/providers/index.ts +0 -16
  207. package/src/core/tracking/api-client.ts +0 -78
  208. package/src/core/tracking/index.ts +0 -21
  209. package/src/core/tracking/payload-builder.ts +0 -137
  210. package/src/core/tracking/usage-tracker.ts +0 -189
  211. package/src/core/wrapper/index.ts +0 -9
  212. package/src/core/wrapper/instance-patcher.ts +0 -288
  213. package/src/core/wrapper/request-handler.ts +0 -423
  214. package/src/core/wrapper/stream-wrapper.ts +0 -100
  215. package/src/index.ts +0 -360
  216. package/src/types/function-parameters.ts +0 -251
  217. package/src/types/index.ts +0 -310
  218. package/src/types/openai-augmentation.ts +0 -232
  219. package/src/types/responses-api.ts +0 -308
  220. package/src/utils/azure-model-resolver.ts +0 -220
  221. package/src/utils/constants.ts +0 -21
  222. package/src/utils/error-handler.ts +0 -251
  223. package/src/utils/metadata-builder.ts +0 -228
  224. package/src/utils/provider-detection.ts +0 -257
  225. package/src/utils/request-handler-factory.ts +0 -285
  226. package/src/utils/stop-reason-mapper.ts +0 -78
  227. package/src/utils/type-guards.ts +0 -202
  228. package/src/utils/url-builder.ts +0 -68
@@ -1,303 +1,381 @@
1
1
  # Revenium OpenAI Middleware - Examples
2
2
 
3
- **TypeScript-first** examples demonstrating automatic Revenium usage tracking with the OpenAI SDK.
3
+ This directory contains examples demonstrating how to use the Revenium OpenAI middleware.
4
4
 
5
- ## Getting Started - Step by Step
5
+ ## Prerequisites
6
6
 
7
- ### 1. Create Your Project
7
+ Before running the examples, make sure you have:
8
8
 
9
- ```bash
10
- # Create project directory
11
- mkdir my-openai-project
12
- cd my-openai-project
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
- # Initialize Node.js project
15
- npm init -y
16
- ```
14
+ ## Setup
17
15
 
18
- ### 2. Install Dependencies
16
+ 1. **Clone the repository** (if you haven't already):
19
17
 
20
- ```bash
21
- npm install @revenium/openai openai dotenv
22
- npm install -D typescript tsx @types/node # For TypeScript
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
- ### 3. Environment Setup
23
+ 2. **Install dependencies**:
26
24
 
27
- Create a `.env` file in your project root:
25
+ ```bash
26
+ npm install
27
+ ```
28
28
 
29
- ```bash
30
- # Required
31
- REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
32
- OPENAI_API_KEY=sk_your_openai_api_key
33
-
34
- # Optional
35
- REVENIUM_METERING_BASE_URL=https://api.revenium.ai
36
- REVENIUM_DEBUG=false
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
- ### 4. Run Examples
39
+ ## Examples
46
40
 
47
- **If you cloned from GitHub:**
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
- # Run examples directly
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
- **If you installed via npm:**
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
- Examples are included in your `node_modules/@revenium/openai/examples/` directory:
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
- 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
81
+ npm run example:openai-basic
82
+ # or
83
+ npx tsx examples/openai/basic.ts
64
84
  ```
65
85
 
66
- ## Available Examples
86
+ **What it does:**
67
87
 
68
- ### `getting_started.ts` - Simple Entry Point
88
+ - Creates chat completions with metadata tracking
89
+ - Generates text embeddings
90
+ - Demonstrates metadata usage for tracking
69
91
 
70
- The simplest example to get you started with Revenium tracking:
92
+ ---
71
93
 
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
94
+ ### 3. OpenAI Metadata
75
95
 
76
- **Key Features:**
96
+ **File:** `openai/metadata.ts`
77
97
 
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
98
+ Demonstrates all available metadata fields:
82
99
 
83
- **Perfect for:** First-time users, quick validation, understanding metadata structure
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
- **See the file for complete code examples.**
112
+ **What it does:**
86
113
 
87
- ### `openai-basic.ts` - Chat Completions and Embeddings
114
+ - Shows all available metadata fields
115
+ - Demonstrates subscriber tracking
116
+ - Includes organization and product tracking
88
117
 
89
- Demonstrates standard OpenAI API usage with automatic tracking:
118
+ **Metadata fields supported:**
90
119
 
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
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
- **Key Features:**
129
+ ---
96
130
 
97
- - TypeScript module augmentation for native `usageMetadata` support
98
- - Full type safety with IntelliSense
99
- - Comprehensive metadata examples
100
- - Error handling patterns
131
+ ### 4. OpenAI Streaming
101
132
 
102
- **Perfect for:** Understanding basic OpenAI API patterns with tracking
133
+ **File:** `openai/streaming.ts`
103
134
 
104
- **See the file for complete code examples.**
135
+ Demonstrates streaming responses:
105
136
 
106
- ### `openai-streaming.ts` - Real-time Streaming
137
+ - Real-time token streaming
138
+ - Accumulating responses
139
+ - Streaming metrics
107
140
 
108
- Demonstrates streaming responses with automatic token tracking:
141
+ **Run:**
109
142
 
110
- - **Streaming chat completions** - Real-time token streaming with metadata
111
- - **Batch embeddings** - Multiple embedding requests efficiently
112
- - **Stream processing** - Type-safe event handling
143
+ ```bash
144
+ npm run example:openai-stream
145
+ # or
146
+ npx tsx examples/openai/streaming.ts
147
+ ```
113
148
 
114
- **Key Features:**
149
+ **What it does:**
115
150
 
116
- - Automatic tracking when stream completes
117
- - Real-time token counting
118
- - Time-to-first-token metrics
119
- - Stream error handling
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
- **Perfect for:** Real-time applications, chatbots, interactive AI assistants
156
+ ---
122
157
 
123
- **See the file for complete code examples.**
158
+ ### 5. OpenAI Responses API (Basic)
124
159
 
125
- ### `openai-responses-basic.ts` - Responses API
160
+ **File:** `openai/responses-basic.ts`
126
161
 
127
162
  Demonstrates OpenAI's Responses API:
128
163
 
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
164
+ - Simplified interface with `input` parameter
165
+ - Stateful API features
166
+ - Automatic tracking
132
167
 
133
- **Key Features:**
168
+ **Run:**
134
169
 
135
- - New Responses API patterns
136
- - Automatic tracking with new API
137
- - Metadata support
138
- - Backward compatibility notes
170
+ ```bash
171
+ npm run example:openai-res-basic
172
+ # or
173
+ npx tsx examples/openai/responses-basic.ts
174
+ ```
139
175
 
140
- **Perfect for:** Applications using OpenAI's latest API features
176
+ **What it does:**
141
177
 
142
- **See the file for complete code examples.**
178
+ - Uses the new Responses API
179
+ - Creates responses with metadata
180
+ - Demonstrates simplified interface
143
181
 
144
- ### `openai-responses-streaming.ts` - Responses API Streaming
182
+ ---
145
183
 
146
- Demonstrates streaming with the new Responses API:
184
+ ### 6. OpenAI Responses API (Embeddings)
147
185
 
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
186
+ **File:** `openai/responses-embed.ts`
151
187
 
152
- **Key Features:**
188
+ Demonstrates embeddings with Responses API:
153
189
 
154
- - Responses API streaming patterns
155
- - Type-safe event processing
156
- - Automatic usage metrics
157
- - Stream completion tracking
190
+ - Embeddings generation
191
+ - Metadata tracking
192
+ - Usage metrics
158
193
 
159
- **Perfect for:** Real-time applications using the new Responses API
194
+ **Run:**
160
195
 
161
- **See the file for complete code examples.**
196
+ ```bash
197
+ npm run example:openai-res-embed
198
+ # or
199
+ npx tsx examples/openai/responses-embed.ts
200
+ ```
162
201
 
163
- ### Azure OpenAI Examples
202
+ **What it does:**
164
203
 
165
- #### `azure-basic.ts` - Azure Chat Completions
204
+ - Generates text embeddings
205
+ - Tracks embedding usage
206
+ - Demonstrates metadata with embeddings
166
207
 
167
- Demonstrates Azure OpenAI integration with automatic detection:
208
+ ---
168
209
 
169
- - **Azure configuration** - Environment-based Azure setup
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
- **Key Features:**
212
+ **File:** `openai/responses-streaming.ts`
174
213
 
175
- - Azure OpenAI deployment configuration
176
- - Model name resolution for Azure
177
- - Accurate Azure pricing
178
- - Metadata tracking with Azure
214
+ Demonstrates streaming with Responses API:
179
215
 
180
- **Perfect for:** Enterprise applications using Azure OpenAI
216
+ - Real-time streaming responses
217
+ - Event handling
218
+ - Streaming metrics
181
219
 
182
- **See the file for complete code examples.**
220
+ **Run:**
183
221
 
184
- #### `azure-streaming.ts` - Azure Streaming
222
+ ```bash
223
+ npm run example:openai-res-stream
224
+ # or
225
+ npx tsx examples/openai/responses-streaming.ts
226
+ ```
185
227
 
186
- Demonstrates streaming with Azure OpenAI:
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
- - **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
238
+ **File:** `azure/basic.ts`
191
239
 
192
- **Perfect for:** Real-time Azure OpenAI applications
240
+ Demonstrates Azure OpenAI integration:
193
241
 
194
- **See the file for complete code examples.**
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
- #### `azure-responses-basic.ts` - Azure Responses API
256
+ **Run:**
197
257
 
198
- Demonstrates new Responses API with Azure OpenAI:
258
+ ```bash
259
+ npm run example:azure-basic
260
+ # or
261
+ npx tsx examples/azure/basic.ts
262
+ ```
199
263
 
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
264
+ **What it does:**
203
265
 
204
- **Perfect for:** Azure applications using latest OpenAI features
266
+ - Automatically detects Azure configuration
267
+ - Uses Azure OpenAI for chat completions
268
+ - Sends metering data with provider='AZURE_OPENAI'
205
269
 
206
- **See the file for complete code examples.**
270
+ **Important for Azure:**
207
271
 
208
- #### `azure-responses-streaming.ts` - Azure Responses Streaming
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
- Demonstrates Responses API streaming with Azure OpenAI:
274
+ ---
211
275
 
212
- - **Azure streaming** - Real-time Responses API on Azure
213
- - **Event handling** - Process Azure response events
214
- - **Complete tracking** - Azure metrics with new API
276
+ ### 9. Azure OpenAI (Streaming)
215
277
 
216
- **Perfect for:** Real-time Azure applications with Responses API
278
+ **File:** `azure/stream.ts`
217
279
 
218
- **See the file for complete code examples.**
280
+ Demonstrates streaming with Azure OpenAI:
219
281
 
220
- ## TypeScript Configuration
282
+ - Real-time streaming from Azure
283
+ - Azure deployment handling
284
+ - Streaming metrics
221
285
 
222
- Ensure your `tsconfig.json` includes:
286
+ **Run:**
223
287
 
224
- ```json
225
- {
226
- "compilerOptions": {
227
- "target": "ES2020",
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
- ## Requirements
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
- - **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)
300
+ ---
245
301
 
246
- ## Troubleshooting
302
+ ### 10. Azure Responses API (Basic)
247
303
 
248
- ### Module Augmentation Not Working
304
+ **File:** `azure/responses-basic.ts`
249
305
 
250
- **Problem:** TypeScript doesn't recognize `usageMetadata` in OpenAI SDK calls
306
+ Demonstrates Responses API with Azure OpenAI:
251
307
 
252
- **Solution:**
308
+ - Azure + Responses API
309
+ - Simplified interface on Azure
310
+ - Automatic tracking
253
311
 
254
- ```typescript
255
- // ❌ Wrong - missing module augmentation import
256
- import { initializeReveniumFromEnv } from "@revenium/openai";
312
+ **Run:**
257
313
 
258
- // ✅ Correct - import for module augmentation
259
- import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
260
- import OpenAI from "openai";
314
+ ```bash
315
+ npm run example:azure-res-basic
316
+ # or
317
+ npx tsx examples/azure/responses-basic.ts
261
318
  ```
262
319
 
263
- ### Environment Variables Not Loading
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
- **Problem:** `REVENIUM_METERING_API_KEY` or `OPENAI_API_KEY` not found
326
+ ---
266
327
 
267
- **Solutions:**
328
+ ### 11. Azure Responses API (Streaming)
268
329
 
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)
330
+ **File:** `azure/responses-stream.ts`
273
331
 
274
- ### TypeScript Compilation Errors
332
+ Demonstrates Responses API streaming with Azure:
275
333
 
276
- **Problem:** Module resolution or import errors
334
+ - Real-time Responses API on Azure
335
+ - Event handling
336
+ - Azure streaming metrics
277
337
 
278
- **Solution:** Verify your `tsconfig.json` settings:
338
+ **Run:**
279
339
 
280
- ```json
281
- {
282
- "compilerOptions": {
283
- "moduleResolution": "node",
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
- ### Azure Configuration Issues
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
- **Problem:** Azure OpenAI not working or incorrect pricing
364
+ ### "invalid Revenium API key format" error
294
365
 
295
- **Solutions:**
366
+ **Solution:** Revenium API keys should start with `hak_`. Check your API key format.
296
367
 
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/`
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
- npx tsx examples/getting_started.ts
389
+ npm run example:getting-started
312
390
  ```
313
391
 
314
- ## Additional Resources
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
- - **Main Documentation**: See root [README.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/README.md)
317
- - **API Reference**: [Revenium Metadata Fields](https://revenium.readme.io/reference/meter_ai_completion)
318
- - **OpenAI Documentation**: [OpenAI API Reference](https://platform.openai.com/docs)
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);