@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.
Files changed (236) hide show
  1. package/.env.example +10 -15
  2. package/CHANGELOG.md +59 -0
  3. package/CODE_OF_CONDUCT.md +57 -0
  4. package/CONTRIBUTING.md +38 -0
  5. package/README.md +109 -515
  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 +12 -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 +30 -35
  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 -174
  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 +27 -19
  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/cjs/utils/stop-reason-mapper.js +4 -0
  52. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
  53. package/dist/cjs/utils/url-builder.js +3 -3
  54. package/dist/esm/core/client/index.js +6 -0
  55. package/dist/esm/core/client/index.js.map +1 -0
  56. package/dist/esm/core/client/manager.js +102 -0
  57. package/dist/esm/core/client/manager.js.map +1 -0
  58. package/dist/esm/core/config/azure-config.js +6 -18
  59. package/dist/esm/core/config/azure-config.js.map +1 -1
  60. package/dist/esm/core/config/index.js +5 -4
  61. package/dist/esm/core/config/index.js.map +1 -1
  62. package/dist/esm/core/config/loader.js +33 -13
  63. package/dist/esm/core/config/loader.js.map +1 -1
  64. package/dist/esm/core/config/manager.js +14 -7
  65. package/dist/esm/core/config/manager.js.map +1 -1
  66. package/dist/esm/core/config/validator.js +3 -44
  67. package/dist/esm/core/config/validator.js.map +1 -1
  68. package/dist/esm/core/middleware/index.js +8 -0
  69. package/dist/esm/core/middleware/index.js.map +1 -0
  70. package/dist/esm/core/middleware/interfaces.js +353 -0
  71. package/dist/esm/core/middleware/interfaces.js.map +1 -0
  72. package/dist/esm/core/middleware/revenium-client.js +105 -0
  73. package/dist/esm/core/middleware/revenium-client.js.map +1 -0
  74. package/dist/esm/core/providers/detector.js +43 -22
  75. package/dist/esm/core/providers/detector.js.map +1 -1
  76. package/dist/esm/core/providers/index.js +2 -2
  77. package/dist/esm/core/providers/index.js.map +1 -1
  78. package/dist/esm/core/tracking/api-client.js +13 -12
  79. package/dist/esm/core/tracking/api-client.js.map +1 -1
  80. package/dist/esm/core/tracking/payload-builder.js +31 -36
  81. package/dist/esm/core/tracking/payload-builder.js.map +1 -1
  82. package/dist/esm/core/tracking/usage-tracker.js +24 -20
  83. package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
  84. package/dist/esm/index.js +9 -157
  85. package/dist/esm/index.js.map +1 -1
  86. package/dist/esm/types/index.js +2 -10
  87. package/dist/esm/types/index.js.map +1 -1
  88. package/dist/esm/types/openai-augmentation.js +0 -49
  89. package/dist/esm/types/openai-augmentation.js.map +1 -1
  90. package/dist/esm/utils/constants.js +16 -19
  91. package/dist/esm/utils/constants.js.map +1 -1
  92. package/dist/esm/utils/error-handler.js +19 -15
  93. package/dist/esm/utils/error-handler.js.map +1 -1
  94. package/dist/esm/utils/metadata-builder.js +27 -19
  95. package/dist/esm/utils/metadata-builder.js.map +1 -1
  96. package/dist/esm/utils/provider-detection.js +26 -29
  97. package/dist/esm/utils/provider-detection.js.map +1 -1
  98. package/dist/esm/utils/stop-reason-mapper.js +4 -0
  99. package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
  100. package/dist/esm/utils/url-builder.js +3 -3
  101. package/dist/types/core/client/index.d.ts +6 -0
  102. package/dist/types/core/client/index.d.ts.map +1 -0
  103. package/dist/types/core/client/manager.d.ts +32 -0
  104. package/dist/types/core/client/manager.d.ts.map +1 -0
  105. package/dist/types/core/config/azure-config.d.ts +2 -2
  106. package/dist/types/core/config/azure-config.d.ts.map +1 -1
  107. package/dist/types/core/config/index.d.ts +4 -4
  108. package/dist/types/core/config/index.d.ts.map +1 -1
  109. package/dist/types/core/config/loader.d.ts +3 -1
  110. package/dist/types/core/config/loader.d.ts.map +1 -1
  111. package/dist/types/core/config/manager.d.ts +1 -1
  112. package/dist/types/core/config/manager.d.ts.map +1 -1
  113. package/dist/types/core/config/validator.d.ts +1 -12
  114. package/dist/types/core/config/validator.d.ts.map +1 -1
  115. package/dist/types/core/middleware/index.d.ts +8 -0
  116. package/dist/types/core/middleware/index.d.ts.map +1 -0
  117. package/dist/types/core/middleware/interfaces.d.ts +74 -0
  118. package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
  119. package/dist/types/core/middleware/revenium-client.d.ts +58 -0
  120. package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
  121. package/dist/types/core/providers/detector.d.ts +9 -2
  122. package/dist/types/core/providers/detector.d.ts.map +1 -1
  123. package/dist/types/core/providers/index.d.ts +2 -2
  124. package/dist/types/core/providers/index.d.ts.map +1 -1
  125. package/dist/types/core/tracking/api-client.d.ts +1 -1
  126. package/dist/types/core/tracking/api-client.d.ts.map +1 -1
  127. package/dist/types/core/tracking/payload-builder.d.ts +3 -3
  128. package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
  129. package/dist/types/core/tracking/usage-tracker.d.ts +2 -2
  130. package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -1
  131. package/dist/types/index.d.ts +11 -114
  132. package/dist/types/index.d.ts.map +1 -1
  133. package/dist/types/types/function-parameters.d.ts +2 -23
  134. package/dist/types/types/function-parameters.d.ts.map +1 -1
  135. package/dist/types/types/index.d.ts +17 -115
  136. package/dist/types/types/index.d.ts.map +1 -1
  137. package/dist/types/types/openai-augmentation.d.ts +4 -139
  138. package/dist/types/types/openai-augmentation.d.ts.map +1 -1
  139. package/dist/types/utils/constants.d.ts +7 -1
  140. package/dist/types/utils/constants.d.ts.map +1 -1
  141. package/dist/types/utils/error-handler.d.ts +2 -2
  142. package/dist/types/utils/error-handler.d.ts.map +1 -1
  143. package/dist/types/utils/metadata-builder.d.ts +4 -3
  144. package/dist/types/utils/metadata-builder.d.ts.map +1 -1
  145. package/dist/types/utils/provider-detection.d.ts +3 -3
  146. package/dist/types/utils/provider-detection.d.ts.map +1 -1
  147. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
  148. package/dist/types/utils/url-builder.d.ts +3 -3
  149. package/examples/README.md +270 -224
  150. package/examples/azure/basic.ts +62 -0
  151. package/examples/azure/responses-basic.ts +45 -0
  152. package/examples/azure/responses-stream.ts +61 -0
  153. package/examples/azure/stream.ts +56 -0
  154. package/examples/getting_started.ts +31 -43
  155. package/examples/openai/basic.ts +45 -0
  156. package/examples/openai/metadata.ts +67 -0
  157. package/examples/openai/responses-basic.ts +44 -0
  158. package/examples/openai/responses-embed.ts +34 -0
  159. package/examples/openai/responses-streaming.ts +63 -0
  160. package/examples/openai/streaming.ts +59 -0
  161. package/package.json +20 -13
  162. package/dist/cjs/core/wrapper/index.js +0 -15
  163. package/dist/cjs/core/wrapper/index.js.map +0 -1
  164. package/dist/cjs/core/wrapper/instance-patcher.js +0 -202
  165. package/dist/cjs/core/wrapper/instance-patcher.js.map +0 -1
  166. package/dist/cjs/core/wrapper/request-handler.js +0 -317
  167. package/dist/cjs/core/wrapper/request-handler.js.map +0 -1
  168. package/dist/cjs/core/wrapper/stream-wrapper.js +0 -82
  169. package/dist/cjs/core/wrapper/stream-wrapper.js.map +0 -1
  170. package/dist/cjs/utils/azure-model-resolver.js +0 -211
  171. package/dist/cjs/utils/azure-model-resolver.js.map +0 -1
  172. package/dist/cjs/utils/request-handler-factory.js +0 -185
  173. package/dist/cjs/utils/request-handler-factory.js.map +0 -1
  174. package/dist/esm/core/wrapper/index.js +0 -9
  175. package/dist/esm/core/wrapper/index.js.map +0 -1
  176. package/dist/esm/core/wrapper/instance-patcher.js +0 -199
  177. package/dist/esm/core/wrapper/instance-patcher.js.map +0 -1
  178. package/dist/esm/core/wrapper/request-handler.js +0 -310
  179. package/dist/esm/core/wrapper/request-handler.js.map +0 -1
  180. package/dist/esm/core/wrapper/stream-wrapper.js +0 -79
  181. package/dist/esm/core/wrapper/stream-wrapper.js.map +0 -1
  182. package/dist/esm/utils/azure-model-resolver.js +0 -204
  183. package/dist/esm/utils/azure-model-resolver.js.map +0 -1
  184. package/dist/esm/utils/request-handler-factory.js +0 -146
  185. package/dist/esm/utils/request-handler-factory.js.map +0 -1
  186. package/dist/types/core/wrapper/index.d.ts +0 -8
  187. package/dist/types/core/wrapper/index.d.ts.map +0 -1
  188. package/dist/types/core/wrapper/instance-patcher.d.ts +0 -33
  189. package/dist/types/core/wrapper/instance-patcher.d.ts.map +0 -1
  190. package/dist/types/core/wrapper/request-handler.d.ts +0 -29
  191. package/dist/types/core/wrapper/request-handler.d.ts.map +0 -1
  192. package/dist/types/core/wrapper/stream-wrapper.d.ts +0 -13
  193. package/dist/types/core/wrapper/stream-wrapper.d.ts.map +0 -1
  194. package/dist/types/utils/azure-model-resolver.d.ts +0 -41
  195. package/dist/types/utils/azure-model-resolver.d.ts.map +0 -1
  196. package/dist/types/utils/request-handler-factory.d.ts +0 -81
  197. package/dist/types/utils/request-handler-factory.d.ts.map +0 -1
  198. package/examples/azure-basic.ts +0 -206
  199. package/examples/azure-responses-basic.ts +0 -233
  200. package/examples/azure-responses-streaming.ts +0 -255
  201. package/examples/azure-streaming.ts +0 -209
  202. package/examples/openai-basic.ts +0 -147
  203. package/examples/openai-function-calling.ts +0 -259
  204. package/examples/openai-responses-basic.ts +0 -212
  205. package/examples/openai-responses-streaming.ts +0 -232
  206. package/examples/openai-streaming.ts +0 -172
  207. package/examples/openai-vision.ts +0 -289
  208. package/src/core/config/azure-config.ts +0 -72
  209. package/src/core/config/index.ts +0 -23
  210. package/src/core/config/loader.ts +0 -66
  211. package/src/core/config/manager.ts +0 -94
  212. package/src/core/config/validator.ts +0 -89
  213. package/src/core/providers/detector.ts +0 -159
  214. package/src/core/providers/index.ts +0 -16
  215. package/src/core/tracking/api-client.ts +0 -78
  216. package/src/core/tracking/index.ts +0 -21
  217. package/src/core/tracking/payload-builder.ts +0 -132
  218. package/src/core/tracking/usage-tracker.ts +0 -189
  219. package/src/core/wrapper/index.ts +0 -9
  220. package/src/core/wrapper/instance-patcher.ts +0 -288
  221. package/src/core/wrapper/request-handler.ts +0 -423
  222. package/src/core/wrapper/stream-wrapper.ts +0 -100
  223. package/src/index.ts +0 -336
  224. package/src/types/function-parameters.ts +0 -251
  225. package/src/types/index.ts +0 -313
  226. package/src/types/openai-augmentation.ts +0 -233
  227. package/src/types/responses-api.ts +0 -308
  228. package/src/utils/azure-model-resolver.ts +0 -220
  229. package/src/utils/constants.ts +0 -21
  230. package/src/utils/error-handler.ts +0 -251
  231. package/src/utils/metadata-builder.ts +0 -219
  232. package/src/utils/provider-detection.ts +0 -257
  233. package/src/utils/request-handler-factory.ts +0 -285
  234. package/src/utils/stop-reason-mapper.ts +0 -74
  235. package/src/utils/type-guards.ts +0 -202
  236. package/src/utils/url-builder.ts +0 -68
