@ax-llm/ax 10.0.36 → 10.0.38

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/index.cjs CHANGED
@@ -5416,14 +5416,7 @@ var AxApacheTika = class {
5416
5416
  };
5417
5417
 
5418
5418
  // ai/balance.ts
5419
- var axCostComparator = (a, b) => {
5420
- const aInfo = a.getModelInfo();
5421
- const bInfo = b.getModelInfo();
5422
- const aTotalCost = (aInfo.promptTokenCostPer1M || Infinity) + (aInfo.completionTokenCostPer1M || Infinity);
5423
- const bTotalCost = (bInfo.promptTokenCostPer1M || Infinity) + (bInfo.completionTokenCostPer1M || Infinity);
5424
- return aTotalCost - bTotalCost;
5425
- };
5426
- var AxBalancer = class {
5419
+ var AxBalancer = class _AxBalancer {
5427
5420
  services;
5428
5421
  currentServiceIndex = 0;
5429
5422
  currentService;
@@ -5431,13 +5424,29 @@ var AxBalancer = class {
5431
5424
  if (services.length === 0) {
5432
5425
  throw new Error("No AI services provided.");
5433
5426
  }
5434
- this.services = [...services].sort(options?.comparator ?? axCostComparator);
5427
+ this.services = [...services].sort(
5428
+ options?.comparator ?? _AxBalancer.costComparator
5429
+ );
5435
5430
  const cs = this.services[this.currentServiceIndex];
5436
5431
  if (cs === void 0) {
5437
5432
  throw new Error("Error initializing the AI services.");
5438
5433
  }
5439
5434
  this.currentService = cs;
5440
5435
  }
5436
+ /**
5437
+ * Service comparator that respects the input order of services.
5438
+ */
5439
+ static inputOrderComparator = () => 0;
5440
+ /**
5441
+ * Service comparator that sorts services by cost.
5442
+ */
5443
+ static costComparator = (a, b) => {
5444
+ const aInfo = a.getModelInfo();
5445
+ const bInfo = b.getModelInfo();
5446
+ const aTotalCost = (aInfo.promptTokenCostPer1M || Infinity) + (aInfo.completionTokenCostPer1M || Infinity);
5447
+ const bTotalCost = (bInfo.promptTokenCostPer1M || Infinity) + (bInfo.completionTokenCostPer1M || Infinity);
5448
+ return aTotalCost - bTotalCost;
5449
+ };
5441
5450
  getModelMap() {
5442
5451
  throw new Error("Method not implemented.");
5443
5452
  }
@@ -5478,9 +5487,11 @@ var AxBalancer = class {
5478
5487
  try {
5479
5488
  return await this.currentService.chat(req, options);
5480
5489
  } catch (e) {
5490
+ console.warn(`Service ${this.currentService.getName()} failed`);
5481
5491
  if (!this.getNextService()) {
5482
5492
  throw e;
5483
5493
  }
5494
+ console.warn(`Switching to service ${this.currentService.getName()}`);
5484
5495
  }
5485
5496
  }
5486
5497
  }
@@ -5490,9 +5501,11 @@ var AxBalancer = class {
5490
5501
  try {
5491
5502
  return await this.currentService.embed(req, options);
5492
5503
  } catch (e) {
5504
+ console.warn(`Service ${this.currentService.getName()} failed`);
5493
5505
  if (!this.getNextService()) {
5494
5506
  throw e;
5495
5507
  }
5508
+ console.warn(`Switching to service ${this.currentService.getName()}`);
5496
5509
  }
5497
5510
  }
5498
5511
  }