@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.
Files changed (49) hide show
  1. package/.env.example +10 -0
  2. package/CHANGELOG.md +82 -113
  3. package/LICENSE +21 -21
  4. package/README.md +408 -292
  5. package/SECURITY.md +34 -34
  6. package/dist/cjs/core/config/index.js +0 -1
  7. package/dist/cjs/core/config/index.js.map +1 -1
  8. package/dist/cjs/core/config/manager.js +0 -1
  9. package/dist/cjs/core/config/manager.js.map +1 -1
  10. package/dist/cjs/core/providers/detector.js +0 -2
  11. package/dist/cjs/core/providers/detector.js.map +1 -1
  12. package/dist/cjs/core/providers/index.js +0 -1
  13. package/dist/cjs/core/providers/index.js.map +1 -1
  14. package/dist/cjs/index.js +1 -34
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/utils/stop-reason-mapper.js +18 -20
  17. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
  18. package/dist/esm/core/config/index.js +3 -4
  19. package/dist/esm/core/config/index.js.map +1 -1
  20. package/dist/esm/core/config/manager.js +0 -1
  21. package/dist/esm/core/config/manager.js.map +1 -1
  22. package/dist/esm/core/providers/detector.js +0 -2
  23. package/dist/esm/core/providers/detector.js.map +1 -1
  24. package/dist/esm/core/providers/index.js +0 -1
  25. package/dist/esm/core/providers/index.js.map +1 -1
  26. package/dist/esm/index.js +1 -34
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/esm/utils/stop-reason-mapper.js +18 -20
  29. package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
  30. package/dist/types/core/config/index.d.ts +3 -4
  31. package/dist/types/core/config/index.d.ts.map +1 -1
  32. package/dist/types/core/config/manager.d.ts +0 -1
  33. package/dist/types/core/config/manager.d.ts.map +1 -1
  34. package/dist/types/core/providers/detector.d.ts +0 -2
  35. package/dist/types/core/providers/detector.d.ts.map +1 -1
  36. package/dist/types/core/providers/index.d.ts +0 -1
  37. package/dist/types/core/providers/index.d.ts.map +1 -1
  38. package/dist/types/index.d.ts +2 -32
  39. package/dist/types/index.d.ts.map +1 -1
  40. package/dist/types/types/index.d.ts +1 -1
  41. package/dist/types/utils/stop-reason-mapper.d.ts +1 -3
  42. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
  43. package/examples/README.md +274 -226
  44. package/examples/advanced.ts +123 -123
  45. package/examples/basic.ts +45 -45
  46. package/examples/getting_started.ts +41 -41
  47. package/examples/metadata.ts +68 -68
  48. package/examples/stream.ts +53 -53
  49. package/package.json +80 -72
