@revenium/perplexity 1.0.23 ā 1.0.25
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 +391 -162
- package/dist/interfaces/meteringResponse.d.ts +28 -27
- package/dist/interfaces/meteringResponse.js +2 -2
- package/dist/models/Metering.js +21 -18
- package/dist/v1/perplexityV1.service.js +166 -0
- package/dist/v2/perplexityV2.service.js +178 -0
- package/examples/v1/basic.ts +50 -0
- package/examples/v1/chat.ts +40 -0
- package/examples/v1/metadata.ts +49 -0
- package/examples/v1/streaming.ts +44 -0
- package/examples/v2/basic.ts +49 -0
- package/examples/v2/chat.ts +60 -0
- package/examples/v2/metadata.ts +71 -0
- package/examples/v2/streaming.ts +61 -0
- package/package.json +26 -11
- package/playground/v1/basic.js +50 -0
- package/playground/v1/chat.js +46 -0
- package/playground/v1/metadata.js +50 -0
- package/playground/v1/streaming.js +44 -0
- package/playground/v2/basic.js +49 -0
- package/playground/v2/chat.js +72 -0
- package/playground/v2/metadata.js +76 -0
- package/playground/v2/streaming.js +67 -0
- package/src/index.ts +14 -1
- package/src/interfaces/chatCompletionRequest.ts +7 -2
- package/src/interfaces/meteringResponse.ts +1 -0
- package/src/interfaces/perplexityResponse.ts +63 -0
- package/src/interfaces/perplexityStreaming.ts +56 -0
- package/src/models/Metering.ts +8 -1
- package/src/utils/constants/perplexityModels.ts +71 -0
- package/src/v1/perplexityV1.controller.ts +164 -0
- package/src/v1/perplexityV1.service.ts +230 -0
- package/src/v2/perplexityV2.controller.ts +219 -0
- package/src/v2/perplexityV2.service.ts +260 -0
- package/dist/index.js +0 -19
- package/dist/interfaces/chatCompletionRequest.d.ts +0 -9
- package/dist/interfaces/chatCompletionRequest.js +0 -2
- package/dist/interfaces/credential.d.ts +0 -4
- package/dist/interfaces/credential.js +0 -2
- package/dist/interfaces/meteringRequest.d.ts +0 -13
- package/dist/interfaces/meteringRequest.js +0 -2
- package/dist/interfaces/operation.d.ts +0 -4
- package/dist/interfaces/operation.js +0 -8
- package/dist/interfaces/subscriber.d.ts +0 -8
- package/dist/interfaces/subscriber.js +0 -2
- package/dist/interfaces/tokenCounts.d.ts +0 -7
- package/dist/interfaces/tokenCounts.js +0 -2
- package/dist/interfaces/usageMetadata.d.ts +0 -27
- package/dist/interfaces/usageMetadata.js +0 -2
- package/dist/middleware.d.ts +0 -22
- package/dist/middleware.js +0 -129
- package/dist/models/Logger.js +0 -35
- package/dist/models/Metering.d.ts +0 -9
- package/dist/utils/calculateDurationMs.d.ts +0 -1
- package/dist/utils/calculateDurationMs.js +0 -6
- package/dist/utils/constants/constants.d.ts +0 -6
- package/dist/utils/constants/constants.js +0 -11
- package/dist/utils/constants/logLevels.d.ts +0 -1
- package/dist/utils/constants/logLevels.js +0 -4
- package/dist/utils/constants/messages.d.ts +0 -5
- package/dist/utils/constants/messages.js +0 -8
- package/dist/utils/constants/models.d.ts +0 -1
- package/dist/utils/constants/models.js +0 -21
- package/dist/utils/extractTokenCount.d.ts +0 -2
- package/dist/utils/extractTokenCount.js +0 -28
- package/dist/utils/formatTimeStamp.d.ts +0 -1
- package/dist/utils/formatTimeStamp.js +0 -6
- package/dist/utils/generateTransactionId.d.ts +0 -1
- package/dist/utils/generateTransactionId.js +0 -7
- package/dist/utils/index.d.ts +0 -6
- package/dist/utils/index.js +0 -23
- package/dist/utils/loadEnv.d.ts +0 -1
- package/dist/utils/loadEnv.js +0 -7
- package/dist/utils/safeExtract.d.ts +0 -29
- package/dist/utils/safeExtract.js +0 -67
- package/examples/basic.ts +0 -17
- package/examples/chat-completions.ts +0 -22
- package/examples/enhanced.ts +0 -20
- package/examples/metadata.ts +0 -43
- package/examples/streaming.ts +0 -24
- package/playground/basic.js +0 -17
- package/playground/chat-completions.js +0 -22
- package/playground/enhanced.js +0 -20
- package/playground/metadata.js +0 -43
- package/playground/streaming.js +0 -24
- package/src/middleware.ts +0 -157
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PerplexityV2Controller } from "../../src/index";
|
|
2
|
+
|
|
3
|
+
const basicV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log(
|
|
6
|
+
"š Perplexity V2 Basic Example (Enhanced Response Format - OpenAI Responses API)"
|
|
7
|
+
);
|
|
8
|
+
console.log("=".repeat(50));
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const controller = new PerplexityV2Controller();
|
|
12
|
+
|
|
13
|
+
// Basic chat completion with V2 enhanced response
|
|
14
|
+
const result = await controller.createChat([
|
|
15
|
+
"What is artificial intelligence and how does it work?",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
console.log("š Response:");
|
|
19
|
+
console.log(result.choices[0].message.content);
|
|
20
|
+
|
|
21
|
+
console.log("\nš Enhanced Usage Info (V2):");
|
|
22
|
+
console.log(`Prompt tokens: ${result.usage.prompt_tokens}`);
|
|
23
|
+
console.log(`Completion tokens: ${result.usage.completion_tokens}`);
|
|
24
|
+
console.log(`Total tokens: ${result.usage.total_tokens}`);
|
|
25
|
+
|
|
26
|
+
console.log("\nš§ Enhanced Metadata (V2):");
|
|
27
|
+
console.log(`ID: ${result.id}`);
|
|
28
|
+
console.log(`Object: ${result.object}`);
|
|
29
|
+
console.log(`Created: ${new Date(result.created * 1000).toISOString()}`);
|
|
30
|
+
console.log(`Model: ${result.model}`);
|
|
31
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
32
|
+
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
33
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
34
|
+
|
|
35
|
+
// Test V2 enhanced features
|
|
36
|
+
console.log("\nšÆ V2 Enhanced Features:");
|
|
37
|
+
const modelInfo = controller.getModelInfo("sonar-pro");
|
|
38
|
+
console.log(`Enhanced Model Info:`, modelInfo);
|
|
39
|
+
|
|
40
|
+
const recommendation = controller.getRecommendedModel("online-search");
|
|
41
|
+
console.log(
|
|
42
|
+
`Recommended for online search: ${recommendation.model} - ${recommendation.reason}`
|
|
43
|
+
);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error("ā Error:", error);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
basicV2Example();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PerplexityV2Controller } from "../../src/index";
|
|
2
|
+
|
|
3
|
+
const chatV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log("š¬ Perplexity V2 Enhanced Chat Example (OpenAI Responses API)");
|
|
6
|
+
console.log("=".repeat(50));
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const controller = new PerplexityV2Controller();
|
|
10
|
+
|
|
11
|
+
// Multi-turn conversation with V2 enhanced features
|
|
12
|
+
const messages = [
|
|
13
|
+
{ role: "user", content: "What are the latest developments in renewable energy technology?" },
|
|
14
|
+
{ role: "assistant", content: "Recent developments include improved solar panel efficiency, advanced battery storage systems, and offshore wind technology innovations." },
|
|
15
|
+
{ role: "user", content: "Tell me more about the battery storage improvements" }
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const result = await controller.createChat(messages, "sonar-pro");
|
|
19
|
+
|
|
20
|
+
console.log("š V2 Enhanced Chat Response:");
|
|
21
|
+
console.log(result.choices[0].message.content);
|
|
22
|
+
|
|
23
|
+
console.log("\nš V2 Enhanced Usage Statistics:");
|
|
24
|
+
console.log(`Prompt tokens: ${result.usage.prompt_tokens}`);
|
|
25
|
+
console.log(`Completion tokens: ${result.usage.completion_tokens}`);
|
|
26
|
+
console.log(`Total tokens: ${result.usage.total_tokens}`);
|
|
27
|
+
|
|
28
|
+
console.log("\nš§ V2 Enhanced Response Metadata:");
|
|
29
|
+
console.log(`ID: ${result.id}`);
|
|
30
|
+
console.log(`Object: ${result.object}`);
|
|
31
|
+
console.log(`Created: ${new Date(result.created * 1000).toISOString()}`);
|
|
32
|
+
console.log(`Model: ${result.model}`);
|
|
33
|
+
console.log(`Finish Reason: ${result.choices[0].finish_reason}`);
|
|
34
|
+
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
35
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
36
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
37
|
+
|
|
38
|
+
// V2 Enhanced Features
|
|
39
|
+
console.log("\nš V2 Enhanced Model Features:");
|
|
40
|
+
const modelInfo = controller.getModelInfo("sonar-pro");
|
|
41
|
+
console.log(`Enhanced: ${modelInfo.enhanced}`);
|
|
42
|
+
console.log(`Response Format: ${modelInfo.responseFormat}`);
|
|
43
|
+
|
|
44
|
+
const performance = controller.getModelPerformance("sonar-pro");
|
|
45
|
+
console.log(`Estimated Latency: ${performance.estimatedLatency}`);
|
|
46
|
+
console.log(`Cost Tier: ${performance.costTier}`);
|
|
47
|
+
|
|
48
|
+
// Model recommendations
|
|
49
|
+
console.log("\nšÆ V2 Model Recommendations:");
|
|
50
|
+
const onlineRec = controller.getRecommendedModel("online-search");
|
|
51
|
+
const offlineRec = controller.getRecommendedModel("offline-chat");
|
|
52
|
+
console.log(`For online search: ${onlineRec.model} - ${onlineRec.reason}`);
|
|
53
|
+
console.log(`For offline chat: ${offlineRec.model} - ${offlineRec.reason}`);
|
|
54
|
+
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("ā Error:", error);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
chatV2Example();
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { PerplexityV2Controller, IUsageMetadata } from "../../src/index";
|
|
2
|
+
|
|
3
|
+
const metadataV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log("š Perplexity V2 Enhanced Metadata Example (OpenAI Responses API)");
|
|
6
|
+
console.log("=".repeat(50));
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const controller = new PerplexityV2Controller();
|
|
10
|
+
|
|
11
|
+
// Enhanced custom metadata for V2
|
|
12
|
+
const customMetadata: IUsageMetadata = {
|
|
13
|
+
traceId: "trace-v2-67890",
|
|
14
|
+
taskType: "advanced-research",
|
|
15
|
+
subscriberEmail: "researcher@example.com",
|
|
16
|
+
subscriberId: "user-v2-123",
|
|
17
|
+
organizationId: "org-v2-456",
|
|
18
|
+
agent: "perplexity-v2-enhanced",
|
|
19
|
+
responseQualityScore: 0.95,
|
|
20
|
+
operationType: "chat-completion",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const result = await controller.createChat([
|
|
24
|
+
"Provide a comprehensive analysis of the impact of artificial intelligence on modern healthcare systems"
|
|
25
|
+
], "sonar-pro", customMetadata);
|
|
26
|
+
|
|
27
|
+
console.log("š V2 Enhanced Response:");
|
|
28
|
+
console.log(result.choices[0].message.content);
|
|
29
|
+
|
|
30
|
+
console.log("\nš V2 Enhanced Usage Statistics:");
|
|
31
|
+
console.log(`Prompt tokens: ${result.usage.prompt_tokens}`);
|
|
32
|
+
console.log(`Completion tokens: ${result.usage.completion_tokens}`);
|
|
33
|
+
console.log(`Total tokens: ${result.usage.total_tokens}`);
|
|
34
|
+
|
|
35
|
+
console.log("\nš§ V2 Enhanced Response Structure:");
|
|
36
|
+
console.log(`ID: ${result.id}`);
|
|
37
|
+
console.log(`Object: ${result.object}`);
|
|
38
|
+
console.log(`Created: ${new Date(result.created * 1000).toISOString()}`);
|
|
39
|
+
console.log(`Model: ${result.model}`);
|
|
40
|
+
console.log(`Choice Index: ${result.choices[0].index}`);
|
|
41
|
+
console.log(`Message Role: ${result.choices[0].message.role}`);
|
|
42
|
+
console.log(`Finish Reason: ${result.choices[0].finish_reason}`);
|
|
43
|
+
|
|
44
|
+
console.log("\nš·ļø V2 Enhanced Metadata:");
|
|
45
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
46
|
+
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
47
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
48
|
+
|
|
49
|
+
console.log("\nš Custom Metadata Sent:");
|
|
50
|
+
console.log(`Trace ID: ${customMetadata.traceId}`);
|
|
51
|
+
console.log(`Task Type: ${customMetadata.taskType}`);
|
|
52
|
+
console.log(`Subscriber: ${customMetadata.subscriberEmail}`);
|
|
53
|
+
console.log(`Organization: ${customMetadata.organizationId}`);
|
|
54
|
+
console.log(`Agent: ${customMetadata.agent}`);
|
|
55
|
+
console.log(`Quality Score: ${customMetadata.responseQualityScore}`);
|
|
56
|
+
console.log(`Operation Type: ${customMetadata.operationType}`);
|
|
57
|
+
|
|
58
|
+
// V2 Enhanced Analytics
|
|
59
|
+
console.log("\nš V2 Enhanced Analytics:");
|
|
60
|
+
const performance = controller.getModelPerformance("sonar-pro");
|
|
61
|
+
console.log(`Context Window: ${performance.contextWindow} tokens`);
|
|
62
|
+
console.log(`Online Capabilities: ${performance.online}`);
|
|
63
|
+
console.log(`Estimated Latency: ${performance.estimatedLatency}`);
|
|
64
|
+
console.log(`Cost Tier: ${performance.costTier}`);
|
|
65
|
+
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error("ā Error:", error);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
metadataV2Example();
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { PerplexityV2Controller } from "../../src/index";
|
|
2
|
+
|
|
3
|
+
const streamingV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log("š Perplexity V2 Streaming Example (Enhanced Response Format)");
|
|
6
|
+
console.log("=".repeat(50));
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const controller = new PerplexityV2Controller();
|
|
10
|
+
|
|
11
|
+
// Streaming chat completion with V2 enhanced response
|
|
12
|
+
const result = await controller.createStreaming([
|
|
13
|
+
"Write a detailed explanation of how machine learning algorithms work, including examples."
|
|
14
|
+
], "sonar-pro");
|
|
15
|
+
|
|
16
|
+
console.log("š V2 Streaming Response:");
|
|
17
|
+
console.log("-".repeat(30));
|
|
18
|
+
|
|
19
|
+
let fullResponse = "";
|
|
20
|
+
let chunkCount = 0;
|
|
21
|
+
let lastUsage: any = null;
|
|
22
|
+
|
|
23
|
+
for await (const chunk of result.stream) {
|
|
24
|
+
chunkCount++;
|
|
25
|
+
const content = chunk.choices?.[0]?.delta?.content || "";
|
|
26
|
+
|
|
27
|
+
if (content) {
|
|
28
|
+
process.stdout.write(content);
|
|
29
|
+
fullResponse += content;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Capture usage data from the last chunk
|
|
33
|
+
if (chunk.usage) {
|
|
34
|
+
lastUsage = chunk.usage;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log("\n" + "-".repeat(30));
|
|
39
|
+
console.log(`\nš V2 Enhanced Streaming Stats:`);
|
|
40
|
+
console.log(`Total chunks received: ${chunkCount}`);
|
|
41
|
+
console.log(`Response length: ${fullResponse.length} characters`);
|
|
42
|
+
|
|
43
|
+
if (lastUsage) {
|
|
44
|
+
console.log(`Prompt tokens: ${lastUsage.prompt_tokens}`);
|
|
45
|
+
console.log(`Completion tokens: ${lastUsage.completion_tokens}`);
|
|
46
|
+
console.log(`Total tokens: ${lastUsage.total_tokens}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
console.log(`\nš§ V2 Metadata:`);
|
|
50
|
+
console.log(`Stream ID: ${result.id}`);
|
|
51
|
+
console.log(`Object: ${result.object}`);
|
|
52
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
53
|
+
console.log(`Start Time: ${new Date(result.metadata.startTime).toISOString()}`);
|
|
54
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
55
|
+
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error("ā Error:", error);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
streamingV2Example();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenium/perplexity",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
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": {
|
|
@@ -14,16 +14,31 @@
|
|
|
14
14
|
"types": "dist/index.d.ts",
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tsc -b",
|
|
17
|
-
"p-basic": "
|
|
18
|
-
"p-streaming": "
|
|
19
|
-
"p-
|
|
20
|
-
"p-
|
|
21
|
-
"p-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"e-
|
|
26
|
-
"e-
|
|
17
|
+
"p-v1-basic": "node playground/v1/basic.js",
|
|
18
|
+
"p-v1-streaming": "node playground/v1/streaming.js",
|
|
19
|
+
"p-v1-chat": "node playground/v1/chat.js",
|
|
20
|
+
"p-v1-metadata": "node playground/v1/metadata.js",
|
|
21
|
+
"p-v2-basic": "node playground/v2/basic.js",
|
|
22
|
+
"p-v2-streaming": "node playground/v2/streaming.js",
|
|
23
|
+
"p-v2-chat": "node playground/v2/chat.js",
|
|
24
|
+
"p-v2-metadata": "node playground/v2/metadata.js",
|
|
25
|
+
"e-v1-basic": "npx ts-node examples/v1/basic.ts",
|
|
26
|
+
"e-v1-streaming": "npx ts-node examples/v1/streaming.ts",
|
|
27
|
+
"e-v1-chat": "npx ts-node examples/v1/chat.ts",
|
|
28
|
+
"e-v1-metadata": "npx ts-node examples/v1/metadata.ts",
|
|
29
|
+
"e-v2-basic": "npx ts-node examples/v2/basic.ts",
|
|
30
|
+
"e-v2-streaming": "npx ts-node examples/v2/streaming.ts",
|
|
31
|
+
"e-v2-chat": "npx ts-node examples/v2/chat.ts",
|
|
32
|
+
"e-v2-metadata": "npx ts-node examples/v2/metadata.ts",
|
|
33
|
+
"examples:v1": "npm run e-v1-basic",
|
|
34
|
+
"examples:v2": "npm run e-v2-basic",
|
|
35
|
+
"examples:all": "npm run examples:v1 && echo '\n=== V2 EXAMPLES ===\n' && npm run examples:v2",
|
|
36
|
+
"playground:v1": "npm run p-v1-basic",
|
|
37
|
+
"playground:v2": "npm run p-v2-basic",
|
|
38
|
+
"playground:all": "npm run playground:v1 && echo '\n=== V2 PLAYGROUND ===\n' && npm run playground:v2",
|
|
39
|
+
"test:v1": "npm run e-v1-basic && npm run e-v1-chat && npm run e-v1-streaming && npm run e-v1-metadata",
|
|
40
|
+
"test:v2": "npm run e-v2-basic && npm run e-v2-chat && npm run e-v2-streaming && npm run e-v2-metadata",
|
|
41
|
+
"test:all": "npm run test:v1 && echo '\n=== TESTING V2 ===\n' && npm run test:v2"
|
|
27
42
|
},
|
|
28
43
|
"keywords": [
|
|
29
44
|
"revenium",
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PerplexityV1Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const basicV1Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log(
|
|
6
|
+
"š„ Perplexity V1 Basic Example (JavaScript - @revenium/perplexity)"
|
|
7
|
+
);
|
|
8
|
+
console.log("=".repeat(50));
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const controller = new PerplexityV1Controller();
|
|
12
|
+
|
|
13
|
+
// Basic chat completion
|
|
14
|
+
const result = await controller.createChat([
|
|
15
|
+
"What is artificial intelligence and how does it work?",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
console.log("š Response:");
|
|
19
|
+
console.log(result.responses[0].text);
|
|
20
|
+
|
|
21
|
+
console.log("\nš Usage Metadata:");
|
|
22
|
+
console.log(`Input tokens: ${result.usageMetadata.inputTokenCount}`);
|
|
23
|
+
console.log(`Output tokens: ${result.usageMetadata.outputTokenCount}`);
|
|
24
|
+
console.log(`Total tokens: ${result.usageMetadata.totalTokenCount}`);
|
|
25
|
+
|
|
26
|
+
console.log("\nš§ Model Info:");
|
|
27
|
+
console.log(`Model: ${result.modelVersion}`);
|
|
28
|
+
console.log(`Transaction ID: ${result.transactionId}`);
|
|
29
|
+
|
|
30
|
+
// Test model capabilities
|
|
31
|
+
console.log("\nšÆ Model Capabilities:");
|
|
32
|
+
const models = controller.getAvailableModels();
|
|
33
|
+
console.log(`Available models: ${models.slice(0, 3).join(", ")}...`);
|
|
34
|
+
|
|
35
|
+
const supportsChat = controller.supportsCapability("sonar-pro", "chat");
|
|
36
|
+
const supportsStreaming = controller.supportsCapability(
|
|
37
|
+
"sonar-pro",
|
|
38
|
+
"streaming"
|
|
39
|
+
);
|
|
40
|
+
const supportsOnline = controller.supportsCapability("sonar-pro", "online");
|
|
41
|
+
|
|
42
|
+
console.log(
|
|
43
|
+
`Sonar-pro capabilities: chat=${supportsChat}, streaming=${supportsStreaming}, online=${supportsOnline}`
|
|
44
|
+
);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("ā Error:", error);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
basicV1Example();
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PerplexityV1Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const chatV1Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log("š¬ Perplexity V1 Multi-turn Chat Example (JavaScript)");
|
|
6
|
+
console.log("=".repeat(50));
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const controller = new PerplexityV1Controller();
|
|
10
|
+
|
|
11
|
+
// Multi-turn conversation using message objects
|
|
12
|
+
const messages = [
|
|
13
|
+
{ role: "user", content: "What are the benefits of renewable energy?" },
|
|
14
|
+
{
|
|
15
|
+
role: "assistant",
|
|
16
|
+
content:
|
|
17
|
+
"Renewable energy offers several key benefits including environmental sustainability, reduced greenhouse gas emissions, and long-term cost savings.",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
role: "user",
|
|
21
|
+
content: "Can you elaborate on the cost savings aspect?",
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
const result = await controller.createChat(messages, "sonar-pro");
|
|
26
|
+
|
|
27
|
+
console.log("š Multi-turn Chat Response:");
|
|
28
|
+
console.log(result.responses[0].text);
|
|
29
|
+
|
|
30
|
+
console.log("\nš Usage Statistics:");
|
|
31
|
+
console.log(`Input tokens: ${result.usageMetadata.inputTokenCount}`);
|
|
32
|
+
console.log(`Output tokens: ${result.usageMetadata.outputTokenCount}`);
|
|
33
|
+
console.log(`Total tokens: ${result.usageMetadata.totalTokenCount}`);
|
|
34
|
+
|
|
35
|
+
console.log("\nš§ Model Information:");
|
|
36
|
+
const modelInfo = controller.getModelInfo("sonar-pro");
|
|
37
|
+
console.log(`Model: ${modelInfo.model}`);
|
|
38
|
+
console.log(`Version: ${modelInfo.version}`);
|
|
39
|
+
console.log(`Context Window: ${modelInfo.contextWindow} tokens`);
|
|
40
|
+
console.log(`Capabilities:`, modelInfo.capabilities);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error("ā Error:", error);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
chatV1Example();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PerplexityV1Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const metadataV1Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log("š Perplexity V1 Custom Metadata Example (JavaScript)");
|
|
6
|
+
console.log("=".repeat(50));
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const controller = new PerplexityV1Controller();
|
|
10
|
+
|
|
11
|
+
// Custom metadata for tracking
|
|
12
|
+
const customMetadata = {
|
|
13
|
+
traceId: "trace-js-12345",
|
|
14
|
+
taskType: "research-query",
|
|
15
|
+
subscriberEmail: "user@example.com",
|
|
16
|
+
subscriberId: "user-js-456",
|
|
17
|
+
organizationId: "org-js-789",
|
|
18
|
+
agent: "perplexity-v1-js-demo",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const result = await controller.createChat(
|
|
22
|
+
["Explain quantum computing in simple terms"],
|
|
23
|
+
"sonar-pro",
|
|
24
|
+
customMetadata
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
console.log("š Response:");
|
|
28
|
+
console.log(result.responses[0].text);
|
|
29
|
+
|
|
30
|
+
console.log("\nš Usage Metadata:");
|
|
31
|
+
console.log(`Input tokens: ${result.usageMetadata.inputTokenCount}`);
|
|
32
|
+
console.log(`Output tokens: ${result.usageMetadata.outputTokenCount}`);
|
|
33
|
+
console.log(`Total tokens: ${result.usageMetadata.totalTokenCount}`);
|
|
34
|
+
|
|
35
|
+
console.log("\nš·ļø Custom Metadata Sent:");
|
|
36
|
+
console.log(`Trace ID: ${customMetadata.traceId}`);
|
|
37
|
+
console.log(`Task Type: ${customMetadata.taskType}`);
|
|
38
|
+
console.log(`Subscriber: ${customMetadata.subscriberEmail}`);
|
|
39
|
+
console.log(`Organization: ${customMetadata.organizationId}`);
|
|
40
|
+
console.log(`Agent: ${customMetadata.agent}`);
|
|
41
|
+
|
|
42
|
+
console.log("\nš§ Transaction Info:");
|
|
43
|
+
console.log(`Model: ${result.modelVersion}`);
|
|
44
|
+
console.log(`Transaction ID: ${result.transactionId}`);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("ā Error:", error);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
metadataV1Example();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { PerplexityV1Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const streamingV1Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log(
|
|
6
|
+
"š Perplexity V1 Streaming Example (JavaScript - @revenium/perplexity)"
|
|
7
|
+
);
|
|
8
|
+
console.log("=".repeat(50));
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const controller = new PerplexityV1Controller();
|
|
12
|
+
|
|
13
|
+
// Streaming chat completion
|
|
14
|
+
const result = await controller.createStreaming(
|
|
15
|
+
[
|
|
16
|
+
"Write a short story about a robot learning to paint. Make it creative and engaging.",
|
|
17
|
+
],
|
|
18
|
+
"sonar-pro"
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
console.log("š Streaming Response:");
|
|
22
|
+
console.log("-".repeat(30));
|
|
23
|
+
|
|
24
|
+
let fullResponse = "";
|
|
25
|
+
for await (const chunk of result.stream) {
|
|
26
|
+
const content = chunk.choices?.[0]?.delta?.content || "";
|
|
27
|
+
if (content) {
|
|
28
|
+
process.stdout.write(content);
|
|
29
|
+
fullResponse += content;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
console.log("\n" + "-".repeat(30));
|
|
34
|
+
console.log(
|
|
35
|
+
`\nš Total response length: ${fullResponse.length} characters`
|
|
36
|
+
);
|
|
37
|
+
console.log(`š§ Model: ${result.modelVersion}`);
|
|
38
|
+
console.log(`š Transaction ID: ${result.transactionId}`);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error("ā Error:", error);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
streamingV1Example();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PerplexityV2Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const basicV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log(
|
|
6
|
+
"š Perplexity V2 Basic Example (JavaScript - Enhanced Response Format)"
|
|
7
|
+
);
|
|
8
|
+
console.log("=".repeat(50));
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const controller = new PerplexityV2Controller();
|
|
12
|
+
|
|
13
|
+
// Basic chat completion with V2 enhanced response
|
|
14
|
+
const result = await controller.createChat([
|
|
15
|
+
"What is artificial intelligence and how does it work?",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
console.log("š Response:");
|
|
19
|
+
console.log(result.choices[0].message.content);
|
|
20
|
+
|
|
21
|
+
console.log("\nš Enhanced Usage Info (V2):");
|
|
22
|
+
console.log(`Prompt tokens: ${result.usage.prompt_tokens}`);
|
|
23
|
+
console.log(`Completion tokens: ${result.usage.completion_tokens}`);
|
|
24
|
+
console.log(`Total tokens: ${result.usage.total_tokens}`);
|
|
25
|
+
|
|
26
|
+
console.log("\nš§ Enhanced Metadata (V2):");
|
|
27
|
+
console.log(`ID: ${result.id}`);
|
|
28
|
+
console.log(`Object: ${result.object}`);
|
|
29
|
+
console.log(`Created: ${new Date(result.created * 1000).toISOString()}`);
|
|
30
|
+
console.log(`Model: ${result.model}`);
|
|
31
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
32
|
+
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
33
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
34
|
+
|
|
35
|
+
// Test V2 enhanced features
|
|
36
|
+
console.log("\nšÆ V2 Enhanced Features:");
|
|
37
|
+
const modelInfo = controller.getModelInfo("sonar-pro");
|
|
38
|
+
console.log(`Enhanced Model Info:`, modelInfo);
|
|
39
|
+
|
|
40
|
+
const recommendation = controller.getRecommendedModel("online-search");
|
|
41
|
+
console.log(
|
|
42
|
+
`Recommended for online search: ${recommendation.model} - ${recommendation.reason}`
|
|
43
|
+
);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error("ā Error:", error);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
basicV2Example();
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { PerplexityV2Controller } from "@revenium/perplexity";
|
|
2
|
+
|
|
3
|
+
const chatV2Example = async () => {
|
|
4
|
+
console.log("=".repeat(50));
|
|
5
|
+
console.log(
|
|
6
|
+
"š¬ Perplexity V2 Enhanced Chat Example (JavaScript - OpenAI Responses API)"
|
|
7
|
+
);
|
|
8
|
+
console.log("=".repeat(50));
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const controller = new PerplexityV2Controller();
|
|
12
|
+
|
|
13
|
+
// Multi-turn conversation with V2 enhanced features
|
|
14
|
+
const messages = [
|
|
15
|
+
{
|
|
16
|
+
role: "user",
|
|
17
|
+
content:
|
|
18
|
+
"What are the latest developments in renewable energy technology?",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
role: "assistant",
|
|
22
|
+
content:
|
|
23
|
+
"Recent developments include improved solar panel efficiency, advanced battery storage systems, and offshore wind technology innovations.",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
role: "user",
|
|
27
|
+
content: "Tell me more about the battery storage improvements",
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
const result = await controller.createChat(messages, "sonar-pro");
|
|
32
|
+
|
|
33
|
+
console.log("š V2 Enhanced Chat Response:");
|
|
34
|
+
console.log(result.choices[0].message.content);
|
|
35
|
+
|
|
36
|
+
console.log("\nš V2 Enhanced Usage Statistics:");
|
|
37
|
+
console.log(`Prompt tokens: ${result.usage.prompt_tokens}`);
|
|
38
|
+
console.log(`Completion tokens: ${result.usage.completion_tokens}`);
|
|
39
|
+
console.log(`Total tokens: ${result.usage.total_tokens}`);
|
|
40
|
+
|
|
41
|
+
console.log("\nš§ V2 Enhanced Response Metadata:");
|
|
42
|
+
console.log(`ID: ${result.id}`);
|
|
43
|
+
console.log(`Object: ${result.object}`);
|
|
44
|
+
console.log(`Created: ${new Date(result.created * 1000).toISOString()}`);
|
|
45
|
+
console.log(`Model: ${result.model}`);
|
|
46
|
+
console.log(`Finish Reason: ${result.choices[0].finish_reason}`);
|
|
47
|
+
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
48
|
+
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
49
|
+
console.log(`Version: ${result.metadata.version}`);
|
|
50
|
+
|
|
51
|
+
// V2 Enhanced Features
|
|
52
|
+
console.log("\nš V2 Enhanced Model Features:");
|
|
53
|
+
const modelInfo = controller.getModelInfo("sonar-pro");
|
|
54
|
+
console.log(`Enhanced: ${modelInfo.enhanced}`);
|
|
55
|
+
console.log(`Response Format: ${modelInfo.responseFormat}`);
|
|
56
|
+
|
|
57
|
+
const performance = controller.getModelPerformance("sonar-pro");
|
|
58
|
+
console.log(`Estimated Latency: ${performance.estimatedLatency}`);
|
|
59
|
+
console.log(`Cost Tier: ${performance.costTier}`);
|
|
60
|
+
|
|
61
|
+
// Model recommendations
|
|
62
|
+
console.log("\nšÆ V2 Model Recommendations:");
|
|
63
|
+
const onlineRec = controller.getRecommendedModel("online-search");
|
|
64
|
+
const offlineRec = controller.getRecommendedModel("offline-chat");
|
|
65
|
+
console.log(`For online search: ${onlineRec.model} - ${onlineRec.reason}`);
|
|
66
|
+
console.log(`For offline chat: ${offlineRec.model} - ${offlineRec.reason}`);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
console.error("ā Error:", error);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
chatV2Example();
|