@revenium/perplexity 1.0.7 → 1.0.8

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/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ PERPLEXITY_API_KEY=your_perplexity_api_key_here
2
+ REVENIUM_METERING_API_KEY=your_revenium_api_key_here
3
+ REVENIUM_METERING_BASE_URL="https://api.revenium.io/meter"
package/README.md CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenium/perplexity",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "NodeJS middleware for perplexity's AI API",
5
5
  "homepage": "https://github.com/revenium/revenium-middleware-perplexity-node#readme",
6
6
  "bugs": {
@@ -27,7 +27,14 @@
27
27
  "multiple-models-example": "npx ts-node examples/multiple-models-example.ts",
28
28
  "openai-client-example": "npx ts-node examples/openai-client-example.ts",
29
29
  "metadata-example": "npx ts-node examples/metadata-example.ts",
30
- "run-all-examples": "npx ts-node examples/run-all-examples.ts"
30
+ "run-all-examples": "npx ts-node examples/run-all-examples.ts",
31
+ "p-basic": "npx ts-node playground/basic.js",
32
+ "p-custom-config": "npx ts-node playground/custom-config.js",
33
+ "p-enhanced": "npx ts-node playground/enhanced.js",
34
+ "p-metadata": "npx ts-node playground/metadata.js",
35
+ "p-multiple-models": "npx ts-node playground/multiple-models.js",
36
+ "p-openai-client": "npx ts-node playground/openai-client.js",
37
+ "p-streaming": "npx ts-node playground/openai-client.js"
31
38
  },