@@ -1,341 +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.io
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.
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
- ### 4. Run Examples
47
+ - Initialize the middleware
48
+ - Create a basic chat completion
49
+ - Display response and usage metrics
46
50
 
47
- **If you cloned from GitHub:**
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:**
57
60
 
58
- Examples are included in your `node_modules/@revenium/openai/examples/` directory:
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
- 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
84
103
 
85
- **See the file for complete code examples.**
104
+ **Run:**
86
105
 
87
- ### `openai-basic.ts` - Chat Completions and Embeddings
106
+ ```bash
107
+ npm run example:openai-metadata
108
+ # or
109
+ npx tsx examples/openai/metadata.ts
110
+ ```
88
111
 
89
- Demonstrates standard OpenAI API usage with automatic tracking:
112
+ **What it does:**
90
113
 
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
114
+ - Shows all available metadata fields
115
+ - Demonstrates subscriber tracking
116
+ - Includes organization and product tracking
94
117
 
95
- **Key Features:**
118
+ **Metadata fields supported:**
96
119
 
97
- - TypeScript module augmentation for native `usageMetadata` support
98
- - Full type safety with IntelliSense
99
- - Comprehensive metadata examples
100
- - Error handling patterns
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
- **Perfect for:** Understanding basic OpenAI API patterns with tracking
129
+ ---
103
130
 
