@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
package/dist/esm/index.js
CHANGED
|
@@ -36,7 +36,10 @@ export { initializePerplexityFromEnv, initializePerplexity, getPerplexityConfig,
|
|
|
36
36
|
// ============================================================================
|
|
37
37
|
export { createChatCompletion, createStreamingChatCompletion, } from "./core/wrapper/perplexity-client";
|
|
38
38
|
export { generateTransactionId, buildMeteringData, sendMeteringData, } from "./core/tracking/metering";
|
|
39
|
-
|
|
39
|
+
// ============================================================================
|
|
40
|
+
// Constants
|
|
41
|
+
// ============================================================================
|
|
42
|
+
export { PERPLEXITY_PATTERNS, DEFAULT_CONFIG, API_ENDPOINTS, MIDDLEWARE_INFO, } from "./constants";
|
|
40
43
|
// ============================================================================
|
|
41
44
|
// Utilities
|
|
42
45
|
// ============================================================================
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,MAAM,EAAE,CAAC;AAET,+EAA+E;AAC/E,gBAAgB;AAChB,+EAA+E;AAE/E,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,cAAc,GAEf,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,GAEpB,MAAM,iCAAiC,CAAC;AAEzC,+EAA+E;AAC/E,WAAW;AACX,+EAA+E;AAE/E,OAAO,EACL,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,MAAM,EAAE,CAAC;AAET,+EAA+E;AAC/E,gBAAgB;AAChB,+EAA+E;AAE/E,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,cAAc,GAEf,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,GAEpB,MAAM,iCAAiC,CAAC;AAEzC,+EAA+E;AAC/E,WAAW;AACX,+EAA+E;AAE/E,OAAO,EACL,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAmBlC,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/esm/types/index.js
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* TypeScript Type Definitions for Perplexity Middleware
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
// Constants
|
|
6
|
-
// ============================================================================
|
|
7
|
-
export const PERPLEXITY_MODELS = {
|
|
8
|
-
// Sonar Models (Online)
|
|
9
|
-
SONAR: "sonar",
|
|
10
|
-
SONAR_PRO: "sonar-pro",
|
|
11
|
-
SONAR_REASONING: "sonar-reasoning",
|
|
12
|
-
// Chat Models (Offline)
|
|
13
|
-
LLAMA_3_1_SONAR_SMALL_128K_CHAT: "llama-3.1-sonar-small-128k-chat",
|
|
14
|
-
LLAMA_3_1_SONAR_LARGE_128K_CHAT: "llama-3.1-sonar-large-128k-chat",
|
|
15
|
-
LLAMA_3_1_SONAR_HUGE_128K_CHAT: "llama-3.1-sonar-huge-128k-chat",
|
|
16
|
-
};
|
|
17
|
-
export const DEFAULT_MODEL = PERPLEXITY_MODELS.SONAR_PRO;
|
|
4
|
+
export {};
|
|
18
5
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration constants for Revenium Perplexity middleware
|
|
3
|
+
* Centralizes all magic numbers and default values
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Middleware identification
|
|
7
|
+
*
|
|
8
|
+
* Per Revenium API spec (https://revenium.readme.io/reference/meter_ai_completion):
|
|
9
|
+
* "middlewareSource: Identifier of the Revenium middleware package or SDK that
|
|
10
|
+
* captured and submitted this AI completion metadata. This field is AUTOMATICALLY
|
|
11
|
+
* SET by Revenium's middleware packages (e.g., 'revenium-openai-python',
|
|
12
|
+
* 'revenium-anthropic-node')."
|
|
13
|
+
*/
|
|
14
|
+
export declare const MIDDLEWARE_INFO: {
|
|
15
|
+
/** Package name */
|
|
16
|
+
readonly NAME: "@revenium/perplexity";
|
|
17
|
+
/** Package version */
|
|
18
|
+
readonly VERSION: "2.0.4";
|
|
19
|
+
/** Full middleware source identifier for API tracking */
|
|
20
|
+
readonly SOURCE: "@revenium/perplexity-2.0.4";
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Perplexity model patterns and API response mappings
|
|
24
|
+
*
|
|
25
|
+
* Per Revenium API spec (https://revenium.readme.io/reference/meter_ai_completion):
|
|
26
|
+
* stopReason enum: "END", "END_SEQUENCE", "TIMEOUT", "TOKEN_LIMIT",
|
|
27
|
+
* "COST_LIMIT", "COMPLETION_LIMIT", "ERROR", "CANCELLED"
|
|
28
|
+
*
|
|
29
|
+
* We map Perplexity's finish_reason values to these Revenium enums.
|
|
30
|
+
* Only provider-reported values are mapped; middleware-level values
|
|
31
|
+
* (TIMEOUT, COST_LIMIT, etc.) are not applicable to provider responses.
|
|
32
|
+
*/
|
|
33
|
+
export declare const PERPLEXITY_PATTERNS: {
|
|
34
|
+
/** Known Perplexity/OpenAI finish reasons */
|
|
35
|
+
readonly FINISH_REASONS: {
|
|
36
|
+
readonly STOP: "stop";
|
|
37
|
+
readonly LENGTH: "length";
|
|
38
|
+
readonly TOOL_CALLS: "tool_calls";
|
|
39
|
+
readonly CONTENT_FILTER: "content_filter";
|
|
40
|
+
readonly FUNCTION_CALL: "function_call";
|
|
41
|
+
};
|
|
42
|
+
/** Revenium stop reason mappings from Perplexity finish_reason to Revenium enum */
|
|
43
|
+
readonly REVENIUM_STOP_REASON_MAP: {
|
|
44
|
+
readonly stop: "END";
|
|
45
|
+
readonly length: "TOKEN_LIMIT";
|
|
46
|
+
readonly tool_calls: "END";
|
|
47
|
+
readonly content_filter: "END";
|
|
48
|
+
readonly function_call: "END";
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Default configuration values
|
|
53
|
+
*/
|
|
54
|
+
export declare const DEFAULT_CONFIG: {
|
|
55
|
+
/** Default Revenium API base URL */
|
|
56
|
+
readonly REVENIUM_BASE_URL: "https://api.revenium.ai";
|
|
57
|
+
/** Default Perplexity API base URL */
|
|
58
|
+
readonly PERPLEXITY_BASE_URL: "https://api.perplexity.ai";
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* API endpoints
|
|
62
|
+
*/
|
|
63
|
+
export declare const API_ENDPOINTS: {
|
|
64
|
+
/** Revenium AI completions endpoint */
|
|
65
|
+
readonly AI_COMPLETIONS: "/meter/v2/ai/completions";
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe;IAC1B,mBAAmB;;IAEnB,sBAAsB;;IAEtB,yDAAyD;;CAEjD,CAAC;AAEX;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB;IAC9B,6CAA6C;;;;;;;;IAS7C,mFAAmF;;;;;;;;CAQ3E,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,cAAc;IACzB,oCAAoC;;IAGpC,sCAAsC;;CAE9B,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,aAAa;IACxB,uCAAuC;;CAE/B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metering.d.ts","sourceRoot":"","sources":["../../../../src/core/tracking/metering.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"metering.d.ts","sourceRoot":"","sources":["../../../../src/core/tracking/metering.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG/D;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAsGD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,EAAE,IAAI,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B,GAAG,YAAY,CA+Ff;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,YAAY,EAAE,YAAY,GACzB,OAAO,CAAC,IAAI,CAAC,CA0Cf"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export { initializeReveniumFromEnv, initializeRevenium, getReveniumConfig, isRev
|
|
|
28
28
|
export { initializePerplexityFromEnv, initializePerplexity, getPerplexityConfig, type PerplexityConfig, } from "./core/config/perplexity-config";
|
|
29
29
|
export { createChatCompletion, createStreamingChatCompletion, } from "./core/wrapper/perplexity-client";
|
|
30
30
|
export { generateTransactionId, buildMeteringData, sendMeteringData, } from "./core/tracking/metering";
|
|
31
|
-
export type { PerplexityMessage, PerplexityChatRequest, PerplexityUsage, PerplexityChoice, PerplexityResponse, PerplexityStreamChunk, UsageMetadata, MeteringData, ChatCompletionResult, StreamingResult,
|
|
32
|
-
export {
|
|
31
|
+
export type { PerplexityMessage, PerplexityChatRequest, PerplexityUsage, PerplexityChoice, PerplexityResponse, PerplexityStreamChunk, UsageMetadata, MeteringData, ChatCompletionResult, StreamingResult, } from "./types";
|
|
32
|
+
export { PERPLEXITY_PATTERNS, DEFAULT_CONFIG, API_ENDPOINTS, MIDDLEWARE_INFO, } from "./constants";
|
|
33
33
|
export { logger } from "./utils/logger";
|
|
34
34
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AASH,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,KAAK,cAAc,GACpB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,gBAAgB,GACtB,MAAM,iCAAiC,CAAC;AAMzC,OAAO,EACL,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAMlC,YAAY,EACV,iBAAiB,EACjB,qBAAqB,EACrB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,aAAa,EACb,YAAY,EACZ,oBAAoB,EACpB,eAAe,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AASH,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,KAAK,cAAc,GACpB,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,gBAAgB,GACtB,MAAM,iCAAiC,CAAC;AAMzC,OAAO,EACL,oBAAoB,EACpB,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAMlC,YAAY,EACV,iBAAiB,EACjB,qBAAqB,EACrB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,aAAa,EACb,YAAY,EACZ,oBAAoB,EACpB,eAAe,GAChB,MAAM,SAAS,CAAC;AAMjB,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,eAAe,GAChB,MAAM,aAAa,CAAC;AAMrB,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -57,6 +57,7 @@ export interface UsageMetadata {
|
|
|
57
57
|
};
|
|
58
58
|
};
|
|
59
59
|
organizationId?: string;
|
|
60
|
+
subscriptionId?: string;
|
|
60
61
|
productId?: string;
|
|
61
62
|
transactionId?: string;
|
|
62
63
|
traceId?: string;
|
|
@@ -75,6 +76,7 @@ export interface UsageMetadata {
|
|
|
75
76
|
agent?: string;
|
|
76
77
|
stopReason?: string;
|
|
77
78
|
costType?: string;
|
|
79
|
+
responseQualityScore?: number;
|
|
78
80
|
subscriberId?: string;
|
|
79
81
|
subscriberEmail?: string;
|
|
80
82
|
subscriberCredentialName?: string;
|
|
@@ -115,7 +117,7 @@ export interface MeteringData {
|
|
|
115
117
|
requestTime: string;
|
|
116
118
|
completionStartTime: string;
|
|
117
119
|
timeToFirstToken: number;
|
|
118
|
-
|
|
120
|
+
middlewareSource: string;
|
|
119
121
|
traceId?: string;
|
|
120
122
|
}
|
|
121
123
|
export interface ChatCompletionResult {
|
|
@@ -146,14 +148,4 @@ export interface PerplexityConfig {
|
|
|
146
148
|
apiKey: string;
|
|
147
149
|
baseUrl: string;
|
|
148
150
|
}
|
|
149
|
-
export declare const PERPLEXITY_MODELS: {
|
|
150
|
-
readonly SONAR: "sonar";
|
|
151
|
-
readonly SONAR_PRO: "sonar-pro";
|
|
152
|
-
readonly SONAR_REASONING: "sonar-reasoning";
|
|
153
|
-
readonly LLAMA_3_1_SONAR_SMALL_128K_CHAT: "llama-3.1-sonar-small-128k-chat";
|
|
154
|
-
readonly LLAMA_3_1_SONAR_LARGE_128K_CHAT: "llama-3.1-sonar-large-128k-chat";
|
|
155
|
-
readonly LLAMA_3_1_SONAR_HUGE_128K_CHAT: "llama-3.1-sonar-huge-128k-chat";
|
|
156
|
-
};
|
|
157
|
-
export type PerplexityModel = (typeof PERPLEXITY_MODELS)[keyof typeof PERPLEXITY_MODELS];
|
|
158
|
-
export declare const DEFAULT_MODEL: PerplexityModel;
|
|
159
151
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,MAAM,WAAW,iBAAiB;IAChC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AM,WAAW,cAAc;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
# Running Examples - Revenium Middleware for Perplexity (Node.js)
|
|
2
|
+
|
|
3
|
+
This guide shows you how to run the Node.js/TypeScript examples in this repository.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 20+ installed
|
|
8
|
+
- Perplexity API key
|
|
9
|
+
- Revenium API key
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
### 1. Clone the Repository
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
|
|
17
|
+
cd revenium-middleware-perplexity-node
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 2. Install Dependencies
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 3. Configure Environment Variables
|
|
27
|
+
|
|
28
|
+
Create a `.env` file in the root directory:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# Create .env file
|
|
32
|
+
cp .env.example .env
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
See [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for the complete list of environment variables and where to get your API keys.
|
|
36
|
+
|
|
37
|
+
**Replace the placeholder values with your actual keys!**
|
|
38
|
+
|
|
39
|
+
## Running Examples
|
|
40
|
+
|
|
41
|
+
### Available Examples
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm run example:basic # Basic chat completion
|
|
45
|
+
npm run example:streaming # Streaming response
|
|
46
|
+
npm run example:chat # Multi-turn conversation
|
|
47
|
+
npm run example:metadata # With custom metadata
|
|
48
|
+
npm run example:advanced # Advanced features
|
|
49
|
+
npm run example:getting-started # Quick start example
|
|
50
|
+
npm test # Run all examples
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Example Structure
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
examples/
|
|
57
|
+
├── basic.ts # Basic chat completion
|
|
58
|
+
├── streaming.ts # Streaming response
|
|
59
|
+
├── chat.ts # Multi-turn conversation
|
|
60
|
+
├── metadata.ts # With custom metadata
|
|
61
|
+
├── advanced-features.ts # Advanced features
|
|
62
|
+
├── getting_started.ts # Quick start example
|
|
63
|
+
└── README.md # This file
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Examples Included
|
|
67
|
+
|
|
68
|
+
### 1. Basic Usage
|
|
69
|
+
|
|
70
|
+
**Location**: `examples/basic.ts`
|
|
71
|
+
|
|
72
|
+
**What it demonstrates:**
|
|
73
|
+
|
|
74
|
+
- Simple chat completion
|
|
75
|
+
- Automatic metering
|
|
76
|
+
- Basic error handling
|
|
77
|
+
|
|
78
|
+
**Run it:**
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm run example:basic
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Expected output:**
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
✓ Chat Completion Successful
|
|
88
|
+
Response: [AI response here]
|
|
89
|
+
Model: sonar-pro
|
|
90
|
+
Tokens Used: 50 (prompt: 10, completion: 40)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
### 2. Streaming
|
|
96
|
+
|
|
97
|
+
**Location**: `examples/streaming.ts`
|
|
98
|
+
|
|
99
|
+
**What it demonstrates:**
|
|
100
|
+
|
|
101
|
+
- Real-time streaming responses
|
|
102
|
+
- Token tracking for streamed content
|
|
103
|
+
- Automatic usage tracking when stream completes
|
|
104
|
+
|
|
105
|
+
**Run it:**
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
npm run example:streaming
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Expected output:**
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
Model: sonar-pro
|
|
115
|
+
Streaming response:
|
|
116
|
+
|
|
117
|
+
[Streaming text appears here in real-time]
|
|
118
|
+
|
|
119
|
+
✓ Streaming completed
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
### 3. Multi-turn Chat
|
|
125
|
+
|
|
126
|
+
**Location**: `examples/chat.ts`
|
|
127
|
+
|
|
128
|
+
**What it demonstrates:**
|
|
129
|
+
|
|
130
|
+
- Conversation history management
|
|
131
|
+
- Context-aware responses
|
|
132
|
+
- Multi-message interactions
|
|
133
|
+
|
|
134
|
+
**Run it:**
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm run example:chat
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Expected output:**
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
--- Turn 1 ---
|
|
144
|
+
User: What is the capital of France?
|
|
145
|
+
Assistant: [Response]
|
|
146
|
+
|
|
147
|
+
--- Turn 2 ---
|
|
148
|
+
User: What is its population?
|
|
149
|
+
Assistant: [Response]
|
|
150
|
+
|
|
151
|
+
✓ Chat completed - Total tokens used: 120
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
### 4. Custom Metadata
|
|
157
|
+
|
|
158
|
+
**Location**: `examples/metadata.ts`
|
|
159
|
+
|
|
160
|
+
**What it demonstrates:**
|
|
161
|
+
|
|
162
|
+
- Custom metadata tracking
|
|
163
|
+
- Business context in metering
|
|
164
|
+
- Subscriber and organization tracking
|
|
165
|
+
|
|
166
|
+
**Run it:**
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
npm run example:metadata
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Expected output:**
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
✓ Chat Completion Successful
|
|
176
|
+
Response: [AI response]
|
|
177
|
+
Model: sonar-pro
|
|
178
|
+
Tokens Used: 50 (prompt: 10, completion: 40)
|
|
179
|
+
Metadata tracked: subscriber, organization, product, agent, trace
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Note:** Custom metadata (subscriber, organizationId, productId, agent, traceId) is automatically sent to Revenium in the background.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
// Manual configuration
|
|
187
|
+
initializeRevenium({
|
|
188
|
+
meteringApiKey: process.env.REVENIUM_METERING_API_KEY!,
|
|
189
|
+
meteringBaseUrl:
|
|
190
|
+
process.env.REVENIUM_METERING_BASE_URL || "https://api.revenium.ai",
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
**Location**: `examples/advanced-features.ts`
|
|
194
|
+
|
|
195
|
+
**What it demonstrates:**
|
|
196
|
+
|
|
197
|
+
- Streaming with metadata
|
|
198
|
+
- Multiple model comparisons
|
|
199
|
+
- Advanced configuration
|
|
200
|
+
|
|
201
|
+
**Run it:**
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
npm run example:advanced
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Expected output:**
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
=== Advanced Features Example ===
|
|
211
|
+
|
|
212
|
+
--- Streaming Responses ---
|
|
213
|
+
Model: sonar-pro
|
|
214
|
+
Response:
|
|
215
|
+
[Streaming text appears here in real-time]
|
|
216
|
+
✓ Streaming completed
|
|
217
|
+
|
|
218
|
+
--- Comprehensive Metadata Tracking ---
|
|
219
|
+
Response: [First 100 characters]...
|
|
220
|
+
✓ Metadata tracked successfully
|
|
221
|
+
|
|
222
|
+
--- Multiple Model Types ---
|
|
223
|
+
Using sonar (lightweight search)...
|
|
224
|
+
Response: [First 80 characters]...
|
|
225
|
+
|
|
226
|
+
Using sonar-reasoning (real-time reasoning)...
|
|
227
|
+
Response: [First 80 characters]...
|
|
228
|
+
|
|
229
|
+
✓ Multiple models demonstrated
|
|
230
|
+
|
|
231
|
+
=== All Advanced Features Demonstrated ===
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
### 6. Getting Started
|
|
237
|
+
|
|
238
|
+
**Location**: `examples/getting_started.ts`
|
|
239
|
+
|
|
240
|
+
**What it demonstrates:**
|
|
241
|
+
|
|
242
|
+
- Quick start example
|
|
243
|
+
- Minimal configuration
|
|
244
|
+
- Basic usage pattern
|
|
245
|
+
|
|
246
|
+
**Run it:**
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm run example:getting-started
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## Troubleshooting
|
|
255
|
+
|
|
256
|
+
### "Missing API Key" Error
|
|
257
|
+
|
|
258
|
+
Make sure you've created a `.env` file with your API keys:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
# Check if .env exists
|
|
262
|
+
ls .env
|
|
263
|
+
|
|
264
|
+
# Verify it has the required keys
|
|
265
|
+
cat .env
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### "Module not found" Error
|
|
269
|
+
|
|
270
|
+
Install dependencies first:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
npm install
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### "Requests not being tracked"
|
|
277
|
+
|
|
278
|
+
1. Check your `.env` file has correct values:
|
|
279
|
+
|
|
280
|
+
- `PERPLEXITY_API_KEY` is set
|
|
281
|
+
- `REVENIUM_METERING_API_KEY` is set
|
|
282
|
+
- `REVENIUM_METERING_BASE_URL` is set to `https://api.revenium.ai`
|
|
283
|
+
|
|
284
|
+
2. Enable debug logging:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
DEBUG=true npm run example:basic
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### TypeScript Errors
|
|
291
|
+
|
|
292
|
+
Ensure you have TypeScript and tsx installed:
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
npm install -D typescript tsx @types/node
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## Getting Your API Keys
|
|
301
|
+
|
|
302
|
+
### Perplexity API Key
|
|
303
|
+
|
|
304
|
+
1. Go to [Perplexity AI Settings](https://www.perplexity.ai/settings/api)
|
|
305
|
+
2. Create a new API key
|
|
306
|
+
3. Add to `.env` as `PERPLEXITY_API_KEY`
|
|
307
|
+
|
|
308
|
+
### Revenium API Key
|
|
309
|
+
|
|
310
|
+
1. Sign up at [Revenium](https://ai.revenium.io)
|
|
311
|
+
2. Create a new API key in your dashboard
|
|
312
|
+
3. Add to `.env` as `REVENIUM_METERING_API_KEY`
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## Support
|
|
317
|
+
|
|
318
|
+
For issues or questions:
|
|
319
|
+
|
|
320
|
+
- [GitHub Issues](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
321
|
+
- [Documentation](https://docs.revenium.io)
|
|
322
|
+
- Contact: Reach out to the Revenium team for additional support
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced Features Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates advanced middleware features:
|
|
5
|
+
* - Streaming responses with real-time content
|
|
6
|
+
* - Comprehensive metadata for analytics
|
|
7
|
+
* - Multiple model types (search, reasoning, research)
|
|
8
|
+
* - Session and quality tracking
|
|
9
|
+
*
|
|
10
|
+
* For complete documentation, see:
|
|
11
|
+
* https://revenium.readme.io/reference/meter_ai_completion
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
initializeReveniumFromEnv,
|
|
16
|
+
initializePerplexityFromEnv,
|
|
17
|
+
createChatCompletion,
|
|
18
|
+
createStreamingChatCompletion,
|
|
19
|
+
UsageMetadata,
|
|
20
|
+
} from "../src";
|
|
21
|
+
|
|
22
|
+
async function demonstrateStreaming() {
|
|
23
|
+
console.log("\n--- Streaming Responses ---");
|
|
24
|
+
const usageMetadata: UsageMetadata = {
|
|
25
|
+
subscriber: { id: "user-streaming-demo" },
|
|
26
|
+
traceId: "streaming-session-" + Date.now(),
|
|
27
|
+
taskType: "list-generation",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const result = await createStreamingChatCompletion({
|
|
31
|
+
messages: [
|
|
32
|
+
{
|
|
33
|
+
role: "user",
|
|
34
|
+
content: "List the first 3 planets in our solar system.",
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
model: "sonar-pro",
|
|
38
|
+
maxTokens: 400,
|
|
39
|
+
usageMetadata,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(`Model: ${result.model}`);
|
|
43
|
+
console.log("Response: ");
|
|
44
|
+
|
|
45
|
+
let fullContent = "";
|
|
46
|
+
for await (const chunk of result.stream) {
|
|
47
|
+
const content = chunk.choices[0]?.delta?.content || "";
|
|
48
|
+
if (content) {
|
|
49
|
+
process.stdout.write(content);
|
|
50
|
+
fullContent += content;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
console.log("\n✓ Streaming completed\n");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function demonstrateComprehensiveMetadata() {
|
|
58
|
+
console.log("\n--- Comprehensive Metadata Tracking ---");
|
|
59
|
+
|
|
60
|
+
const usageMetadata: UsageMetadata = {
|
|
61
|
+
traceId: "session-advanced-demo-" + Date.now(),
|
|
62
|
+
taskType: "educational-query",
|
|
63
|
+
subscriber: {
|
|
64
|
+
id: "user-456",
|
|
65
|
+
email: "advanced-user@example.com",
|
|
66
|
+
credential: {
|
|
67
|
+
name: "premium-api-key",
|
|
68
|
+
value: "key-premium-abc123",
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
organizationId: "org-enterprise-789",
|
|
72
|
+
subscriptionId: "sub-enterprise-2024",
|
|
73
|
+
productId: "learning-platform",
|
|
74
|
+
agent: "educational-assistant-v2",
|
|
75
|
+
responseQualityScore: 0.92,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const result = await createChatCompletion({
|
|
79
|
+
messages: [
|
|
80
|
+
{
|
|
81
|
+
role: "user",
|
|
82
|
+
content: "What is machine learning?",
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
model: "sonar-pro",
|
|
86
|
+
maxTokens: 400,
|
|
87
|
+
usageMetadata,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
console.log(`Response: ${result.content.substring(0, 100)}...`);
|
|
91
|
+
console.log(`✓ Metadata tracked successfully\n`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function demonstrateMultipleModels() {
|
|
95
|
+
console.log("\n--- Multiple Model Types ---");
|
|
96
|
+
|
|
97
|
+
const usageMetadata: UsageMetadata = {
|
|
98
|
+
subscriber: { id: "model-comparison-user" },
|
|
99
|
+
taskType: "weather-search",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Search model
|
|
103
|
+
console.log("Using sonar (lightweight search)...");
|
|
104
|
+
const searchResult = await createChatCompletion({
|
|
105
|
+
messages: [{ role: "user", content: "Current weather in Tokyo" }],
|
|
106
|
+
model: "sonar-pro",
|
|
107
|
+
maxTokens: 400,
|
|
108
|
+
usageMetadata,
|
|
109
|
+
});
|
|
110
|
+
console.log(`Response: ${searchResult.content.substring(0, 80)}...`);
|
|
111
|
+
|
|
112
|
+
// Reasoning model
|
|
113
|
+
console.log("\nUsing sonar-reasoning (real-time reasoning)...");
|
|
114
|
+
const reasoningResult = await createChatCompletion({
|
|
115
|
+
messages: [
|
|
116
|
+
{ role: "user", content: "Why is the sky blue? Explain briefly." },
|
|
117
|
+
],
|
|
118
|
+
model: "sonar-reasoning",
|
|
119
|
+
maxTokens: 400,
|
|
120
|
+
usageMetadata: {
|
|
121
|
+
...usageMetadata,
|
|
122
|
+
taskType: "reasoning-query",
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
console.log(`Response: ${reasoningResult.content.substring(0, 80)}...`);
|
|
126
|
+
|
|
127
|
+
console.log("\n✓ Multiple models demonstrated\n");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function main() {
|
|
131
|
+
console.log("\n=== Advanced Features Example ===");
|
|
132
|
+
|
|
133
|
+
// Initialize configurations
|
|
134
|
+
initializeReveniumFromEnv();
|
|
135
|
+
initializePerplexityFromEnv();
|
|
136
|
+
|
|
137
|
+
// Demonstrate features
|
|
138
|
+
await demonstrateStreaming();
|
|
139
|
+
await demonstrateComprehensiveMetadata();
|
|
140
|
+
await demonstrateMultipleModels();
|
|
141
|
+
|
|
142
|
+
console.log("=== All Advanced Features Demonstrated ===\n");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
main().catch((error) => {
|
|
146
|
+
console.error("\nError:", error.message);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
});
|