@revenium/perplexity 2.0.5 → 2.0.7
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 +113 -97
- package/LICENSE +21 -21
- package/README.md +376 -290
- package/SECURITY.md +34 -34
- package/dist/cjs/core/client/manager.js +6 -3
- package/dist/cjs/core/client/manager.js.map +1 -1
- 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/config/validator.js +18 -16
- package/dist/cjs/core/config/validator.js.map +1 -1
- package/dist/cjs/core/middleware/interfaces.js +4 -4
- package/dist/cjs/core/middleware/interfaces.js.map +1 -1
- package/dist/cjs/core/middleware/streaming-wrapper.js +4 -4
- package/dist/cjs/core/middleware/streaming-wrapper.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/core/tracking/api-client.js +3 -3
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/index.js +2 -40
- 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/client/manager.js +6 -3
- package/dist/esm/core/client/manager.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/config/validator.js +18 -16
- package/dist/esm/core/config/validator.js.map +1 -1
- package/dist/esm/core/middleware/interfaces.js +1 -1
- package/dist/esm/core/middleware/interfaces.js.map +1 -1
- package/dist/esm/core/middleware/streaming-wrapper.js +2 -2
- package/dist/esm/core/middleware/streaming-wrapper.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/core/tracking/api-client.js +1 -1
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/index.js +1 -38
- 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/client/manager.d.ts.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/config/validator.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 -37
- 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 +230 -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 +72 -71
- package/dist/cjs/constants/models.js +0 -38
- package/dist/cjs/constants/models.js.map +0 -1
- package/dist/esm/constants/models.js +0 -35
- package/dist/esm/constants/models.js.map +0 -1
- package/dist/types/constants/models.d.ts +0 -39
- package/dist/types/constants/models.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,290 +1,376 @@
|
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
#
|
|
52
|
-
npm install
|
|
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
|
-
|
|
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
|
+
- **Zero Configuration** - Works out of the box with environment variables
|
|
14
|
+
- **Automatic Metering** - Tracks all API calls with detailed usage metrics
|
|
15
|
+
- **Streaming Support** - Full support for streaming responses
|
|
16
|
+
- **TypeScript First** - Built with TypeScript, includes full type definitions
|
|
17
|
+
- **Multi-Format** - Supports both ESM and CommonJS
|
|
18
|
+
- **Custom Metadata** - Add custom tracking metadata to any request
|
|
19
|
+
- **Production Ready** - Battle-tested and optimized for production use
|
|
20
|
+
|
|
21
|
+
## Getting Started
|
|
22
|
+
|
|
23
|
+
### Quick Start
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @revenium/perplexity
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Note:** The `dotenv` package is optional. The middleware automatically loads `.env` files via `Initialize()`.
|
|
30
|
+
|
|
31
|
+
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).
|
|
32
|
+
|
|
33
|
+
### Step-by-Step Guide
|
|
34
|
+
|
|
35
|
+
The following guide walks you through creating a new project from scratch:
|
|
36
|
+
|
|
37
|
+
#### Step 1: Create Your Project
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Create project directory
|
|
41
|
+
mkdir my-perplexity-project
|
|
42
|
+
cd my-perplexity-project
|
|
43
|
+
|
|
44
|
+
# Initialize npm project
|
|
45
|
+
npm init -y
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Step 2: Install Dependencies
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# Install Revenium middleware
|
|
52
|
+
npm install @revenium/perplexity
|
|
53
|
+
|
|
54
|
+
# For TypeScript projects (optional)
|
|
55
|
+
npm install -D typescript tsx @types/node
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### Step 3: Setup Environment Variables
|
|
59
|
+
|
|
60
|
+
Create a `.env` file in your project root with your API keys:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
# .env
|
|
64
|
+
PERPLEXITY_API_KEY=pplx_your_perplexity_api_key
|
|
65
|
+
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
|
|
66
|
+
REVENIUM_METERING_BASE_URL=https://api.revenium.ai # Optional, defaults to https://api.revenium.ai
|
|
67
|
+
REVENIUM_DEBUG=false # Optional, enables debug logging
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Replace the placeholder values with your actual keys!**
|
|
71
|
+
|
|
72
|
+
For a complete list of all available environment variables, see the [Configuration Options](#configuration-options) section below.
|
|
73
|
+
|
|
74
|
+
#### Step 4: Implement in Your Code
|
|
75
|
+
|
|
76
|
+
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:
|
|
77
|
+
|
|
78
|
+
- How to initialize the middleware with your configuration
|
|
79
|
+
- Making API calls with automatic metering
|
|
80
|
+
- Handling streaming responses
|
|
81
|
+
- Adding custom metadata to track business context
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## Running Examples from Cloned Repository
|
|
86
|
+
|
|
87
|
+
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):
|
|
88
|
+
|
|
89
|
+
### Setup
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# Install dependencies
|
|
93
|
+
npm install
|
|
94
|
+
|
|
95
|
+
# Build the package
|
|
96
|
+
npm run build
|
|
97
|
+
|
|
98
|
+
# Configure environment variables
|
|
99
|
+
cp .env.example .env
|
|
100
|
+
# Edit .env with your API keys
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Run Examples
|
|
104
|
+
|
|
105
|
+
**Using npm scripts:**
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
npm run example:getting-started # Getting started example
|
|
109
|
+
npm run example:basic # Basic chat completion
|
|
110
|
+
npm run example:stream # Streaming response
|
|
111
|
+
npm run example:metadata # Custom metadata
|
|
112
|
+
npm run example:advanced # Advanced features
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Or use npx tsx directly:**
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npx tsx examples/getting_started.ts
|
|
119
|
+
npx tsx examples/basic.ts
|
|
120
|
+
npx tsx examples/stream.ts
|
|
121
|
+
npx tsx examples/metadata.ts
|
|
122
|
+
npx tsx examples/advanced.ts
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
For detailed information about each example, see [examples/README.md](examples/README.md).
|
|
126
|
+
|
|
127
|
+
### Want to Modify the Middleware Code?
|
|
128
|
+
|
|
129
|
+
If you're planning to modify the examples or experiment with the code, the setup above is sufficient. However, if you want to **modify the middleware source code itself** (files in `src/`), you'll need to understand the development workflow.
|
|
130
|
+
|
|
131
|
+
See [Local Development and Contributing](#local-development-and-contributing) below for the complete development guide.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Local Development and Contributing
|
|
136
|
+
|
|
137
|
+
**Are you planning to modify the middleware source code?** (Not just run examples)
|
|
138
|
+
|
|
139
|
+
If you want to:
|
|
140
|
+
|
|
141
|
+
- Fix bugs in the middleware
|
|
142
|
+
- Add new features to @revenium/perplexity
|
|
143
|
+
- Contribute to the project
|
|
144
|
+
- Test changes to the middleware before publishing
|
|
145
|
+
|
|
146
|
+
Then follow the complete development workflow in [DEVELOPMENT.md](DEVELOPMENT.md), which covers:
|
|
147
|
+
|
|
148
|
+
### What DEVELOPMENT.md Includes:
|
|
149
|
+
|
|
150
|
+
- **Development Workflow** - Step-by-step process for making changes
|
|
151
|
+
- **Build System** - Understanding ESM, CJS, and TypeScript compilation
|
|
152
|
+
- **Testing Local Changes** - How to test your modifications properly
|
|
153
|
+
- **When to Rebuild** - Understanding when `npm run build` is needed
|
|
154
|
+
- **Publishing Checklist** - Steps to publish new versions
|
|
155
|
+
- **Architecture Notes** - Understanding the codebase structure
|
|
156
|
+
- **Contributing Guidelines** - How to contribute to the project
|
|
157
|
+
|
|
158
|
+
### Key Difference:
|
|
159
|
+
|
|
160
|
+
- **Running Examples** (above): You can modify example files (`.ts` in `examples/`) and run them directly with `npx tsx` - no rebuild needed
|
|
161
|
+
- **Modifying Middleware** (DEVELOPMENT.md): If you modify source files (`.ts` in `src/`), you must run `npm run build` before testing
|
|
162
|
+
|
|
163
|
+
**Quick Start for Contributors:**
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
# 1. Make changes to source code
|
|
167
|
+
vim src/index.ts
|
|
168
|
+
|
|
169
|
+
# 2. Rebuild the package
|
|
170
|
+
npm run build
|
|
171
|
+
|
|
172
|
+
# 3. Test your changes
|
|
173
|
+
npm run example:basic
|
|
174
|
+
|
|
175
|
+
# 4. See DEVELOPMENT.md for complete workflow
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## What Gets Tracked
|
|
181
|
+
|
|
182
|
+
The middleware automatically captures comprehensive usage data:
|
|
183
|
+
|
|
184
|
+
### **Usage Metrics**
|
|
185
|
+
|
|
186
|
+
- **Token Counts** - Input tokens, output tokens, total tokens
|
|
187
|
+
- **Model Information** - Model name, provider (Perplexity)
|
|
188
|
+
- **Request Timing** - Request duration, response time
|
|
189
|
+
- **Cost Calculation** - Estimated costs based on current pricing
|
|
190
|
+
|
|
191
|
+
### **Business Context (Optional)**
|
|
192
|
+
|
|
193
|
+
- **User Tracking** - Subscriber ID, email, credentials
|
|
194
|
+
- **Organization Data** - Organization ID, subscription ID, product ID
|
|
195
|
+
- **Task Classification** - Task type, agent identifier, trace ID
|
|
196
|
+
- **Quality Metrics** - Response quality scores, task identifiers
|
|
197
|
+
|
|
198
|
+
### **Technical Details**
|
|
199
|
+
|
|
200
|
+
- **API Endpoints** - Chat completions
|
|
201
|
+
- **Request Types** - Streaming vs non-streaming
|
|
202
|
+
- **Error Tracking** - Failed requests, error types, retry attempts
|
|
203
|
+
- **Environment Info** - Development vs production usage
|
|
204
|
+
|
|
205
|
+
## API Overview
|
|
206
|
+
|
|
207
|
+
- **`Initialize(config?)`** - Initialize the middleware (from environment or explicit config)
|
|
208
|
+
- **`GetClient()`** - Get the global Revenium client instance
|
|
209
|
+
- **`Configure(config)`** - Alias for `Initialize()` for programmatic configuration
|
|
210
|
+
- **`IsInitialized()`** - Check if the middleware is initialized
|
|
211
|
+
- **`Reset()`** - Reset the global client (useful for testing)
|
|
212
|
+
|
|
213
|
+
**For complete API documentation and usage examples, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).**
|
|
214
|
+
|
|
215
|
+
## Metadata Fields
|
|
216
|
+
|
|
217
|
+
The middleware supports the following optional metadata fields for tracking:
|
|
218
|
+
|
|
219
|
+
| Field | Type | Description |
|
|
220
|
+
| ----------------------- | ------ | ---------------------------------------------------------- |
|
|
221
|
+
| `traceId` | string | Unique identifier for session or conversation tracking |
|
|
222
|
+
| `taskType` | string | Type of AI task being performed (e.g., "chat", "research") |
|
|
223
|
+
| `agent` | string | AI agent or bot identifier |
|
|
224
|
+
| `organizationId` | string | Organization or company identifier |
|
|
225
|
+
| `productId` | string | Your product or feature identifier |
|
|
226
|
+
| `subscriptionId` | string | Subscription plan identifier |
|
|
227
|
+
| `responseQualityScore` | number | Custom quality rating (0.0-1.0) |
|
|
228
|
+
| `subscriber.id` | string | Unique user identifier |
|
|
229
|
+
| `subscriber.email` | string | User email address |
|
|
230
|
+
| `subscriber.credential` | object | Authentication credential (`name` and `value` fields) |
|
|
231
|
+
|
|
232
|
+
**All metadata fields are optional.** For complete metadata documentation and usage examples, see:
|
|
233
|
+
|
|
234
|
+
- [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) - All usage examples
|
|
235
|
+
- [Revenium API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete API documentation
|
|
236
|
+
|
|
237
|
+
## Configuration Options
|
|
238
|
+
|
|
239
|
+
### Environment Variables
|
|
240
|
+
|
|
241
|
+
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).
|
|
242
|
+
|
|
243
|
+
## Examples
|
|
244
|
+
|
|
245
|
+
The package includes comprehensive examples in the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory.
|
|
246
|
+
|
|
247
|
+
## Project Structure
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
revenium-middleware-perplexity-node/
|
|
251
|
+
├── src/
|
|
252
|
+
│ ├── core/
|
|
253
|
+
│ │ ├── client/ # Client manager (Initialize/GetClient)
|
|
254
|
+
│ │ ├── config/ # Configuration management
|
|
255
|
+
│ │ ├── middleware/ # Perplexity API middleware wrapper
|
|
256
|
+
│ │ ├── providers/ # Provider detection
|
|
257
|
+
│ │ └── tracking/ # Metering and tracking
|
|
258
|
+
│ ├── types/ # TypeScript type definitions
|
|
259
|
+
│ ├── utils/ # Utility functions
|
|
260
|
+
│ └── index.ts # Main entry point
|
|
261
|
+
├── examples/ # TypeScript examples
|
|
262
|
+
├── package.json
|
|
263
|
+
├── tsconfig.json
|
|
264
|
+
└── README.md
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## How It Works
|
|
268
|
+
|
|
269
|
+
1. **Initialize**: Call `Initialize()` to set up the middleware with your configuration
|
|
270
|
+
2. **Get Client**: Call `GetClient()` to get a wrapped Perplexity client instance
|
|
271
|
+
3. **Make Requests**: Use the client normally - all requests are automatically tracked
|
|
272
|
+
4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
|
|
273
|
+
5. **Transparent Response**: Original Perplexity responses are returned unchanged
|
|
274
|
+
|
|
275
|
+
The middleware never blocks your application - if Revenium tracking fails, your Perplexity requests continue normally.
|
|
276
|
+
|
|
277
|
+
**Supported APIs:**
|
|
278
|
+
|
|
279
|
+
- Chat Completions API (`client.chat().completions().create()`)
|
|
280
|
+
- Streaming API (`client.chat().completions().createStreaming()`)
|
|
281
|
+
|
|
282
|
+
## Troubleshooting
|
|
283
|
+
|
|
284
|
+
### Common Issues
|
|
285
|
+
|
|
286
|
+
**No tracking data appears:**
|
|
287
|
+
|
|
288
|
+
1. Verify environment variables are set correctly in `.env`
|
|
289
|
+
2. Enable debug logging by setting `REVENIUM_DEBUG=true` in `.env`
|
|
290
|
+
3. Check console for `[Revenium]` log messages
|
|
291
|
+
4. Verify your `REVENIUM_METERING_API_KEY` is valid
|
|
292
|
+
|
|
293
|
+
**Client not initialized error:**
|
|
294
|
+
|
|
295
|
+
- Make sure you call `Initialize()` before `GetClient()`
|
|
296
|
+
- Check that your `.env` file is in the project root
|
|
297
|
+
- Verify `REVENIUM_METERING_API_KEY` is set
|
|
298
|
+
|
|
299
|
+
**Perplexity API errors:**
|
|
300
|
+
|
|
301
|
+
- Verify `PERPLEXITY_API_KEY` is set correctly
|
|
302
|
+
- Check that your API key starts with `pplx-`
|
|
303
|
+
- Ensure you're using a valid model name
|
|
304
|
+
|
|
305
|
+
### Debug Mode
|
|
306
|
+
|
|
307
|
+
Enable detailed logging by adding to your `.env`:
|
|
308
|
+
|
|
309
|
+
```env
|
|
310
|
+
REVENIUM_DEBUG=true
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### Getting Help
|
|
314
|
+
|
|
315
|
+
If issues persist:
|
|
316
|
+
|
|
317
|
+
1. Enable debug logging (`REVENIUM_DEBUG=true`)
|
|
318
|
+
2. Check the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory for working examples
|
|
319
|
+
3. Review [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for detailed setup instructions
|
|
320
|
+
4. Contact support@revenium.io with debug logs
|
|
321
|
+
|
|
322
|
+
## Supported Models
|
|
323
|
+
|
|
324
|
+
This middleware works with any Perplexity model. For the complete model list, see the [Perplexity Models Documentation](https://docs.perplexity.ai/getting-started/models).
|
|
325
|
+
|
|
326
|
+
### API Support Matrix
|
|
327
|
+
|
|
328
|
+
The following table shows what has been tested and verified with working examples:
|
|
329
|
+
|
|
330
|
+
| Feature | Chat Completions | Streaming |
|
|
331
|
+
| --------------------- | ---------------- | --------- |
|
|
332
|
+
| **Basic Usage** | Yes | Yes |
|
|
333
|
+
| **Metadata Tracking** | Yes | Yes |
|
|
334
|
+
| **Token Counting** | Yes | Yes |
|
|
335
|
+
|
|
336
|
+
**Note:** "Yes" = Tested with working examples in [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory
|
|
337
|
+
|
|
338
|
+
## Requirements
|
|
339
|
+
|
|
340
|
+
- Node.js 20+
|
|
341
|
+
- TypeScript 5.0+ (for TypeScript projects)
|
|
342
|
+
- Revenium API key
|
|
343
|
+
- Perplexity API key
|
|
344
|
+
|
|
345
|
+
## Documentation
|
|
346
|
+
|
|
347
|
+
For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
|
|
348
|
+
|
|
349
|
+
## Contributing
|
|
350
|
+
|
|
351
|
+
See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CONTRIBUTING.md)
|
|
352
|
+
|
|
353
|
+
## Code of Conduct
|
|
354
|
+
|
|
355
|
+
See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CODE_OF_CONDUCT.md)
|
|
356
|
+
|
|
357
|
+
## Security
|
|
358
|
+
|
|
359
|
+
See [SECURITY.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/SECURITY.md)
|
|
360
|
+
|
|
361
|
+
## License
|
|
362
|
+
|
|
363
|
+
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.
|
|
364
|
+
|
|
365
|
+
## Support
|
|
366
|
+
|
|
367
|
+
For issues, feature requests, or contributions:
|
|
368
|
+
|
|
369
|
+
- **GitHub Repository**: [revenium/revenium-middleware-perplexity-node](https://github.com/revenium/revenium-middleware-perplexity-node)
|
|
370
|
+
- **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
371
|
+
- **Documentation**: [docs.revenium.io](https://docs.revenium.io)
|
|
372
|
+
- **Contact**: Reach out to the Revenium team for additional support
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
**Built by Revenium**
|