@revenium/perplexity 2.0.2 → 2.0.4
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/CHANGELOG.md +98 -0
- package/LICENSE +21 -21
- package/README.md +271 -625
- package/dist/cjs/constants.js +70 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/core/tracking/metering.js +86 -6
- package/dist/cjs/core/tracking/metering.js.map +1 -1
- package/dist/cjs/core/wrapper/perplexity-client.js +11 -1
- package/dist/cjs/core/wrapper/perplexity-client.js.map +1 -1
- package/dist/cjs/index.js +9 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js +0 -15
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/esm/constants.js +67 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/core/tracking/metering.js +86 -6
- package/dist/esm/core/tracking/metering.js.map +1 -1
- package/dist/esm/core/wrapper/perplexity-client.js +11 -1
- package/dist/esm/core/wrapper/perplexity-client.js.map +1 -1
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js +1 -14
- package/dist/esm/types/index.js.map +1 -1
- package/dist/types/constants.d.ts +67 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/core/tracking/metering.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +3 -11
- package/dist/types/types/index.d.ts.map +1 -1
- package/examples/README.md +322 -0
- package/examples/advanced-features.ts +148 -0
- package/examples/basic.ts +50 -0
- package/examples/chat.ts +73 -0
- package/examples/getting_started.ts +64 -0
- package/examples/metadata.ts +65 -0
- package/examples/streaming.ts +50 -0
- package/package.json +72 -69
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic Chat Completion Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the Perplexity middleware
|
|
5
|
+
* to create a simple chat completion with Revenium metering.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
initializeReveniumFromEnv,
|
|
10
|
+
initializePerplexityFromEnv,
|
|
11
|
+
createChatCompletion,
|
|
12
|
+
UsageMetadata,
|
|
13
|
+
} from "../src";
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
// Initialize configurations from environment variables
|
|
17
|
+
initializeReveniumFromEnv();
|
|
18
|
+
initializePerplexityFromEnv();
|
|
19
|
+
|
|
20
|
+
// Create a simple chat completion with basic metadata
|
|
21
|
+
const usageMetadata: UsageMetadata = {
|
|
22
|
+
subscriber: { id: "user-123" },
|
|
23
|
+
traceId: "session-abc",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const result = await createChatCompletion({
|
|
27
|
+
messages: [
|
|
28
|
+
{
|
|
29
|
+
role: "user",
|
|
30
|
+
content: "What is the capital of France? Answer in one sentence.",
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
model: "sonar-pro",
|
|
34
|
+
maxTokens: 100,
|
|
35
|
+
usageMetadata,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Display essential results
|
|
39
|
+
console.log("\n✓ Chat Completion Successful");
|
|
40
|
+
console.log(`Response: ${result.content}`);
|
|
41
|
+
console.log(`Model: ${result.model}`);
|
|
42
|
+
console.log(
|
|
43
|
+
`Tokens Used: ${result.usage.totalTokens} (prompt: ${result.usage.promptTokens}, completion: ${result.usage.completionTokens})`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch((error) => {
|
|
48
|
+
console.error("Error:", error.message);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
});
|
package/examples/chat.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-turn Chat Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to have a multi-turn conversation
|
|
5
|
+
* with the Perplexity middleware.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
initializeReveniumFromEnv,
|
|
10
|
+
initializePerplexityFromEnv,
|
|
11
|
+
createChatCompletion,
|
|
12
|
+
type PerplexityMessage,
|
|
13
|
+
} from "../src";
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
// Initialize configurations from environment variables
|
|
17
|
+
initializeReveniumFromEnv();
|
|
18
|
+
initializePerplexityFromEnv();
|
|
19
|
+
|
|
20
|
+
// Start a conversation
|
|
21
|
+
const messages: PerplexityMessage[] = [
|
|
22
|
+
{
|
|
23
|
+
role: "system",
|
|
24
|
+
content: "You are a helpful assistant that provides concise answers.",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
role: "user",
|
|
28
|
+
content: "What is the capital of France?",
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
console.log("\n--- Turn 1 ---");
|
|
33
|
+
console.log("User: What is the capital of France?");
|
|
34
|
+
|
|
35
|
+
const response1 = await createChatCompletion({
|
|
36
|
+
messages,
|
|
37
|
+
model: "sonar",
|
|
38
|
+
maxTokens: 100,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log(`Assistant: ${response1.content}`);
|
|
42
|
+
|
|
43
|
+
// Continue the conversation
|
|
44
|
+
messages.push({
|
|
45
|
+
role: "assistant",
|
|
46
|
+
content: response1.content,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
messages.push({
|
|
50
|
+
role: "user",
|
|
51
|
+
content: "What is its population?",
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
console.log("\n--- Turn 2 ---");
|
|
55
|
+
console.log("User: What is its population?");
|
|
56
|
+
|
|
57
|
+
const response2 = await createChatCompletion({
|
|
58
|
+
messages,
|
|
59
|
+
model: "sonar-pro",
|
|
60
|
+
maxTokens: 500,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log(`Assistant: ${response2.content}`);
|
|
64
|
+
|
|
65
|
+
// Summary
|
|
66
|
+
const totalTokens = response1.usage.totalTokens + response2.usage.totalTokens;
|
|
67
|
+
console.log(`\n✓ Chat completed - Total tokens used: ${totalTokens}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main().catch((error) => {
|
|
71
|
+
console.error("Error:", error.message);
|
|
72
|
+
process.exit(1);
|
|
73
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Getting Started with Revenium Perplexity Middleware
|
|
3
|
+
*
|
|
4
|
+
* This is the simplest possible entry point for new users.
|
|
5
|
+
* Shows basic usage with all metadata options documented.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
initializeReveniumFromEnv,
|
|
10
|
+
initializePerplexityFromEnv,
|
|
11
|
+
createChatCompletion,
|
|
12
|
+
} from "../src";
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
// Initialize from environment variables (.env file)
|
|
16
|
+
initializeReveniumFromEnv();
|
|
17
|
+
initializePerplexityFromEnv();
|
|
18
|
+
|
|
19
|
+
// Create a simple chat completion
|
|
20
|
+
const result = await createChatCompletion({
|
|
21
|
+
messages: [
|
|
22
|
+
{
|
|
23
|
+
role: "user",
|
|
24
|
+
content: "What is artificial intelligence?",
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
model: "sonar-pro",
|
|
28
|
+
maxTokens: 500,
|
|
29
|
+
|
|
30
|
+
/* Optional: Add metadata for advanced tracking and analytics
|
|
31
|
+
usageMetadata: {
|
|
32
|
+
// User identification
|
|
33
|
+
subscriber: {
|
|
34
|
+
id: 'user-123',
|
|
35
|
+
email: 'user@example.com',
|
|
36
|
+
credential: {
|
|
37
|
+
name: 'api-key-prod',
|
|
38
|
+
value: 'key-abc-123'
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
// Organization & billing
|
|
43
|
+
organizationId: 'my-customers-name',
|
|
44
|
+
subscriptionId: 'plan-enterprise-2024',
|
|
45
|
+
|
|
46
|
+
// Product & task tracking
|
|
47
|
+
productId: 'my-product',
|
|
48
|
+
taskType: 'qa',
|
|
49
|
+
agent: 'customer-support',
|
|
50
|
+
|
|
51
|
+
// Session tracking
|
|
52
|
+
traceId: 'session-' + Date.now(),
|
|
53
|
+
|
|
54
|
+
// Quality metrics
|
|
55
|
+
responseQualityScore: 0.95
|
|
56
|
+
}
|
|
57
|
+
*/
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.log("Response:", result.content);
|
|
61
|
+
console.log("\nUsage:", result.usage);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom Metadata Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to pass comprehensive metadata
|
|
5
|
+
* for enhanced tracking, analytics, and metering purposes.
|
|
6
|
+
*
|
|
7
|
+
* For complete metadata options and details, see:
|
|
8
|
+
* https://revenium.readme.io/reference/meter_ai_completion
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
initializeReveniumFromEnv,
|
|
13
|
+
initializePerplexityFromEnv,
|
|
14
|
+
createChatCompletion,
|
|
15
|
+
UsageMetadata,
|
|
16
|
+
} from "../src";
|
|
17
|
+
|
|
18
|
+
async function main() {
|
|
19
|
+
// Initialize configurations from environment variables
|
|
20
|
+
initializeReveniumFromEnv();
|
|
21
|
+
initializePerplexityFromEnv();
|
|
22
|
+
|
|
23
|
+
// Create a chat completion with comprehensive metadata
|
|
24
|
+
const usageMetadata: UsageMetadata = {
|
|
25
|
+
subscriber: {
|
|
26
|
+
id: "user-123",
|
|
27
|
+
email: "metadata-user@example.com",
|
|
28
|
+
credential: {
|
|
29
|
+
name: "api-key",
|
|
30
|
+
value: "key-premium-abc123",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
organizationId: "org-example",
|
|
34
|
+
productId: "product-premium",
|
|
35
|
+
agent: "calculator-bot-v1",
|
|
36
|
+
traceId: "trace-abc-123",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const result = await createChatCompletion({
|
|
40
|
+
messages: [
|
|
41
|
+
{
|
|
42
|
+
role: "user",
|
|
43
|
+
content: "What is 2 + 2?",
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
model: "sonar-pro",
|
|
47
|
+
maxTokens: 200,
|
|
48
|
+
usageMetadata,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log("\n✓ Chat Completion Successful");
|
|
52
|
+
console.log(`Response: ${result.content}`);
|
|
53
|
+
console.log(`Model: ${result.model}`);
|
|
54
|
+
console.log(
|
|
55
|
+
`Tokens Used: ${result.usage.totalTokens} (prompt: ${result.usage.promptTokens}, completion: ${result.usage.completionTokens})`
|
|
56
|
+
);
|
|
57
|
+
console.log(
|
|
58
|
+
"Metadata tracked: subscriber, organization, product, agent, trace"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
main().catch((error) => {
|
|
63
|
+
console.error("Error:", error.message);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming Chat Completion Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use streaming responses
|
|
5
|
+
* with the Perplexity middleware and Revenium metering.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
initializeReveniumFromEnv,
|
|
10
|
+
initializePerplexityFromEnv,
|
|
11
|
+
createStreamingChatCompletion,
|
|
12
|
+
} from "../src";
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
// Initialize configurations from environment variables
|
|
16
|
+
initializeReveniumFromEnv();
|
|
17
|
+
initializePerplexityFromEnv();
|
|
18
|
+
|
|
19
|
+
// Create a streaming chat completion
|
|
20
|
+
const result = await createStreamingChatCompletion({
|
|
21
|
+
messages: [
|
|
22
|
+
{
|
|
23
|
+
role: "user",
|
|
24
|
+
content: "Count from 1 to 5 slowly, one number per line.",
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
model: "sonar-pro",
|
|
28
|
+
maxTokens: 200,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log(`\nModel: ${result.model}`);
|
|
32
|
+
console.log("Streaming response:\n");
|
|
33
|
+
|
|
34
|
+
// Process the stream
|
|
35
|
+
let fullContent = "";
|
|
36
|
+
for await (const chunk of result.stream) {
|
|
37
|
+
const content = chunk.choices[0]?.delta?.content || "";
|
|
38
|
+
if (content) {
|
|
39
|
+
process.stdout.write(content);
|
|
40
|
+
fullContent += content;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log("\n\n✓ Streaming completed");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch((error) => {
|
|
48
|
+
console.error("Error:", error.message);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
});
|
package/package.json
CHANGED
|
@@ -1,69 +1,72 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@revenium/perplexity",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "NodeJS middleware for Perplexity AI API with Revenium metering",
|
|
5
|
-
"homepage": "https://github.com/revenium/revenium-middleware-perplexity-node#readme",
|
|
6
|
-
"bugs": {
|
|
7
|
-
"url": "https://github.com/revenium/revenium-middleware-perplexity-node/issues"
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/revenium/revenium-middleware-perplexity-node.git"
|
|
12
|
-
},
|
|
13
|
-
"main": "dist/cjs/index.js",
|
|
14
|
-
"module": "dist/esm/index.js",
|
|
15
|
-
"types": "dist/types/index.d.ts",
|
|
16
|
-
"exports": {
|
|
17
|
-
".": {
|
|
18
|
-
"import": "./dist/esm/index.js",
|
|
19
|
-
"require": "./dist/cjs/index.js",
|
|
20
|
-
"types": "./dist/types/index.d.ts"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"files": [
|
|
24
|
-
"dist
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"build:
|
|
33
|
-
"build": "
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"example:
|
|
38
|
-
"example:
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"test": "npm run example:basic && npm run example:streaming && npm run example:chat && npm run example:metadata"
|
|
44
|
-
},
|
|
45
|
-
"keywords": [
|
|
46
|
-
"revenium",
|
|
47
|
-
"middleware",
|
|
48
|
-
"perplexity",
|
|
49
|
-
"ai",
|
|
50
|
-
"chat",
|
|
51
|
-
"completion",
|
|
52
|
-
"metering",
|
|
53
|
-
"monitoring",
|
|
54
|
-
"typescript"
|
|
55
|
-
],
|
|
56
|
-
"author": "",
|
|
57
|
-
"license": "
|
|
58
|
-
"type": "commonjs",
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"
|
|
68
|
-
}
|
|
69
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@revenium/perplexity",
|
|
3
|
+
"version": "2.0.4",
|
|
4
|
+
"description": "NodeJS middleware for Perplexity AI API with Revenium metering",
|
|
5
|
+
"homepage": "https://github.com/revenium/revenium-middleware-perplexity-node#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/revenium/revenium-middleware-perplexity-node/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/revenium/revenium-middleware-perplexity-node.git"
|
|
12
|
+
},
|
|
13
|
+
"main": "dist/cjs/index.js",
|
|
14
|
+
"module": "dist/esm/index.js",
|
|
15
|
+
"types": "dist/types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/esm/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js",
|
|
20
|
+
"types": "./dist/types/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/",
|
|
25
|
+
"examples/",
|
|
26
|
+
"README.md",
|
|
27
|
+
"CHANGELOG.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"clean": "rm -rf dist",
|
|
32
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
33
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
34
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
35
|
+
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:types",
|
|
36
|
+
"prepublishOnly": "npm run build",
|
|
37
|
+
"example:getting-started": "tsx examples/getting_started.ts",
|
|
38
|
+
"example:basic": "tsx examples/basic.ts",
|
|
39
|
+
"example:streaming": "tsx examples/streaming.ts",
|
|
40
|
+
"example:chat": "tsx examples/chat.ts",
|
|
41
|
+
"example:metadata": "tsx examples/metadata.ts",
|
|
42
|
+
"example:advanced": "tsx examples/advanced-features.ts",
|
|
43
|
+
"test": "npm run example:getting-started && npm run example:basic && npm run example:streaming && npm run example:chat && npm run example:metadata && npm run example:advanced"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"revenium",
|
|
47
|
+
"middleware",
|
|
48
|
+
"perplexity",
|
|
49
|
+
"ai",
|
|
50
|
+
"chat",
|
|
51
|
+
"completion",
|
|
52
|
+
"metering",
|
|
53
|
+
"monitoring",
|
|
54
|
+
"typescript"
|
|
55
|
+
],
|
|
56
|
+
"author": "Revenium",
|
|
57
|
+
"license": "MIT",
|
|
58
|
+
"type": "commonjs",
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=20.0.0"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"dotenv": "^17.2.2",
|
|
64
|
+
"openai": "^5.23.1",
|
|
65
|
+
"tsx": "^4.19.2",
|
|
66
|
+
"typescript": "^5.9.2",
|
|
67
|
+
"uuid": "^13.0.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@types/node": "^24.7.1"
|
|
71
|
+
}
|
|
72
|
+
}
|