@revenium/perplexity 2.0.3 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -1
- package/README.md +86 -681
- package/dist/cjs/constants.js +70 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/core/config/perplexity-config.js.map +1 -0
- package/dist/cjs/core/config/revenium-config.js.map +1 -0
- package/dist/cjs/core/tracking/metering.js +86 -6
- package/dist/cjs/core/tracking/metering.js.map +1 -0
- package/dist/cjs/core/wrapper/perplexity-client.js +11 -1
- package/dist/cjs/core/wrapper/perplexity-client.js.map +1 -0
- package/dist/cjs/index.js +9 -4
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types/index.js +0 -15
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/utils/logger.js.map +1 -0
- package/dist/esm/constants.js +67 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/core/config/perplexity-config.js.map +1 -0
- package/dist/esm/core/config/revenium-config.js.map +1 -0
- package/dist/esm/core/tracking/metering.js +86 -6
- package/dist/esm/core/tracking/metering.js.map +1 -0
- package/dist/esm/core/wrapper/perplexity-client.js +11 -1
- package/dist/esm/core/wrapper/perplexity-client.js.map +1 -0
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types/index.js +1 -14
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/utils/logger.js.map +1 -0
- package/dist/types/constants.d.ts +67 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/core/config/perplexity-config.d.ts.map +1 -0
- package/dist/types/core/config/revenium-config.d.ts.map +1 -0
- package/dist/types/core/tracking/metering.d.ts.map +1 -0
- package/dist/types/core/wrapper/perplexity-client.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +3 -11
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/utils/logger.d.ts.map +1 -0
- package/examples/README.md +220 -221
- package/examples/advanced-features.ts +148 -0
- package/examples/basic.ts +16 -21
- package/examples/chat.ts +11 -25
- package/examples/getting_started.ts +64 -0
- package/examples/metadata.ts +33 -40
- package/examples/streaming.ts +6 -17
- package/package.json +7 -11
package/examples/README.md
CHANGED
|
@@ -1,323 +1,322 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Running Examples - Revenium Middleware for Perplexity (Node.js)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This guide shows you how to run the Node.js/TypeScript examples in this repository.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Prerequisites
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Node.js 20+ installed
|
|
8
|
+
- Perplexity API key
|
|
9
|
+
- Revenium API key
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
- **JavaScript projects**: See playground/ directory for JavaScript versions
|
|
11
|
+
## Setup
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
### 1. Clone the Repository
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
|
|
17
|
+
cd revenium-middleware-perplexity-node
|
|
18
|
+
```
|
|
17
19
|
|
|
18
|
-
###
|
|
20
|
+
### 2. Install Dependencies
|
|
19
21
|
|
|
20
22
|
```bash
|
|
21
|
-
npm install
|
|
22
|
-
npm install --save-dev tsx # For TypeScript projects
|
|
23
|
+
npm install
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
###
|
|
26
|
+
### 3. Configure Environment Variables
|
|
26
27
|
|
|
27
|
-
Create a `.env` file in
|
|
28
|
+
Create a `.env` file in the root directory:
|
|
28
29
|
|
|
29
|
-
```
|
|
30
|
-
#
|
|
31
|
-
|
|
30
|
+
```bash
|
|
31
|
+
# Create .env file
|
|
32
|
+
cp .env.example .env
|
|
33
|
+
```
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
REVENIUM_METERING_API_KEY=hak_your_revenium_key
|
|
35
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io/meter/v2
|
|
35
|
+
See [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for the complete list of environment variables and where to get your API keys.
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
PERPLEXITY_API_BASE_URL=https://api.perplexity.ai
|
|
37
|
+
**Replace the placeholder values with your actual keys!**
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
DEBUG=true
|
|
42
|
-
```
|
|
39
|
+
## Running Examples
|
|
43
40
|
|
|
44
|
-
###
|
|
41
|
+
### Available Examples
|
|
45
42
|
|
|
46
|
-
From the repository:
|
|
47
43
|
```bash
|
|
48
|
-
npm run example:basic
|
|
49
|
-
npm run example:streaming
|
|
50
|
-
npm run example:chat
|
|
51
|
-
npm run example:metadata
|
|
44
|
+
npm run example:basic # Basic chat completion
|
|
45
|
+
npm run example:streaming # Streaming response
|
|
46
|
+
npm run example:chat # Multi-turn conversation
|
|
47
|
+
npm run example:metadata # With custom metadata
|
|
48
|
+
npm run example:advanced # Advanced features
|
|
49
|
+
npm run example:getting-started # Quick start example
|
|
50
|
+
npm test # Run all examples
|
|
52
51
|
```
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
```bash
|
|
56
|
-
npm test
|
|
57
|
-
```
|
|
53
|
+
## Example Structure
|
|
58
54
|
|
|
59
|
-
|
|
55
|
+
```
|
|
56
|
+
examples/
|
|
57
|
+
├── basic.ts # Basic chat completion
|
|
58
|
+
├── streaming.ts # Streaming response
|
|
59
|
+
├── chat.ts # Multi-turn conversation
|
|
60
|
+
├── metadata.ts # With custom metadata
|
|
61
|
+
├── advanced-features.ts # Advanced features
|
|
62
|
+
├── getting_started.ts # Quick start example
|
|
63
|
+
└── README.md # This file
|
|
64
|
+
```
|
|
60
65
|
|
|
61
|
-
|
|
66
|
+
## Examples Included
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
// test-perplexity.ts
|
|
66
|
-
import { config } from "dotenv";
|
|
67
|
-
import {
|
|
68
|
-
initializeReveniumFromEnv,
|
|
69
|
-
initializePerplexityFromEnv,
|
|
70
|
-
createChatCompletion,
|
|
71
|
-
} from "@revenium/perplexity";
|
|
68
|
+
### 1. Basic Usage
|
|
72
69
|
|
|
73
|
-
|
|
70
|
+
**Location**: `examples/basic.ts`
|
|
74
71
|
|
|
75
|
-
|
|
76
|
-
// Initialize configurations
|
|
77
|
-
initializeReveniumFromEnv();
|
|
78
|
-
initializePerplexityFromEnv();
|
|
72
|
+
**What it demonstrates:**
|
|
79
73
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
model: "sonar-pro",
|
|
84
|
-
});
|
|
74
|
+
- Simple chat completion
|
|
75
|
+
- Automatic metering
|
|
76
|
+
- Basic error handling
|
|
85
77
|
|
|
86
|
-
|
|
87
|
-
}
|
|
78
|
+
**Run it:**
|
|
88
79
|
|
|
89
|
-
|
|
80
|
+
```bash
|
|
81
|
+
npm run example:basic
|
|
90
82
|
```
|
|
91
83
|
|
|
92
|
-
**
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
createChatCompletion
|
|
100
|
-
} = require("@revenium/perplexity");
|
|
101
|
-
|
|
102
|
-
async function test() {
|
|
103
|
-
initializeReveniumFromEnv();
|
|
104
|
-
initializePerplexityFromEnv();
|
|
105
|
-
|
|
106
|
-
const result = await createChatCompletion({
|
|
107
|
-
messages: [{ role: "user", content: "Hello!" }],
|
|
108
|
-
model: "sonar-pro",
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
console.log(result.choices[0].message.content);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
test();
|
|
84
|
+
**Expected output:**
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
✓ Chat Completion Successful
|
|
88
|
+
Response: [AI response here]
|
|
89
|
+
Model: sonar-pro
|
|
90
|
+
Tokens Used: 50 (prompt: 10, completion: 40)
|
|
115
91
|
```
|
|
116
92
|
|
|
117
|
-
|
|
93
|
+
---
|
|
118
94
|
|
|
119
|
-
|
|
95
|
+
### 2. Streaming
|
|
120
96
|
|
|
121
|
-
|
|
122
|
-
{
|
|
123
|
-
"scripts": {
|
|
124
|
-
"test:perplexity": "tsx test-perplexity.ts"
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
```
|
|
97
|
+
**Location**: `examples/streaming.ts`
|
|
128
98
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
```
|
|
99
|
+
**What it demonstrates:**
|
|
100
|
+
|
|
101
|
+
- Real-time streaming responses
|
|
102
|
+
- Token tracking for streamed content
|
|
103
|
+
- Automatic usage tracking when stream completes
|
|
137
104
|
|
|
138
|
-
|
|
105
|
+
**Run it:**
|
|
139
106
|
|
|
140
107
|
```bash
|
|
141
|
-
npm run
|
|
108
|
+
npm run example:streaming
|
|
142
109
|
```
|
|
143
110
|
|
|
144
|
-
|
|
111
|
+
**Expected output:**
|
|
145
112
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
- `chat.ts` - Multi-turn conversations with context
|
|
150
|
-
- `metadata.ts` - Custom usage tracking and analytics
|
|
151
|
-
|
|
152
|
-
### Step 5: Project Structure
|
|
113
|
+
```
|
|
114
|
+
Model: sonar-pro
|
|
115
|
+
Streaming response:
|
|
153
116
|
|
|
154
|
-
|
|
117
|
+
[Streaming text appears here in real-time]
|
|
155
118
|
|
|
119
|
+
✓ Streaming completed
|
|
156
120
|
```
|
|
157
|
-
your-project/
|
|
158
|
-
├── .env # Your API keys (DON'T COMMIT!)
|
|
159
|
-
├── .env.example # Template for others
|
|
160
|
-
├── .gitignore # Include .env
|
|
161
|
-
├── package.json
|
|
162
|
-
├── src/
|
|
163
|
-
│ ├── perplexity/
|
|
164
|
-
│ │ ├── client.ts # Perplexity client setup
|
|
165
|
-
│ │ └── prompts.ts # Your prompts
|
|
166
|
-
│ └── index.ts
|
|
167
|
-
└── tests/
|
|
168
|
-
└── perplexity.test.ts
|
|
169
|
-
```
|
|
170
121
|
|
|
171
|
-
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
### 3. Multi-turn Chat
|
|
125
|
+
|
|
126
|
+
**Location**: `examples/chat.ts`
|
|
172
127
|
|
|
173
|
-
|
|
128
|
+
**What it demonstrates:**
|
|
174
129
|
|
|
175
|
-
|
|
130
|
+
- Conversation history management
|
|
131
|
+
- Context-aware responses
|
|
132
|
+
- Multi-message interactions
|
|
176
133
|
|
|
177
|
-
**
|
|
178
|
-
- Basic configuration initialization
|
|
179
|
-
- Simple chat completion request
|
|
180
|
-
- Automatic metering to Revenium
|
|
134
|
+
**Run it:**
|
|
181
135
|
|
|
182
|
-
**Run:**
|
|
183
136
|
```bash
|
|
184
|
-
npm run example:
|
|
137
|
+
npm run example:chat
|
|
185
138
|
```
|
|
186
139
|
|
|
187
|
-
|
|
140
|
+
**Expected output:**
|
|
188
141
|
|
|
189
|
-
|
|
142
|
+
```
|
|
143
|
+
--- Turn 1 ---
|
|
144
|
+
User: What is the capital of France?
|
|
145
|
+
Assistant: [Response]
|
|
190
146
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
- Automatic tracking when stream completes
|
|
147
|
+
--- Turn 2 ---
|
|
148
|
+
User: What is its population?
|
|
149
|
+
Assistant: [Response]
|
|
195
150
|
|
|
196
|
-
|
|
197
|
-
```bash
|
|
198
|
-
npm run example:streaming
|
|
151
|
+
✓ Chat completed - Total tokens used: 120
|
|
199
152
|
```
|
|
200
153
|
|
|
201
|
-
|
|
154
|
+
---
|
|
202
155
|
|
|
203
|
-
|
|
156
|
+
### 4. Custom Metadata
|
|
204
157
|
|
|
205
|
-
**
|
|
206
|
-
- Conversation history management
|
|
207
|
-
- Context-aware responses
|
|
208
|
-
- Multi-message interactions
|
|
209
|
-
|
|
210
|
-
**Run:**
|
|
211
|
-
```bash
|
|
212
|
-
npm run example:chat
|
|
213
|
-
```
|
|
158
|
+
**Location**: `examples/metadata.ts`
|
|
214
159
|
|
|
215
|
-
|
|
160
|
+
**What it demonstrates:**
|
|
216
161
|
|
|
217
|
-
Custom
|
|
162
|
+
- Custom metadata tracking
|
|
163
|
+
- Business context in metering
|
|
164
|
+
- Subscriber and organization tracking
|
|
218
165
|
|
|
219
|
-
**
|
|
220
|
-
- Custom subscriber information
|
|
221
|
-
- Organization and product tracking
|
|
222
|
-
- Quality scoring
|
|
223
|
-
- Business analytics integration
|
|
166
|
+
**Run it:**
|
|
224
167
|
|
|
225
|
-
**Run:**
|
|
226
168
|
```bash
|
|
227
169
|
npm run example:metadata
|
|
228
170
|
```
|
|
229
171
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
### Pattern A: Direct Import (Recommended)
|
|
172
|
+
**Expected output:**
|
|
233
173
|
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
174
|
+
```
|
|
175
|
+
✓ Chat Completion Successful
|
|
176
|
+
Response: [AI response]
|
|
177
|
+
Model: sonar-pro
|
|
178
|
+
Tokens Used: 50 (prompt: 10, completion: 40)
|
|
179
|
+
Metadata tracked: subscriber, organization, product, agent, trace
|
|
240
180
|
```
|
|
241
181
|
|
|
242
|
-
|
|
182
|
+
**Note:** Custom metadata (subscriber, organizationId, productId, agent, traceId) is automatically sent to Revenium in the background.
|
|
243
183
|
|
|
244
|
-
|
|
245
|
-
import {
|
|
246
|
-
initializeRevenium,
|
|
247
|
-
initializePerplexity,
|
|
248
|
-
createChatCompletion
|
|
249
|
-
} from "@revenium/perplexity";
|
|
184
|
+
---
|
|
250
185
|
|
|
251
186
|
// Manual configuration
|
|
252
187
|
initializeRevenium({
|
|
253
|
-
|
|
254
|
-
|
|
188
|
+
meteringApiKey: process.env.REVENIUM_METERING_API_KEY!,
|
|
189
|
+
meteringBaseUrl:
|
|
190
|
+
process.env.REVENIUM_METERING_BASE_URL || "https://api.revenium.ai",
|
|
255
191
|
});
|
|
256
192
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
193
|
+
**Location**: `examples/advanced-features.ts`
|
|
194
|
+
|
|
195
|
+
**What it demonstrates:**
|
|
196
|
+
|
|
197
|
+
- Streaming with metadata
|
|
198
|
+
- Multiple model comparisons
|
|
199
|
+
- Advanced configuration
|
|
200
|
+
|
|
201
|
+
**Run it:**
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
npm run example:advanced
|
|
260
205
|
```
|
|
261
206
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
```typescript
|
|
265
|
-
const result = await createChatCompletion({
|
|
266
|
-
messages: [{ role: "user", content: "Hello" }],
|
|
267
|
-
model: "sonar-pro",
|
|
268
|
-
usageMetadata: {
|
|
269
|
-
subscriberId: "user-123",
|
|
270
|
-
subscriberEmail: "user@example.com",
|
|
271
|
-
organizationId: "acme-corp",
|
|
272
|
-
productId: "chat-app"
|
|
273
|
-
}
|
|
274
|
-
});
|
|
207
|
+
**Expected output:**
|
|
208
|
+
|
|
275
209
|
```
|
|
210
|
+
=== Advanced Features Example ===
|
|
211
|
+
|
|
212
|
+
--- Streaming Responses ---
|
|
213
|
+
Model: sonar-pro
|
|
214
|
+
Response:
|
|
215
|
+
[Streaming text appears here in real-time]
|
|
216
|
+
✓ Streaming completed
|
|
217
|
+
|
|
218
|
+
--- Comprehensive Metadata Tracking ---
|
|
219
|
+
Response: [First 100 characters]...
|
|
220
|
+
✓ Metadata tracked successfully
|
|
221
|
+
|
|
222
|
+
--- Multiple Model Types ---
|
|
223
|
+
Using sonar (lightweight search)...
|
|
224
|
+
Response: [First 80 characters]...
|
|
225
|
+
|
|
226
|
+
Using sonar-reasoning (real-time reasoning)...
|
|
227
|
+
Response: [First 80 characters]...
|
|
228
|
+
|
|
229
|
+
✓ Multiple models demonstrated
|
|
230
|
+
|
|
231
|
+
=== All Advanced Features Demonstrated ===
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
### 6. Getting Started
|
|
237
|
+
|
|
238
|
+
**Location**: `examples/getting_started.ts`
|
|
239
|
+
|
|
240
|
+
**What it demonstrates:**
|
|
241
|
+
|
|
242
|
+
- Quick start example
|
|
243
|
+
- Minimal configuration
|
|
244
|
+
- Basic usage pattern
|
|
245
|
+
|
|
246
|
+
**Run it:**
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm run example:getting-started
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
276
253
|
|
|
277
254
|
## Troubleshooting
|
|
278
255
|
|
|
279
|
-
###
|
|
256
|
+
### "Missing API Key" Error
|
|
257
|
+
|
|
258
|
+
Make sure you've created a `.env` file with your API keys:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
# Check if .env exists
|
|
262
|
+
ls .env
|
|
280
263
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
4. **TypeScript errors**: Ensure you have `tsx` or `ts-node` installed for TypeScript projects
|
|
264
|
+
# Verify it has the required keys
|
|
265
|
+
cat .env
|
|
266
|
+
```
|
|
285
267
|
|
|
286
|
-
###
|
|
268
|
+
### "Module not found" Error
|
|
287
269
|
|
|
288
|
-
|
|
270
|
+
Install dependencies first:
|
|
289
271
|
|
|
290
272
|
```bash
|
|
291
|
-
|
|
273
|
+
npm install
|
|
292
274
|
```
|
|
293
275
|
|
|
294
|
-
###
|
|
276
|
+
### "Requests not being tracked"
|
|
277
|
+
|
|
278
|
+
1. Check your `.env` file has correct values:
|
|
279
|
+
|
|
280
|
+
- `PERPLEXITY_API_KEY` is set
|
|
281
|
+
- `REVENIUM_METERING_API_KEY` is set
|
|
282
|
+
- `REVENIUM_METERING_BASE_URL` is set to `https://api.revenium.ai`
|
|
295
283
|
|
|
296
|
-
|
|
284
|
+
2. Enable debug logging:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
DEBUG=true npm run example:basic
|
|
297
288
|
```
|
|
298
|
-
Basic Chat Completion Example
|
|
299
289
|
|
|
300
|
-
|
|
301
|
-
Configurations initialized
|
|
290
|
+
### TypeScript Errors
|
|
302
291
|
|
|
303
|
-
|
|
304
|
-
Chat completion created
|
|
292
|
+
Ensure you have TypeScript and tsx installed:
|
|
305
293
|
|
|
306
|
-
|
|
307
|
-
|
|
294
|
+
```bash
|
|
295
|
+
npm install -D typescript tsx @types/node
|
|
308
296
|
```
|
|
309
297
|
|
|
310
|
-
|
|
298
|
+
---
|
|
311
299
|
|
|
312
|
-
|
|
300
|
+
## Getting Your API Keys
|
|
313
301
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
302
|
+
### Perplexity API Key
|
|
303
|
+
|
|
304
|
+
1. Go to [Perplexity AI Settings](https://www.perplexity.ai/settings/api)
|
|
305
|
+
2. Create a new API key
|
|
306
|
+
3. Add to `.env` as `PERPLEXITY_API_KEY`
|
|
307
|
+
|
|
308
|
+
### Revenium API Key
|
|
309
|
+
|
|
310
|
+
1. Sign up at [Revenium](https://ai.revenium.io)
|
|
311
|
+
2. Create a new API key in your dashboard
|
|
312
|
+
3. Add to `.env` as `REVENIUM_METERING_API_KEY`
|
|
313
|
+
|
|
314
|
+
---
|
|
318
315
|
|
|
319
316
|
## Support
|
|
320
317
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
-
|
|
318
|
+
For issues or questions:
|
|
319
|
+
|
|
320
|
+
- [GitHub Issues](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
321
|
+
- [Documentation](https://docs.revenium.io)
|
|
322
|
+
- Contact: Reach out to the Revenium team for additional support
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced Features Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates advanced middleware features:
|
|
5
|
+
* - Streaming responses with real-time content
|
|
6
|
+
* - Comprehensive metadata for analytics
|
|
7
|
+
* - Multiple model types (search, reasoning, research)
|
|
8
|
+
* - Session and quality tracking
|
|
9
|
+
*
|
|
10
|
+
* For complete documentation, see:
|
|
11
|
+
* https://revenium.readme.io/reference/meter_ai_completion
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
initializeReveniumFromEnv,
|
|
16
|
+
initializePerplexityFromEnv,
|
|
17
|
+
createChatCompletion,
|
|
18
|
+
createStreamingChatCompletion,
|
|
19
|
+
UsageMetadata,
|
|
20
|
+
} from "../src";
|
|
21
|
+
|
|
22
|
+
async function demonstrateStreaming() {
|
|
23
|
+
console.log("\n--- Streaming Responses ---");
|
|
24
|
+
const usageMetadata: UsageMetadata = {
|
|
25
|
+
subscriber: { id: "user-streaming-demo" },
|
|
26
|
+
traceId: "streaming-session-" + Date.now(),
|
|
27
|
+
taskType: "list-generation",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const result = await createStreamingChatCompletion({
|
|
31
|
+
messages: [
|
|
32
|
+
{
|
|
33
|
+
role: "user",
|
|
34
|
+
content: "List the first 3 planets in our solar system.",
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
model: "sonar-pro",
|
|
38
|
+
maxTokens: 400,
|
|
39
|
+
usageMetadata,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(`Model: ${result.model}`);
|
|
43
|
+
console.log("Response: ");
|
|
44
|
+
|
|
45
|
+
let fullContent = "";
|
|
46
|
+
for await (const chunk of result.stream) {
|
|
47
|
+
const content = chunk.choices[0]?.delta?.content || "";
|
|
48
|
+
if (content) {
|
|
49
|
+
process.stdout.write(content);
|
|
50
|
+
fullContent += content;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
console.log("\n✓ Streaming completed\n");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function demonstrateComprehensiveMetadata() {
|
|
58
|
+
console.log("\n--- Comprehensive Metadata Tracking ---");
|
|
59
|
+
|
|
60
|
+
const usageMetadata: UsageMetadata = {
|
|
61
|
+
traceId: "session-advanced-demo-" + Date.now(),
|
|
62
|
+
taskType: "educational-query",
|
|
63
|
+
subscriber: {
|
|
64
|
+
id: "user-456",
|
|
65
|
+
email: "advanced-user@example.com",
|
|
66
|
+
credential: {
|
|
67
|
+
name: "premium-api-key",
|
|
68
|
+
value: "key-premium-abc123",
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
organizationId: "org-enterprise-789",
|
|
72
|
+
subscriptionId: "sub-enterprise-2024",
|
|
73
|
+
productId: "learning-platform",
|
|
74
|
+
agent: "educational-assistant-v2",
|
|
75
|
+
responseQualityScore: 0.92,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const result = await createChatCompletion({
|
|
79
|
+
messages: [
|
|
80
|
+
{
|
|
81
|
+
role: "user",
|
|
82
|
+
content: "What is machine learning?",
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
model: "sonar-pro",
|
|
86
|
+
maxTokens: 400,
|
|
87
|
+
usageMetadata,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
console.log(`Response: ${result.content.substring(0, 100)}...`);
|
|
91
|
+
console.log(`✓ Metadata tracked successfully\n`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function demonstrateMultipleModels() {
|
|
95
|
+
console.log("\n--- Multiple Model Types ---");
|
|
96
|
+
|
|
97
|
+
const usageMetadata: UsageMetadata = {
|
|
98
|
+
subscriber: { id: "model-comparison-user" },
|
|
99
|
+
taskType: "weather-search",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Search model
|
|
103
|
+
console.log("Using sonar (lightweight search)...");
|
|
104
|
+
const searchResult = await createChatCompletion({
|
|
105
|
+
messages: [{ role: "user", content: "Current weather in Tokyo" }],
|
|
106
|
+
model: "sonar-pro",
|
|
107
|
+
maxTokens: 400,
|
|
108
|
+
usageMetadata,
|
|
109
|
+
});
|
|
110
|
+
console.log(`Response: ${searchResult.content.substring(0, 80)}...`);
|
|
111
|
+
|
|
112
|
+
// Reasoning model
|
|
113
|
+
console.log("\nUsing sonar-reasoning (real-time reasoning)...");
|
|
114
|
+
const reasoningResult = await createChatCompletion({
|
|
115
|
+
messages: [
|
|
116
|
+
{ role: "user", content: "Why is the sky blue? Explain briefly." },
|
|
117
|
+
],
|
|
118
|
+
model: "sonar-reasoning",
|
|
119
|
+
maxTokens: 400,
|
|
120
|
+
usageMetadata: {
|
|
121
|
+
...usageMetadata,
|
|
122
|
+
taskType: "reasoning-query",
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
console.log(`Response: ${reasoningResult.content.substring(0, 80)}...`);
|
|
126
|
+
|
|
127
|
+
console.log("\n✓ Multiple models demonstrated\n");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function main() {
|
|
131
|
+
console.log("\n=== Advanced Features Example ===");
|
|
132
|
+
|
|
133
|
+
// Initialize configurations
|
|
134
|
+
initializeReveniumFromEnv();
|
|
135
|
+
initializePerplexityFromEnv();
|
|
136
|
+
|
|
137
|
+
// Demonstrate features
|
|
138
|
+
await demonstrateStreaming();
|
|
139
|
+
await demonstrateComprehensiveMetadata();
|
|
140
|
+
await demonstrateMultipleModels();
|
|
141
|
+
|
|
142
|
+
console.log("=== All Advanced Features Demonstrated ===\n");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
main().catch((error) => {
|
|
146
|
+
console.error("\nError:", error.message);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
});
|