@revenium/perplexity 1.0.15 → 1.0.17
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 +105 -48
- package/package.json +2 -3
- package/src/middleware.ts +1 -1
package/README.md
CHANGED
|
@@ -69,29 +69,32 @@ Create `test-perplexity.js`:
|
|
|
69
69
|
|
|
70
70
|
```javascript
|
|
71
71
|
// test-perplexity.js
|
|
72
|
-
import {
|
|
72
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
73
73
|
|
|
74
|
-
const
|
|
74
|
+
const basicExample = async () => {
|
|
75
|
+
try {
|
|
76
|
+
const middleware = new PerplexityReveniumMiddleware();
|
|
77
|
+
const model = middleware.getGenerativeModel("sonar-pro");
|
|
78
|
+
const result = await model.createChatCompletion({
|
|
79
|
+
messages: [
|
|
80
|
+
{
|
|
81
|
+
role: "user",
|
|
82
|
+
content: "What is the universe?",
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const text = result.choices[0]?.message?.content;
|
|
88
|
+
console.log("*** RESPONSE ***");
|
|
89
|
+
console.log(text);
|
|
90
|
+
console.log("✅ Basic Perplexity AI example successful!");
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error("❌ Perplexity basic example failed:", error);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
75
96
|
|
|
76
|
-
|
|
77
|
-
const result = await client.createChatCompletion({
|
|
78
|
-
model: "sonar-pro",
|
|
79
|
-
messages: [
|
|
80
|
-
{
|
|
81
|
-
role: "user",
|
|
82
|
-
content: "What is the universe?",
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
const text = result.choices[0]?.message?.content;
|
|
88
|
-
console.log("*** RESPONSE ***");
|
|
89
|
-
console.log(text);
|
|
90
|
-
console.log("✅ Basic Perplexity AI example successful!");
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.error("❌ Perplexity basic example failed:", error);
|
|
93
|
-
process.exit(1);
|
|
94
|
-
}
|
|
97
|
+
basicExample();
|
|
95
98
|
```
|
|
96
99
|
|
|
97
100
|
### Step 5: Update package.json
|
|
@@ -147,38 +150,89 @@ Create `examples/streaming-perplexity.js`:
|
|
|
147
150
|
|
|
148
151
|
```javascript
|
|
149
152
|
// examples/streaming-perplexity.js
|
|
150
|
-
import {
|
|
153
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
151
154
|
|
|
152
|
-
const
|
|
155
|
+
const streamingExample = async () => {
|
|
156
|
+
try {
|
|
157
|
+
const middleware = new PerplexityReveniumMiddleware();
|
|
158
|
+
const model = middleware.getGenerativeModel("sonar-pro");
|
|
159
|
+
const stream = await model.createChatCompletionStream({
|
|
160
|
+
messages: [
|
|
161
|
+
{
|
|
162
|
+
role: "user",
|
|
163
|
+
content: "What is artificial intelligence?",
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
console.log("*** STREAMING RESPONSE ***");
|
|
169
|
+
let fullText = "";
|
|
170
|
+
|
|
171
|
+
for await (const chunk of stream) {
|
|
172
|
+
const content = chunk.choices[0]?.delta?.content;
|
|
173
|
+
if (content) {
|
|
174
|
+
process.stdout.write(content);
|
|
175
|
+
fullText += content;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
153
178
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
},
|
|
162
|
-
],
|
|
163
|
-
});
|
|
179
|
+
console.log("\n✅ Streaming with metering successful!");
|
|
180
|
+
console.log(`📊 Total response length: ${fullText.length} characters`);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error("❌ Perplexity streaming example failed:", error);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
164
186
|
|
|
165
|
-
|
|
166
|
-
|
|
187
|
+
streamingExample();
|
|
188
|
+
```
|
|
167
189
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
190
|
+
#### Metadata Example
|
|
191
|
+
|
|
192
|
+
Create `examples/metadata-perplexity.js`:
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
// examples/metadata-perplexity.js
|
|
196
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
197
|
+
|
|
198
|
+
const metadataExample = async () => {
|
|
199
|
+
try {
|
|
200
|
+
const middleware = new PerplexityReveniumMiddleware();
|
|
201
|
+
const model = middleware.getGenerativeModel("sonar-pro");
|
|
202
|
+
const result = await model.createChatCompletion({
|
|
203
|
+
messages: [
|
|
204
|
+
{
|
|
205
|
+
role: "user",
|
|
206
|
+
content: "Analyze this quarterly report",
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
usageMetadata: {
|
|
210
|
+
// User tracking
|
|
211
|
+
traceId: "conv-28a7e9d4",
|
|
212
|
+
taskType: "document-analysis",
|
|
213
|
+
subscriberEmail: "user@example.com",
|
|
214
|
+
subscriberId: "user-123",
|
|
215
|
+
subscriberCredentialName: "api-key-1",
|
|
216
|
+
|
|
217
|
+
// Business context
|
|
218
|
+
organizationId: "acme-corp",
|
|
219
|
+
subscriptionId: "premium-plan",
|
|
220
|
+
productId: "business-intelligence",
|
|
221
|
+
agent: "analysis-assistant",
|
|
222
|
+
responseQualityScore: 0.95,
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
console.log("*** METADATA RESPONSE ***");
|
|
227
|
+
console.log("Response:", result.choices[0]?.message?.content);
|
|
228
|
+
console.log("✅ Metadata tracking with metering successful!");
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error("❌ Perplexity metadata example failed:", error);
|
|
231
|
+
process.exit(1);
|
|
174
232
|
}
|
|
233
|
+
};
|
|
175
234
|
|
|
176
|
-
|
|
177
|
-
console.log(`📊 Total response length: ${fullText.length} characters`);
|
|
178
|
-
} catch (error) {
|
|
179
|
-
console.error("❌ Perplexity streaming example failed:", error);
|
|
180
|
-
process.exit(1);
|
|
181
|
-
}
|
|
235
|
+
metadataExample();
|
|
182
236
|
```
|
|
183
237
|
|
|
184
238
|
### Step 8: Update package.json
|
|
@@ -190,7 +244,8 @@ try {
|
|
|
190
244
|
"type": "module",
|
|
191
245
|
"scripts": {
|
|
192
246
|
"test-perplexity": "node test-perplexity.js",
|
|
193
|
-
"test-perplexity-stream": "node examples/streaming-perplexity.js"
|
|
247
|
+
"test-perplexity-stream": "node examples/streaming-perplexity.js",
|
|
248
|
+
"test-perplexity-metadata": "node examples/metadata-perplexity.js"
|
|
194
249
|
},
|
|
195
250
|
"dependencies": {
|
|
196
251
|
"@revenium/perplexity": "^1.0.0"
|
|
@@ -204,6 +259,8 @@ try {
|
|
|
204
259
|
# Test streaming
|
|
205
260
|
npm run test-perplexity-stream
|
|
206
261
|
|
|
262
|
+
# Test metadata
|
|
263
|
+
npm run test-perplexity-metadata
|
|
207
264
|
```
|
|
208
265
|
|
|
209
266
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenium/perplexity",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
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": {
|
|
@@ -35,9 +35,8 @@
|
|
|
35
35
|
],
|
|
36
36
|
"author": "",
|
|
37
37
|
"license": "ISC",
|
|
38
|
-
"type": "
|
|
38
|
+
"type": "commonjs",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@revenium/perplexity": "^1.0.14",
|
|
41
40
|
"dotenv": "^17.2.2",
|
|
42
41
|
"openai": "^5.23.1",
|
|
43
42
|
"ts-node": "^10.9.2",
|
package/src/middleware.ts
CHANGED