@revenium/perplexity 1.0.11 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -199
- package/examples/chat-completions.ts +0 -1
- package/examples/enhanced.ts +0 -1
- package/examples/streaming.ts +0 -1
- package/package.json +1 -1
- package/playground/basic.js +0 -1
- package/playground/enhanced.js +0 -1
- package/playground/streaming.js +0 -1
package/README.md
CHANGED
|
@@ -55,11 +55,11 @@ Copy and paste the following into `.env`:
|
|
|
55
55
|
|
|
56
56
|
```bash
|
|
57
57
|
# Perplexity AI Configuration
|
|
58
|
-
PERPLEXITY_API_KEY=your_perplexity_api_key_here
|
|
58
|
+
PERPLEXITY_API_KEY="your_perplexity_api_key_here"
|
|
59
59
|
|
|
60
60
|
# Revenium Configuration
|
|
61
|
-
REVENIUM_METERING_API_KEY=your_revenium_api_key_here
|
|
62
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io/meter
|
|
61
|
+
REVENIUM_METERING_API_KEY="your_revenium_api_key_here"
|
|
62
|
+
REVENIUM_METERING_BASE_URL="https://api.revenium.io/meter"
|
|
63
63
|
|
|
64
64
|
```
|
|
65
65
|
|
|
@@ -181,51 +181,6 @@ try {
|
|
|
181
181
|
}
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
-
#### Metadata Example
|
|
185
|
-
|
|
186
|
-
Create `examples/metadata-perplexity.js`:
|
|
187
|
-
|
|
188
|
-
```javascript
|
|
189
|
-
// examples/metadata-perplexity.js
|
|
190
|
-
import { createPerplexityClient } from "@revenium/perplexity";
|
|
191
|
-
|
|
192
|
-
const client = createPerplexityClient();
|
|
193
|
-
|
|
194
|
-
try {
|
|
195
|
-
const result = await client.createChatCompletion({
|
|
196
|
-
model: "sonar-pro",
|
|
197
|
-
messages: [
|
|
198
|
-
{
|
|
199
|
-
role: "user",
|
|
200
|
-
content: "Analyze this quarterly report",
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
usageMetadata: {
|
|
204
|
-
// User tracking
|
|
205
|
-
traceId: "conv-28a7e9d4",
|
|
206
|
-
taskType: "document-analysis",
|
|
207
|
-
subscriberEmail: "user@example.com",
|
|
208
|
-
subscriberId: "user-123",
|
|
209
|
-
subscriberCredentialName: "api-key-1",
|
|
210
|
-
|
|
211
|
-
// Business context
|
|
212
|
-
organizationId: "acme-corp",
|
|
213
|
-
subscriptionId: "premium-plan",
|
|
214
|
-
productId: "business-intelligence",
|
|
215
|
-
agent: "analysis-assistant",
|
|
216
|
-
responseQualityScore: 0.95,
|
|
217
|
-
},
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
console.log("*** METADATA RESPONSE ***");
|
|
221
|
-
console.log("Response:", result.choices[0]?.message?.content);
|
|
222
|
-
console.log("✅ Metadata tracking with metering successful!");
|
|
223
|
-
} catch (error) {
|
|
224
|
-
console.error("❌ Perplexity metadata example failed:", error);
|
|
225
|
-
process.exit(1);
|
|
226
|
-
}
|
|
227
|
-
```
|
|
228
|
-
|
|
229
184
|
### Step 8: Update package.json
|
|
230
185
|
|
|
231
186
|
```json
|
|
@@ -235,8 +190,7 @@ try {
|
|
|
235
190
|
"type": "module",
|
|
236
191
|
"scripts": {
|
|
237
192
|
"test-perplexity": "node test-perplexity.js",
|
|
238
|
-
"test-perplexity-stream": "node examples/streaming-perplexity.js"
|
|
239
|
-
"test-perplexity-metadata": "node examples/metadata-perplexity.js"
|
|
193
|
+
"test-perplexity-stream": "node examples/streaming-perplexity.js"
|
|
240
194
|
},
|
|
241
195
|
"dependencies": {
|
|
242
196
|
"@revenium/perplexity": "^1.0.0"
|
|
@@ -250,8 +204,6 @@ try {
|
|
|
250
204
|
# Test streaming
|
|
251
205
|
npm run test-perplexity-stream
|
|
252
206
|
|
|
253
|
-
# Test metadata
|
|
254
|
-
npm run test-perplexity-metadata
|
|
255
207
|
```
|
|
256
208
|
|
|
257
209
|
---
|
|
@@ -267,6 +219,7 @@ cd revenium-middleware-perplexity-node
|
|
|
267
219
|
|
|
268
220
|
# Install dependencies
|
|
269
221
|
npm install
|
|
222
|
+
npm install @revenium/perplexity
|
|
270
223
|
|
|
271
224
|
# Create your .env file
|
|
272
225
|
cp .env.example .env
|
|
@@ -279,26 +232,28 @@ Edit your `.env` file:
|
|
|
279
232
|
|
|
280
233
|
```bash
|
|
281
234
|
# For Perplexity AI SDK
|
|
282
|
-
PERPLEXITY_API_KEY=your_perplexity_api_key_here
|
|
283
|
-
REVENIUM_METERING_API_KEY=your_revenium_api_key_here
|
|
284
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io/meter
|
|
235
|
+
PERPLEXITY_API_KEY="your_perplexity_api_key_here"
|
|
236
|
+
REVENIUM_METERING_API_KEY="your_revenium_api_key_here"
|
|
237
|
+
REVENIUM_METERING_BASE_URL="https://api.revenium.io/meter"
|
|
285
238
|
```
|
|
286
239
|
|
|
287
240
|
### Run Perplexity AI Examples
|
|
288
241
|
|
|
289
242
|
```bash
|
|
290
243
|
# Perplexity AI examples
|
|
291
|
-
|
|
292
|
-
npm run
|
|
293
|
-
npm run
|
|
244
|
+
|
|
245
|
+
npm run e-basic # Basic chat completion
|
|
246
|
+
npm run e-streaming # Streaming response
|
|
247
|
+
npm run e-enhanced # Enhanced request
|
|
248
|
+
npm run e-chat-completions # Chat completions
|
|
294
249
|
|
|
295
250
|
# Playground examples
|
|
296
251
|
# Required build first
|
|
297
252
|
npm run build
|
|
298
253
|
# Then run any of the following
|
|
299
|
-
npm run p-
|
|
300
|
-
npm run p-
|
|
301
|
-
npm run p-
|
|
254
|
+
npm run p-basic
|
|
255
|
+
npm run p-streaming
|
|
256
|
+
npm run p-enhanced
|
|
302
257
|
```
|
|
303
258
|
|
|
304
259
|
---
|
|
@@ -318,11 +273,9 @@ npm install @revenium/perplexity
|
|
|
318
273
|
Add to your existing `.env` file:
|
|
319
274
|
|
|
320
275
|
```bash
|
|
321
|
-
PERPLEXITY_API_KEY=your_perplexity_api_key_here
|
|
322
|
-
REVENIUM_METERING_API_KEY=your_revenium_api_key_here
|
|
323
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io/meter
|
|
324
|
-
# Optional: Enable debug logging
|
|
325
|
-
REVENIUM_LOG_LEVEL=INFO
|
|
276
|
+
PERPLEXITY_API_KEY="your_perplexity_api_key_here"
|
|
277
|
+
REVENIUM_METERING_API_KEY="your_revenium_api_key_here"
|
|
278
|
+
REVENIUM_METERING_BASE_URL="https://api.revenium.io/meter"
|
|
326
279
|
```
|
|
327
280
|
|
|
328
281
|
### Step 3. Replace Your Imports
|
|
@@ -336,14 +289,7 @@ import { OpenAI } from "openai";
|
|
|
336
289
|
**After:**
|
|
337
290
|
|
|
338
291
|
```javascript
|
|
339
|
-
import {
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
**Or Use OpenAI Client (Alternative):**
|
|
343
|
-
|
|
344
|
-
```javascript
|
|
345
|
-
import { OpenAI } from "openai";
|
|
346
|
-
import "@revenium/perplexity"; // Import middleware to activate tracking
|
|
292
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
347
293
|
```
|
|
348
294
|
|
|
349
295
|
### Step 4. Update Your Code
|
|
@@ -351,37 +297,20 @@ import "@revenium/perplexity"; // Import middleware to activate tracking
|
|
|
351
297
|
#### Revenium Client Example
|
|
352
298
|
|
|
353
299
|
```javascript
|
|
354
|
-
import {
|
|
300
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
355
301
|
|
|
356
302
|
// Initialize (API key from environment variable)
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
#### OpenAI Client Example
|
|
368
|
-
|
|
369
|
-
```javascript
|
|
370
|
-
import { OpenAI } from "openai";
|
|
371
|
-
import "@revenium/perplexity"; // Import middleware for automatic tracking
|
|
372
|
-
|
|
373
|
-
// Initialize OpenAI client for Perplexity
|
|
374
|
-
const client = new OpenAI({
|
|
375
|
-
apiKey: process.env.PERPLEXITY_API_KEY,
|
|
376
|
-
baseURL: "https://api.perplexity.ai",
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
// Make your request - metering happens automatically!
|
|
380
|
-
const result = await client.chat.completions.create({
|
|
381
|
-
model: "sonar-pro",
|
|
382
|
-
messages: [{ role: "user", content: "What is artificial intelligence?" }],
|
|
303
|
+
const middleware = new PerplexityReveniumMiddleware();
|
|
304
|
+
const model = middleware.getGenerativeModel("sonar-pro");
|
|
305
|
+
const result = await model.createChatCompletion({
|
|
306
|
+
messages: [
|
|
307
|
+
{
|
|
308
|
+
role: "user",
|
|
309
|
+
content: "Hello world",
|
|
310
|
+
},
|
|
311
|
+
],
|
|
383
312
|
});
|
|
384
|
-
console.log(result.choices[0]
|
|
313
|
+
console.log("[BASIC EXAMPLE]", result.choices[0].message.content);
|
|
385
314
|
```
|
|
386
315
|
|
|
387
316
|
---
|
|
@@ -393,75 +322,19 @@ console.log(result.choices[0]?.message?.content);
|
|
|
393
322
|
#### Revenium Client Streaming
|
|
394
323
|
|
|
395
324
|
```javascript
|
|
396
|
-
const stream = await
|
|
397
|
-
model: "sonar-pro",
|
|
398
|
-
messages: [{ role: "user", content: "Write a story about AI" }],
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
for await (const chunk of stream) {
|
|
402
|
-
const content = chunk.choices[0]?.delta?.content;
|
|
403
|
-
if (content) process.stdout.write(content);
|
|
404
|
-
}
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
#### OpenAI Client Streaming
|
|
408
|
-
|
|
409
|
-
```javascript
|
|
410
|
-
import { OpenAI } from "openai";
|
|
411
|
-
import "@revenium/perplexity";
|
|
412
|
-
|
|
413
|
-
const client = new OpenAI({
|
|
414
|
-
apiKey: process.env.PERPLEXITY_API_KEY,
|
|
415
|
-
baseURL: "https://api.perplexity.ai",
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
const stream = await client.chat.completions.create({
|
|
419
|
-
model: "sonar-pro",
|
|
420
|
-
messages: [{ role: "user", content: "Write a story about AI" }],
|
|
421
|
-
stream: true,
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
for await (const chunk of stream) {
|
|
425
|
-
const content = chunk.choices[0]?.delta?.content;
|
|
426
|
-
if (content) process.stdout.write(content);
|
|
427
|
-
}
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
### Rich Metadata Tracking
|
|
431
|
-
|
|
432
|
-
```javascript
|
|
433
|
-
import { createPerplexityClient } from "@revenium/perplexity";
|
|
434
|
-
|
|
435
|
-
const client = createPerplexityClient();
|
|
436
|
-
|
|
437
|
-
const response = await client.createChatCompletion({
|
|
438
|
-
model: "sonar-pro",
|
|
325
|
+
const stream = await model.createChatCompletionStream({
|
|
439
326
|
messages: [
|
|
440
327
|
{
|
|
441
328
|
role: "user",
|
|
442
|
-
content: "
|
|
329
|
+
content: "Hello world",
|
|
443
330
|
},
|
|
444
331
|
],
|
|
445
|
-
usageMetadata: {
|
|
446
|
-
// User tracking
|
|
447
|
-
traceId: "conv-28a7e9d4",
|
|
448
|
-
taskType: "document-analysis",
|
|
449
|
-
subscriberEmail: "user@example.com",
|
|
450
|
-
subscriberId: "user-123",
|
|
451
|
-
subscriberCredentialName: "api-key-1",
|
|
452
|
-
|
|
453
|
-
// Business context
|
|
454
|
-
organizationId: "acme-corp",
|
|
455
|
-
subscriptionId: "premium-plan",
|
|
456
|
-
productId: "business-intelligence",
|
|
457
|
-
agent: "analysis-assistant",
|
|
458
|
-
responseQualityScore: 0.95,
|
|
459
|
-
},
|
|
460
332
|
});
|
|
333
|
+
for await (const chunk of stream) {
|
|
334
|
+
process.stdout.write(chunk.choices[0]?.delta?.content || "");
|
|
335
|
+
}
|
|
461
336
|
```
|
|
462
337
|
|
|
463
|
-
---
|
|
464
|
-
|
|
465
338
|
## 📊 What Gets Tracked
|
|
466
339
|
|
|
467
340
|
- **Token Usage**: Input and output tokens for accurate billing
|
|
@@ -490,29 +363,11 @@ _Note: Model availability depends on your Perplexity AI account and API access l
|
|
|
490
363
|
|
|
491
364
|
### Environment Variables
|
|
492
365
|
|
|
493
|
-
| Variable | Required | Description
|
|
494
|
-
| ---------------------------- | -------- |
|
|
495
|
-
| `PERPLEXITY_API_KEY` | ✅ | Your Perplexity API key
|
|
496
|
-
| `REVENIUM_METERING_API_KEY` | ✅ | Your Revenium API key
|
|
497
|
-
| `
|
|
498
|
-
| `REVENIUM_METERING_BASE_URL` | ❌ | Revenium base URL (default: https://api.revenium.io/meter/v2) |
|
|
499
|
-
| `REVENIUM_LOG_LEVEL` | ❌ | Log level: DEBUG, INFO, WARNING, ERROR |
|
|
500
|
-
|
|
501
|
-
### Manual Configuration
|
|
502
|
-
|
|
503
|
-
```javascript
|
|
504
|
-
// Revenium client with custom configuration
|
|
505
|
-
const client = createPerplexityClient(
|
|
506
|
-
"your-api-key",
|
|
507
|
-
"https://api.perplexity.ai"
|
|
508
|
-
);
|
|
509
|
-
|
|
510
|
-
// OpenAI client with custom base URL
|
|
511
|
-
const openaiClient = new OpenAI({
|
|
512
|
-
apiKey: "your-api-key",
|
|
513
|
-
baseURL: "https://custom-perplexity-endpoint.com",
|
|
514
|
-
});
|
|
515
|
-
```
|
|
366
|
+
| Variable | Required | Description |
|
|
367
|
+
| ---------------------------- | -------- | ---------------------------------------------------------- |
|
|
368
|
+
| `PERPLEXITY_API_KEY` | ✅ | Your Perplexity API key |
|
|
369
|
+
| `REVENIUM_METERING_API_KEY` | ✅ | Your Revenium API key |
|
|
370
|
+
| `REVENIUM_METERING_BASE_URL` | ❌ | Revenium base URL (default: https://api.revenium.io/meter) |
|
|
516
371
|
|
|
517
372
|
---
|
|
518
373
|
|
|
@@ -542,16 +397,6 @@ export REVENIUM_LOG_LEVEL="DEBUG" # Enable debug logging
|
|
|
542
397
|
}
|
|
543
398
|
```
|
|
544
399
|
|
|
545
|
-
### Debug Mode
|
|
546
|
-
|
|
547
|
-
Enable detailed logging to troubleshoot issues:
|
|
548
|
-
|
|
549
|
-
```bash
|
|
550
|
-
export REVENIUM_LOG_LEVEL="DEBUG"
|
|
551
|
-
export REVENIUM_VERBOSE_STARTUP="true"
|
|
552
|
-
node your-script.js
|
|
553
|
-
```
|
|
554
|
-
|
|
555
400
|
This will show:
|
|
556
401
|
|
|
557
402
|
- Request/response details
|
|
@@ -593,9 +438,9 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
593
438
|
|
|
594
439
|
## 🤝 Support
|
|
595
440
|
|
|
596
|
-
- 📖 [Documentation](https://docs.revenium.
|
|
597
|
-
- 💬 [Community Support](https://community.revenium.
|
|
598
|
-
- 📧 [Email Support](mailto:support@revenium.
|
|
441
|
+
- 📖 [Documentation](https://docs.revenium.com)
|
|
442
|
+
- 💬 [Community Support](https://community.revenium.com)
|
|
443
|
+
- 📧 [Email Support](mailto:support@revenium.com)
|
|
599
444
|
- 🐛 [Report Issues](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
600
445
|
|
|
601
446
|
---
|
|
@@ -6,7 +6,6 @@ const chatCompletionsExample = async () => {
|
|
|
6
6
|
const middleware = new PerplexityReveniumMiddleware();
|
|
7
7
|
const model = middleware.getGenerativeModel("sonar-pro");
|
|
8
8
|
const result = await model.createChatCompletion({
|
|
9
|
-
model: "sonar-pro",
|
|
10
9
|
messages: [
|
|
11
10
|
{
|
|
12
11
|
role: "user",
|
package/examples/enhanced.ts
CHANGED
|
@@ -6,7 +6,6 @@ async function enhancedRequest() {
|
|
|
6
6
|
const middleware = new PerplexityReveniumMiddleware();
|
|
7
7
|
const model = middleware.getGenerativeModel("sonar-pro");
|
|
8
8
|
const result = await model.createChatCompletion({
|
|
9
|
-
model: "sonar-pro",
|
|
10
9
|
messages: [
|
|
11
10
|
{ role: "user", content: "Explain quantum computing in simple terms" },
|
|
12
11
|
],
|
package/examples/streaming.ts
CHANGED
|
@@ -6,7 +6,6 @@ const chatCompletionStreamingExample = async () => {
|
|
|
6
6
|
const middleware = new PerplexityReveniumMiddleware();
|
|
7
7
|
const model = middleware.getGenerativeModel("sonar-pro");
|
|
8
8
|
const result = await model.createChatCompletionStream({
|
|
9
|
-
model: "sonar-pro",
|
|
10
9
|
messages: [
|
|
11
10
|
{
|
|
12
11
|
role: "user",
|
package/package.json
CHANGED
package/playground/basic.js
CHANGED
package/playground/enhanced.js
CHANGED
package/playground/streaming.js
CHANGED
|
@@ -6,7 +6,6 @@ const streamingExample = async () => {
|
|
|
6
6
|
const middleware = new PerplexityReveniumMiddleware();
|
|
7
7
|
const model = middleware.getGenerativeModel("sonar-pro");
|
|
8
8
|
const result = await model.createChatCompletionStream({
|
|
9
|
-
model: "sonar-pro",
|
|
10
9
|
messages: [
|
|
11
10
|
{
|
|
12
11
|
role: "user",
|