@revenium/openai 1.0.11 → 1.0.12

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 (66) hide show
  1. package/.env.example +20 -0
  2. package/CHANGELOG.md +21 -47
  3. package/README.md +141 -690
  4. package/dist/cjs/core/config/loader.js +1 -1
  5. package/dist/cjs/core/config/loader.js.map +1 -1
  6. package/dist/cjs/core/tracking/api-client.js +1 -1
  7. package/dist/cjs/core/tracking/api-client.js.map +1 -1
  8. package/dist/cjs/index.js +2 -2
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/utils/url-builder.js +32 -7
  11. package/dist/cjs/utils/url-builder.js.map +1 -1
  12. package/dist/esm/core/config/loader.js +1 -1
  13. package/dist/esm/core/config/loader.js.map +1 -1
  14. package/dist/esm/core/tracking/api-client.js +1 -1
  15. package/dist/esm/core/tracking/api-client.js.map +1 -1
  16. package/dist/esm/index.js +2 -2
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/utils/url-builder.js +32 -7
  19. package/dist/esm/utils/url-builder.js.map +1 -1
  20. package/dist/types/index.d.ts +2 -2
  21. package/dist/types/types/index.d.ts +2 -2
  22. package/dist/types/types/index.d.ts.map +1 -1
  23. package/dist/types/utils/url-builder.d.ts +11 -3
  24. package/dist/types/utils/url-builder.d.ts.map +1 -1
  25. package/examples/README.md +250 -254
  26. package/examples/azure-basic.ts +25 -13
  27. package/examples/azure-responses-basic.ts +36 -7
  28. package/examples/azure-responses-streaming.ts +36 -7
  29. package/examples/azure-streaming.ts +40 -19
  30. package/examples/getting_started.ts +54 -0
  31. package/examples/openai-basic.ts +39 -17
  32. package/examples/openai-function-calling.ts +259 -0
  33. package/examples/openai-responses-basic.ts +36 -7
  34. package/examples/openai-responses-streaming.ts +36 -7
  35. package/examples/openai-streaming.ts +24 -13
  36. package/examples/openai-vision.ts +289 -0
  37. package/package.json +3 -9
  38. package/src/core/config/azure-config.ts +72 -0
  39. package/src/core/config/index.ts +23 -0
  40. package/src/core/config/loader.ts +66 -0
  41. package/src/core/config/manager.ts +94 -0
  42. package/src/core/config/validator.ts +89 -0
  43. package/src/core/providers/detector.ts +159 -0
  44. package/src/core/providers/index.ts +16 -0
  45. package/src/core/tracking/api-client.ts +78 -0
  46. package/src/core/tracking/index.ts +21 -0
  47. package/src/core/tracking/payload-builder.ts +132 -0
  48. package/src/core/tracking/usage-tracker.ts +189 -0
  49. package/src/core/wrapper/index.ts +9 -0
  50. package/src/core/wrapper/instance-patcher.ts +288 -0
  51. package/src/core/wrapper/request-handler.ts +423 -0
  52. package/src/core/wrapper/stream-wrapper.ts +100 -0
  53. package/src/index.ts +336 -0
  54. package/src/types/function-parameters.ts +251 -0
  55. package/src/types/index.ts +313 -0
  56. package/src/types/openai-augmentation.ts +233 -0
  57. package/src/types/responses-api.ts +308 -0
  58. package/src/utils/azure-model-resolver.ts +220 -0
  59. package/src/utils/constants.ts +21 -0
  60. package/src/utils/error-handler.ts +251 -0
  61. package/src/utils/metadata-builder.ts +219 -0
  62. package/src/utils/provider-detection.ts +257 -0
  63. package/src/utils/request-handler-factory.ts +285 -0
  64. package/src/utils/stop-reason-mapper.ts +74 -0
  65. package/src/utils/type-guards.ts +202 -0
  66. package/src/utils/url-builder.ts +68 -0
@@ -1,8 +1,17 @@
1
1
  /**
2
- * Azure OpenAI Basic Example
2
+ * Azure OpenAI Basic Example
3
3
  *
4
4
  * Shows how to use Revenium middleware with Azure OpenAI chat completions and embeddings.
5
5
  * Demonstrates seamless metadata integration with Azure - all metadata fields are optional!
6
+ *
7
+ * Metadata Options:
8
+ * - Start with basic usage (no metadata) - tracking works automatically
9
+ * - Add subscriber info for user tracking
10
+ * - Include organization/product IDs for business analytics
11
+ * - Use task type and trace ID for detailed analysis
12
+ *
13
+ * For complete metadata field reference, see:
14
+ * https://revenium.readme.io/reference/meter_ai_completion
6
15
  */
