@revenium/perplexity 1.0.25 → 2.0.1
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 +443 -568
- package/dist/cjs/core/config/perplexity-config.js +45 -0
- package/dist/cjs/core/config/perplexity-config.js.map +1 -0
- package/dist/cjs/core/config/revenium-config.js +80 -0
- package/dist/cjs/core/config/revenium-config.js.map +1 -0
- package/dist/cjs/core/tracking/metering.js +131 -0
- package/dist/cjs/core/tracking/metering.js.map +1 -0
- package/dist/cjs/core/wrapper/perplexity-client.js +177 -0
- package/dist/cjs/core/wrapper/perplexity-client.js.map +1 -0
- package/dist/cjs/index.js +64 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types/index.js +21 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/utils/logger.js +23 -0
- package/dist/cjs/utils/logger.js.map +1 -0
- package/dist/esm/core/config/perplexity-config.js +40 -0
- package/dist/esm/core/config/perplexity-config.js.map +1 -0
- package/dist/esm/core/config/revenium-config.js +72 -0
- package/dist/esm/core/config/revenium-config.js.map +1 -0
- package/dist/esm/core/tracking/metering.js +126 -0
- package/dist/esm/core/tracking/metering.js.map +1 -0
- package/dist/esm/core/wrapper/perplexity-client.js +170 -0
- package/dist/esm/core/wrapper/perplexity-client.js.map +1 -0
- package/dist/esm/index.js +44 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types/index.js +18 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/utils/logger.js +20 -0
- package/dist/esm/utils/logger.js.map +1 -0
- package/dist/types/core/config/perplexity-config.d.ts +24 -0
- package/dist/types/core/config/perplexity-config.d.ts.map +1 -0
- package/dist/types/core/config/revenium-config.d.ts +37 -0
- package/dist/types/core/config/revenium-config.d.ts.map +1 -0
- package/dist/types/core/tracking/metering.d.ts +31 -0
- package/dist/types/core/tracking/metering.d.ts.map +1 -0
- package/dist/types/core/wrapper/perplexity-client.d.ts +32 -0
- package/dist/types/core/wrapper/perplexity-client.d.ts.map +1 -0
- package/dist/types/index.d.ts +34 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +159 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/utils/logger.d.ts +10 -0
- package/dist/types/utils/logger.d.ts.map +1 -0
- package/package.json +36 -31
- package/.env.example +0 -3
- package/dist/interfaces/meteringResponse.d.ts +0 -28
- package/dist/interfaces/meteringResponse.js +0 -2
- package/dist/models/Metering.js +0 -83
- package/dist/v1/perplexityV1.service.js +0 -166
- package/dist/v2/perplexityV2.service.js +0 -178
- package/examples/v1/basic.ts +0 -50
- package/examples/v1/chat.ts +0 -40
- package/examples/v1/metadata.ts +0 -49
- package/examples/v1/streaming.ts +0 -44
- package/examples/v2/basic.ts +0 -49
- package/examples/v2/chat.ts +0 -60
- package/examples/v2/metadata.ts +0 -71
- package/examples/v2/streaming.ts +0 -61
- package/playground/v1/basic.js +0 -50
- package/playground/v1/chat.js +0 -46
- package/playground/v1/metadata.js +0 -50
- package/playground/v1/streaming.js +0 -44
- package/playground/v2/basic.js +0 -49
- package/playground/v2/chat.js +0 -72
- package/playground/v2/metadata.js +0 -76
- package/playground/v2/streaming.js +0 -67
- package/src/index.ts +0 -17
- package/src/interfaces/chatCompletionRequest.ts +0 -15
- package/src/interfaces/credential.ts +0 -4
- package/src/interfaces/meteringRequest.ts +0 -14
- package/src/interfaces/meteringResponse.ts +0 -29
- package/src/interfaces/operation.ts +0 -4
- package/src/interfaces/perplexityResponse.ts +0 -63
- package/src/interfaces/perplexityStreaming.ts +0 -56
- package/src/interfaces/subscriber.ts +0 -8
- package/src/interfaces/tokenCounts.ts +0 -7
- package/src/interfaces/usageMetadata.ts +0 -27
- package/src/models/Logger.ts +0 -38
- package/src/models/Metering.ts +0 -121
- package/src/utils/calculateDurationMs.ts +0 -3
- package/src/utils/constants/constants.ts +0 -10
- package/src/utils/constants/logLevels.ts +0 -1
- package/src/utils/constants/messages.ts +0 -11
- package/src/utils/constants/models.ts +0 -20
- package/src/utils/constants/perplexityModels.ts +0 -71
- package/src/utils/extractTokenCount.ts +0 -26
- package/src/utils/formatTimestamp.ts +0 -3
- package/src/utils/generateTransactionId.ts +0 -5
- package/src/utils/index.ts +0 -39
- package/src/utils/loadEnv.ts +0 -8
- package/src/utils/safeExtract.ts +0 -67
- package/src/v1/perplexityV1.controller.ts +0 -164
- package/src/v1/perplexityV1.service.ts +0 -230
- package/src/v2/perplexityV2.controller.ts +0 -219
- package/src/v2/perplexityV2.service.ts +0 -260
- package/tsconfig.json +0 -15
package/playground/v1/chat.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
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();
|
|
@@ -1,50 +0,0 @@
|
|
|
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();
|
|
@@ -1,44 +0,0 @@
|
|
|
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();
|
package/playground/v2/basic.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
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();
|
package/playground/v2/chat.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
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();
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { PerplexityV2Controller } from "@revenium/perplexity";
|
|
2
|
-
|
|
3
|
-
const metadataV2Example = async () => {
|
|
4
|
-
console.log("=".repeat(50));
|
|
5
|
-
console.log(
|
|
6
|
-
"📊 Perplexity V2 Enhanced Metadata Example (JavaScript - OpenAI Responses API)"
|
|
7
|
-
);
|
|
8
|
-
console.log("=".repeat(50));
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const controller = new PerplexityV2Controller();
|
|
12
|
-
|
|
13
|
-
// Enhanced custom metadata for V2
|
|
14
|
-
const customMetadata = {
|
|
15
|
-
traceId: "trace-v2-js-67890",
|
|
16
|
-
taskType: "advanced-research",
|
|
17
|
-
subscriberEmail: "researcher@example.com",
|
|
18
|
-
subscriberId: "user-v2-js-123",
|
|
19
|
-
organizationId: "org-v2-js-456",
|
|
20
|
-
agent: "perplexity-v2-js-enhanced",
|
|
21
|
-
responseQualityScore: 0.95,
|
|
22
|
-
operationType: "chat-completion",
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const result = await controller.createChat(
|
|
26
|
-
[
|
|
27
|
-
"Provide a comprehensive analysis of the impact of artificial intelligence on modern healthcare systems",
|
|
28
|
-
],
|
|
29
|
-
"sonar-pro",
|
|
30
|
-
customMetadata
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
console.log("📝 V2 Enhanced 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 Structure:");
|
|
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(`Choice Index: ${result.choices[0].index}`);
|
|
47
|
-
console.log(`Message Role: ${result.choices[0].message.role}`);
|
|
48
|
-
console.log(`Finish Reason: ${result.choices[0].finish_reason}`);
|
|
49
|
-
|
|
50
|
-
console.log("\n🏷️ V2 Enhanced Metadata:");
|
|
51
|
-
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
52
|
-
console.log(`Processing Time: ${result.metadata.processingTime}ms`);
|
|
53
|
-
console.log(`Version: ${result.metadata.version}`);
|
|
54
|
-
|
|
55
|
-
console.log("\n📋 Custom Metadata Sent:");
|
|
56
|
-
console.log(`Trace ID: ${customMetadata.traceId}`);
|
|
57
|
-
console.log(`Task Type: ${customMetadata.taskType}`);
|
|
58
|
-
console.log(`Subscriber: ${customMetadata.subscriberEmail}`);
|
|
59
|
-
console.log(`Organization: ${customMetadata.organizationId}`);
|
|
60
|
-
console.log(`Agent: ${customMetadata.agent}`);
|
|
61
|
-
console.log(`Quality Score: ${customMetadata.responseQualityScore}`);
|
|
62
|
-
console.log(`Operation Type: ${customMetadata.operationType}`);
|
|
63
|
-
|
|
64
|
-
// V2 Enhanced Analytics
|
|
65
|
-
console.log("\n📈 V2 Enhanced Analytics:");
|
|
66
|
-
const performance = controller.getModelPerformance("sonar-pro");
|
|
67
|
-
console.log(`Context Window: ${performance.contextWindow} tokens`);
|
|
68
|
-
console.log(`Online Capabilities: ${performance.online}`);
|
|
69
|
-
console.log(`Estimated Latency: ${performance.estimatedLatency}`);
|
|
70
|
-
console.log(`Cost Tier: ${performance.costTier}`);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error("❌ Error:", error);
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
metadataV2Example();
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { PerplexityV2Controller } from "@revenium/perplexity";
|
|
2
|
-
|
|
3
|
-
const streamingV2Example = async () => {
|
|
4
|
-
console.log("=".repeat(50));
|
|
5
|
-
console.log(
|
|
6
|
-
"🌊 Perplexity V2 Streaming Example (JavaScript - Enhanced Response Format)"
|
|
7
|
-
);
|
|
8
|
-
console.log("=".repeat(50));
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const controller = new PerplexityV2Controller();
|
|
12
|
-
|
|
13
|
-
// Streaming chat completion with V2 enhanced response
|
|
14
|
-
const result = await controller.createStreaming(
|
|
15
|
-
[
|
|
16
|
-
"Write a detailed explanation of how machine learning algorithms work, including examples.",
|
|
17
|
-
],
|
|
18
|
-
"sonar-pro"
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
console.log("📝 V2 Streaming Response:");
|
|
22
|
-
console.log("-".repeat(30));
|
|
23
|
-
|
|
24
|
-
let fullResponse = "";
|
|
25
|
-
let chunkCount = 0;
|
|
26
|
-
let lastUsage = null;
|
|
27
|
-
|
|
28
|
-
for await (const chunk of result.stream) {
|
|
29
|
-
chunkCount++;
|
|
30
|
-
const content = chunk.choices?.[0]?.delta?.content || "";
|
|
31
|
-
|
|
32
|
-
if (content) {
|
|
33
|
-
process.stdout.write(content);
|
|
34
|
-
fullResponse += content;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Capture usage data from the last chunk
|
|
38
|
-
if (chunk.usage) {
|
|
39
|
-
lastUsage = chunk.usage;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
console.log("\n" + "-".repeat(30));
|
|
44
|
-
console.log(`\n📊 V2 Enhanced Streaming Stats:`);
|
|
45
|
-
console.log(`Total chunks received: ${chunkCount}`);
|
|
46
|
-
console.log(`Response length: ${fullResponse.length} characters`);
|
|
47
|
-
|
|
48
|
-
if (lastUsage) {
|
|
49
|
-
console.log(`Prompt tokens: ${lastUsage.prompt_tokens}`);
|
|
50
|
-
console.log(`Completion tokens: ${lastUsage.completion_tokens}`);
|
|
51
|
-
console.log(`Total tokens: ${lastUsage.total_tokens}`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
console.log(`\n🔧 V2 Metadata:`);
|
|
55
|
-
console.log(`Stream ID: ${result.id}`);
|
|
56
|
-
console.log(`Object: ${result.object}`);
|
|
57
|
-
console.log(`Transaction ID: ${result.metadata.transactionId}`);
|
|
58
|
-
console.log(
|
|
59
|
-
`Start Time: ${new Date(result.metadata.startTime).toISOString()}`
|
|
60
|
-
);
|
|
61
|
-
console.log(`Version: ${result.metadata.version}`);
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error("❌ Error:", error);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
streamingV2Example();
|
package/src/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { config } from "dotenv";
|
|
2
|
-
config();
|
|
3
|
-
|
|
4
|
-
// V1 Controllers (compatible with current API)
|
|
5
|
-
export * from "./v1/perplexityV1.controller";
|
|
6
|
-
|
|
7
|
-
// V2 Controllers (enhanced response format following OpenAI Responses API)
|
|
8
|
-
export * from "./v2/perplexityV2.controller";
|
|
9
|
-
|
|
10
|
-
// Interfaces
|
|
11
|
-
export * from "./interfaces/perplexityResponse";
|
|
12
|
-
export * from "./interfaces/perplexityStreaming";
|
|
13
|
-
export * from "./interfaces/usageMetadata";
|
|
14
|
-
export * from "./interfaces/chatCompletionRequest";
|
|
15
|
-
|
|
16
|
-
// Constants
|
|
17
|
-
export * from "./utils/constants/perplexityModels";
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { IUsageMetadata } from "./usageMetadata";
|
|
2
|
-
|
|
3
|
-
// Flexible message interface for Perplexity
|
|
4
|
-
export interface IPerplexityMessage {
|
|
5
|
-
role: "system" | "user" | "assistant";
|
|
6
|
-
content: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface IChatCompletionRequest {
|
|
10
|
-
model?: string;
|
|
11
|
-
messages: IPerplexityMessage[];
|
|
12
|
-
stream?: boolean;
|
|
13
|
-
usageMetadata?: IUsageMetadata;
|
|
14
|
-
[key: string]: any;
|
|
15
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { IOperationType } from "./operation";
|
|
2
|
-
import { ITokenCounts } from "./tokenCounts";
|
|
3
|
-
import { IUsageMetadata } from "./usageMetadata";
|
|
4
|
-
|
|
5
|
-
export interface IMeteringRequest {
|
|
6
|
-
transactionId?: string;
|
|
7
|
-
startTime: Date;
|
|
8
|
-
endTime: Date;
|
|
9
|
-
modelName: string;
|
|
10
|
-
tokenCounts: ITokenCounts;
|
|
11
|
-
stopReason: string;
|
|
12
|
-
operationType: IOperationType;
|
|
13
|
-
usageMetadata?: IUsageMetadata;
|
|
14
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { ISubscriber } from "./subscriber";
|
|
2
|
-
|
|
3
|
-
export interface IMeteringResponse {
|
|
4
|
-
stopReason: string;
|
|
5
|
-
costType: string;
|
|
6
|
-
isStreamed: boolean;
|
|
7
|
-
taskType: string;
|
|
8
|
-
agent: string;
|
|
9
|
-
operationType: string;
|
|
10
|
-
inputTokenCount: number;
|
|
11
|
-
outputTokenCount: number;
|
|
12
|
-
reasoningTokenCount: number;
|
|
13
|
-
cacheCreationTokenCount: number;
|
|
14
|
-
cacheReadTokenCount: number;
|
|
15
|
-
totalTokenCount: number;
|
|
16
|
-
organizationId: string;
|
|
17
|
-
productId: string;
|
|
18
|
-
subscriber: ISubscriber;
|
|
19
|
-
model: string;
|
|
20
|
-
transactionId: string;
|
|
21
|
-
responseTime: string;
|
|
22
|
-
requestDuration: number;
|
|
23
|
-
provider: string;
|
|
24
|
-
requestTime: string;
|
|
25
|
-
completionStartTime: string;
|
|
26
|
-
timeToFirstToken: number;
|
|
27
|
-
middleware_source: string;
|
|
28
|
-
traceId?: string;
|
|
29
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { IUsageMetadata } from "./usageMetadata";
|
|
2
|
-
|
|
3
|
-
// Perplexity Chat Response Interface (V1 - Compatible with current middleware)
|
|
4
|
-
export interface IPerplexityResponseChat {
|
|
5
|
-
responses: IPerplexityResponseMessage[];
|
|
6
|
-
usageMetadata: IUsageMetadata;
|
|
7
|
-
modelVersion: string;
|
|
8
|
-
transactionId: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IPerplexityResponseMessage {
|
|
12
|
-
text: string;
|
|
13
|
-
role: "assistant" | "user" | "system";
|
|
14
|
-
finishReason?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Raw OpenAI-compatible response from Perplexity API
|
|
18
|
-
export interface IPerplexityRawResponse {
|
|
19
|
-
id: string;
|
|
20
|
-
object: string;
|
|
21
|
-
created: number;
|
|
22
|
-
model: string;
|
|
23
|
-
choices: Array<{
|
|
24
|
-
index: number;
|
|
25
|
-
message: {
|
|
26
|
-
role: string;
|
|
27
|
-
content: string;
|
|
28
|
-
};
|
|
29
|
-
finish_reason: string;
|
|
30
|
-
}>;
|
|
31
|
-
usage: {
|
|
32
|
-
prompt_tokens: number;
|
|
33
|
-
completion_tokens: number;
|
|
34
|
-
total_tokens: number;
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// V2 Enhanced Response Structure (Following OpenAI Responses API)
|
|
39
|
-
export interface IPerplexityV2Response {
|
|
40
|
-
id: string;
|
|
41
|
-
object: "chat.completion";
|
|
42
|
-
created: number;
|
|
43
|
-
model: string;
|
|
44
|
-
choices: Array<{
|
|
45
|
-
index: number;
|
|
46
|
-
message: {
|
|
47
|
-
role: string;
|
|
48
|
-
content: string;
|
|
49
|
-
};
|
|
50
|
-
finish_reason: string;
|
|
51
|
-
}>;
|
|
52
|
-
usage: {
|
|
53
|
-
prompt_tokens: number;
|
|
54
|
-
completion_tokens: number;
|
|
55
|
-
total_tokens: number;
|
|
56
|
-
};
|
|
57
|
-
// Enhanced metadata for V2
|
|
58
|
-
metadata: {
|
|
59
|
-
transactionId: string;
|
|
60
|
-
processingTime: number;
|
|
61
|
-
version: "v2";
|
|
62
|
-
};
|
|
63
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { IUsageMetadata } from "./usageMetadata";
|
|
2
|
-
|
|
3
|
-
// Perplexity Streaming Response Interface (V1 - Compatible with current middleware)
|
|
4
|
-
export interface IPerplexityStreamingResponse {
|
|
5
|
-
stream: AsyncIterable<IPerplexityStreamChunk>;
|
|
6
|
-
usageMetadata: IUsageMetadata;
|
|
7
|
-
modelVersion: string;
|
|
8
|
-
transactionId: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IPerplexityStreamChunk {
|
|
12
|
-
id: string;
|
|
13
|
-
object: string;
|
|
14
|
-
created: number;
|
|
15
|
-
model: string;
|
|
16
|
-
choices: Array<{
|
|
17
|
-
index: number;
|
|
18
|
-
delta: {
|
|
19
|
-
role?: string;
|
|
20
|
-
content?: string;
|
|
21
|
-
};
|
|
22
|
-
finish_reason?: string;
|
|
23
|
-
}>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// V2 Enhanced Streaming Response (Following OpenAI Responses API)
|
|
27
|
-
export interface IPerplexityV2StreamingResponse {
|
|
28
|
-
id: string;
|
|
29
|
-
object: "chat.completion.chunk";
|
|
30
|
-
stream: AsyncIterable<IPerplexityV2StreamChunk>;
|
|
31
|
-
metadata: {
|
|
32
|
-
transactionId: string;
|
|
33
|
-
startTime: number;
|
|
34
|
-
version: "v2";
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface IPerplexityV2StreamChunk {
|
|
39
|
-
id: string;
|
|
40
|
-
object: "chat.completion.chunk";
|
|
41
|
-
created: number;
|
|
42
|
-
model: string;
|
|
43
|
-
choices: Array<{
|
|
44
|
-
index: number;
|
|
45
|
-
delta: {
|
|
46
|
-
role?: string;
|
|
47
|
-
content?: string;
|
|
48
|
-
};
|
|
49
|
-
finish_reason?: string;
|
|
50
|
-
}>;
|
|
51
|
-
usage?: {
|
|
52
|
-
prompt_tokens: number;
|
|
53
|
-
completion_tokens: number;
|
|
54
|
-
total_tokens: number;
|
|
55
|
-
};
|
|
56
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export interface IUsageMetadata {
|
|
2
|
-
traceId?: string;
|
|
3
|
-
taskType?: string;
|
|
4
|
-
subscriberEmail?: string;
|
|
5
|
-
subscriberId?: string;
|
|
6
|
-
subscriberCredentialName?: string;
|
|
7
|
-
subscriberCredential?: string;
|
|
8
|
-
organizationId?: string;
|
|
9
|
-
subscriptionId?: string;
|
|
10
|
-
productId?: string;
|
|
11
|
-
agent?: string;
|
|
12
|
-
responseQualityScore?: number;
|
|
13
|
-
transactionId?: string;
|
|
14
|
-
timeToFirstToken?: number;
|
|
15
|
-
requestTime?: Date;
|
|
16
|
-
completionStartTime?: Date;
|
|
17
|
-
operationType?: string;
|
|
18
|
-
inputTokenCount?: number;
|
|
19
|
-
outputTokenCount?: number;
|
|
20
|
-
reasoningTokenCount?: number;
|
|
21
|
-
cacheCreationTokenCount?: number;
|
|
22
|
-
cacheReadTokenCount?: number;
|
|
23
|
-
totalTokenCount?: number;
|
|
24
|
-
responseTime?: Date;
|
|
25
|
-
requestDuration?: number;
|
|
26
|
-
stopReason?: string;
|
|
27
|
-
}
|