@ax-llm/ax 10.0.37 → 10.0.39
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 +143 -39
- package/index.cjs.map +1 -1
- package/index.d.cts +36 -8
- package/index.d.ts +36 -8
- package/index.js +143 -39
- package/index.js.map +1 -1
- package/package.json +4 -3
package/index.cjs
CHANGED
|
@@ -649,7 +649,7 @@ var AxBaseAI = class {
|
|
|
649
649
|
{
|
|
650
650
|
name: apiConfig.name,
|
|
651
651
|
url: this.apiURL,
|
|
652
|
-
headers: this.buildHeaders(apiConfig.headers),
|
|
652
|
+
headers: await this.buildHeaders(apiConfig.headers),
|
|
653
653
|
stream: modelConfig.stream,
|
|
654
654
|
debug: this.debug,
|
|
655
655
|
fetch: this.fetch,
|
|
@@ -768,7 +768,7 @@ var AxBaseAI = class {
|
|
|
768
768
|
{
|
|
769
769
|
name: apiConfig.name,
|
|
770
770
|
url: this.apiURL,
|
|
771
|
-
headers: this.buildHeaders(apiConfig.headers),
|
|
771
|
+
headers: await this.buildHeaders(apiConfig.headers),
|
|
772
772
|
debug: this.debug,
|
|
773
773
|
fetch: this.fetch,
|
|
774
774
|
span
|
|
@@ -792,8 +792,8 @@ var AxBaseAI = class {
|
|
|
792
792
|
span?.end();
|
|
793
793
|
return res;
|
|
794
794
|
}
|
|
795
|
-
buildHeaders(headers = {}) {
|
|
796
|
-
return { ...headers, ...this.headers };
|
|
795
|
+
async buildHeaders(headers = {}) {
|
|
796
|
+
return { ...headers, ...await this.headers() };
|
|
797
797
|
}
|
|
798
798
|
};
|
|
799
799
|
var logChatRequest = (req) => {
|
|
@@ -1153,11 +1153,11 @@ var AxAIAnthropic = class extends AxBaseAI {
|
|
|
1153
1153
|
super(aiImpl, {
|
|
1154
1154
|
name: "Anthropic",
|
|
1155
1155
|
apiURL: "https://api.anthropic.com/v1",
|
|
1156
|
-
headers: {
|
|
1156
|
+
headers: async () => ({
|
|
1157
1157
|
"anthropic-version": "2023-06-01",
|
|
1158
1158
|
"anthropic-beta": "prompt-caching-2024-07-31",
|
|
1159
1159
|
"x-api-key": apiKey
|
|
1160
|
-
},
|
|
1160
|
+
}),
|
|
1161
1161
|
modelInfo: axModelInfoAnthropic,
|
|
1162
1162
|
models: { model: _config.model },
|
|
1163
1163
|
options,
|
|
@@ -1662,7 +1662,7 @@ var AxAIOpenAI = class extends AxBaseAI {
|
|
|
1662
1662
|
super(aiImpl, {
|
|
1663
1663
|
name: "OpenAI",
|
|
1664
1664
|
apiURL: apiURL ? apiURL : "https://api.openai.com/v1",
|
|
1665
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
1665
|
+
headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
|
|
1666
1666
|
modelInfo,
|
|
1667
1667
|
models: {
|
|
1668
1668
|
model: _config.model,
|
|
@@ -1714,7 +1714,7 @@ var AxAIAzureOpenAI = class extends AxAIOpenAI {
|
|
|
1714
1714
|
host
|
|
1715
1715
|
).href
|
|
1716
1716
|
);
|
|
1717
|
-
super.setHeaders({ "api-key": apiKey });
|
|
1717
|
+
super.setHeaders(async () => ({ "api-key": apiKey }));
|
|
1718
1718
|
}
|
|
1719
1719
|
};
|
|
1720
1720
|
|
|
@@ -1974,7 +1974,7 @@ var AxAICohere = class extends AxBaseAI {
|
|
|
1974
1974
|
super(aiImpl, {
|
|
1975
1975
|
name: "Cohere",
|
|
1976
1976
|
apiURL: "https://api.cohere.ai/v1",
|
|
1977
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
1977
|
+
headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
|
|
1978
1978
|
modelInfo: axModelInfoCohere,
|
|
1979
1979
|
models: { model: _config.model },
|
|
1980
1980
|
supportFor: { functions: true, streaming: true },
|
|
@@ -2094,6 +2094,61 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
|
|
|
2094
2094
|
}
|
|
2095
2095
|
};
|
|
2096
2096
|
|
|
2097
|
+
// ai/google-gemini/auth.ts
|
|
2098
|
+
var import_google_auth_library = require("google-auth-library");
|
|
2099
|
+
var GoogleVertexAuth = class {
|
|
2100
|
+
auth;
|
|
2101
|
+
client;
|
|
2102
|
+
currentToken;
|
|
2103
|
+
tokenExpiry;
|
|
2104
|
+
constructor(config = {}) {
|
|
2105
|
+
this.auth = new import_google_auth_library.GoogleAuth({
|
|
2106
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
|
2107
|
+
...config
|
|
2108
|
+
});
|
|
2109
|
+
}
|
|
2110
|
+
async getAuthenticatedClient() {
|
|
2111
|
+
if (!this.client) {
|
|
2112
|
+
this.client = await this.auth.getClient();
|
|
2113
|
+
}
|
|
2114
|
+
return this.client;
|
|
2115
|
+
}
|
|
2116
|
+
async getAccessToken() {
|
|
2117
|
+
if (this.currentToken && this.tokenExpiry && Date.now() < this.tokenExpiry) {
|
|
2118
|
+
return this.currentToken;
|
|
2119
|
+
}
|
|
2120
|
+
const client = await this.getAuthenticatedClient();
|
|
2121
|
+
const tokenResponse = await client.getAccessToken();
|
|
2122
|
+
this.currentToken = tokenResponse.token ?? void 0;
|
|
2123
|
+
const expiry = this.getExpiry(tokenResponse);
|
|
2124
|
+
const fiveMinutes = 5 * 60 * 1e3;
|
|
2125
|
+
this.tokenExpiry = expiry - fiveMinutes;
|
|
2126
|
+
return this.currentToken;
|
|
2127
|
+
}
|
|
2128
|
+
/**
|
|
2129
|
+
* Get the expiry date from the token response.
|
|
2130
|
+
*/
|
|
2131
|
+
getExpiry(tokenResponse) {
|
|
2132
|
+
const oneHour = 3600 * 1e3;
|
|
2133
|
+
let expiry = Date.now() + oneHour;
|
|
2134
|
+
let responseExpiry = tokenResponse.res?.data?.expiry_date;
|
|
2135
|
+
if (responseExpiry) {
|
|
2136
|
+
if (typeof responseExpiry === "number") {
|
|
2137
|
+
expiry = responseExpiry;
|
|
2138
|
+
} else if (responseExpiry instanceof Date) {
|
|
2139
|
+
expiry = responseExpiry.getTime();
|
|
2140
|
+
} else if (typeof responseExpiry === "string") {
|
|
2141
|
+
expiry = new Date(responseExpiry).getTime();
|
|
2142
|
+
} else {
|
|
2143
|
+
console.warn("Unknown expiry type", responseExpiry);
|
|
2144
|
+
}
|
|
2145
|
+
} else {
|
|
2146
|
+
console.warn("No expiry date found in response", tokenResponse.res?.data);
|
|
2147
|
+
}
|
|
2148
|
+
return expiry;
|
|
2149
|
+
}
|
|
2150
|
+
};
|
|
2151
|
+
|
|
2097
2152
|
// ai/google-gemini/types.ts
|
|
2098
2153
|
var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
2099
2154
|
AxAIGoogleGeminiModel2["Gemini1Pro"] = "gemini-1.0-pro";
|
|
@@ -2106,6 +2161,7 @@ var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
|
2106
2161
|
})(AxAIGoogleGeminiModel || {});
|
|
2107
2162
|
var AxAIGoogleGeminiEmbedModel = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedModel2) => {
|
|
2108
2163
|
AxAIGoogleGeminiEmbedModel2["TextEmbedding004"] = "text-embedding-004";
|
|
2164
|
+
AxAIGoogleGeminiEmbedModel2["TextEmbedding005"] = "text-embedding-005";
|
|
2109
2165
|
return AxAIGoogleGeminiEmbedModel2;
|
|
2110
2166
|
})(AxAIGoogleGeminiEmbedModel || {});
|
|
2111
2167
|
var AxAIGoogleGeminiSafetyCategory = /* @__PURE__ */ ((AxAIGoogleGeminiSafetyCategory2) => {
|
|
@@ -2182,10 +2238,11 @@ var axAIGoogleGeminiDefaultConfig = () => structuredClone({
|
|
|
2182
2238
|
...axBaseAIDefaultConfig()
|
|
2183
2239
|
});
|
|
2184
2240
|
var AxAIGoogleGeminiImpl = class {
|
|
2185
|
-
constructor(config, apiKey,
|
|
2241
|
+
constructor(config, isVertex, apiKey, keyFile, options) {
|
|
2186
2242
|
this.config = config;
|
|
2187
|
-
this.apiKey = apiKey;
|
|
2188
2243
|
this.isVertex = isVertex;
|
|
2244
|
+
this.apiKey = apiKey;
|
|
2245
|
+
this.keyFile = keyFile;
|
|
2189
2246
|
this.options = options;
|
|
2190
2247
|
}
|
|
2191
2248
|
getModelConfig() {
|
|
@@ -2212,7 +2269,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2212
2269
|
const apiConfig = {
|
|
2213
2270
|
name: stream ? `/models/${model}:streamGenerateContent?alt=sse` : `/models/${model}:generateContent`
|
|
2214
2271
|
};
|
|
2215
|
-
if (this.isVertex
|
|
2272
|
+
if (!this.isVertex) {
|
|
2216
2273
|
const pf = stream ? "&" : "?";
|
|
2217
2274
|
apiConfig.name += `${pf}key=${this.apiKey}`;
|
|
2218
2275
|
}
|
|
@@ -2359,15 +2416,28 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2359
2416
|
if (!req.texts || req.texts.length === 0) {
|
|
2360
2417
|
throw new Error("Embed texts is empty");
|
|
2361
2418
|
}
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2419
|
+
let apiConfig;
|
|
2420
|
+
let reqValue;
|
|
2421
|
+
if (this.isVertex) {
|
|
2422
|
+
apiConfig = {
|
|
2423
|
+
name: `/models/${model}:predict`
|
|
2424
|
+
};
|
|
2425
|
+
reqValue = {
|
|
2426
|
+
instances: req.texts.map((text) => ({
|
|
2427
|
+
content: text
|
|
2428
|
+
}))
|
|
2429
|
+
};
|
|
2430
|
+
} else {
|
|
2431
|
+
apiConfig = {
|
|
2432
|
+
name: `/models/${model}:batchEmbedContents?key=${this.apiKey}`
|
|
2433
|
+
};
|
|
2434
|
+
reqValue = {
|
|
2435
|
+
requests: req.texts.map((text) => ({
|
|
2436
|
+
model: "models/" + model,
|
|
2437
|
+
content: { parts: [{ text }] }
|
|
2438
|
+
}))
|
|
2439
|
+
};
|
|
2440
|
+
}
|
|
2371
2441
|
return [apiConfig, reqValue];
|
|
2372
2442
|
};
|
|
2373
2443
|
createChatResp = (resp) => {
|
|
@@ -2429,7 +2499,16 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2429
2499
|
return this.createChatResp(resp);
|
|
2430
2500
|
};
|
|
2431
2501
|
createEmbedResp = (resp) => {
|
|
2432
|
-
|
|
2502
|
+
let embeddings;
|
|
2503
|
+
if (this.isVertex) {
|
|
2504
|
+
embeddings = resp.predictions.map(
|
|
2505
|
+
(prediction) => prediction.embeddings.values
|
|
2506
|
+
);
|
|
2507
|
+
} else {
|
|
2508
|
+
embeddings = resp.embeddings.map(
|
|
2509
|
+
(embedding) => embedding.values
|
|
2510
|
+
);
|
|
2511
|
+
}
|
|
2433
2512
|
return {
|
|
2434
2513
|
embeddings
|
|
2435
2514
|
};
|
|
@@ -2440,28 +2519,44 @@ var AxAIGoogleGemini = class extends AxBaseAI {
|
|
|
2440
2519
|
apiKey,
|
|
2441
2520
|
projectId,
|
|
2442
2521
|
region,
|
|
2522
|
+
keyFile,
|
|
2443
2523
|
config,
|
|
2444
2524
|
options,
|
|
2445
2525
|
modelMap
|
|
2446
2526
|
}) {
|
|
2447
|
-
if (!apiKey || apiKey === "") {
|
|
2448
|
-
throw new Error("GoogleGemini AI API key not set");
|
|
2449
|
-
}
|
|
2450
2527
|
const isVertex = projectId !== void 0 && region !== void 0;
|
|
2451
2528
|
let apiURL;
|
|
2452
2529
|
let headers;
|
|
2453
2530
|
if (isVertex) {
|
|
2454
2531
|
apiURL = `https://${region}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${region}/publishers/google/`;
|
|
2455
|
-
|
|
2532
|
+
if (apiKey) {
|
|
2533
|
+
headers = async () => ({ Authorization: `Bearer ${apiKey}` });
|
|
2534
|
+
} else {
|
|
2535
|
+
const vertexAuth = new GoogleVertexAuth({
|
|
2536
|
+
keyFile
|
|
2537
|
+
});
|
|
2538
|
+
headers = async () => ({
|
|
2539
|
+
Authorization: `Bearer ${await vertexAuth.getAccessToken()}`
|
|
2540
|
+
});
|
|
2541
|
+
}
|
|
2456
2542
|
} else {
|
|
2543
|
+
if (!apiKey) {
|
|
2544
|
+
throw new Error("GoogleGemini AI API key not set");
|
|
2545
|
+
}
|
|
2457
2546
|
apiURL = "https://generativelanguage.googleapis.com/v1beta";
|
|
2458
|
-
headers = {};
|
|
2547
|
+
headers = async () => ({});
|
|
2459
2548
|
}
|
|
2460
2549
|
const _config = {
|
|
2461
2550
|
...axAIGoogleGeminiDefaultConfig(),
|
|
2462
2551
|
...config
|
|
2463
2552
|
};
|
|
2464
|
-
const aiImpl = new AxAIGoogleGeminiImpl(
|
|
2553
|
+
const aiImpl = new AxAIGoogleGeminiImpl(
|
|
2554
|
+
_config,
|
|
2555
|
+
isVertex,
|
|
2556
|
+
apiKey,
|
|
2557
|
+
keyFile,
|
|
2558
|
+
options
|
|
2559
|
+
);
|
|
2465
2560
|
super(aiImpl, {
|
|
2466
2561
|
name: "GoogleGeminiAI",
|
|
2467
2562
|
apiURL,
|
|
@@ -2731,7 +2826,7 @@ var AxAIHuggingFace = class extends AxBaseAI {
|
|
|
2731
2826
|
super(aiImpl, {
|
|
2732
2827
|
name: "HuggingFace",
|
|
2733
2828
|
apiURL: "https://api-inference.huggingface.co",
|
|
2734
|
-
headers: { Authorization: `Bearer ${apiKey}` },
|
|
2829
|
+
headers: async () => ({ Authorization: `Bearer ${apiKey}` }),
|
|
2735
2830
|
modelInfo: axModelInfoHuggingFace,
|
|
2736
2831
|
models: { model: _config.model },
|
|
2737
2832
|
options,
|
|
@@ -3080,7 +3175,7 @@ var AxAIReka = class extends AxBaseAI {
|
|
|
3080
3175
|
super(aiImpl, {
|
|
3081
3176
|
name: "Reka",
|
|
3082
3177
|
apiURL: apiURL ? apiURL : "https://api.reka.ai/v1/chat",
|
|
3083
|
-
headers: { "X-Api-Key": apiKey },
|
|
3178
|
+
headers: async () => ({ "X-Api-Key": apiKey }),
|
|
3084
3179
|
modelInfo,
|
|
3085
3180
|
models: {
|
|
3086
3181
|
model: _config.model
|
|
@@ -5416,14 +5511,7 @@ var AxApacheTika = class {
|
|
|
5416
5511
|
};
|
|
5417
5512
|
|
|
5418
5513
|
// ai/balance.ts
|
|
5419
|
-
var
|
|
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 {
|
|
5514
|
+
var AxBalancer = class _AxBalancer {
|
|
5427
5515
|
services;
|
|
5428
5516
|
currentServiceIndex = 0;
|
|
5429
5517
|
currentService;
|
|
@@ -5431,13 +5519,29 @@ var AxBalancer = class {
|
|
|
5431
5519
|
if (services.length === 0) {
|
|
5432
5520
|
throw new Error("No AI services provided.");
|
|
5433
5521
|
}
|
|
5434
|
-
this.services = [...services].sort(
|
|
5522
|
+
this.services = [...services].sort(
|
|
5523
|
+
options?.comparator ?? _AxBalancer.costComparator
|
|
5524
|
+
);
|
|
5435
5525
|
const cs = this.services[this.currentServiceIndex];
|
|
5436
5526
|
if (cs === void 0) {
|
|
5437
5527
|
throw new Error("Error initializing the AI services.");
|
|
5438
5528
|
}
|
|
5439
5529
|
this.currentService = cs;
|
|
5440
5530
|
}
|
|
5531
|
+
/**
|
|
5532
|
+
* Service comparator that respects the input order of services.
|
|
5533
|
+
*/
|
|
5534
|
+
static inputOrderComparator = () => 0;
|
|
5535
|
+
/**
|
|
5536
|
+
* Service comparator that sorts services by cost.
|
|
5537
|
+
*/
|
|
5538
|
+
static costComparator = (a, b) => {
|
|
5539
|
+
const aInfo = a.getModelInfo();
|
|
5540
|
+
const bInfo = b.getModelInfo();
|
|
5541
|
+
const aTotalCost = (aInfo.promptTokenCostPer1M || Infinity) + (aInfo.completionTokenCostPer1M || Infinity);
|
|
5542
|
+
const bTotalCost = (bInfo.promptTokenCostPer1M || Infinity) + (bInfo.completionTokenCostPer1M || Infinity);
|
|
5543
|
+
return aTotalCost - bTotalCost;
|
|
5544
|
+
};
|
|
5441
5545
|
getModelMap() {
|
|
5442
5546
|
throw new Error("Method not implemented.");
|
|
5443
5547
|
}
|