104
- **See the file for complete code examples.**
131
+ ### 4. OpenAI Streaming
105
132
 
106
- ### `openai-streaming.ts` - Real-time Streaming
133
+ **File:** `openai/streaming.ts`
107
134
 
108
- Demonstrates streaming responses with automatic token tracking:
135
+ Demonstrates streaming responses:
109
136
 
110
- - **Streaming chat completions** - Real-time token streaming with metadata
111
- - **Batch embeddings** - Multiple embedding requests efficiently
112
- - **Stream processing** - Type-safe event handling
137
+ - Real-time token streaming
138
+ - Accumulating responses
139
+ - Streaming metrics
113
140
 
114
- **Key Features:**
141
+ **Run:**
115
142
 
116
- - Automatic tracking when stream completes
117
- - Real-time token counting
118
- - Time-to-first-token metrics
119
- - Stream error handling
143
+ ```bash
144
+ npm run example:openai-stream
145
+ # or
146
+ npx tsx examples/openai/streaming.ts
147
+ ```
120
148
 
121
- **Perfect for:** Real-time applications, chatbots, interactive AI assistants
149
+ **What it does:**
122
150
 
123
- **See the file for complete code examples.**
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
- ### `openai-function-calling.ts` - Function Calling with Tools
156
+ ---
126
157
 