7
16
 
8
17
  import 'dotenv/config';
@@ -73,29 +82,31 @@ async function azureBasicExample() {
73
82
  },
74
83
  ],
75
84
 
76
- // All metadata fields are optional - add what you need for Azure analytics!
77
- // Note: Nested subscriber structure for consistency across language implementations
85
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
78
86
  usageMetadata: {
79
- // User tracking (optional) - nested subscriber object
87
+ // User identification
80
88
  subscriber: {
81
89
  id: 'azure-user-789',
82
90
  email: 'azure-dev@company.com',
83
91
  credential: {
84
- name: 'azure-key',
85
- value: 'azure789',
92
+ name: 'api-key-prod',
93
+ value: 'key-jkl-012',
86
94
  },
87
95
  },
88
96
 
89
- // Business context (optional)
97
+ // Organization & billing
90
98
  organizationId: 'enterprise-corp',
91
- productId: 'azure-ai-platform',
99
+ subscriptionId: 'plan-azure-enterprise-2024',
92
100
 
93
- // Task classification (optional)
101
+ // Product & task tracking
102
+ productId: 'azure-ai-platform',
94
103
  taskType: 'azure-comparison',
95
- traceId: `azure-${Date.now()}`,
96
-
97
- // Azure-specific fields (optional)
98
104
  agent: 'azure-basic-chat-node',
105
+
106
+ // Session tracking
107
+ traceId: 'azure-' + Date.now(),
108
+
109
+ // Quality metrics
99
110
  responseQualityScore: 0.92,
100
111
  },
101
112
  });
@@ -148,8 +159,9 @@ async function azureBasicExample() {
148
159
  },
149
160
  },
150
161
  organizationId: 'enterprise-corp',
151
- taskType: 'enterprise-document-embedding',
152
162
  productId: 'azure-search-platform',
163
+ subscriptionId: 'sub-azure-premium-999',
164
+ taskType: 'enterprise-document-embedding',
153
165
  traceId: `azure-embed-${Date.now()}`,
154
166
  agent: 'azure-basic-embeddings-node',
155
167
  },
@@ -5,7 +5,16 @@
5
5
  * The Responses API is a new stateful API that brings together capabilities from chat completions
6
6
  * and assistants API in one unified experience.
7
7
  *
8
- * Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses
8
+ * Metadata Options:
9
+ * - Start with basic usage (no metadata) - tracking works automatically
10
+ * - Add subscriber info for user tracking
11
+ * - Include organization/product IDs for business analytics
12
+ * - Use task type and trace ID for detailed analysis
13
+ *
14
+ * For complete metadata field reference, see:
15
+ * https://revenium.readme.io/reference/meter_ai_completion
16
+ *
17
+ * Responses API Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses
9
18
  */
10
19
 
11
20
  import 'dotenv/config';
@@ -89,19 +98,29 @@ async function main() {
89
98
  temperature: 0.6,
90
99
  max_output_tokens: 200,
91
100
  usageMetadata: {
101
+ // User identification
92
102
  subscriber: {
93
103
  id: 'azure-enterprise-user-123',
94
104
  email: 'enterprise@azurecorp.com',
95
105
  credential: {
96
- name: 'azure-enterprise-key',
97
- value: 'azure-key-...',
106
+ name: 'api-key-prod',
107
+ value: 'key-stu-901',
98
108
  },
99
109
  },
110
+
111
+ // Organization & billing
100
112
  organizationId: 'azure-enterprise-org-456',
113
+ subscriptionId: 'plan-azure-responses-2024',
114
+
115
+ // Product & task tracking
101
116
  productId: 'azure-ai-integration-assistant',
102
117
  taskType: 'enterprise-architecture-guidance',
103
- traceId: 'azure-trace-789',
104
118
  agent: 'azure-ai-architect',
119
+
120
+ // Session tracking
121
+ traceId: 'azure-trace-789',
122
+
123
+ // Quality metrics
105
124
  responseQualityScore: 0.96,
106
125
  },
107
126
  } as ResponsesCreateParams);
