@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.
- package/.env.example +10 -0
- package/CHANGELOG.md +82 -113
- package/LICENSE +21 -21
- package/README.md +408 -292
- package/SECURITY.md +34 -34
- package/dist/cjs/core/config/index.js +0 -1
- package/dist/cjs/core/config/index.js.map +1 -1
- package/dist/cjs/core/config/manager.js +0 -1
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/providers/detector.js +0 -2
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/providers/index.js +0 -1
- package/dist/cjs/core/providers/index.js.map +1 -1
- package/dist/cjs/index.js +1 -34
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/stop-reason-mapper.js +18 -20
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
- package/dist/esm/core/config/index.js +3 -4
- package/dist/esm/core/config/index.js.map +1 -1
- package/dist/esm/core/config/manager.js +0 -1
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/providers/detector.js +0 -2
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/providers/index.js +0 -1
- package/dist/esm/core/providers/index.js.map +1 -1
- package/dist/esm/index.js +1 -34
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/stop-reason-mapper.js +18 -20
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
- package/dist/types/core/config/index.d.ts +3 -4
- package/dist/types/core/config/index.d.ts.map +1 -1
- package/dist/types/core/config/manager.d.ts +0 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/providers/detector.d.ts +0 -2
- package/dist/types/core/providers/detector.d.ts.map +1 -1
- package/dist/types/core/providers/index.d.ts +0 -1
- package/dist/types/core/providers/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -32
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/utils/stop-reason-mapper.d.ts +1 -3
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
- package/examples/README.md +274 -226
- package/examples/advanced.ts +123 -123
- package/examples/basic.ts +45 -45
- package/examples/getting_started.ts +41 -41
- package/examples/metadata.ts +68 -68
- package/examples/stream.ts +53 -53
- package/package.json +80 -72
package/README.md
CHANGED
|
@@ -1,292 +1,408 @@
|
|
|
1
|
-
# Revenium Middleware for Perplexity
|
|
2
|
-
|
|
3
|
-
A lightweight, production-ready middleware that adds **Revenium metering and tracking** to Perplexity AI API calls.
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/@revenium/perplexity)
|
|
6
|
-
[](https://nodejs.org/)
|
|
7
|
-
[](https://docs.revenium.io)
|
|
8
|
-
[](https://www.revenium.ai)
|
|
9
|
-
[](https://opensource.org/licenses/MIT)
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
- **
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
npm
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
-
|
|
98
|
-
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
**
|
|
189
|
-
|
|
190
|
-
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
**
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
##
|
|
251
|
-
|
|
252
|
-
-
|
|
253
|
-
-
|
|
254
|
-
-
|
|
255
|
-
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
**
|
|
1
|
+
# Revenium Middleware for Perplexity
|
|
2
|
+
|
|
3
|
+
A lightweight, production-ready middleware that adds **Revenium metering and tracking** to Perplexity AI API calls.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@revenium/perplexity)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](https://docs.revenium.io)
|
|
8
|
+
[](https://www.revenium.ai)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Automatic Metering** - Tracks all API calls with detailed usage metrics
|
|
14
|
+
- **Streaming Support** - Full support for streaming responses
|
|
15
|
+
- **TypeScript First** - Built with TypeScript, includes full type definitions
|
|
16
|
+
- **Multi-Format** - Supports both ESM and CommonJS
|
|
17
|
+
- **Custom Metadata** - Add custom tracking metadata to any request
|
|
18
|
+
- **Production Ready** - Battle-tested and optimized for production use
|
|
19
|
+
|
|
20
|
+
## Getting Started
|
|
21
|
+
|
|
22
|
+
### Quick Start
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @revenium/perplexity
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Note:** The `dotenv` package is optional. The middleware automatically loads `.env` files via `Initialize()`.
|
|
29
|
+
|
|
30
|
+
For complete setup instructions, TypeScript patterns, and usage examples, see [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).
|
|
31
|
+
|
|
32
|
+
### Step-by-Step Guide
|
|
33
|
+
|
|
34
|
+
The following guide walks you through creating a new project from scratch:
|
|
35
|
+
|
|
36
|
+
#### Step 1: Create Your Project
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Create project directory
|
|
40
|
+
mkdir my-perplexity-project
|
|
41
|
+
cd my-perplexity-project
|
|
42
|
+
|
|
43
|
+
# Initialize npm project
|
|
44
|
+
npm init -y
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Step 2: Install Dependencies
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Install Revenium middleware
|
|
51
|
+
npm install @revenium/perplexity
|
|
52
|
+
|
|
53
|
+
# For TypeScript projects (optional)
|
|
54
|
+
npm install -D typescript tsx @types/node
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Step 3: Setup Environment Variables
|
|
58
|
+
|
|
59
|
+
Create a `.env` file in your project root:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Create .env file
|
|
63
|
+
echo. > .env # On Windows (CMD)
|
|
64
|
+
touch .env # On Mac/Linux
|
|
65
|
+
# OR
|
|
66
|
+
# PowerShell
|
|
67
|
+
New-Item -Path .env -ItemType File
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Copy and paste the following into `.env`:
|
|
71
|
+
|
|
72
|
+
```env
|
|
73
|
+
# Perplexity Configuration
|
|
74
|
+
PERPLEXITY_API_KEY=pplx_your_perplexity_api_key
|
|
75
|
+
|
|
76
|
+
# Revenium Configuration
|
|
77
|
+
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
|
|
78
|
+
|
|
79
|
+
# Optional: For development/testing (defaults to https://api.revenium.ai)
|
|
80
|
+
# REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
81
|
+
|
|
82
|
+
# Optional: Perplexity API base URL (defaults to https://api.perplexity.ai)
|
|
83
|
+
# PERPLEXITY_API_BASE_URL=https://api.perplexity.ai
|
|
84
|
+
|
|
85
|
+
# Optional: Enable debug logging
|
|
86
|
+
# REVENIUM_DEBUG=false
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
**Replace the placeholder values with your actual keys!**
|
|
90
|
+
|
|
91
|
+
For a complete list of all available environment variables, see the [Configuration Options](#configuration-options) section below.
|
|
92
|
+
|
|
93
|
+
#### Step 4: Implement in Your Code
|
|
94
|
+
|
|
95
|
+
Use the examples as reference for implementing the middleware in your project. See [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for complete implementation examples including:
|
|
96
|
+
|
|
97
|
+
- How to initialize the middleware with your configuration
|
|
98
|
+
- Making API calls with automatic metering
|
|
99
|
+
- Handling streaming responses
|
|
100
|
+
- Adding custom metadata to track business context
|
|
101
|
+
|
|
102
|
+
**Note for ESM projects:** If you get a "Cannot use import statement outside a module" error, make sure your `package.json` includes `"type": "module"`:
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"name": "my-perplexity-project",
|
|
107
|
+
"version": "1.0.0",
|
|
108
|
+
"type": "module",
|
|
109
|
+
"dependencies": {
|
|
110
|
+
"@revenium/perplexity": "^2.0.0"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Running Examples from Cloned Repository
|
|
118
|
+
|
|
119
|
+
If you've cloned this repository from GitHub and want to **run the included examples** to see how the middleware works (without modifying the middleware source code):
|
|
120
|
+
|
|
121
|
+
### Setup
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Install dependencies
|
|
125
|
+
npm install
|
|
126
|
+
|
|
127
|
+
# Build the package
|
|
128
|
+
npm run build
|
|
129
|
+
|
|
130
|
+
# Configure environment variables
|
|
131
|
+
cp .env.example .env # On Mac/Linux
|
|
132
|
+
copy .env.example .env # On Windows (CMD)
|
|
133
|
+
# OR PowerShell
|
|
134
|
+
Copy-Item .env.example .env
|
|
135
|
+
|
|
136
|
+
# Edit .env with your API keys
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Run Examples
|
|
140
|
+
|
|
141
|
+
**Using npm scripts:**
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
npm run example:getting-started # Getting started example
|
|
145
|
+
npm run example:basic # Basic chat completion
|
|
146
|
+
npm run example:stream # Streaming response
|
|
147
|
+
npm run example:metadata # Custom metadata
|
|
148
|
+
npm run example:advanced # Advanced features
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Or use npx tsx directly:**
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
npx tsx examples/getting_started.ts
|
|
155
|
+
npx tsx examples/basic.ts
|
|
156
|
+
npx tsx examples/stream.ts
|
|
157
|
+
npx tsx examples/metadata.ts
|
|
158
|
+
npx tsx examples/advanced.ts
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
For detailed information about each example, see [examples/README.md](examples/README.md).
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Local Development and Contributing
|
|
166
|
+
|
|
167
|
+
For information on modifying the middleware source code, development workflow, and contributing to the project, see:
|
|
168
|
+
|
|
169
|
+
- **[DEVELOPMENT.md](DEVELOPMENT.md)** - Complete development guide, build system, and testing
|
|
170
|
+
- **[CONTRIBUTING.md](CONTRIBUTING.md)** - Contribution guidelines and process
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## What Gets Tracked
|
|
175
|
+
|
|
176
|
+
The middleware automatically captures comprehensive usage data:
|
|
177
|
+
|
|
178
|
+
### **Usage Metrics**
|
|
179
|
+
|
|
180
|
+
- **Token Counts** - Input tokens, output tokens, total tokens
|
|
181
|
+
- **Model Information** - Model name, provider (Perplexity)
|
|
182
|
+
- **Request Timing** - Request duration, response time
|
|
183
|
+
- **Cost Calculation** - Estimated costs based on current pricing
|
|
184
|
+
|
|
185
|
+
### **Business Context (Optional)**
|
|
186
|
+
|
|
187
|
+
- **User Tracking** - Subscriber ID, email, credentials
|
|
188
|
+
- **Organization Data** - Organization ID, subscription ID, product ID
|
|
189
|
+
- **Task Classification** - Task type, agent identifier, trace ID
|
|
190
|
+
- **Quality Metrics** - Response quality scores, task identifiers
|
|
191
|
+
|
|
192
|
+
### **Technical Details**
|
|
193
|
+
|
|
194
|
+
- **API Endpoints** - Chat completions
|
|
195
|
+
- **Request Types** - Streaming vs non-streaming
|
|
196
|
+
- **Error Tracking** - Failed requests, error types, retry attempts
|
|
197
|
+
- **Environment Info** - Development vs production usage
|
|
198
|
+
|
|
199
|
+
## Metadata Fields
|
|
200
|
+
|
|
201
|
+
The following table shows all fields this middleware sends to the Revenium API:
|
|
202
|
+
|
|
203
|
+
| Field | Type | Required | Description |
|
|
204
|
+
|-------|------|----------|-------------|
|
|
205
|
+
| **Core Fields** | | | |
|
|
206
|
+
| `model` | string | Yes | Perplexity model name (e.g., "sonar-pro") |
|
|
207
|
+
| `provider` | string | Yes | Always "Perplexity" |
|
|
208
|
+
| `inputTokenCount` | integer | Yes | Number of input tokens consumed |
|
|
209
|
+
| `outputTokenCount` | integer | Yes | Number of output tokens generated |
|
|
210
|
+
| `totalTokenCount` | integer | Yes | Total tokens (input + output) |
|
|
211
|
+
| `requestDuration` | integer | Yes | Request duration in milliseconds |
|
|
212
|
+
| **Timing** | | | |
|
|
213
|
+
| `requestTime` | string | Auto | ISO 8601 timestamp when request started |
|
|
214
|
+
| `responseTime` | string | Auto | ISO 8601 timestamp when response completed |
|
|
215
|
+
| `completionStartTime` | string | Auto | ISO 8601 timestamp when completion started |
|
|
216
|
+
| `timeToFirstToken` | integer | Streaming | Time to first token in ms (streaming only) |
|
|
217
|
+
| **Request Details** | | | |
|
|
218
|
+
| `transactionId` | string | Auto | Unique transaction identifier |
|
|
219
|
+
| `operationType` | string | Auto | Always "CHAT" for chat completions |
|
|
220
|
+
| `stopReason` | string | Auto | Completion finish reason ("END", "STOP", etc.) |
|
|
221
|
+
| `isStreamed` | boolean | Auto | Whether response was streamed |
|
|
222
|
+
| `costType` | string | Auto | Always "AI" |
|
|
223
|
+
| `modelSource` | string | Auto | Always "PERPLEXITY" |
|
|
224
|
+
| `middlewareSource` | string | Auto | Always "revenium-perplexity-node" |
|
|
225
|
+
| **Cost Information** | | | |
|
|
226
|
+
| `inputTokenCost` | number | Optional | Cost for input tokens (if provided by Perplexity) |
|
|
227
|
+
| `outputTokenCost` | number | Optional | Cost for output tokens (if provided by Perplexity) |
|
|
228
|
+
| `totalCost` | number | Optional | Total cost (if provided by Perplexity) |
|
|
229
|
+
| **Business Context** | | | |
|
|
230
|
+
| `organizationId` | string | Optional | Your organization identifier |
|
|
231
|
+
| `productId` | string | Optional | Your product identifier |
|
|
232
|
+
| `subscriptionId` | string | Optional | Your subscription identifier |
|
|
233
|
+
| `taskType` | string | Optional | Type of AI task (e.g., "chat", "research") |
|
|
234
|
+
| `traceId` | string | Optional | Session or conversation tracking ID |
|
|
235
|
+
| `agent` | string | Optional | AI agent or bot identifier |
|
|
236
|
+
| `responseQualityScore` | number | Optional | Custom quality rating (0.0-1.0) |
|
|
237
|
+
| `subscriber.id` | string | Optional | User identifier |
|
|
238
|
+
| `subscriber.email` | string | Optional | User email address |
|
|
239
|
+
| `subscriber.credential` | object | Optional | Authentication credential (name, value) |
|
|
240
|
+
|
|
241
|
+
**Notes:**
|
|
242
|
+
- **Required** fields are always sent with every request
|
|
243
|
+
- **Auto** fields are automatically populated by the middleware
|
|
244
|
+
- **Optional** fields are only sent if you provide them via `usageMetadata`
|
|
245
|
+
- **Streaming** fields are only sent for streaming requests
|
|
246
|
+
|
|
247
|
+
**Reference:**
|
|
248
|
+
- [API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete metadata field documentation
|
|
249
|
+
|
|
250
|
+
## API Overview
|
|
251
|
+
|
|
252
|
+
- **`Initialize(config?)`** - Initialize the middleware (from environment or explicit config)
|
|
253
|
+
- **`GetClient()`** - Get the global Revenium client instance
|
|
254
|
+
- **`Configure(config)`** - Alias for `Initialize()` for programmatic configuration
|
|
255
|
+
- **`IsInitialized()`** - Check if the middleware is initialized
|
|
256
|
+
- **`Reset()`** - Reset the global client (useful for testing)
|
|
257
|
+
|
|
258
|
+
**For complete API documentation and usage examples, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).**
|
|
259
|
+
|
|
260
|
+
## Configuration Options
|
|
261
|
+
|
|
262
|
+
### Environment Variables
|
|
263
|
+
|
|
264
|
+
For a complete list of all available environment variables with examples, see [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example).
|
|
265
|
+
|
|
266
|
+
## Examples
|
|
267
|
+
|
|
268
|
+
The package includes comprehensive examples in the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory.
|
|
269
|
+
|
|
270
|
+
## Project Structure
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
revenium-middleware-perplexity-node/
|
|
274
|
+
├── src/
|
|
275
|
+
│ ├── core/
|
|
276
|
+
│ │ ├── client/ # Client manager (Initialize/GetClient)
|
|
277
|
+
│ │ ├── config/ # Configuration management
|
|
278
|
+
│ │ ├── middleware/ # Perplexity API middleware wrapper
|
|
279
|
+
│ │ ├── providers/ # Provider detection
|
|
280
|
+
│ │ └── tracking/ # Metering and tracking
|
|
281
|
+
│ ├── types/ # TypeScript type definitions
|
|
282
|
+
│ ├── utils/ # Utility functions
|
|
283
|
+
│ └── index.ts # Main entry point
|
|
284
|
+
├── examples/ # TypeScript examples
|
|
285
|
+
├── package.json
|
|
286
|
+
├── tsconfig.json
|
|
287
|
+
└── README.md
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## How It Works
|
|
291
|
+
|
|
292
|
+
1. **Initialize**: Call `Initialize()` to set up the middleware with your configuration
|
|
293
|
+
2. **Get Client**: Call `GetClient()` to get a wrapped Perplexity client instance
|
|
294
|
+
3. **Make Requests**: Use the client normally - all requests are automatically tracked
|
|
295
|
+
4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
|
|
296
|
+
5. **Transparent Response**: Original Perplexity responses are returned unchanged
|
|
297
|
+
|
|
298
|
+
The middleware never blocks your application - if Revenium tracking fails, your Perplexity requests continue normally.
|
|
299
|
+
|
|
300
|
+
**Supported APIs:**
|
|
301
|
+
|
|
302
|
+
- Chat Completions API (`client.chat().completions().create()`)
|
|
303
|
+
- Streaming API (`client.chat().completions().createStreaming()`)
|
|
304
|
+
|
|
305
|
+
## Troubleshooting
|
|
306
|
+
|
|
307
|
+
### Common Issues
|
|
308
|
+
|
|
309
|
+
**No tracking data appears:**
|
|
310
|
+
|
|
311
|
+
1. Verify environment variables are set correctly in `.env`
|
|
312
|
+
2. Enable debug logging by setting `REVENIUM_DEBUG=true` in `.env`
|
|
313
|
+
3. Check console for `[Revenium]` log messages
|
|
314
|
+
4. Verify your `REVENIUM_METERING_API_KEY` is valid
|
|
315
|
+
|
|
316
|
+
**Client not initialized error:**
|
|
317
|
+
|
|
318
|
+
- Make sure you call `Initialize()` before `GetClient()`
|
|
319
|
+
- Check that your `.env` file is in the project root
|
|
320
|
+
- Verify `REVENIUM_METERING_API_KEY` is set
|
|
321
|
+
|
|
322
|
+
**Perplexity API errors:**
|
|
323
|
+
|
|
324
|
+
- Verify `PERPLEXITY_API_KEY` is set correctly
|
|
325
|
+
- Check that your API key starts with `pplx-`
|
|
326
|
+
- Ensure you're using a valid model name
|
|
327
|
+
|
|
328
|
+
**Windows-specific issues:**
|
|
329
|
+
|
|
330
|
+
If you're developing on Windows and encounter build errors with `npm run clean`:
|
|
331
|
+
|
|
332
|
+
- The `clean` script uses `rm -rf` which may not work in Windows CMD
|
|
333
|
+
- Use PowerShell or Git Bash instead
|
|
334
|
+
- Or manually delete the `dist` folder before building
|
|
335
|
+
- Alternatively, install `rimraf` globally: `npm install -g rimraf` and update the script to use `rimraf dist`
|
|
336
|
+
|
|
337
|
+
### Debug Mode
|
|
338
|
+
|
|
339
|
+
Enable detailed logging by adding to your `.env`:
|
|
340
|
+
|
|
341
|
+
```env
|
|
342
|
+
REVENIUM_DEBUG=true
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Getting Help
|
|
346
|
+
|
|
347
|
+
If issues persist:
|
|
348
|
+
|
|
349
|
+
1. Enable debug logging (`REVENIUM_DEBUG=true`)
|
|
350
|
+
2. Check the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory for working examples
|
|
351
|
+
3. Review [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for detailed setup instructions
|
|
352
|
+
4. Contact support@revenium.io with debug logs
|
|
353
|
+
|
|
354
|
+
## Supported Models
|
|
355
|
+
|
|
356
|
+
This middleware works with any Perplexity model. For the complete model list, see the [Perplexity Models Documentation](https://docs.perplexity.ai/getting-started/models).
|
|
357
|
+
|
|
358
|
+
### API Support Matrix
|
|
359
|
+
|
|
360
|
+
The following table shows what has been tested and verified with working examples:
|
|
361
|
+
|
|
362
|
+
| Feature | Chat Completions | Streaming |
|
|
363
|
+
| --------------------- | ---------------- | --------- |
|
|
364
|
+
| **Basic Usage** | Yes | Yes |
|
|
365
|
+
| **Metadata Tracking** | Yes | Yes |
|
|
366
|
+
| **Token Counting** | Yes | Yes |
|
|
367
|
+
|
|
368
|
+
**Note:** "Yes" = Tested with working examples in [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory
|
|
369
|
+
|
|
370
|
+
## Requirements
|
|
371
|
+
|
|
372
|
+
- Node.js 20+
|
|
373
|
+
- TypeScript 5.0+ (for TypeScript projects)
|
|
374
|
+
- Revenium API key
|
|
375
|
+
- Perplexity API key
|
|
376
|
+
|
|
377
|
+
## Documentation
|
|
378
|
+
|
|
379
|
+
For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
|
|
380
|
+
|
|
381
|
+
## Contributing
|
|
382
|
+
|
|
383
|
+
See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CONTRIBUTING.md)
|
|
384
|
+
|
|
385
|
+
## Code of Conduct
|
|
386
|
+
|
|
387
|
+
See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CODE_OF_CONDUCT.md)
|
|
388
|
+
|
|
389
|
+
## Security
|
|
390
|
+
|
|
391
|
+
See [SECURITY.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/SECURITY.md)
|
|
392
|
+
|
|
393
|
+
## License
|
|
394
|
+
|
|
395
|
+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/LICENSE) file for details.
|
|
396
|
+
|
|
397
|
+
## Support
|
|
398
|
+
|
|
399
|
+
For issues, feature requests, or contributions:
|
|
400
|
+
|
|
401
|
+
- **GitHub Repository**: [revenium/revenium-middleware-perplexity-node](https://github.com/revenium/revenium-middleware-perplexity-node)
|
|
402
|
+
- **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
403
|
+
- **Documentation**: [docs.revenium.io](https://docs.revenium.io)
|
|
404
|
+
- **Contact**: Reach out to the Revenium team for additional support
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
**Built by Revenium**
|