127
- Demonstrates OpenAI function calling (tools) with tracking:
158
+ ### 5. OpenAI Responses API (Basic)
128
159
 
129
- - **Tool definitions** - Define functions for the AI to call
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
- **Key Features:**
162
+ Demonstrates OpenAI's Responses API:
134
163
 
135
- - Complete function calling workflow
136
- - Type-safe tool definitions
137
- - Automatic tracking of tool usage
138
- - Multi-turn conversation handling
164
+ - Simplified interface with `input` parameter
165
+ - Stateful API features
166
+ - Automatic tracking
139
167
 
140
- **Perfect for:** AI agents, function-calling applications, structured outputs
168
+ **Run:**
141
169
 
142
- **See the file for complete code examples.**
170
+ ```bash
171
+ npm run example:openai-res-basic
172
+ # or
173
+ npx tsx examples/openai/responses-basic.ts
174
+ ```
143
175
 
144
- ### `openai-vision.ts` - Vision API with Images
176
+ **What it does:**
145
177
 
146
- Demonstrates OpenAI Vision API with image analysis:
178
+ - Uses the new Responses API
179
+ - Creates responses with metadata
180
+ - Demonstrates simplified interface
147
181
 
148
- - **Image URL analysis** - Analyze images from URLs
149
- - **Base64 images** - Process local images as base64
150
- - **Multi-image analysis** - Compare multiple images in one request
182
+ ---
151
183
 
152
- **Key Features:**
184
+ ### 6. OpenAI Responses API (Embeddings)
153
185
 
154
- - Image analysis with GPT-4 Vision
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
- **Perfect for:** Image analysis applications, visual search, accessibility tools
188
+ Demonstrates embeddings with Responses API:
160
189
 
161
- **See the file for complete code examples.**
190
+ - Embeddings generation
191
+ - Metadata tracking
192
+ - Usage metrics
162
193
 
163
- ### `openai-responses-basic.ts` - New Responses API
194
+ **Run:**
164
195
 
165
- Demonstrates OpenAI's new Responses API (SDK 5.8+):
196
+ ```bash
197
+ npm run example:openai-res-embed
198
+ # or
199
+ npx tsx examples/openai/responses-embed.ts
200
+ ```
166
201
 