@@ -1,226 +1,274 @@
1
- # Revenium Perplexity Middleware - Examples
2
-
3
- This directory contains examples demonstrating how to use the Revenium Perplexity middleware.
4
-
5
- ## Prerequisites
6
-
7
- Before running the examples, make sure you have:
8
-
9
- 1. **Node.js 16 or later** installed
10
- 2. **Revenium API Key** - Get one from [Revenium Dashboard](https://app.revenium.io)
11
- 3. **Perplexity API Key** - Get one from [Perplexity Platform](https://www.perplexity.ai)
12
-
13
- ## Setup
14
-
15
- 1. **Clone the repository** (if you haven't already):
16
-
17
- ```bash
18
- git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
19
- cd revenium-middleware-perplexity-node
20
- ```
21
-
22
- 2. **Install dependencies**:
23
-
24
- ```bash
25
- npm install
26
- ```
27
-
28
- 3. **Configure environment variables**:
29
-
30
- Copy the `.env.example` file to `.env` and edit it with your API keys:
31
-
32
- ```bash
33
- cp .env.example .env
34
- ```
35
-
36
- See [.env.example](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for all available configuration options.
37
-
38
- ## Examples
39
-
40
- ### 1. Getting Started
41
-
42
- **File:** `getting_started.ts`
43
-
44
- The simplest example to get you started with Revenium tracking:
45
-
46
- - Initialize the middleware
47
- - Create a basic chat completion
48
- - Display response and usage metrics
49
-
50
- **Run:**
51
-
52
- ```bash
53
- npm run example:getting-started
54
- # or
55
- npx tsx examples/getting_started.ts
56
- ```
57
-
58
- **What it does:**
59
-
60
- - Loads configuration from environment variables
61
- - Creates a simple chat completion request
62
- - Automatically sends metering data to Revenium API
63
- - Displays the response
64
-
65
- ---
66
-
67
- ### 2. Basic Usage
68
-
69
- **File:** `basic.ts`
70
-
71
- Demonstrates standard Perplexity API usage:
72
-
73
- - Chat completions with metadata
74
- - Simple metadata tracking
75
-
76
- **Run:**
77
-
78
- ```bash
79
- npm run example:basic
80
- # or
81
- npx tsx examples/basic.ts
82
- ```
83
-
84
- **What it does:**
85
-
86
- - Creates chat completions with metadata tracking
87
- - Demonstrates basic metadata usage
88
-
89
- ---
90
-
91
- ### 3. Metadata
92
-
93
- **File:** `metadata.ts`
94
-
95
- Demonstrates all available metadata fields:
96
-
97
- - Complete metadata structure
98
- - All optional fields documented
99
- - Subscriber information
100
-
101
- **Run:**
102
-
103
- ```bash
104
- npm run example:metadata
105
- # or
106
- npx tsx examples/metadata.ts
107
- ```
108
-
109
- **What it does:**
110
-
111
- - Shows all available metadata fields
112
- - Demonstrates subscriber tracking
113
- - Includes organization and product tracking
114
-
115
- **Metadata fields supported:**
116
-
117
- - `traceId` - Session or conversation tracking identifier
118
- - `taskType` - Type of AI task being performed
119
- - `agent` - AI agent or bot identifier
120
- - `organizationId` - Organization identifier
121
- - `productId` - Product or service identifier
122
- - `subscriptionId` - Subscription tier identifier
123
- - `responseQualityScore` - Quality rating (0.0-1.0)
124
- - `subscriber` - Nested subscriber object with `id`, `email`, `credential` (with `name` and `value`)
125
-
126
- ---
127
-
128
- ### 4. Streaming
129
-
130
- **File:** `stream.ts`
131
-
132
- Demonstrates streaming responses:
133
-
134
- - Real-time token streaming
135
- - Accumulating responses
136
- - Streaming metrics
137
-
138
- **Run:**
139
-
140
- ```bash
141
- npm run example:stream
142
- # or
143
- npx tsx examples/stream.ts
144
- ```
145
-
146
- **What it does:**
147
-
148
- - Creates a streaming chat completion
149
- - Displays tokens as they arrive in real-time
150
- - Tracks streaming metrics
151
- - Sends metering data after stream completes
152
-
153
- ---
154
-
155
- ### 5. Advanced Features
156
-
157
- **File:** `advanced.ts`
158
-
159
- Demonstrates advanced Perplexity features:
160
-
161
- - Multi-turn conversations
162
- - Different temperature settings
163
- - Multiple Perplexity models
164
-
165
- **Run:**
166
-
167
- ```bash
168
- npm run example:advanced
169
- # or
170
- npx tsx examples/advanced.ts
171
- ```
172
-
173
- **What it does:**
174
-
175
- - Demonstrates multi-turn conversation with system prompt
176
- - Shows creative responses with higher temperature
177
- - Uses sonar-reasoning model for complex reasoning tasks
178
-
179
- ---
180
-
181
- ## Common Issues
182
-
183
- ### "Client not initialized" error
184
-
185
- **Solution:** Make sure to call `Initialize()` before using `GetClient()`.
186
-
187
- ### "REVENIUM_METERING_API_KEY is required" error
188
-
189
- **Solution:** Set the `REVENIUM_METERING_API_KEY` environment variable in your `.env` file.
190
-
191
- ### "invalid Revenium API key format" error
192
-
193
- **Solution:** Revenium API keys should start with `hak_`. Check your API key format.
194
-
195
- ### Environment variables not loading
196
-
197
- **Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
198
-
199
- ### Perplexity API errors
200
-
201
- **Solution:** Make sure you have set `PERPLEXITY_API_KEY` in your `.env` file and that it starts with `pplx-`.
202
-
203
- ### Debug Mode
204
-
205
- Enable detailed logging to troubleshoot issues:
206
-
207
- ```bash
208
- # In .env file
209
- REVENIUM_DEBUG=true
210
-
211
- # Then run examples
212
- npm run example:getting-started
213
- ```
214
-
215
- ## Next Steps
216
-
217
- - Check the [main README](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/README.md) for detailed documentation
218
- - Visit the [Revenium Dashboard](https://app.revenium.io) to view your metering data
219
- - See [.env.example](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for all configuration options
220
-
221
- ## Support
222
-
223
- For issues or questions:
224
-
225
- - Documentation: https://docs.revenium.io
226
- - Email: support@revenium.io
1
+ # Revenium Perplexity Middleware - Examples
2
+
3
+ This directory contains examples demonstrating how to use the Revenium Perplexity middleware.
4
+
5
+ ## Prerequisites
6
+
7
+ Before running the examples, make sure you have:
8
+
9
+ 1. **Node.js 20+** installed
10
+ 2. **Revenium API Key** - Get one from [Revenium Dashboard](https://app.revenium.ai)
11
+ 3. **Perplexity API Key** - Get one from [Perplexity Platform](https://www.perplexity.ai)
12
+
13
+ ## Setup
14
+
15
+ Choose one of the following approaches:
16
+
17
+ ### Option 1: Create a New Project (Recommended)
18
+
19
+ Follow the step-by-step guide below to create a new project and install the middleware from npm.
20
+
21
+ ### Option 2: Clone the Repository (Optional)
22
+
23
+ If you want to explore the source code or contribute to the project:
24
+
25
+ ```bash
26
+ git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
27
+ cd revenium-middleware-perplexity-node
28
+ npm install
29
+ npm run build
30
+ ```
31
+
32
+ Then skip to [Step 3: Environment Setup](#3-environment-setup) below.
33
+
34
+ ---
35
+
36
+ ## Getting Started - Step by Step
37
+
38
+ ### 1. Create Your Project
39
+
40
+ ```bash
41
+ # Create project directory
42
+ mkdir my-perplexity-project
43
+ cd my-perplexity-project
44
+
45
+ # Initialize Node.js project
46
+ npm init -y
47
+ ```
48
+
49
+ ### 2. Install Dependencies
50
+
51
+ ```bash
52
+ npm install @revenium/perplexity openai
53
+ npm install -D typescript tsx @types/node # For TypeScript
54
+ ```
55
+
56
+ ### 3. Environment Setup
57
+
58
+ Create a `.env` file in the project root with your API keys:
59
+
60
+ ```bash
61
+ # Required
62
+ REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
63
+ PERPLEXITY_API_KEY=pplx_your_perplexity_api_key
64
+
65
+ # Optional
66
+ REVENIUM_METERING_BASE_URL=https://api.revenium.ai
67
+ PERPLEXITY_API_BASE_URL=https://api.perplexity.ai
68
+ REVENIUM_DEBUG=false
69
+ ```
70
+
71
+ ### 4. Run Examples
72
+
73
+ **If you cloned from GitHub:**
74
+
75
+ ```bash
76
+ # Run examples directly (or use npm run example:<name> scripts)
77
+ npx tsx examples/getting_started.ts
78
+ npx tsx examples/basic.ts
79
+ npx tsx examples/metadata.ts
80
+ npx tsx examples/stream.ts
81
+ npx tsx examples/advanced.ts
82
+ ```
83
+
84
+ **If you installed via npm:**
85
+
86
+ Examples are included in your `node_modules/@revenium/perplexity/examples/` directory:
87
+
88
+ ```bash
89
+ npx tsx node_modules/@revenium/perplexity/examples/getting_started.ts
90
+ npx tsx node_modules/@revenium/perplexity/examples/basic.ts
91
+ npx tsx node_modules/@revenium/perplexity/examples/metadata.ts
92
+ npx tsx node_modules/@revenium/perplexity/examples/stream.ts
93
+ npx tsx node_modules/@revenium/perplexity/examples/advanced.ts
94
+ ```
95
+
96
+ ## Examples
97
+
98
+ ### 1. Getting Started
99
+
100
+ **File:** `getting_started.ts`
101
+
102
+ The simplest example to get you started with Revenium tracking:
103
+
104
+ - Initialize the middleware
105
+ - Create a basic chat completion
106
+ - Display response and usage metrics
107
+
108
+ **Run:**
109
+
110
+ ```bash
111
+ npx tsx examples/getting_started.ts
112
+ ```
113
+
114
+ **What it does:**
115
+
116
+ - Loads configuration from environment variables
117
+ - Creates a simple chat completion request
118
+ - Automatically sends metering data to Revenium API
119
+ - Displays the response
120
+
121
+ ---
122
+
123
+ ### 2. Basic Usage
124
+
125
+ **File:** `basic.ts`
126
+
127
+ Demonstrates standard Perplexity API usage:
128
+
129
+ - Chat completions with metadata
130
+ - Simple metadata tracking
131
+
132
+ **Run:**
133
+
134
+ ```bash
135
+ npx tsx examples/basic.ts
136
+ ```
137
+
138
+ **What it does:**
139
+
140
+ - Creates chat completions with metadata tracking
141
+ - Demonstrates basic metadata usage
142
+
143
+ ---
144
+
145
+ ### 3. Metadata
146
+
147
+ **File:** `metadata.ts`
148
+
149
+ Demonstrates all available metadata fields:
150
+
151
+ - Complete metadata structure
152
+ - All optional fields documented
153
+ - Subscriber information
154
+
155
+ **Run:**
156
+
157
+ ```bash
158
+ npx tsx examples/metadata.ts
159
+ ```
160
+
161
+ **What it does:**
162
+
163
+ - Shows all available metadata fields
164
+ - Demonstrates subscriber tracking
165
+ - Includes organization and product tracking
166
+
167
+ **Metadata fields supported:**
168
+
169
+ - `traceId` - Session or conversation tracking identifier
170
+ - `taskType` - Type of AI task being performed
171
+ - `agent` - AI agent or bot identifier
172
+ - `organizationId` - Organization identifier
173
+ - `productId` - Product or service identifier
174
+ - `subscriptionId` - Subscription tier identifier
175
+ - `responseQualityScore` - Quality rating (0.0-1.0)
176
+ - `subscriber` - Nested subscriber object with `id`, `email`, `credential` (with `name` and `value`)
177
+
178
+ ---
179
+
180
+ ### 4. Streaming
181
+
182
+ **File:** `stream.ts`
183
+
184
+ Demonstrates streaming responses:
185
+
186
+ - Real-time token streaming
187
+ - Accumulating responses
188
+ - Streaming metrics
189
+
190
+ **Run:**
191
+
192
+ ```bash
193
+ npx tsx examples/stream.ts
194
+ ```
195
+
196
+ **What it does:**
197
+
198
+ - Creates a streaming chat completion
199
+ - Displays tokens as they arrive in real-time
200
+ - Tracks streaming metrics
201
+ - Sends metering data after stream completes
202
+
203
+ ---
204
+
205
+ ### 5. Advanced Features
206
+
207
+ **File:** `advanced.ts`
208
+
209
+ Demonstrates advanced Perplexity features:
210
+
211
+ - Multi-turn conversations
212
+ - Different temperature settings
213
+ - Multiple Perplexity models
214
+
215
+ **Run:**
216
+
217
+ ```bash
218
+ npx tsx examples/advanced.ts
219
+ ```
220
+
221
+ **What it does:**
222
+
223
+ - Demonstrates multi-turn conversation with system prompt
224
+ - Shows creative responses with higher temperature
225
+ - Uses sonar-reasoning model for complex reasoning tasks
226
+
227
+ ---
228
+
229
+ ## Common Issues
230
+
231
+ ### "Client not initialized" error
232
+
233
+ **Solution:** Make sure to call `Initialize()` before using `GetClient()`.
234
+
235
+ ### "REVENIUM_METERING_API_KEY is required" error
236
+
237
+ **Solution:** Set the `REVENIUM_METERING_API_KEY` environment variable in your `.env` file.
238
+
239
+ ### "invalid Revenium API key format" error
240
+
241
+ **Solution:** Revenium API keys should start with `hak_`. Check your API key format.
242
+
243
+ ### Environment variables not loading
244
+
245
+ **Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
246
+
247
+ ### Perplexity API errors
248
+
249
+ **Solution:** Make sure you have set `PERPLEXITY_API_KEY` in your `.env` file and that it starts with `pplx-`.
250
+
251
+ ### Debug Mode
252
+
253
+ Enable detailed logging to troubleshoot issues:
254
+
255
+ ```bash
256
+ # In .env file
257
+ REVENIUM_DEBUG=true
258
+
259
+ # Then run examples
260
+ npm run example:getting-started
261
+ ```
262
+
263
+ ## Next Steps
264
+
265
+ - Check the [main README](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/README.md) for detailed documentation
266
+ - Visit the [Revenium Dashboard](https://app.revenium.ai) to view your metering data
267
+ - See [.env.example](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for all configuration options
268
+
269
+ ## Support
270
+
271
+ For issues or questions:
272
+
273
+ - Documentation: https://docs.revenium.io
274
+ - Email: support@revenium.io