@@ -167,19 +186,29 @@ async function main() {
167
186
  instructions:
168
187
  'You are an Azure AI solutions architect providing detailed technical guidance.',
169
188
  usageMetadata: {
189
+ // User identification
170
190
  subscriber: {
171
191
  id: 'azure-saas-architect-789',
172
192
  email: 'architect@azuresaas.com',
173
193
  credential: {
174
- name: 'azure-saas-enterprise-key',
175
- value: 'azure-saas-key-...',
194
+ name: 'api-key-prod',
195
+ value: 'key-vwx-234',
176
196
  },
177
197
  },
198
+
199
+ // Organization & billing
178
200
  organizationId: 'azure-saas-enterprise-012',
201
+ subscriptionId: 'plan-azure-saas-2024',
202
+
203
+ // Product & task tracking
179
204
  productId: 'azure-saas-ai-architect',
180
205
  taskType: 'multi-tenant-architecture-design',
181
- traceId: 'azure-saas-trace-345',
182
206
  agent: 'azure-saas-solutions-architect',
207
+
208
+ // Session tracking
209
+ traceId: 'azure-saas-trace-345',
210
+
211
+ // Quality metrics
183
212
  responseQualityScore: 0.99,
184
213
  },
185
214
  } as ResponsesCreateParams);
@@ -5,7 +5,16 @@
5
5
  * using the Revenium middleware. The Responses API supports streaming for real-time
6
6
  * response generation in Azure environments.
7
7
  *
8
- * Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses
8
+ * Metadata Options:
9
+ * - Start with basic usage (no metadata) - tracking works automatically
10
+ * - Add subscriber info for user tracking
11
+ * - Include organization/product IDs for business analytics
12
+ * - Use task type and trace ID for detailed analysis
13
+ *
14
+ * For complete metadata field reference, see:
15
+ * https://revenium.readme.io/reference/meter_ai_completion
16
+ *
17
+ * Responses API Reference: https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses
9
18
  */
10
19
 
11
20
  import 'dotenv/config';
@@ -94,19 +103,29 @@ async function main() {
94
103
  temperature: 0.7,
95
104
  max_output_tokens: 250,
96
105
  usageMetadata: {
106
+ // User identification
97
107
  subscriber: {
98
108
  id: 'azure-security-expert-123',
99
109
  email: 'security@azureenterprise.com',
100
110
  credential: {
101
- name: 'azure-security-key',
102
- value: 'azure-sec-key-...',
111
+ name: 'api-key-prod',
112
+ value: 'key-yza-567',
103
113
  },
104
114
  },
115
+
116
+ // Organization & billing
105
117
  organizationId: 'azure-security-org-456',
118
+ subscriptionId: 'plan-azure-security-2024',
119
+
120
+ // Product & task tracking
106
121
  productId: 'azure-ai-security-advisor',
107
122
  taskType: 'enterprise-security-streaming',
108
- traceId: 'azure-security-trace-789',
109
123
  agent: 'azure-security-architect',
124
+
125
+ // Session tracking
126
+ traceId: 'azure-security-trace-789',
127
+
128
+ // Quality metrics
110
129
  responseQualityScore: 0.94,
111
130
  },
112
131
  } as ResponsesCreateParams);
@@ -186,19 +205,29 @@ async function main() {
186
205
  instructions:
187
206
  'You are an Azure solutions architect specializing in scalable AI implementations with enterprise governance.',
188
207
  usageMetadata: {
208
+ // User identification
189
209
  subscriber: {
190
210
  id: 'azure-enterprise-architect-789',
191
211
  email: 'architect@azureenterprise.com',
192
212
  credential: {
193
- name: 'azure-enterprise-streaming-key',
194
- value: 'azure-ent-stream-key-...',
213
+ name: 'api-key-prod',
214
+ value: 'key-bcd-890',
195
215
  },
196
216
  },
217
+
218
+ // Organization & billing
197
219
  organizationId: 'azure-enterprise-streaming-012',
220
+ subscriptionId: 'plan-azure-scalable-2024',
221
+
222
+ // Product & task tracking
198
223
  productId: 'azure-scalable-ai-architect',
199
224
  taskType: 'enterprise-scalable-ai-streaming',
200
- traceId: 'azure-scalable-trace-345',
201
225
  agent: 'azure-enterprise-solutions-architect',
226
+
227
+ // Session tracking
228
+ traceId: 'azure-scalable-trace-345',
229
+
230
+ // Quality metrics
202
231
  responseQualityScore: 0.98,
203
232
  },
204
233
  } as ResponsesCreateParams);