167
- - **Simplified interface** - Uses `input` instead of `messages` parameter
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
- **Key Features:**
204
+ - Generates text embeddings
205
+ - Tracks embedding usage
206
+ - Demonstrates metadata with embeddings
172
207
 
173
- - New Responses API patterns
174
- - Automatic tracking with new API
175
- - Metadata support
176
- - Backward compatibility notes
208
+ ---
177
209
 
178
- **Perfect for:** Applications using OpenAI's latest API features
210
+ ### 7. OpenAI Responses API (Streaming)
179
211
 
180
- **See the file for complete code examples.**
212
+ **File:** `openai/responses-streaming.ts`
181
213
 
182
- ### `openai-responses-streaming.ts` - Responses API Streaming
214
+ Demonstrates streaming with Responses API:
183
215
 
184
- Demonstrates streaming with the new Responses API:
216
+ - Real-time streaming responses
217
+ - Event handling
218
+ - Streaming metrics
185
219
 
186
- - **Streaming responses** - Real-time responses with new API
187
- - **Event handling** - Process response events as they arrive
188
- - **Usage tracking** - Automatic tracking for streaming responses
220
+ **Run:**
189
221
 
190
- **Key Features:**
222
+ ```bash
223
+ npm run example:openai-res-stream
224
+ # or
225
+ npx tsx examples/openai/responses-streaming.ts
226
+ ```
191
227
 
192
- - Responses API streaming patterns
193
- - Type-safe event processing
194
- - Automatic usage metrics
195
- - Stream completion tracking
228
+ **What it does:**
196
229
 
197
- **Perfect for:** Real-time applications using the new Responses API
230
+ - Creates streaming responses
231
+ - Processes response events in real-time
232
+ - Tracks streaming usage metrics
198
233
 
199
- **See the file for complete code examples.**
234
+ ---
200
235
 
201
- ### Azure OpenAI Examples
236
+ ### 8. Azure OpenAI (Basic)
202
237
 
203
- #### `azure-basic.ts` - Azure Chat Completions
238
+ **File:** `azure/basic.ts`
204
239
 
205
- Demonstrates Azure OpenAI integration with automatic detection:
240
+ Demonstrates Azure OpenAI integration:
206
241
 
207
- - **Azure configuration** - Environment-based Azure setup
208
- - **Chat completions** - Azure-hosted models with tracking
209
- - **Automatic detection** - Middleware detects Azure vs OpenAI automatically
242
+ - Automatic Azure detection
243
+ - Azure-specific configuration
244
+ - Chat completions with Azure
210
245
 
211
- **Key Features:**
246
+ **Prerequisites:**
212
247
 
213
- - Azure OpenAI deployment configuration
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
- **Perfect for:** Enterprise applications using Azure OpenAI
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
- **See the file for complete code examples.**
256
+ **Run:**
221
257
 
222
- #### `azure-streaming.ts` - Azure Streaming
258
+ ```bash
259
+ npm run example:azure-basic
260
+ # or
261
+ npx tsx examples/azure/basic.ts
262
+ ```
223
263
 
224
- Demonstrates streaming with Azure OpenAI:
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
- - **Azure streaming** - Real-time responses from Azure-hosted models
227
- - **Deployment resolution** - Automatic Azure deployment name handling
228
- - **Usage tracking** - Azure-specific metrics and pricing
270
+ **Important for Azure:**
229
271
 
230
- **Perfect for:** Real-time Azure OpenAI applications
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
- **See the file for complete code examples.**
274
+ ---
233
275
 
234
- #### `azure-responses-basic.ts` - Azure Responses API
276
+ ### 9. Azure OpenAI (Streaming)
277
+
278
+ **File:** `azure/stream.ts`
279
+
280
+ Demonstrates streaming with Azure OpenAI:
235
281
 
236
- Demonstrates new Responses API with Azure OpenAI:
282
+ - Real-time streaming from Azure
283
+ - Azure deployment handling
284
+ - Streaming metrics
237
285
 
238
- - **Azure + Responses API** - Combine Azure hosting with new API
239
- - **Unified interface** - Same Responses API patterns on Azure
240
- - **Automatic tracking** - Azure-aware usage tracking
286
+ **Run:**
241
287
 
242
- **Perfect for:** Azure applications using latest OpenAI features
288
+ ```bash
289
+ npm run example:azure-stream
290
+ # or
291
+ npx tsx examples/azure/stream.ts
292
+ ```
243
293
 
