@revenium/perplexity 2.0.6 → 2.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.
Files changed (49) hide show
  1. package/.env.example +10 -0
  2. package/CHANGELOG.md +82 -113
  3. package/LICENSE +21 -21
  4. package/README.md +408 -292
  5. package/SECURITY.md +34 -34
  6. package/dist/cjs/core/config/index.js +0 -1
  7. package/dist/cjs/core/config/index.js.map +1 -1
  8. package/dist/cjs/core/config/manager.js +0 -1
  9. package/dist/cjs/core/config/manager.js.map +1 -1
  10. package/dist/cjs/core/providers/detector.js +0 -2
  11. package/dist/cjs/core/providers/detector.js.map +1 -1
  12. package/dist/cjs/core/providers/index.js +0 -1
  13. package/dist/cjs/core/providers/index.js.map +1 -1
  14. package/dist/cjs/index.js +1 -34
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/utils/stop-reason-mapper.js +18 -20
  17. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
  18. package/dist/esm/core/config/index.js +3 -4
  19. package/dist/esm/core/config/index.js.map +1 -1
  20. package/dist/esm/core/config/manager.js +0 -1
  21. package/dist/esm/core/config/manager.js.map +1 -1
  22. package/dist/esm/core/providers/detector.js +0 -2
  23. package/dist/esm/core/providers/detector.js.map +1 -1
  24. package/dist/esm/core/providers/index.js +0 -1
  25. package/dist/esm/core/providers/index.js.map +1 -1
  26. package/dist/esm/index.js +1 -34
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/esm/utils/stop-reason-mapper.js +18 -20
  29. package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
  30. package/dist/types/core/config/index.d.ts +3 -4
  31. package/dist/types/core/config/index.d.ts.map +1 -1
  32. package/dist/types/core/config/manager.d.ts +0 -1
  33. package/dist/types/core/config/manager.d.ts.map +1 -1
  34. package/dist/types/core/providers/detector.d.ts +0 -2
  35. package/dist/types/core/providers/detector.d.ts.map +1 -1
  36. package/dist/types/core/providers/index.d.ts +0 -1
  37. package/dist/types/core/providers/index.d.ts.map +1 -1
  38. package/dist/types/index.d.ts +2 -32
  39. package/dist/types/index.d.ts.map +1 -1
  40. package/dist/types/types/index.d.ts +1 -1
  41. package/dist/types/utils/stop-reason-mapper.d.ts +1 -3
  42. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
  43. package/examples/README.md +274 -226
  44. package/examples/advanced.ts +123 -123
  45. package/examples/basic.ts +45 -45
  46. package/examples/getting_started.ts +41 -41
  47. package/examples/metadata.ts +68 -68
  48. package/examples/stream.ts +53 -53
  49. package/package.json +80 -72
