@probeo/anymodel 0.3.1 → 0.4.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 +6 -0
- package/dist/cli.cjs +189 -2
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +189 -2
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +189 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +189 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1347,6 +1347,190 @@ function createGoogleAdapter(apiKey) {
|
|
|
1347
1347
|
return adapter;
|
|
1348
1348
|
}
|
|
1349
1349
|
|
|
1350
|
+
// src/providers/perplexity.ts
|
|
1351
|
+
var PERPLEXITY_API_BASE = "https://api.perplexity.ai";
|
|
1352
|
+
var SUPPORTED_PARAMS4 = /* @__PURE__ */ new Set([
|
|
1353
|
+
"temperature",
|
|
1354
|
+
"max_tokens",
|
|
1355
|
+
"top_p",
|
|
1356
|
+
"frequency_penalty",
|
|
1357
|
+
"presence_penalty",
|
|
1358
|
+
"stream",
|
|
1359
|
+
"stop",
|
|
1360
|
+
"response_format",
|
|
1361
|
+
"tools",
|
|
1362
|
+
"tool_choice"
|
|
1363
|
+
]);
|
|
1364
|
+
var MODELS = [
|
|
1365
|
+
{ id: "sonar", name: "Sonar", context: 128e3, maxOutput: 4096, modality: "text->text", inputModalities: ["text"] },
|
|
1366
|
+
{ id: "sonar-pro", name: "Sonar Pro", context: 2e5, maxOutput: 8192, modality: "text->text", inputModalities: ["text"] },
|
|
1367
|
+
{ id: "sonar-reasoning", name: "Sonar Reasoning", context: 128e3, maxOutput: 8192, modality: "text->text", inputModalities: ["text"] },
|
|
1368
|
+
{ id: "sonar-reasoning-pro", name: "Sonar Reasoning Pro", context: 128e3, maxOutput: 16384, modality: "text->text", inputModalities: ["text"] },
|
|
1369
|
+
{ id: "sonar-deep-research", name: "Sonar Deep Research", context: 128e3, maxOutput: 16384, modality: "text->text", inputModalities: ["text"] },
|
|
1370
|
+
{ id: "r1-1776", name: "R1 1776", context: 128e3, maxOutput: 16384, modality: "text->text", inputModalities: ["text"] }
|
|
1371
|
+
];
|
|
1372
|
+
function createPerplexityAdapter(apiKey) {
|
|
1373
|
+
async function makeRequest(path2, body, method = "POST") {
|
|
1374
|
+
const res = await fetch(`${PERPLEXITY_API_BASE}${path2}`, {
|
|
1375
|
+
method,
|
|
1376
|
+
headers: {
|
|
1377
|
+
"Content-Type": "application/json",
|
|
1378
|
+
"Authorization": `Bearer ${apiKey}`
|
|
1379
|
+
},
|
|
1380
|
+
body: body ? JSON.stringify(body) : void 0
|
|
1381
|
+
});
|
|
1382
|
+
if (!res.ok) {
|
|
1383
|
+
let errorBody;
|
|
1384
|
+
try {
|
|
1385
|
+
errorBody = await res.json();
|
|
1386
|
+
} catch {
|
|
1387
|
+
errorBody = { message: res.statusText };
|
|
1388
|
+
}
|
|
1389
|
+
const msg = errorBody?.error?.message || errorBody?.message || res.statusText;
|
|
1390
|
+
throw new AnyModelError(mapErrorCode(res.status), msg, {
|
|
1391
|
+
provider_name: "perplexity",
|
|
1392
|
+
raw: errorBody
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
return res;
|
|
1396
|
+
}
|
|
1397
|
+
function mapErrorCode(status) {
|
|
1398
|
+
if (status === 401 || status === 403) return 401;
|
|
1399
|
+
if (status === 429) return 429;
|
|
1400
|
+
if (status === 400 || status === 422) return 400;
|
|
1401
|
+
if (status >= 500) return 502;
|
|
1402
|
+
return status;
|
|
1403
|
+
}
|
|
1404
|
+
function rePrefixId(id) {
|
|
1405
|
+
if (id && id.startsWith("chatcmpl-")) {
|
|
1406
|
+
return `gen-${id.substring(9)}`;
|
|
1407
|
+
}
|
|
1408
|
+
return id.startsWith("gen-") ? id : `gen-${id}`;
|
|
1409
|
+
}
|
|
1410
|
+
function buildRequestBody(request) {
|
|
1411
|
+
const body = {
|
|
1412
|
+
model: request.model,
|
|
1413
|
+
messages: request.messages
|
|
1414
|
+
};
|
|
1415
|
+
if (request.temperature !== void 0) body.temperature = request.temperature;
|
|
1416
|
+
if (request.max_tokens !== void 0) body.max_tokens = request.max_tokens;
|
|
1417
|
+
if (request.top_p !== void 0) body.top_p = request.top_p;
|
|
1418
|
+
if (request.frequency_penalty !== void 0) body.frequency_penalty = request.frequency_penalty;
|
|
1419
|
+
if (request.presence_penalty !== void 0) body.presence_penalty = request.presence_penalty;
|
|
1420
|
+
if (request.stop !== void 0) body.stop = request.stop;
|
|
1421
|
+
if (request.stream !== void 0) body.stream = request.stream;
|
|
1422
|
+
if (request.response_format !== void 0) body.response_format = request.response_format;
|
|
1423
|
+
if (request.tools !== void 0) body.tools = request.tools;
|
|
1424
|
+
if (request.tool_choice !== void 0) body.tool_choice = request.tool_choice;
|
|
1425
|
+
return body;
|
|
1426
|
+
}
|
|
1427
|
+
const adapter = {
|
|
1428
|
+
name: "perplexity",
|
|
1429
|
+
translateRequest(request) {
|
|
1430
|
+
return buildRequestBody(request);
|
|
1431
|
+
},
|
|
1432
|
+
translateResponse(response) {
|
|
1433
|
+
const r = response;
|
|
1434
|
+
const result = {
|
|
1435
|
+
id: rePrefixId(r.id),
|
|
1436
|
+
object: "chat.completion",
|
|
1437
|
+
created: r.created,
|
|
1438
|
+
model: `perplexity/${r.model}`,
|
|
1439
|
+
choices: r.choices,
|
|
1440
|
+
usage: r.usage
|
|
1441
|
+
};
|
|
1442
|
+
if (r.citations && result.choices?.[0]?.message) {
|
|
1443
|
+
result.citations = r.citations;
|
|
1444
|
+
}
|
|
1445
|
+
return result;
|
|
1446
|
+
},
|
|
1447
|
+
async *translateStream(stream) {
|
|
1448
|
+
const reader = stream.getReader();
|
|
1449
|
+
const decoder = new TextDecoder();
|
|
1450
|
+
let buffer = "";
|
|
1451
|
+
try {
|
|
1452
|
+
while (true) {
|
|
1453
|
+
const { done, value } = await reader.read();
|
|
1454
|
+
if (done) break;
|
|
1455
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1456
|
+
const lines = buffer.split("\n");
|
|
1457
|
+
buffer = lines.pop() || "";
|
|
1458
|
+
for (const line of lines) {
|
|
1459
|
+
const trimmed = line.trim();
|
|
1460
|
+
if (!trimmed || trimmed.startsWith(":")) continue;
|
|
1461
|
+
if (trimmed === "data: [DONE]") return;
|
|
1462
|
+
if (trimmed.startsWith("data: ")) {
|
|
1463
|
+
const json = JSON.parse(trimmed.substring(6));
|
|
1464
|
+
json.id = rePrefixId(json.id);
|
|
1465
|
+
json.model = `perplexity/${json.model}`;
|
|
1466
|
+
yield json;
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
} finally {
|
|
1471
|
+
reader.releaseLock();
|
|
1472
|
+
}
|
|
1473
|
+
},
|
|
1474
|
+
translateError(error) {
|
|
1475
|
+
if (error instanceof AnyModelError) {
|
|
1476
|
+
return { code: error.code, message: error.message, metadata: error.metadata };
|
|
1477
|
+
}
|
|
1478
|
+
const err = error;
|
|
1479
|
+
const status = err?.status || err?.code || 500;
|
|
1480
|
+
return {
|
|
1481
|
+
code: mapErrorCode(status),
|
|
1482
|
+
message: err?.message || "Unknown Perplexity error",
|
|
1483
|
+
metadata: { provider_name: "perplexity", raw: error }
|
|
1484
|
+
};
|
|
1485
|
+
},
|
|
1486
|
+
async listModels() {
|
|
1487
|
+
return MODELS.map((m) => ({
|
|
1488
|
+
id: `perplexity/${m.id}`,
|
|
1489
|
+
name: m.name,
|
|
1490
|
+
created: 0,
|
|
1491
|
+
description: "",
|
|
1492
|
+
context_length: m.context,
|
|
1493
|
+
pricing: { prompt: "0", completion: "0" },
|
|
1494
|
+
architecture: {
|
|
1495
|
+
modality: m.modality,
|
|
1496
|
+
input_modalities: m.inputModalities,
|
|
1497
|
+
output_modalities: ["text"],
|
|
1498
|
+
tokenizer: "unknown"
|
|
1499
|
+
},
|
|
1500
|
+
top_provider: {
|
|
1501
|
+
context_length: m.context,
|
|
1502
|
+
max_completion_tokens: m.maxOutput,
|
|
1503
|
+
is_moderated: false
|
|
1504
|
+
},
|
|
1505
|
+
supported_parameters: Array.from(SUPPORTED_PARAMS4)
|
|
1506
|
+
}));
|
|
1507
|
+
},
|
|
1508
|
+
supportsParameter(param) {
|
|
1509
|
+
return SUPPORTED_PARAMS4.has(param);
|
|
1510
|
+
},
|
|
1511
|
+
supportsBatch() {
|
|
1512
|
+
return false;
|
|
1513
|
+
},
|
|
1514
|
+
async sendRequest(request) {
|
|
1515
|
+
const body = buildRequestBody(request);
|
|
1516
|
+
const res = await makeRequest("/chat/completions", body);
|
|
1517
|
+
const json = await res.json();
|
|
1518
|
+
return adapter.translateResponse(json);
|
|
1519
|
+
},
|
|
1520
|
+
async sendStreamingRequest(request) {
|
|
1521
|
+
const body = buildRequestBody({ ...request, stream: true });
|
|
1522
|
+
const res = await makeRequest("/chat/completions", body);
|
|
1523
|
+
if (!res.body) {
|
|
1524
|
+
throw new AnyModelError(502, "No response body for streaming request", {
|
|
1525
|
+
provider_name: "perplexity"
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
return adapter.translateStream(res.body);
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
return adapter;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1350
1534
|
// src/providers/custom.ts
|
|
1351
1535
|
function createCustomAdapter(name, config) {
|
|
1352
1536
|
const openaiAdapter = createOpenAIAdapter(config.apiKey || "", config.baseURL);
|
|
@@ -2550,14 +2734,17 @@ var AnyModel = class {
|
|
|
2550
2734
|
if (googleKey) {
|
|
2551
2735
|
this.registry.register("google", createGoogleAdapter(googleKey));
|
|
2552
2736
|
}
|
|
2737
|
+
const perplexityKey = config.perplexity?.apiKey || process.env.PERPLEXITY_API_KEY;
|
|
2738
|
+
if (perplexityKey) {
|
|
2739
|
+
this.registry.register("perplexity", createPerplexityAdapter(perplexityKey));
|
|
2740
|
+
}
|
|
2553
2741
|
const builtinProviders = [
|
|
2554
2742
|
{ name: "mistral", baseURL: "https://api.mistral.ai/v1", configKey: "mistral", envVar: "MISTRAL_API_KEY" },
|
|
2555
2743
|
{ name: "groq", baseURL: "https://api.groq.com/openai/v1", configKey: "groq", envVar: "GROQ_API_KEY" },
|
|
2556
2744
|
{ name: "deepseek", baseURL: "https://api.deepseek.com", configKey: "deepseek", envVar: "DEEPSEEK_API_KEY" },
|
|
2557
2745
|
{ name: "xai", baseURL: "https://api.x.ai/v1", configKey: "xai", envVar: "XAI_API_KEY" },
|
|
2558
2746
|
{ name: "together", baseURL: "https://api.together.xyz/v1", configKey: "together", envVar: "TOGETHER_API_KEY" },
|
|
2559
|
-
{ name: "fireworks", baseURL: "https://api.fireworks.ai/inference/v1", configKey: "fireworks", envVar: "FIREWORKS_API_KEY" }
|
|
2560
|
-
{ name: "perplexity", baseURL: "https://api.perplexity.ai", configKey: "perplexity", envVar: "PERPLEXITY_API_KEY" }
|
|
2747
|
+
{ name: "fireworks", baseURL: "https://api.fireworks.ai/inference/v1", configKey: "fireworks", envVar: "FIREWORKS_API_KEY" }
|
|
2561
2748
|
];
|
|
2562
2749
|
for (const { name, baseURL, configKey, envVar } of builtinProviders) {
|
|
2563
2750
|
const providerConfig = config[configKey];
|