@@ -1,8 +1,17 @@
1
1
  /**
2
- * Azure OpenAI Streaming Example
2
+ * Azure OpenAI Streaming Example
3
3
  *
4
4
  * Shows how to use Revenium middleware with Azure OpenAI streaming responses.
5
5
  * Demonstrates seamless metadata integration with Azure streaming - all metadata fields are optional!
6
+ *
7
+ * Metadata Options:
8
+ * - Start with basic usage (no metadata) - tracking works automatically
9
+ * - Add subscriber info for user tracking
10
+ * - Include organization/product IDs for business analytics
11
+ * - Use task type and trace ID for detailed analysis
12
+ *
13
+ * For complete metadata field reference, see:
14
+ * https://revenium.readme.io/reference/meter_ai_completion
6
15
  */
7
16
 
8
17
  import 'dotenv/config';
@@ -83,29 +92,31 @@ async function azureStreamingExample() {
83
92
  ],
84
93
  stream: true,
85
94
 
86
- // All metadata fields are optional - add what you need for Azure enterprise analytics!
87
- // Note: Nested subscriber structure matches Python middleware implementation
95
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
88
96
  usageMetadata: {
89
- // User tracking (optional) - nested subscriber object
97
+ // User identification
90
98
  subscriber: {
91
99
  id: 'azure-stream-user-789',
92
100
  email: 'enterprise@company.com',
93
101
  credential: {
94
- name: 'azure-stream-key',
95
- value: 'stream789',
102
+ name: 'api-key-prod',
103
+ value: 'key-mno-345',
96
104
  },
97
105
  },
98
106
 
99
- // Business context (optional)
107
+ // Organization & billing
100
108
  organizationId: 'enterprise-corp',
101
- productId: 'azure-ai-consultant',
109
+ subscriptionId: 'plan-azure-stream-2024',
102
110
 
103
- // Task classification (optional)
111
+ // Product & task tracking
112
+ productId: 'azure-ai-consultant',
104
113
  taskType: 'enterprise-consultation',
105
- traceId: `azure-stream-${Date.now()}`,
114
+ agent: 'azure-streaming-chat-node',
115
+
116
+ // Session tracking
117
+ traceId: 'azure-stream-' + Date.now(),
106
118
 
107
- // Azure-specific fields (optional)
108
- agent: 'azure-streaming-chat-metadata-node',
119
+ // Quality metrics
109
120
  responseQualityScore: 0.95,
110
121
  },
111
122
  });
@@ -148,22 +159,32 @@ async function azureStreamingExample() {
148
159
  'Enterprise document: Integration with Azure Active Directory',
149
160
  ],
150
161
 
151
- // All metadata fields are optional - perfect for Azure enterprise document processing!
152
- // Note: Nested subscriber structure for consistency across language implementations
162
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
153
163
  usageMetadata: {
164
+ // User identification
154
165
  subscriber: {
155
166
  id: 'azure-enterprise-processor',
156
167
  email: 'processor@enterprise-corp.com',
157
168
  credential: {
158
- name: 'azure-enterprise-key',
159
- value: 'enterprise789',
169
+ name: 'api-key-prod',
170
+ value: 'key-pqr-678',
160
171
  },
161
172
  },
173
+
174
+ // Organization & billing
162
175
  organizationId: 'enterprise-corp',
163
- taskType: 'enterprise-document-processing',
176
+ subscriptionId: 'plan-azure-enterprise-2024',
177
+
178
+ // Product & task tracking
164
179
  productId: 'azure-document-intelligence',
165
- traceId: `azure-enterprise-${Date.now()}`,
166
- agent: 'azure-batch-embeddings-metadata-node',
180
+ taskType: 'enterprise-document-processing',
181
+ agent: 'azure-batch-embeddings-node',
182
+
183
+ // Session tracking
184
+ traceId: 'azure-enterprise-' + Date.now(),
185
+
186
+ // Quality metrics
187
+ responseQualityScore: 0.96,
167
188
  },
168
189
  });
169
190
 