@@ -1,123 +1,123 @@
1
- /**
2
- * Advanced Example
3
- * Demonstrates advanced features like system prompts, conversation history,
4
- * and different model parameters with Revenium Perplexity middleware.
5
- */
6
-
7
- import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
8
-
9
- async function main() {
10
- // Initialize middleware
11
- Initialize();
12
-
13
- // Get client
14
- const client = GetClient();
15
-
16
- // Metadata for tracking
17
- const metadata: UsageMetadata = {
18
- organizationId: "org-advanced-demo",
19
- productId: "prod-perplexity-advanced",
20
- taskType: "multi-turn-conversation",
21
- subscriber: {
22
- id: "user-advanced-001",
23
- email: "advanced@example.com",
24
- },
25
- };
26
-
27
- console.log("=== Advanced Perplexity Example ===\n");
28
-
29
- // Example 1: Using system prompt and conversation history
30
- console.log("1. Multi-turn conversation with system prompt:\n");
31
-
32
- const response1 = await client
33
- .chat()
34
- .completions()
35
- .create(
36
- {
37
- model: "sonar-pro",
38
- messages: [
39
- {
40
- role: "system",
41
- content:
42
- "You are a helpful AI assistant that provides concise, accurate answers.",
43
- },
44
- {
45
- role: "user",
46
- content: "What is the capital of France?",
47
- },
48
- {
49
- role: "assistant",
50
- content: "The capital of France is Paris.",
51
- },
52
- {
53
- role: "user",
54
- content: "What is its population?",
55
- },
56
- ],
57
- max_tokens: 500,
58
- temperature: 0.7,
59
- },
60
- metadata
61
- );
62
-
63
- console.log(`Assistant: ${response1.choices[0].message.content}\n`);
64
- console.log(`Tokens used: ${response1.usage?.total_tokens}\n`);
65
-
66
- // Example 2: Using different temperature for creative responses
67
- console.log("2. Creative response with higher temperature:\n");
68
-
69
- const response2 = await client
70
- .chat()
71
- .completions()
72
- .create(
73
- {
74
- model: "sonar-pro",
75
- messages: [
76
- {
77
- role: "user",
78
- content:
79
- "Write a creative tagline for an AI company in 10 words or less.",
80
- },
81
- ],
82
- max_tokens: 100,
83
- temperature: 1.0, // Higher temperature for more creativity
84
- },
85
- {
86
- ...metadata,
87
- taskType: "creative-generation",
88
- }
89
- );
90
-
91
- console.log(`Assistant: ${response2.choices[0].message.content}\n`);
92
-
93
- // Example 3: Using sonar-reasoning for complex reasoning tasks
94
- console.log("3. Complex reasoning with sonar-reasoning model:\n");
95
-
96
- const response3 = await client
97
- .chat()
98
- .completions()
99
- .create(
100
- {
101
- model: "sonar-reasoning",
102
- messages: [
103
- {
104
- role: "user",
105
- content:
106
- "If a train travels 120 km in 2 hours, and then 180 km in 3 hours, what is its average speed for the entire journey?",
107
- },
108
- ],
109
- max_tokens: 500,
110
- },
111
- {
112
- ...metadata,
113
- taskType: "reasoning",
114
- }
115
- );
116
-
117
- console.log(`Assistant: ${response3.choices[0].message.content}\n`);
118
- console.log(`Tokens used: ${response3.usage?.total_tokens}\n`);
119
-
120
- console.log("\nAll usage data sent to Revenium! Check your dashboard");
121
- }
122
-
123
- main().catch(console.error);
1
+ /**
2
+ * Advanced Example
3
+ * Demonstrates advanced features like system prompts, conversation history,
4
+ * and different model parameters with Revenium Perplexity middleware.
5
+ */
6
+
7
+ import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
8
+
9
+ async function main() {
10
+ // Initialize middleware
11
+ Initialize();
12
+
13
+ // Get client
14
+ const client = GetClient();
15
+
16
+ // Metadata for tracking
17
+ const metadata: UsageMetadata = {
18
+ organizationId: "org-advanced-demo",
19
+ productId: "prod-perplexity-advanced",
20
+ taskType: "multi-turn-conversation",
21
+ subscriber: {
22
+ id: "user-advanced-001",
23
+ email: "advanced@example.com",
24
+ },
25
+ };
26
+
27
+ console.log("=== Advanced Perplexity Example ===\n");
28
+
29
+ // Example 1: Using system prompt and conversation history
30
+ console.log("1. Multi-turn conversation with system prompt:\n");
31
+
32
+ const response1 = await client
33
+ .chat()
34
+ .completions()
35
+ .create(
36
+ {
37
+ model: "sonar-pro",
38
+ messages: [
39
+ {
40
+ role: "system",
41
+ content:
42
+ "You are a helpful AI assistant that provides concise, accurate answers.",
43
+ },
44
+ {
45
+ role: "user",
46
+ content: "What is the capital of France?",
47
+ },
48
+ {
49
+ role: "assistant",
50
+ content: "The capital of France is Paris.",
51
+ },
52
+ {
53
+ role: "user",
54
+ content: "What is its population?",
55
+ },
56
+ ],
57
+ max_tokens: 500,
58
+ temperature: 0.7,
59
+ },
60
+ metadata
61
+ );
62
+
63
+ console.log(`Assistant: ${response1.choices[0].message.content}\n`);
64
+ console.log(`Tokens used: ${response1.usage?.total_tokens}\n`);
65
+
66
+ // Example 2: Using different temperature for creative responses
67
+ console.log("2. Creative response with higher temperature:\n");
68
+
69
+ const response2 = await client
70
+ .chat()
71
+ .completions()
72
+ .create(
73
+ {
74
+ model: "sonar-pro",
75
+ messages: [
76
+ {
77
+ role: "user",
78
+ content:
79
+ "Write a creative tagline for an AI company in 10 words or less.",
80
+ },
81
+ ],
82
+ max_tokens: 100,
83
+ temperature: 1.0, // Higher temperature for more creativity
84
+ },
85
+ {
86
+ ...metadata,
87
+ taskType: "creative-generation",
88
+ }
89
+ );
90
+
91
+ console.log(`Assistant: ${response2.choices[0].message.content}\n`);
92
+
93
+ // Example 3: Using sonar-reasoning for complex reasoning tasks
94
+ console.log("3. Complex reasoning with sonar-reasoning model:\n");
95
+
96
+ const response3 = await client
97
+ .chat()
98
+ .completions()
99
+ .create(
100
+ {
101
+ model: "sonar-reasoning",
102
+ messages: [
103
+ {
104
+ role: "user",
105
+ content:
106
+ "If a train travels 120 km in 2 hours, and then 180 km in 3 hours, what is its average speed for the entire journey?",
107
+ },
108
+ ],
109
+ max_tokens: 500,
110
+ },
111
+ {
112
+ ...metadata,
113
+ taskType: "reasoning",
114
+ }
115
+ );
116
+
117
+ console.log(`Assistant: ${response3.choices[0].message.content}\n`);
118
+ console.log(`Tokens used: ${response3.usage?.total_tokens}\n`);
119
+
120
+ console.log("\nAll usage data sent to Revenium! Check your dashboard");
121
+ }
122
+
123
+ main().catch(console.error);
package/examples/basic.ts CHANGED
@@ -1,45 +1,45 @@
1
- /**
2
- * Basic Example
3
- * Demonstrates basic usage of Revenium Perplexity middleware.
4
- */
5
-
6
- import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
7
-
8
- async function main() {
9
- // Initialize middleware
10
- Initialize();
11
-
12
- // Get client
13
- const client = GetClient();
14
- const metadata: UsageMetadata = {
15
- organizationId: "org-basic-demo",
16
- productId: "prod-perplexity-basic",
17
- };
18
-
19
- // Create chat completion
20
- const response = await client
21
- .chat()
22
- .completions()
23
- .create(
24
- {
25
- model: "sonar-pro",
26
- messages: [
27
- {
28
- role: "user",
29
- content:
30
- "Say hello in Spanish and explain why Spanish is a beautiful language in 2-3 sentences.",
31
- },
32
- ],
33
- max_tokens: 1000,
34
- },
35
- metadata
36
- );
37
-
38
- // Display response
39
- if (response.choices.length > 0) {
40
- console.log(`Assistant: ${response.choices[0].message.content}`);
41
- }
42
- console.log("\nUsage data sent to Revenium! Check your dashboard");
43
- }
44
-
45
- main().catch(console.error);
1
+ /**
2
+ * Basic Example
3
+ * Demonstrates basic usage of Revenium Perplexity middleware.
4
+ */
5
+
6
+ import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
7
+
8
+ async function main() {
9
+ // Initialize middleware
10
+ Initialize();
11
+
12
+ // Get client
13
+ const client = GetClient();
14
+ const metadata: UsageMetadata = {
15
+ organizationId: "org-basic-demo",
16
+ productId: "prod-perplexity-basic",
17
+ };
18
+
19
+ // Create chat completion
20
+ const response = await client
21
+ .chat()
22
+ .completions()
23
+ .create(
24
+ {
25
+ model: "sonar-pro",
26
+ messages: [
27
+ {
28
+ role: "user",
29
+ content:
30
+ "Say hello in Spanish and explain why Spanish is a beautiful language in 2-3 sentences.",
31
+ },
32
+ ],
33
+ max_tokens: 1000,
34
+ },
35
+ metadata
36
+ );
37
+
38
+ // Display response
39
+ if (response.choices.length > 0) {
40
+ console.log(`Assistant: ${response.choices[0].message.content}`);
41
+ }
42
+ console.log("\nUsage data sent to Revenium! Check your dashboard");
43
+ }
44
+
45
+ main().catch(console.error);
@@ -1,41 +1,41 @@
1
- /**
2
- * Getting Started with Revenium Perplexity Middleware
3
- *
4
- * This example shows the new Initialize/GetClient pattern.
5
- */
6
-
7
- import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
8
-
9
- async function main() {
10
- Initialize();
11
-
12
- // Get client
13
- const client = GetClient();
14
- const metadata: UsageMetadata = {
15
- organizationId: "org-getting-started",
16
- productId: "prod-perplexity-getting-started",
17
- };
18
-
19
- // Create a simple chat completion
20
- const response = await client
21
- .chat()
22
- .completions()
23
- .create(
24
- {
25
- model: "sonar-pro",
26
- messages: [
27
- {
28
- role: "user",
29
- content: "What is artificial intelligence?",
30
- },
31
- ],
32
- max_tokens: 500,
33
- },
34
- metadata
35
- );
36
-
37
- console.log("Assistant:", response.choices[0]?.message?.content);
38
- console.log("\nUsage:", response.usage);
39
- }
40
-
41
- main().catch(console.error);
1
+ /**
2
+ * Getting Started with Revenium Perplexity Middleware
3
+ *
4
+ * This example shows the new Initialize/GetClient pattern.
5
+ */
6
+
7
+ import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
8
+
9
+ async function main() {
10
+ Initialize();
11
+
12
+ // Get client
13
+ const client = GetClient();
14
+ const metadata: UsageMetadata = {
15
+ organizationId: "org-getting-started",
16
+ productId: "prod-perplexity-getting-started",
17
+ };
18
+
19
+ // Create a simple chat completion
20
+ const response = await client
21
+ .chat()
22
+ .completions()
23
+ .create(
24
+ {
25
+ model: "sonar-pro",
26
+ messages: [
27
+ {
28
+ role: "user",
29
+ content: "What is artificial intelligence?",
30
+ },
31
+ ],
32
+ max_tokens: 500,
33
+ },
34
+ metadata
35
+ );
36
+
37
+ console.log("Assistant:", response.choices[0]?.message?.content);
38
+ console.log("\nUsage:", response.usage);
39
+ }
40
+
41
+ main().catch(console.error);
@@ -1,68 +1,68 @@
1
- /**
2
- * Metadata Example
3
- * Demonstrates advanced metadata usage with Revenium Perplexity middleware.
4
- */
5
-
6
- import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
7
-
8
- async function main() {
9
- // Initialize middleware
10
- Initialize();
11
-
12
- // Get client
13
- const client = GetClient();
14
-
15
- // Optional metadata for advanced reporting, lineage tracking, and cost allocation
16
- const metadata: UsageMetadata = {
17
- // Organization & billing
18
- organizationId: "org-metadata-demo",
19
- subscriptionId: "plan-premium-2025",
20
-
21
- // Product & task tracking
22
- productId: "ai-assistant",
23
- taskType: "explanation-request",
24
- agent: "perplexity-metadata-chat-node",
25
-
26
- // Session tracking
27
- traceId: "session-" + Date.now(),
28
-
29
- // Quality metrics
30
- responseQualityScore: 0.95, // 0.0-1.0 scale
31
-
32
- // User tracking
33
- subscriber: {
34
- id: "user-12345",
35
- email: "developer@company.com",
36
- credential: {
37
- name: "api-key-prod",
38
- value: "key-abc-123",
39
- },
40
- },
41
- };
42
-
43
- const response = await client
44
- .chat()
45
- .completions()
46
- .create(
47
- {
48
- model: "sonar-pro",
49
- messages: [
50
- {
51
- role: "user",
52
- content:
53
- "Say hello in Spanish and explain why Spanish is a beautiful language in 2-3 sentences.",
54
- },
55
- ],
56
- max_tokens: 1000,
57
- },
58
- metadata
59
- );
60
-
61
- // Display response
62
- if (response.choices.length > 0) {
63
- console.log(`Assistant: ${response.choices[0].message.content}`);
64
- }
65
- console.log("\nUsage data sent to Revenium! Check your dashboard");
66
- }
67
-
68
- main().catch(console.error);
1
+ /**
2
+ * Metadata Example
3
+ * Demonstrates advanced metadata usage with Revenium Perplexity middleware.
4
+ */
5
+
6
+ import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
7
+
8
+ async function main() {
9
+ // Initialize middleware
10
+ Initialize();
11
+
12
+ // Get client
13
+ const client = GetClient();
14
+
15
+ // Optional metadata for advanced reporting, lineage tracking, and cost allocation
16
+ const metadata: UsageMetadata = {
17
+ // Organization & billing
18
+ organizationId: "org-metadata-demo",
19
+ subscriptionId: "plan-premium-2025",
20
+
21
+ // Product & task tracking
22
+ productId: "ai-assistant",
23
+ taskType: "explanation-request",
24
+ agent: "perplexity-metadata-chat-node",
25
+
26
+ // Session tracking
27
+ traceId: "session-" + Date.now(),
28
+
29
+ // Quality metrics
30
+ responseQualityScore: 0.95, // 0.0-1.0 scale
31
+
32
+ // User tracking
33
+ subscriber: {
34
+ id: "user-12345",
35
+ email: "developer@company.com",
36
+ credential: {
37
+ name: "api-key-prod",
38
+ value: "key-abc-123",
39
+ },
40
+ },
41
+ };
42
+
43
+ const response = await client
44
+ .chat()
45
+ .completions()
46
+ .create(
47
+ {
48
+ model: "sonar-pro",
49
+ messages: [
50
+ {
51
+ role: "user",
52
+ content:
53
+ "Say hello in Spanish and explain why Spanish is a beautiful language in 2-3 sentences.",
54
+ },
55
+ ],
56
+ max_tokens: 1000,
57
+ },
58
+ metadata
59
+ );
60
+
61
+ // Display response
62
+ if (response.choices.length > 0) {
63
+ console.log(`Assistant: ${response.choices[0].message.content}`);
64
+ }
65
+ console.log("\nUsage data sent to Revenium! Check your dashboard");
66
+ }
67
+
68
+ main().catch(console.error);
@@ -1,53 +1,53 @@
1
- /**
2
- * Streaming Example with Revenium Perplexity Middleware (New Go-Aligned API)
3
- *
4
- * This example shows how to use streaming with the new API.
5
- */
6
-
7
- import { Initialize, GetClient } from "@revenium/perplexity";
8
-
9
- async function main() {
10
- // Initialize from environment variables
11
- Initialize();
12
-
13
- // Get client
14
- const client = GetClient();
15
-
16
- console.log("Streaming response:\n");
17
-
18
- // Create a streaming chat completion
19
- const stream = await client
20
- .chat()
21
- .completions()
22
- .createStreaming(
23
- {
24
- model: "sonar-pro",
25
- messages: [
26
- {
27
- role: "user",
28
- content: "Write a short poem about AI",
29
- },
30
- ],
31
- max_tokens: 200,
32
- },
33
- {
34
- // Optional metadata
35
- organizationId: "my-customers-name",
36
- productId: "my-product",
37
- subscriber: {
38
- id: "user-123",
39
- email: "user@example.com",
40
- },
41
- }
42
- );
43
-
44
- // Process stream
45
- for await (const chunk of stream) {
46
- const content = chunk.choices[0]?.delta?.content || "";
47
- process.stdout.write(content);
48
- }
49
-
50
- console.log("\n\nUsage data sent to Revenium! Check your dashboard");
51
- }
52
-
53
- main().catch(console.error);
1
+ /**
2
+ * Streaming Example with Revenium Perplexity Middleware
3
+ *
4
+ * This example shows how to use streaming with the new API.
5
+ */
6
+
7
+ import { Initialize, GetClient } from "@revenium/perplexity";
8
+
9
+ async function main() {
10
+ // Initialize from environment variables
11
+ Initialize();
12
+
13
+ // Get client
14
+ const client = GetClient();
15
+
16
+ console.log("Streaming response:\n");
17
+
18
+ // Create a streaming chat completion
19
+ const stream = await client
20
+ .chat()
21
+ .completions()
22
+ .createStreaming(
23
+ {
24
+ model: "sonar-pro",
25
+ messages: [
26
+ {
27
+ role: "user",
28
+ content: "Write a short poem about AI",
29
+ },
30
+ ],
31
+ max_tokens: 200,
32
+ },
33
+ {
34
+ // Optional metadata
35
+ organizationId: "my-customers-name",
36
+ productId: "my-product",
37
+ subscriber: {
38
+ id: "user-123",
39
+ email: "user@example.com",
40
+ },
41
+ }
42
+ );
43
+
44
+ // Process stream
45
+ for await (const chunk of stream) {
46
+ const content = chunk.choices[0]?.delta?.content || "";
47
+ process.stdout.write(content);
48
+ }
49
+
50
+ console.log("\n\nUsage data sent to Revenium! Check your dashboard");
51
+ }
52
+
53
+ main().catch(console.error);