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