32
39
  "keywords": [
33
40
  "revenium",
@@ -1,82 +1,82 @@
1
- import { createPerplexityClient } from "@revenium/perplexity";
2
-
3
- async function customConfigExample() {
4
- console.log("\n⚙️ Perplexity AI - Custom Configuration Example");
5
- console.log("=".repeat(50));
6
-
7
- try {
8
- // Example 1: Using environment variables (default)
9
- console.log("\n1️⃣ Using environment variables:");
10
- const defaultClient = createPerplexityClient();
11
-
12
- const response1 = await defaultClient.createChatCompletion({
13
- model: "sonar-pro",
14
- messages: [
15
- {
16
- role: "user",
17
- content: "Hello from default configuration!",
18
- },
19
- ],
20
- });
21
- console.log("✅ Default config response:", response1.choices[0]?.message?.content?.substring(0, 50) + "...");
22
-
23
- // Example 2: Custom API key only
24
- console.log("\n2️⃣ Using custom API key:");
25
- const customKeyClient = createPerplexityClient(process.env.PERPLEXITY_API_KEY);
26
-
27
- const response2 = await customKeyClient.createChatCompletion({
28
- model: "sonar-pro",
29
- messages: [
30
- {
31
- role: "user",
32
- content: "Hello from custom API key configuration!",
33
- },
34
- ],
35
- });
36
- console.log("✅ Custom key response:", response2.choices[0]?.message?.content?.substring(0, 50) + "...");
37
-
38
- // Example 3: Custom API key and base URL
39
- console.log("\n3️⃣ Using custom API key and base URL:");
40
- const fullCustomClient = createPerplexityClient(
41
- process.env.PERPLEXITY_API_KEY,
42
- "https://api.perplexity.ai"
43
- );
44
-
45
- const response3 = await fullCustomClient.createChatCompletion({
46
- model: "sonar-pro",
47
- messages: [
48
- {
49
- role: "user",
50
- content: "Hello from fully custom configuration!",
51
- },
52
- ],
53
- });
54
- console.log("✅ Full custom response:", response3.choices[0]?.message?.content?.substring(0, 50) + "...");
55
-
56
- // Example 4: Custom base URL only (API key from env)
57
- console.log("\n4️⃣ Using custom base URL only:");
58
- const customUrlClient = createPerplexityClient(
59
- undefined,
60
- "https://api.perplexity.ai"
61
- );
62
-
63
- const response4 = await customUrlClient.createChatCompletion({
64
- model: "sonar-pro",
65
- messages: [
66
- {
67
- role: "user",
68
- content: "Hello from custom URL configuration!",
69
- },
70
- ],
71
- });
72
- console.log("✅ Custom URL response:", response4.choices[0]?.message?.content?.substring(0, 50) + "...");
73
-
74
- console.log("\n🎉 All configuration examples completed successfully!");
75
- console.log("📊 All requests tracked by middleware regardless of configuration");
76
-
77
- } catch (error) {
78
- console.error("❌ Error:", error);
79
- }
80
- }
81
-
82
- customConfigExample();
1
+ import { createPerplexityClient } from "@revenium/perplexity";
2
+
3
+ async function customConfigExample() {
4
+ console.log("\n⚙️ Perplexity AI - Custom Configuration Example");
5
+ console.log("=".repeat(50));
6
+
7
+ try {
8
+ // Example 1: Using environment variables (default)
9
+ console.log("\n1️⃣ Using environment variables:");
10
+ const defaultClient = createPerplexityClient();
11
+
12
+ const response1 = await defaultClient.createChatCompletion({
13
+ model: "sonar-pro",
14
+ messages: [
15
+ {
16
+ role: "user",
17
+ content: "Hello from default configuration!",
18
+ },
19
+ ],
20
+ });
21
+ console.log("✅ Default config response:", response1.choices[0]?.message?.content?.substring(0, 50) + "...");
22
+
23
+ // Example 2: Custom API key only
24
+ console.log("\n2️⃣ Using custom API key:");
25
+ const customKeyClient = createPerplexityClient(process.env.PERPLEXITY_API_KEY);
26
+
27
+ const response2 = await customKeyClient.createChatCompletion({
28
+ model: "sonar-pro",
29
+ messages: [
30
+ {
31
+ role: "user",
32
+ content: "Hello from custom API key configuration!",
33
+ },
34
+ ],
35
+ });
36
+ console.log("✅ Custom key response:", response2.choices[0]?.message?.content?.substring(0, 50) + "...");
37
+
38
+ // Example 3: Custom API key and base URL
39
+ console.log("\n3️⃣ Using custom API key and base URL:");
40
+ const fullCustomClient = createPerplexityClient(
41
+ process.env.PERPLEXITY_API_KEY,
42
+ "https://api.perplexity.ai"
43
+ );
44
+
45
+ const response3 = await fullCustomClient.createChatCompletion({
46
+ model: "sonar-pro",
47
+ messages: [
48
+ {
49
+ role: "user",
50
+ content: "Hello from fully custom configuration!",
51
+ },
52
+ ],
53
+ });
54
+ console.log("✅ Full custom response:", response3.choices[0]?.message?.content?.substring(0, 50) + "...");
55
+
56
+ // Example 4: Custom base URL only (API key from env)
57
+ console.log("\n4️⃣ Using custom base URL only:");
58
+ const customUrlClient = createPerplexityClient(
59
+ undefined,
60
+ "https://api.perplexity.ai"
61
+ );
62
+
63
+ const response4 = await customUrlClient.createChatCompletion({
64
+ model: "sonar-pro",
65
+ messages: [
66
+ {
67
+ role: "user",
68
+ content: "Hello from custom URL configuration!",
69
+ },
70
+ ],
71
+ });
72
+ console.log("✅ Custom URL response:", response4.choices[0]?.message?.content?.substring(0, 50) + "...");
73
+
74
+ console.log("\n🎉 All configuration examples completed successfully!");
75
+ console.log("📊 All requests tracked by middleware regardless of configuration");
76
+
77
+ } catch (error) {
78
+ console.error("❌ Error:", error);
79
+ }
80
+ }
81
+
82
+ customConfigExample();
@@ -1,94 +1,94 @@
1
- import { createPerplexityClient } from "@revenium/perplexity";
2
-
3
- async function metadataExample() {
4
- console.log("\n📊 Perplexity AI - Metadata Tracking Example");
5
- console.log("=".repeat(50));
6
-
7
- try {
8
- // Create client instance
9
- const client = createPerplexityClient();
10
-
11
- // Example 1: Basic metadata
12
- console.log("\n1️⃣ Basic metadata tracking:");
13
- const basicResponse = await client.createChatCompletion({
14
- model: "sonar-pro",
15
- messages: [
16
- {
17
- role: "user",
18
- content: "Analyze this quarterly report for key insights",
19
- },
20
- ],
21
- usageMetadata: {
22
- traceId: "conv-28a7e9d4",
23
- taskType: "document-analysis",
24
- subscriberEmail: "user@example.com",
25
- organizationId: "acme-corp",
26
- subscriptionId: "premium-plan",
27
- },
28
- });
29
-
30
- console.log(
31
- "✅ Response:",
32
- basicResponse.choices[0]?.message?.content?.substring(0, 100) + "..."
33
- );
34
-
35
- // Example 2: Advanced metadata
36
- console.log("\n2️⃣ Advanced metadata tracking:");
37
- const advancedResponse = await client.createChatCompletion({
38
- model: "sonar-pro",
39
- messages: [
40
- {
41
- role: "user",
42
- content: "What are the latest developments in AI?",
43
- },
44
- ],
45
- usageMetadata: {
46
- traceId: "conv-advanced-123",
47
- taskType: "research",
48
- subscriberId: "user-12345",
49
- subscriberCredentialName: "api-key-1",
50
- productId: "business-intelligence",
51
- agent: "research-assistant-v2",
52
- responseQualityScore: 0.95,
53
- },
54
- });
55
-
56
- console.log(
57
- "✅ Response:",
58
- advancedResponse.choices[0]?.message?.content?.substring(0, 100) + "..."
59
- );
60
-
61
- // Example 3: Streaming with metadata
62
- console.log("\n3️⃣ Streaming with metadata:");
63
- const stream = await client.createStreamingChatCompletion({
64
- model: "sonar-pro",
65
- messages: [
66
- {
67
- role: "user",
68
- content: "Write a creative story about AI",
69
- },
70
- ],
71
- usageMetadata: {
72
- traceId: "conv-streaming-456",
73
- taskType: "creative-writing",
74
- organizationId: "creative-studio",
75
- agent: "story-generator",
76
- },
77
- });
78
-
79
- console.log("📝 Streaming response with metadata:");
80
- for await (const chunk of stream) {
81
- const content = chunk.choices[0]?.delta?.content;
82
- if (content) {
83
- process.stdout.write(content);
84
- }
85
- }
86
-
87
- console.log("\n\n🎉 All metadata examples completed successfully!");
88
- console.log("📊 All usage tracked with custom metadata");
89
- } catch (error) {
90
- console.error("❌ Error:", error);
91
- }
92
- }
93
-
94
- metadataExample();
1
+ import { createPerplexityClient } from "@revenium/perplexity";
2
+
3
+ async function metadataExample() {
4
+ console.log("\n📊 Perplexity AI - Metadata Tracking Example");
5
+ console.log("=".repeat(50));
6
+
7
+ try {
8
+ // Create client instance
9
+ const client = createPerplexityClient();
10
+
11
+ // Example 1: Basic metadata
12
+ console.log("\n1️⃣ Basic metadata tracking:");
13
+ const basicResponse = await client.createChatCompletion({
14
+ model: "sonar-pro",
15
+ messages: [
16
+ {
17
+ role: "user",
18
+ content: "Analyze this quarterly report for key insights",
19
+ },
20
+ ],
21
+ usageMetadata: {
22
+ traceId: "conv-28a7e9d4",
23
+ taskType: "document-analysis",
24
+ subscriberEmail: "user@example.com",
25
+ organizationId: "acme-corp",
26
+ subscriptionId: "premium-plan",
27
+ },
28
+ });
29
+
30
+ console.log(
31
+ "✅ Response:",
32
+ basicResponse.choices[0]?.message?.content?.substring(0, 100) + "..."
33
+ );
34
+
35
+ // Example 2: Advanced metadata
36
+ console.log("\n2️⃣ Advanced metadata tracking:");
37
+ const advancedResponse = await client.createChatCompletion({
38
+ model: "sonar-pro",
39
+ messages: [
40
+ {
41
+ role: "user",
42
+ content: "What are the latest developments in AI?",
43
+ },
44
+ ],
45
+ usageMetadata: {
46
+ traceId: "conv-advanced-123",
47
+ taskType: "research",
48
+ subscriberId: "user-12345",
49
+ subscriberCredentialName: "api-key-1",
50
+ productId: "business-intelligence",
51
+ agent: "research-assistant-v2",
52
+ responseQualityScore: 0.95,
53
+ },
54
+ });
55
+
56
+ console.log(
57
+ "✅ Response:",
58
+ advancedResponse.choices[0]?.message?.content?.substring(0, 100) + "..."
59
+ );
60
+
61
+ // Example 3: Streaming with metadata
62
+ console.log("\n3️⃣ Streaming with metadata:");
63
+ const stream = await client.createStreamingChatCompletion({
64
+ model: "sonar-pro",
65
+ messages: [
66
+ {
67
+ role: "user",
68
+ content: "Write a creative story about AI",
69
+ },
70
+ ],
71
+ usageMetadata: {
72
+ traceId: "conv-streaming-456",
73
+ taskType: "creative-writing",
74
+ organizationId: "creative-studio",
75
+ agent: "story-generator",
76
+ },
77
+ });
78
+
79
+ console.log("📝 Streaming response with metadata:");
80
+ for await (const chunk of stream) {
81
+ const content = chunk.choices[0]?.delta?.content;
82
+ if (content) {
83
+ process.stdout.write(content);
84
+ }
85
+ }
86
+
87
+ console.log("\n\n🎉 All metadata examples completed successfully!");
88
+ console.log("📊 All usage tracked with custom metadata");
89
+ } catch (error) {
90
+ console.error("❌ Error:", error);
91
+ }
92
+ }
93
+
94
+ metadataExample();
@@ -1,62 +1,62 @@
1
- import { createPerplexityClient } from "@revenium/perplexity";
2
-
3
- async function multipleModelsExample() {
4
- console.log("\n🔧 Perplexity AI - Multiple Models Example");
5
- console.log("=".repeat(50));
6
-
7
- try {
8
- // Create client instance
9
- const client = createPerplexityClient();
10
-
11
- // Test different prompts with the same model
12
- const testCases = [
13
- {
14
- name: "sonar-pro (Quantum Computing)",
15
- description: "Latest and most capable model",
16
- prompt: "Explain quantum computing in simple terms",
17
- },
18
- {
19
- name: "sonar-pro (AI Developments)",
20
- description: "Latest and most capable model",
21
- prompt: "What are the latest developments in AI?",
22
- },
23
- {
24
- name: "sonar-pro (Weather)",
25
- description: "Latest and most capable model",
26
- prompt: "What is the weather like today?",
27
- },
28
- ];
29
-
30
- for (const testCase of testCases) {
31
- console.log(`\n🤖 Testing: ${testCase.name}`);
32
- console.log(`📝 Description: ${testCase.description}`);
33
- console.log("-".repeat(40));
34
-
35
- try {
36
- const response = await client.createChatCompletion({
37
- model: "sonar-pro",
38
- messages: [
39
- {
40
- role: "user",
41
- content: testCase.prompt,
42
- },
43
- ],
44
- });
45
-
46
- console.log("✅ Response:");
47
- console.log(response.choices[0]?.message?.content?.substring(0, 200) + "...");
48
- console.log("📊 Usage tracked by middleware");
49
-
50
- } catch (error) {
51
- console.log(`❌ Error with ${testCase.name}:`, error);
52
- }
53
- }
54
-
55
- console.log("\n🎉 Multiple models example completed!");
56
-
57
- } catch (error) {
58
- console.error("❌ Error:", error);
59
- }
60
- }
61
-
62
- multipleModelsExample();
1
+ import { createPerplexityClient } from "@revenium/perplexity";
2
+
3
+ async function multipleModelsExample() {
4
+ console.log("\n🔧 Perplexity AI - Multiple Models Example");
5
+ console.log("=".repeat(50));
6
+
7
+ try {
8
+ // Create client instance
9
+ const client = createPerplexityClient();
10
+
11
+ // Test different prompts with the same model
12
+ const testCases = [
13
+ {
14
+ name: "sonar-pro (Quantum Computing)",
15
+ description: "Latest and most capable model",
16
+ prompt: "Explain quantum computing in simple terms",
17
+ },
18
+ {
19
+ name: "sonar-pro (AI Developments)",
20
+ description: "Latest and most capable model",
21
+ prompt: "What are the latest developments in AI?",
22
+ },
23
+ {
24
+ name: "sonar-pro (Weather)",
25
+ description: "Latest and most capable model",
26
+ prompt: "What is the weather like today?",
27
+ },
28
+ ];
29
+
30
+ for (const testCase of testCases) {
31
+ console.log(`\n🤖 Testing: ${testCase.name}`);
32
+ console.log(`📝 Description: ${testCase.description}`);
33
+ console.log("-".repeat(40));
34
+
35
+ try {
36
+ const response = await client.createChatCompletion({
37
+ model: "sonar-pro",
38
+ messages: [
39
+ {
40
+ role: "user",
41
+ content: testCase.prompt,
42
+ },
43
+ ],
44
+ });
45
+
46
+ console.log("✅ Response:");
47
+ console.log(response.choices[0]?.message?.content?.substring(0, 200) + "...");
48
+ console.log("📊 Usage tracked by middleware");
49
+
50
+ } catch (error) {
51
+ console.log(`❌ Error with ${testCase.name}:`, error);
52
+ }
53
+ }
54
+
55
+ console.log("\n🎉 Multiple models example completed!");
56
+
57
+ } catch (error) {
58
+ console.error("❌ Error:", error);
59
+ }
60
+ }
61
+
62
+ multipleModelsExample();
@@ -1,73 +1,73 @@
1
- import { OpenAI } from "openai";
2
-
3
- // Import middleware to activate automatic tracking
4
- import "@revenium/perplexity";
5
-
6
- async function openaiClientExample() {
7
- console.log("\n🤖 Perplexity AI - OpenAI Client Example");
8
- console.log("=".repeat(50));
9
-
10
- try {
11
- // Create OpenAI client configured for Perplexity (like in basic example)
12
- const client = new OpenAI({
13
- apiKey: process.env.PERPLEXITY_API_KEY,
14
- baseURL: "https://api.perplexity.ai",
15
- });
16
-
17
- console.log("✅ OpenAI client configured for Perplexity AI");
18
- console.log("📊 Middleware automatically tracking all requests");
19
-
20
- // Test different types of requests
21
- console.log("\n1️⃣ Basic chat completion:");
22
- const basicResponse = await client.chat.completions.create({
23
- model: "sonar-pro",
24
- messages: [
25
- {
26
- role: "user",
27
- content: "What is the meaning of life, the universe and everything?",
28
- },
29
- ],
30
- });
31
- console.log("✅ Response:", basicResponse.choices[0]?.message?.content?.substring(0, 100) + "...");
32
-
33
- console.log("\n2️⃣ Streaming chat completion:");
34
- const stream = await client.chat.completions.create({
35
- model: "sonar-pro",
36
- messages: [
37
- {
38
- role: "user",
39
- content: "Write a short poem about AI",
40
- },
41
- ],
42
- stream: true,
43
- });
44
-
45
- console.log("📝 Streaming response:");
46
- for await (const chunk of stream) {
47
- const content = chunk.choices[0]?.delta?.content;
48
- if (content) {
49
- process.stdout.write(content);
50
- }
51
- }
52
-
53
- console.log("\n\n3️⃣ Different prompt:");
54
- const differentResponse = await client.chat.completions.create({
55
- model: "sonar-pro",
56
- messages: [
57
- {
58
- role: "user",
59
- content: "Explain machine learning in one sentence",
60
- },
61
- ],
62
- });
63
- console.log("✅ Response:", differentResponse.choices[0]?.message?.content);
64
-
65
- console.log("\n🎉 All examples completed successfully!");
66
- console.log("📊 All usage automatically tracked by middleware");
67
-
68
- } catch (error) {
69
- console.error("❌ Error:", error);
70
- }
71
- }
72
-
73
- openaiClientExample();
1
+ import { OpenAI } from "openai";
2
+
3
+ // Import middleware to activate automatic tracking
4
+ import "@revenium/perplexity";
5
+
6
+ async function openaiClientExample() {
7
+ console.log("\n🤖 Perplexity AI - OpenAI Client Example");
8
+ console.log("=".repeat(50));
9
+
10
+ try {
11
+ // Create OpenAI client configured for Perplexity (like in basic example)
12
+ const client = new OpenAI({
13
+ apiKey: process.env.PERPLEXITY_API_KEY,
14
+ baseURL: "https://api.perplexity.ai",
15
+ });
16
+
17
+ console.log("✅ OpenAI client configured for Perplexity AI");
18
+ console.log("📊 Middleware automatically tracking all requests");
19
+
20
+ // Test different types of requests
21
+ console.log("\n1️⃣ Basic chat completion:");
22
+ const basicResponse = await client.chat.completions.create({
23
+ model: "sonar-pro",
24
+ messages: [
25
+ {
26
+ role: "user",
27
+ content: "What is the meaning of life, the universe and everything?",
28
+ },
29
+ ],
30
+ });
31
+ console.log("✅ Response:", basicResponse.choices[0]?.message?.content?.substring(0, 100) + "...");
32
+
33
+ console.log("\n2️⃣ Streaming chat completion:");
34
+ const stream = await client.chat.completions.create({
35
+ model: "sonar-pro",
36
+ messages: [
37
+ {
38
+ role: "user",
39
+ content: "Write a short poem about AI",
40
+ },
41
+ ],
42
+ stream: true,
43
+ });
44
+
45
+ console.log("📝 Streaming response:");
46
+ for await (const chunk of stream) {
47
+ const content = chunk.choices[0]?.delta?.content;
48
+ if (content) {
49
+ process.stdout.write(content);
50
+ }
51
+ }
52
+
53
+ console.log("\n\n3️⃣ Different prompt:");
54
+ const differentResponse = await client.chat.completions.create({
55
+ model: "sonar-pro",
56
+ messages: [
57
+ {
58
+ role: "user",
59
+ content: "Explain machine learning in one sentence",
60
+ },
61
+ ],
62
+ });
63
+ console.log("✅ Response:", differentResponse.choices[0]?.message?.content);
64
+
65
+ console.log("\n🎉 All examples completed successfully!");
66
+ console.log("📊 All usage automatically tracked by middleware");
67
+
68
+ } catch (error) {
69
+ console.error("❌ Error:", error);
70
+ }
71
+ }
72
+
73
+ openaiClientExample();