@@ -0,0 +1,54 @@
1
+ import 'dotenv/config';
2
+ import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
3
+ import OpenAI from 'openai';
4
+
5
+ async function main() {
6
+ const initResult = initializeReveniumFromEnv();
7
+ if (!initResult.success) {
8
+ console.error('Failed to initialize Revenium:', initResult.message);
9
+ process.exit(1);
10
+ }
11
+
12
+ const openai = patchOpenAIInstance(new OpenAI());
13
+
14
+ const response = await openai.chat.completions.create({
15
+ model: 'gpt-4o-mini',
16
+ messages: [
17
+ { role: 'system', content: 'You are a helpful assistant.' },
18
+ { role: 'user', content: 'Please verify you are ready to assist me.' }
19
+ ],
20
+
21
+ /* Optional metadata for advanced reporting, lineage tracking, and cost allocation
22
+ usageMetadata: {
23
+ // User identification
24
+ subscriber: {
25
+ id: 'user-123',
26
+ email: 'user@example.com',
27
+ credential: {
28
+ name: 'api-key-prod',
29
+ value: 'key-abc-123'
30
+ }
31
+ },
32
+
33
+ // Organization & billing
34
+ organizationId: 'my-customers-name',
35
+ subscriptionId: 'plan-enterprise-2024',
36
+
37
+ // Product & task tracking
38
+ productId: 'my-product',
39
+ taskType: 'doc-summary',
40
+ agent: 'customer-support',
41
+
42
+ // Session tracking
43
+ traceId: 'session-' + Date.now(),
44
+
45
+ // Quality metrics
46
+ responseQualityScore: 0.95
47
+ }
48
+ */
49
+ });
50
+
51
+ console.log('Response:', response.choices[0]?.message?.content);
52
+ }
53
+
54
+ main().catch(console.error);
@@ -3,6 +3,15 @@
3
3
  *
4
4
  * Shows how to use Revenium middleware with OpenAI chat completions and embeddings.
5
5
  * Demonstrates seamless metadata integration - all metadata fields are optional!
6
+ *
7
+ * Metadata Options:
8
+ * - Start with basic usage (no metadata) - tracking works automatically
9
+ * - Add subscriber info for user tracking
10
+ * - Include organization/product IDs for business analytics
11
+ * - Use task type and trace ID for detailed analysis
12
+ *
13
+ * For complete metadata field reference, see:
14
+ * https://revenium.readme.io/reference/meter_ai_completion
6
15
  */
7
16
 
8
17
  import 'dotenv/config';
@@ -45,29 +54,32 @@ async function openaiBasicExample() {
45
54
  { role: 'user', content: 'Explain the benefits of using middleware in 2 sentences.' },
46
55
  ],
47
56
 
48
- // All metadata fields are optional - add what you need!
49
- // Note: Nested subscriber structure matches Python middleware for consistency
57
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
50
58
  usageMetadata: {
51
- // User tracking (optional) - nested subscriber object
59
+ // User identification
52
60
  subscriber: {
53
61
  id: 'user-12345',
54
62
  email: 'developer@company.com',
55
63
  credential: {
56
- name: 'api-key',
57
- value: 'key123',
64
+ name: 'api-key-prod',
65
+ value: 'key-abc-123',
58
66
  },
59
67
  },
60
68
 
61
- // Business context (optional)
69
+ // Organization & billing
62
70
  organizationId: 'my-customer',
63
- productId: 'ai-assistant',
71
+ subscriptionId: 'plan-premium-2024',
64
72
 
65
- // Task classification (optional)
73
+ // Product & task tracking
74
+ productId: 'ai-assistant',
66
75
  taskType: 'explanation-request',
67
- traceId: `session-${Date.now()}`,
68
-
69
- // Custom fields (optional)
70
76
  agent: 'openai-basic-chat-node',
77
+
78
+ // Session tracking
79
+ traceId: 'session-' + Date.now(),
80
+
81
+ // Quality metrics
82
+ responseQualityScore: 0.95,
71
83
  },
72
84
  });
73
85
 
@@ -96,22 +108,32 @@ async function openaiBasicExample() {
96
108
  model: 'text-embedding-3-small',
97
109
  input: 'Advanced text embedding with comprehensive tracking metadata',
98
110
 
99
- // All metadata fields are optional - customize for your use case!
100
- // Note: Nested subscriber structure for consistency across language implementations
111
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
101
112
  usageMetadata: {
113
+ // User identification
102
114
  subscriber: {
103
115
  id: 'embedding-user-789',
104
116
  email: 'embeddings@company.com',
105
117
  credential: {
106
- name: 'embed-key',
107
- value: 'embed123',
118
+ name: 'api-key-prod',
119
+ value: 'key-def-456',
108
120
  },
109
121
  },
122
+
123
+ // Organization & billing
110
124
  organizationId: 'my-company',
111
- taskType: 'document-embedding',
125
+ subscriptionId: 'plan-enterprise-2024',
126
+
127
+ // Product & task tracking
112
128
  productId: 'search-engine',
113
- traceId: `embed-${Date.now()}`,
129
+ taskType: 'document-embedding',
114
130
  agent: 'openai-basic-embeddings-node',
131
+
132
+ // Session tracking
133
+ traceId: 'embed-' + Date.now(),
134
+
135
+ // Quality metrics
136
+ responseQualityScore: 0.98,
115
137
  },
116
138
  });
117
139