244
- **See the file for complete code examples.**
294
+ **What it does:**
245
295
 
246
- #### `azure-responses-streaming.ts` - Azure Responses Streaming
296
+ - Creates streaming chat completions with Azure
297
+ - Displays tokens in real-time
298
+ - Tracks Azure streaming metrics
247
299
 
248
- Demonstrates Responses API streaming with Azure OpenAI:
300
+ ---
249
301
 
250
- - **Azure streaming** - Real-time Responses API on Azure
251
- - **Event handling** - Process Azure response events
252
- - **Complete tracking** - Azure metrics with new API
302
+ ### 10. Azure Responses API (Basic)
253
303
 
254
- **Perfect for:** Real-time Azure applications with Responses API
304
+ **File:** `azure/responses-basic.ts`
255
305
 
256
- **See the file for complete code examples.**
306
+ Demonstrates Responses API with Azure OpenAI:
257
307
 
258
- ## TypeScript Configuration
308
+ - Azure + Responses API
309
+ - Simplified interface on Azure
310
+ - Automatic tracking
259
311
 
260
- Ensure your `tsconfig.json` includes:
312
+ **Run:**
261
313
 
262
- ```json
263
- {
264
- "compilerOptions": {
265
- "target": "ES2020",
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
- ## Requirements
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
- - **Node.js 16+** with TypeScript support
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
- ## Troubleshooting
328
+ ### 11. Azure Responses API (Streaming)
285
329
 
286
- ### Module Augmentation Not Working
330
+ **File:** `azure/responses-stream.ts`
287
331
 
288
- **Problem:** TypeScript doesn't recognize `usageMetadata` in OpenAI SDK calls
332
+ Demonstrates Responses API streaming with Azure:
289
333
 
290
- **Solution:**
334
+ - Real-time Responses API on Azure
335
+ - Event handling
336
+ - Azure streaming metrics
291
337
 
292
- ```typescript
293
- // ❌ Wrong - missing module augmentation import
294
- import { initializeReveniumFromEnv } from "@revenium/openai";
338
+ **Run:**
295
339
 
296
- // ✅ Correct - import for module augmentation
297
- import { initializeReveniumFromEnv, patchOpenAIInstance } from "@revenium/openai";
298
- import OpenAI from "openai";
340
+ ```bash
341
+ npm run example:azure-res-stream
342
+ # or
343
+ npx tsx examples/azure/responses-stream.ts
299
344
  ```
300
345
 
301
- ### Environment Variables Not Loading
346
+ **What it does:**
302
347
 
303
- **Problem:** `REVENIUM_METERING_API_KEY` or `OPENAI_API_KEY` not found
348
+ - Creates streaming responses with Azure
349
+ - Processes response events in real-time
350
+ - Tracks Azure streaming usage
304
351
 
305
- **Solutions:**
352
+ ---
306
353
 
307
- - Ensure `.env` file is in project root
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
- ### TypeScript Compilation Errors
356
+ ### "Client not initialized" error
313
357
 
314
- **Problem:** Module resolution or import errors
358
+ **Solution:** Make sure to call `Initialize()` before using `GetClient()`.
315
359
 
316
- **Solution:** Verify your `tsconfig.json` settings:
360
+ ### "REVENIUM_METERING_API_KEY is required" error
317
361
 
318
- ```json
319
- {
320
- "compilerOptions": {
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
- ### Azure Configuration Issues
366
+ **Solution:** Revenium API keys should start with `hak_`. Check your API key format.
330
367
 
331
- **Problem:** Azure OpenAI not working or incorrect pricing
368
+ ### Environment variables not loading
332
369
 
333
- **Solutions:**
370
+ **Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
334
371
 
335
- - Verify all Azure environment variables are set (`AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_DEPLOYMENT`)
336
- - Check deployment name matches your Azure resource
337
- - Ensure API version is compatible (`2024-12-01-preview` or later recommended)
338
- - Verify endpoint URL format: `https://your-resource-name.openai.azure.com/`
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
- npx tsx examples/getting_started.ts
389
+ npm run example:getting-started
350
390
  ```
351
391
 
352
- ## 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:
353
401
 
354
- - **Main Documentation**: See root [README.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/README.md)
355
- - **API Reference**: [Revenium Metadata Fields](https://revenium.readme.io/reference/meter_ai_completion)
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