@pga-ai/adapters-llm-perplexity 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @pga-ai/adapters-llm-perplexity
|
|
2
|
+
|
|
3
|
+
> Perplexity adapter for GSEP — web-search powered self-evolving agents
|
|
4
|
+
|
|
5
|
+
Your agent can search the web in real-time while evolving its prompts.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @pga-ai/core @pga-ai/adapters-llm-perplexity
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { PGA } from '@pga-ai/core';
|
|
17
|
+
import { PerplexityAdapter } from '@pga-ai/adapters-llm-perplexity';
|
|
18
|
+
|
|
19
|
+
const pga = new PGA({
|
|
20
|
+
llm: new PerplexityAdapter({
|
|
21
|
+
apiKey: process.env.PERPLEXITY_API_KEY!,
|
|
22
|
+
model: 'sonar-pro',
|
|
23
|
+
}),
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Configuration
|
|
28
|
+
|
|
29
|
+
| Option | Type | Default | Description |
|
|
30
|
+
|--------|------|---------|-------------|
|
|
31
|
+
| `apiKey` | `string` | required | Perplexity API key |
|
|
32
|
+
| `model` | `string` | `'sonar-pro'` | Model to use |
|
|
33
|
+
| `temperature` | `number` | `0.7` | Temperature (0-2) |
|
|
34
|
+
| `searchRecency` | `string` | — | Filter results: `'month'`, `'week'`, `'day'`, `'hour'` |
|
|
35
|
+
|
|
36
|
+
## Supported Models
|
|
37
|
+
|
|
38
|
+
- `sonar-pro` (recommended — best quality with web search)
|
|
39
|
+
- `sonar` (fast with web search)
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LLMAdapter, Message, ChatOptions, ChatResponse, ChatChunk } from '@pga-ai/core';
|
|
2
|
+
export interface PerplexityAdapterConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
model?: string;
|
|
5
|
+
temperature?: number;
|
|
6
|
+
searchRecency?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class PerplexityAdapter implements LLMAdapter {
|
|
9
|
+
readonly name = "perplexity";
|
|
10
|
+
readonly model: string;
|
|
11
|
+
private client;
|
|
12
|
+
private config;
|
|
13
|
+
constructor(config: PerplexityAdapterConfig);
|
|
14
|
+
chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse>;
|
|
15
|
+
stream(messages: Message[], options?: ChatOptions): AsyncIterableIterator<ChatChunk>;
|
|
16
|
+
estimateCost(messages: Message[]): Promise<number>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EACR,UAAU,EACV,OAAO,EACP,WAAW,EACX,YAAY,EACZ,SAAS,EACZ,MAAM,cAAc,CAAC;AAEtB,MAAM,WAAW,uBAAuB;IAIpC,MAAM,EAAE,MAAM,CAAC;IAMf,KAAK,CAAC,EAAE,MAAM,CAAC;IAMf,WAAW,CAAC,EAAE,MAAM,CAAC;IAMrB,aAAa,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,qBAAa,iBAAkB,YAAW,UAAU;IAChD,QAAQ,CAAC,IAAI,gBAAgB;IAC7B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAA0B;gBAE5B,MAAM,EAAE,uBAAuB;IAUrC,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAuCtE,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,qBAAqB,CAAC,SAAS,CAAC;IAiCrF,YAAY,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;CAY3D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
export class PerplexityAdapter {
|
|
3
|
+
name = 'perplexity';
|
|
4
|
+
model;
|
|
5
|
+
client;
|
|
6
|
+
config;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
this.model = config.model || 'sonar-pro';
|
|
10
|
+
this.client = new OpenAI({
|
|
11
|
+
apiKey: config.apiKey,
|
|
12
|
+
baseURL: 'https://api.perplexity.ai',
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async chat(messages, options) {
|
|
16
|
+
const openaiMessages = messages.map(m => ({
|
|
17
|
+
role: m.role,
|
|
18
|
+
content: m.content,
|
|
19
|
+
}));
|
|
20
|
+
try {
|
|
21
|
+
const response = await this.client.chat.completions.create({
|
|
22
|
+
model: this.model,
|
|
23
|
+
messages: openaiMessages,
|
|
24
|
+
temperature: options?.temperature ?? this.config.temperature ?? 0.7,
|
|
25
|
+
max_tokens: options?.maxTokens ?? 4096,
|
|
26
|
+
stream: false,
|
|
27
|
+
});
|
|
28
|
+
const choice = response.choices[0];
|
|
29
|
+
if (!choice) {
|
|
30
|
+
throw new Error('No response from Perplexity');
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
content: choice.message.content || '',
|
|
34
|
+
usage: {
|
|
35
|
+
inputTokens: response.usage?.prompt_tokens ?? 0,
|
|
36
|
+
outputTokens: response.usage?.completion_tokens ?? 0,
|
|
37
|
+
},
|
|
38
|
+
metadata: {
|
|
39
|
+
model: this.model,
|
|
40
|
+
finishReason: choice.finish_reason || 'stop',
|
|
41
|
+
citations: response.citations ?? [],
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`Perplexity API error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async *stream(messages, options) {
|
|
50
|
+
const openaiMessages = messages.map(m => ({
|
|
51
|
+
role: m.role,
|
|
52
|
+
content: m.content,
|
|
53
|
+
}));
|
|
54
|
+
try {
|
|
55
|
+
const stream = await this.client.chat.completions.create({
|
|
56
|
+
model: this.model,
|
|
57
|
+
messages: openaiMessages,
|
|
58
|
+
temperature: options?.temperature ?? this.config.temperature ?? 0.7,
|
|
59
|
+
max_tokens: options?.maxTokens ?? 4096,
|
|
60
|
+
stream: true,
|
|
61
|
+
});
|
|
62
|
+
for await (const chunk of stream) {
|
|
63
|
+
const delta = chunk.choices[0]?.delta;
|
|
64
|
+
if (!delta?.content) {
|
|
65
|
+
if (chunk.choices[0]?.finish_reason) {
|
|
66
|
+
yield { delta: '', done: true };
|
|
67
|
+
}
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
yield { delta: delta.content, done: false };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
throw new Error(`Perplexity streaming error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async estimateCost(messages) {
|
|
78
|
+
const totalChars = messages.reduce((sum, m) => sum + m.content.length, 0);
|
|
79
|
+
const estimatedTokens = Math.ceil(totalChars / 4);
|
|
80
|
+
const inputCost = (estimatedTokens * 0.7 * 3) / 1_000_000;
|
|
81
|
+
const outputCost = (estimatedTokens * 0.3 * 15) / 1_000_000;
|
|
82
|
+
return inputCost + outputCost;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAkC5B,MAAM,OAAO,iBAAiB;IACjB,IAAI,GAAG,YAAY,CAAC;IACpB,KAAK,CAAS;IAEf,MAAM,CAAS;IACf,MAAM,CAA0B;IAExC,YAAY,MAA+B;QACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,WAAW,CAAC;QAEzC,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACrB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,2BAA2B;SACvC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,QAAmB,EAAE,OAAqB;QACjD,MAAM,cAAc,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACtC,IAAI,EAAE,CAAC,CAAC,IAAuC;YAC/C,OAAO,EAAE,CAAC,CAAC,OAAO;SACrB,CAAC,CAAC,CAAC;QAEJ,IAAI,CAAC;YACD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;gBACvD,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,QAAQ,EAAE,cAAc;gBACxB,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,GAAG;gBACnE,UAAU,EAAE,OAAO,EAAE,SAAS,IAAI,IAAI;gBACtC,MAAM,EAAE,KAAK;aAChB,CAAC,CAAC;YAEH,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAA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}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pga-ai/adapters-llm-perplexity",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Perplexity adapter for GSEP — web-search powered responses",
|
|
5
|
+
"author": "Luis Alfredo Velasquez Duran <contact@gsepcore.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://gsepcore.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/LuisvelMarketer/pga-platform",
|
|
11
|
+
"directory": "packages/adapters-llm/perplexity"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/LuisvelMarketer/pga-platform/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc --build",
|
|
30
|
+
"clean": "rm -rf dist",
|
|
31
|
+
"dev": "tsc --watch"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"openai": "^4.77.3",
|
|
35
|
+
"@pga-ai/core": "*"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"typescript": "^5.6.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|