@ax-llm/ax 11.0.41 → 11.0.44
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 +310 -73
- package/index.cjs.map +1 -1
- package/index.d.cts +49 -7
- package/index.d.ts +49 -7
- package/index.js +310 -73
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { SpanKind } from "@opentelemetry/api";
|
|
|
5
5
|
var axSpanAttributes = {
|
|
6
6
|
// LLM
|
|
7
7
|
LLM_SYSTEM: "gen_ai.system",
|
|
8
|
+
LLM_OPERATION_NAME: "gen_ai.operation.name",
|
|
8
9
|
LLM_REQUEST_MODEL: "gen_ai.request.model",
|
|
9
10
|
LLM_REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
|
|
10
11
|
LLM_REQUEST_TEMPERATURE: "gen_ai.request.temperature",
|
|
@@ -14,8 +15,10 @@ var axSpanAttributes = {
|
|
|
14
15
|
LLM_REQUEST_STOP_SEQUENCES: "gen_ai.request.stop_sequences",
|
|
15
16
|
LLM_REQUEST_LLM_IS_STREAMING: "gen_ai.request.llm_is_streaming",
|
|
16
17
|
LLM_REQUEST_TOP_P: "gen_ai.request.top_p",
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
LLM_USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
|
|
19
|
+
LLM_USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
|
|
20
|
+
LLM_USAGE_TOTAL_TOKENS: "gen_ai.usage.total_tokens",
|
|
21
|
+
LLM_USAGE_THOUGHTS_TOKENS: "gen_ai.usage.thoughts_tokens",
|
|
19
22
|
// Vector DB
|
|
20
23
|
DB_SYSTEM: "db.system",
|
|
21
24
|
DB_TABLE: "db.table",
|
|
@@ -38,7 +41,13 @@ var axSpanAttributes = {
|
|
|
38
41
|
DB_QUERY_RESULT_DOCUMENT: "db.query.result.document"
|
|
39
42
|
};
|
|
40
43
|
var axSpanEvents = {
|
|
41
|
-
|
|
44
|
+
GEN_AI_USER_MESSAGE: "gen_ai.user.message",
|
|
45
|
+
GEN_AI_SYSTEM_MESSAGE: "gen_ai.system.message",
|
|
46
|
+
GEN_AI_ASSISTANT_MESSAGE: "gen_ai.assistant.message",
|
|
47
|
+
GEN_AI_TOOL_MESSAGE: "gen_ai.tool.message",
|
|
48
|
+
// For tool messages in request & response tool calls
|
|
49
|
+
GEN_AI_CHOICE: "gen_ai.choice",
|
|
50
|
+
GEN_AI_USAGE: "gen_ai.usage"
|
|
42
51
|
};
|
|
43
52
|
var AxLLMRequestTypeValues = /* @__PURE__ */ ((AxLLMRequestTypeValues2) => {
|
|
44
53
|
AxLLMRequestTypeValues2["COMPLETION"] = "completion";
|
|
@@ -482,9 +491,6 @@ var apiCall = async (api, json) => {
|
|
|
482
491
|
} finally {
|
|
483
492
|
clearTimeout(timeoutId);
|
|
484
493
|
reader.releaseLock();
|
|
485
|
-
if (api.span?.isRecording()) {
|
|
486
|
-
api.span.end();
|
|
487
|
-
}
|
|
488
494
|
}
|
|
489
495
|
}
|
|
490
496
|
read();
|
|
@@ -532,9 +538,6 @@ var apiCall = async (api, json) => {
|
|
|
532
538
|
if (timeoutId !== void 0) {
|
|
533
539
|
clearTimeout(timeoutId);
|
|
534
540
|
}
|
|
535
|
-
if (api.span?.isRecording()) {
|
|
536
|
-
api.span.end();
|
|
537
|
-
}
|
|
538
541
|
}
|
|
539
542
|
}
|
|
540
543
|
};
|
|
@@ -749,6 +752,8 @@ var AxBaseAI = class {
|
|
|
749
752
|
rt;
|
|
750
753
|
fetch;
|
|
751
754
|
tracer;
|
|
755
|
+
timeout;
|
|
756
|
+
excludeContentFromTrace;
|
|
752
757
|
models;
|
|
753
758
|
modelInfo;
|
|
754
759
|
modelUsage;
|
|
@@ -807,14 +812,18 @@ var AxBaseAI = class {
|
|
|
807
812
|
this.debug = options.debug ?? false;
|
|
808
813
|
this.rt = options.rateLimiter;
|
|
809
814
|
this.fetch = options.fetch;
|
|
815
|
+
this.timeout = options.timeout;
|
|
810
816
|
this.tracer = options.tracer;
|
|
817
|
+
this.excludeContentFromTrace = options.excludeContentFromTrace;
|
|
811
818
|
}
|
|
812
819
|
getOptions() {
|
|
813
820
|
return {
|
|
814
821
|
debug: this.debug,
|
|
815
822
|
rateLimiter: this.rt,
|
|
816
823
|
fetch: this.fetch,
|
|
817
|
-
tracer: this.tracer
|
|
824
|
+
tracer: this.tracer,
|
|
825
|
+
timeout: this.timeout,
|
|
826
|
+
excludeContentFromTrace: this.excludeContentFromTrace
|
|
818
827
|
};
|
|
819
828
|
}
|
|
820
829
|
getModelList() {
|
|
@@ -912,13 +921,18 @@ var AxBaseAI = class {
|
|
|
912
921
|
if (!canStream) {
|
|
913
922
|
modelConfig.stream = false;
|
|
914
923
|
}
|
|
924
|
+
const canSetThinkingTokenBudget = this.getFeatures(model).thinkingTokenBudget;
|
|
925
|
+
if (!canSetThinkingTokenBudget && options?.thinkingTokenBudget) {
|
|
926
|
+
throw new Error("Thinking token budget is not supported for this model");
|
|
927
|
+
}
|
|
915
928
|
if (this.tracer) {
|
|
916
929
|
return await this.tracer?.startActiveSpan(
|
|
917
|
-
"Chat Request",
|
|
930
|
+
"AI Chat Request",
|
|
918
931
|
{
|
|
919
932
|
kind: SpanKind.SERVER,
|
|
920
933
|
attributes: {
|
|
921
934
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
935
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
922
936
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
923
937
|
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
924
938
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
@@ -928,17 +942,10 @@ var AxBaseAI = class {
|
|
|
928
942
|
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
929
943
|
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
930
944
|
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
931
|
-
// [AxSpanAttributes.LLM_PROMPTS]: _req.chatPrompt
|
|
932
|
-
// ?.map((v) => v.content)
|
|
933
|
-
// .join('\n')
|
|
934
945
|
}
|
|
935
946
|
},
|
|
936
947
|
async (span) => {
|
|
937
|
-
|
|
938
|
-
return await this._chat2(model, modelConfig, req, options, span);
|
|
939
|
-
} finally {
|
|
940
|
-
span.end();
|
|
941
|
-
}
|
|
948
|
+
return await this._chat2(model, modelConfig, req, options, span);
|
|
942
949
|
}
|
|
943
950
|
);
|
|
944
951
|
}
|
|
@@ -984,12 +991,16 @@ var AxBaseAI = class {
|
|
|
984
991
|
req,
|
|
985
992
|
options
|
|
986
993
|
);
|
|
994
|
+
if (span?.isRecording()) {
|
|
995
|
+
setChatRequestEvents(chatReq, span, this.excludeContentFromTrace);
|
|
996
|
+
}
|
|
987
997
|
const res2 = await apiCall(
|
|
988
998
|
{
|
|
989
999
|
name: apiConfig.name,
|
|
990
1000
|
url: this.apiURL,
|
|
991
1001
|
headers: await this.buildHeaders(apiConfig.headers),
|
|
992
1002
|
stream: modelConfig.stream,
|
|
1003
|
+
timeout: this.timeout,
|
|
993
1004
|
debug,
|
|
994
1005
|
fetch: this.fetch,
|
|
995
1006
|
span
|
|
@@ -1020,7 +1031,7 @@ var AxBaseAI = class {
|
|
|
1020
1031
|
}
|
|
1021
1032
|
this.modelUsage = res2.modelUsage;
|
|
1022
1033
|
if (span?.isRecording()) {
|
|
1023
|
-
|
|
1034
|
+
setChatResponseEvents(res2, span, this.excludeContentFromTrace);
|
|
1024
1035
|
}
|
|
1025
1036
|
if (debug) {
|
|
1026
1037
|
logResponse(res2);
|
|
@@ -1031,6 +1042,9 @@ var AxBaseAI = class {
|
|
|
1031
1042
|
if (debug) {
|
|
1032
1043
|
process.stdout.write("\n");
|
|
1033
1044
|
}
|
|
1045
|
+
if (span?.isRecording()) {
|
|
1046
|
+
span.end();
|
|
1047
|
+
}
|
|
1034
1048
|
};
|
|
1035
1049
|
const st = rv.pipeThrough(
|
|
1036
1050
|
new RespTransformStream(
|
|
@@ -1045,16 +1059,26 @@ var AxBaseAI = class {
|
|
|
1045
1059
|
}
|
|
1046
1060
|
const res = this.aiImpl.createChatResp(rv);
|
|
1047
1061
|
res.sessionId = options?.sessionId;
|
|
1062
|
+
if (!res.modelUsage) {
|
|
1063
|
+
const tokenUsage = this.aiImpl.getTokenUsage();
|
|
1064
|
+
if (tokenUsage) {
|
|
1065
|
+
res.modelUsage = {
|
|
1066
|
+
ai: this.name,
|
|
1067
|
+
model,
|
|
1068
|
+
tokens: tokenUsage
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1048
1072
|
if (res.modelUsage) {
|
|
1049
1073
|
this.modelUsage = res.modelUsage;
|
|
1050
1074
|
}
|
|
1051
1075
|
if (span?.isRecording()) {
|
|
1052
|
-
|
|
1076
|
+
setChatResponseEvents(res, span, this.excludeContentFromTrace);
|
|
1077
|
+
span.end();
|
|
1053
1078
|
}
|
|
1054
1079
|
if (debug) {
|
|
1055
1080
|
logResponse(res);
|
|
1056
1081
|
}
|
|
1057
|
-
span?.end();
|
|
1058
1082
|
return res;
|
|
1059
1083
|
}
|
|
1060
1084
|
async embed(req, options) {
|
|
@@ -1078,11 +1102,12 @@ var AxBaseAI = class {
|
|
|
1078
1102
|
}
|
|
1079
1103
|
if (this.tracer) {
|
|
1080
1104
|
await this.tracer?.startActiveSpan(
|
|
1081
|
-
"Embed Request",
|
|
1105
|
+
"AI Embed Request",
|
|
1082
1106
|
{
|
|
1083
1107
|
kind: SpanKind.SERVER,
|
|
1084
1108
|
attributes: {
|
|
1085
1109
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1110
|
+
[axSpanAttributes.LLM_OPERATION_NAME]: "embeddings",
|
|
1086
1111
|
[axSpanAttributes.LLM_REQUEST_MODEL]: embedModel
|
|
1087
1112
|
}
|
|
1088
1113
|
},
|
|
@@ -1119,6 +1144,7 @@ var AxBaseAI = class {
|
|
|
1119
1144
|
headers: await this.buildHeaders(apiConfig.headers),
|
|
1120
1145
|
debug,
|
|
1121
1146
|
fetch: this.fetch,
|
|
1147
|
+
timeout: this.timeout,
|
|
1122
1148
|
span
|
|
1123
1149
|
},
|
|
1124
1150
|
reqValue
|
|
@@ -1136,8 +1162,12 @@ var AxBaseAI = class {
|
|
|
1136
1162
|
};
|
|
1137
1163
|
}
|
|
1138
1164
|
this.embedModelUsage = res.modelUsage;
|
|
1139
|
-
if (span?.isRecording()) {
|
|
1140
|
-
|
|
1165
|
+
if (span?.isRecording() && res.modelUsage?.tokens) {
|
|
1166
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1167
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1168
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1169
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens
|
|
1170
|
+
});
|
|
1141
1171
|
}
|
|
1142
1172
|
span?.end();
|
|
1143
1173
|
return res;
|
|
@@ -1161,11 +1191,113 @@ var AxBaseAI = class {
|
|
|
1161
1191
|
return item && "embedModel" in item ? item.embedModel : void 0;
|
|
1162
1192
|
}
|
|
1163
1193
|
};
|
|
1164
|
-
function
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1194
|
+
function setChatRequestEvents(req, span, excludeContentFromTrace) {
|
|
1195
|
+
const userMessages = [];
|
|
1196
|
+
if (req.chatPrompt && Array.isArray(req.chatPrompt) && req.chatPrompt.length > 0) {
|
|
1197
|
+
for (const prompt of req.chatPrompt) {
|
|
1198
|
+
switch (prompt.role) {
|
|
1199
|
+
case "system":
|
|
1200
|
+
if (prompt.content) {
|
|
1201
|
+
const eventData2 = {};
|
|
1202
|
+
if (!excludeContentFromTrace) {
|
|
1203
|
+
eventData2.content = prompt.content;
|
|
1204
|
+
}
|
|
1205
|
+
span.addEvent(axSpanEvents.GEN_AI_SYSTEM_MESSAGE, eventData2);
|
|
1206
|
+
}
|
|
1207
|
+
break;
|
|
1208
|
+
case "user":
|
|
1209
|
+
if (typeof prompt.content === "string") {
|
|
1210
|
+
userMessages.push(prompt.content);
|
|
1211
|
+
} else if (Array.isArray(prompt.content)) {
|
|
1212
|
+
for (const part of prompt.content) {
|
|
1213
|
+
if (part.type === "text") {
|
|
1214
|
+
userMessages.push(part.text);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
break;
|
|
1219
|
+
case "assistant":
|
|
1220
|
+
const functionCalls = prompt.functionCalls?.map((call) => {
|
|
1221
|
+
return {
|
|
1222
|
+
id: call.id,
|
|
1223
|
+
type: call.type,
|
|
1224
|
+
function: call.function.name,
|
|
1225
|
+
arguments: call.function.params
|
|
1226
|
+
};
|
|
1227
|
+
});
|
|
1228
|
+
if (functionCalls && functionCalls.length > 0) {
|
|
1229
|
+
const eventData2 = {
|
|
1230
|
+
function_calls: JSON.stringify(functionCalls, null, 2)
|
|
1231
|
+
};
|
|
1232
|
+
if (!excludeContentFromTrace && prompt.content) {
|
|
1233
|
+
eventData2.content = prompt.content;
|
|
1234
|
+
}
|
|
1235
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1236
|
+
} else if (prompt.content) {
|
|
1237
|
+
const eventData2 = {};
|
|
1238
|
+
if (!excludeContentFromTrace) {
|
|
1239
|
+
eventData2.content = prompt.content;
|
|
1240
|
+
}
|
|
1241
|
+
span.addEvent(axSpanEvents.GEN_AI_ASSISTANT_MESSAGE, eventData2);
|
|
1242
|
+
}
|
|
1243
|
+
break;
|
|
1244
|
+
case "function":
|
|
1245
|
+
const eventData = {
|
|
1246
|
+
id: prompt.functionId
|
|
1247
|
+
};
|
|
1248
|
+
if (!excludeContentFromTrace) {
|
|
1249
|
+
eventData.content = prompt.result;
|
|
1250
|
+
}
|
|
1251
|
+
span.addEvent(axSpanEvents.GEN_AI_TOOL_MESSAGE, eventData);
|
|
1252
|
+
break;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
const userEventData = {};
|
|
1257
|
+
if (!excludeContentFromTrace) {
|
|
1258
|
+
userEventData.content = userMessages.join("\n");
|
|
1259
|
+
}
|
|
1260
|
+
span.addEvent(axSpanEvents.GEN_AI_USER_MESSAGE, userEventData);
|
|
1261
|
+
}
|
|
1262
|
+
function setChatResponseEvents(res, span, excludeContentFromTrace) {
|
|
1263
|
+
if (res.modelUsage?.tokens) {
|
|
1264
|
+
const thoughTokens = res.modelUsage.tokens.thoughtsTokens ? {
|
|
1265
|
+
[axSpanAttributes.LLM_USAGE_THOUGHTS_TOKENS]: res.modelUsage.tokens.thoughtsTokens
|
|
1266
|
+
} : {};
|
|
1267
|
+
span.addEvent(axSpanEvents.GEN_AI_USAGE, {
|
|
1268
|
+
[axSpanAttributes.LLM_USAGE_INPUT_TOKENS]: res.modelUsage.tokens.promptTokens,
|
|
1269
|
+
[axSpanAttributes.LLM_USAGE_OUTPUT_TOKENS]: res.modelUsage.tokens.completionTokens ?? 0,
|
|
1270
|
+
[axSpanAttributes.LLM_USAGE_TOTAL_TOKENS]: res.modelUsage.tokens.totalTokens,
|
|
1271
|
+
...thoughTokens
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
if (!res.results) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
for (const [index, result] of res.results.entries()) {
|
|
1278
|
+
const toolCalls = result.functionCalls?.map((call) => {
|
|
1279
|
+
return {
|
|
1280
|
+
id: call.id,
|
|
1281
|
+
type: call.type,
|
|
1282
|
+
function: call.function.name,
|
|
1283
|
+
arguments: call.function.params
|
|
1284
|
+
};
|
|
1285
|
+
});
|
|
1286
|
+
let message = {};
|
|
1287
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
1288
|
+
if (!excludeContentFromTrace) {
|
|
1289
|
+
message.content = result.content;
|
|
1290
|
+
}
|
|
1291
|
+
message.tool_calls = toolCalls;
|
|
1292
|
+
} else {
|
|
1293
|
+
if (!excludeContentFromTrace) {
|
|
1294
|
+
message.content = result.content ?? "";
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
span.addEvent(axSpanEvents.GEN_AI_CHOICE, {
|
|
1298
|
+
finish_reason: result.finishReason,
|
|
1299
|
+
index,
|
|
1300
|
+
message: JSON.stringify(message, null, 2)
|
|
1169
1301
|
});
|
|
1170
1302
|
}
|
|
1171
1303
|
}
|
|
@@ -2558,7 +2690,7 @@ var AxAIDeepSeek = class extends AxAIOpenAIBase {
|
|
|
2558
2690
|
|
|
2559
2691
|
// ai/google-gemini/types.ts
|
|
2560
2692
|
var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
|
|
2561
|
-
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-
|
|
2693
|
+
AxAIGoogleGeminiModel2["Gemini25Pro"] = "gemini-2.5-pro-preview-05-06";
|
|
2562
2694
|
AxAIGoogleGeminiModel2["Gemini25Flash"] = "gemini-2.5-flash-preview-04-17";
|
|
2563
2695
|
AxAIGoogleGeminiModel2["Gemini20Flash"] = "gemini-2.0-flash";
|
|
2564
2696
|
AxAIGoogleGeminiModel2["Gemini20FlashLite"] = "gemini-2.0-flash-lite-preview-02-05";
|
|
@@ -2607,7 +2739,7 @@ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2)
|
|
|
2607
2739
|
// ai/google-gemini/info.ts
|
|
2608
2740
|
var axModelInfoGoogleGemini = [
|
|
2609
2741
|
{
|
|
2610
|
-
name: "gemini-2.5-pro-preview-
|
|
2742
|
+
name: "gemini-2.5-pro-preview-05-06" /* Gemini25Pro */,
|
|
2611
2743
|
currency: "usd",
|
|
2612
2744
|
characterIsToken: false,
|
|
2613
2745
|
promptTokenCostPer1M: 2.5,
|
|
@@ -2691,7 +2823,7 @@ var safetySettings = [
|
|
|
2691
2823
|
}
|
|
2692
2824
|
];
|
|
2693
2825
|
var axAIGoogleGeminiDefaultConfig = () => structuredClone({
|
|
2694
|
-
model: "gemini-2.
|
|
2826
|
+
model: "gemini-2.5-flash-preview-04-17" /* Gemini25Flash */,
|
|
2695
2827
|
embedModel: "text-embedding-005" /* TextEmbedding005 */,
|
|
2696
2828
|
safetySettings,
|
|
2697
2829
|
...axBaseAIDefaultConfig()
|
|
@@ -2732,7 +2864,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2732
2864
|
n: config.n
|
|
2733
2865
|
};
|
|
2734
2866
|
}
|
|
2735
|
-
createChatReq = (req) => {
|
|
2867
|
+
createChatReq = (req, config) => {
|
|
2736
2868
|
const model = req.model;
|
|
2737
2869
|
const stream = req.modelConfig?.stream ?? this.config.stream;
|
|
2738
2870
|
if (!req.chatPrompt || req.chatPrompt.length === 0) {
|
|
@@ -2843,6 +2975,9 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2843
2975
|
}
|
|
2844
2976
|
});
|
|
2845
2977
|
}
|
|
2978
|
+
if (this.options?.urlContext) {
|
|
2979
|
+
tools.push({ url_context: {} });
|
|
2980
|
+
}
|
|
2846
2981
|
if (tools.length === 0) {
|
|
2847
2982
|
tools = void 0;
|
|
2848
2983
|
}
|
|
@@ -2868,6 +3003,16 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2868
3003
|
} else if (tools && tools.length > 0) {
|
|
2869
3004
|
toolConfig = { function_calling_config: { mode: "AUTO" } };
|
|
2870
3005
|
}
|
|
3006
|
+
const thinkingConfig = {};
|
|
3007
|
+
if (this.config.thinking?.includeThoughts) {
|
|
3008
|
+
thinkingConfig.includeThoughts = true;
|
|
3009
|
+
}
|
|
3010
|
+
if (this.config.thinking?.thinkingTokenBudget) {
|
|
3011
|
+
thinkingConfig.thinkingBudget = this.config.thinking.thinkingTokenBudget;
|
|
3012
|
+
}
|
|
3013
|
+
if (config.thinkingTokenBudget) {
|
|
3014
|
+
thinkingConfig.thinkingBudget = config.thinkingTokenBudget;
|
|
3015
|
+
}
|
|
2871
3016
|
const generationConfig = {
|
|
2872
3017
|
maxOutputTokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
2873
3018
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
@@ -2877,9 +3022,7 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2877
3022
|
candidateCount: 1,
|
|
2878
3023
|
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
|
|
2879
3024
|
responseMimeType: "text/plain",
|
|
2880
|
-
...
|
|
2881
|
-
thinkingConfig: this.config.thinkingConfig
|
|
2882
|
-
}
|
|
3025
|
+
...thinkingConfig ? { thinkingConfig } : {}
|
|
2883
3026
|
};
|
|
2884
3027
|
const safetySettings2 = this.config.safetySettings;
|
|
2885
3028
|
const reqValue = {
|
|
@@ -2960,7 +3103,11 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2960
3103
|
}
|
|
2961
3104
|
for (const part of candidate.content.parts) {
|
|
2962
3105
|
if ("text" in part) {
|
|
2963
|
-
|
|
3106
|
+
if ("thought" in part && part.thought) {
|
|
3107
|
+
result.thought = part.text;
|
|
3108
|
+
} else {
|
|
3109
|
+
result.content = part.text;
|
|
3110
|
+
}
|
|
2964
3111
|
continue;
|
|
2965
3112
|
}
|
|
2966
3113
|
if ("functionCall" in part) {
|
|
@@ -2983,7 +3130,8 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2983
3130
|
this.tokensUsed = {
|
|
2984
3131
|
totalTokens: resp.usageMetadata.totalTokenCount,
|
|
2985
3132
|
promptTokens: resp.usageMetadata.promptTokenCount,
|
|
2986
|
-
completionTokens: resp.usageMetadata.candidatesTokenCount
|
|
3133
|
+
completionTokens: resp.usageMetadata.candidatesTokenCount,
|
|
3134
|
+
thoughtsTokens: resp.usageMetadata.thoughtsTokenCount
|
|
2987
3135
|
};
|
|
2988
3136
|
}
|
|
2989
3137
|
return { results };
|
|
@@ -4574,6 +4722,14 @@ var AxSignature = class _AxSignature {
|
|
|
4574
4722
|
};
|
|
4575
4723
|
hash = () => this.sigHash;
|
|
4576
4724
|
toString = () => this.sigString;
|
|
4725
|
+
toJSON = () => {
|
|
4726
|
+
return {
|
|
4727
|
+
id: this.hash(),
|
|
4728
|
+
description: this.description,
|
|
4729
|
+
inputFields: this.inputFields,
|
|
4730
|
+
outputFields: this.outputFields
|
|
4731
|
+
};
|
|
4732
|
+
};
|
|
4577
4733
|
};
|
|
4578
4734
|
function renderField(field) {
|
|
4579
4735
|
let result = field.name;
|
|
@@ -5797,7 +5953,7 @@ function* streamValues(sig, content, values, xstate) {
|
|
|
5797
5953
|
const v = value.slice(s);
|
|
5798
5954
|
if (v && v.length > 0) {
|
|
5799
5955
|
yield { [key]: v };
|
|
5800
|
-
xstate.streamedIndex[key] = s +
|
|
5956
|
+
xstate.streamedIndex[key] = s + v.length;
|
|
5801
5957
|
}
|
|
5802
5958
|
continue;
|
|
5803
5959
|
}
|
|
@@ -6114,7 +6270,7 @@ var parseFunctions = (newFuncs, existingFuncs) => {
|
|
|
6114
6270
|
}
|
|
6115
6271
|
return [...existingFuncs ?? [], ...functions];
|
|
6116
6272
|
};
|
|
6117
|
-
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId) => {
|
|
6273
|
+
var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
|
|
6118
6274
|
const funcProc = new AxFunctionProcessor(functionList);
|
|
6119
6275
|
const functionsExecuted = /* @__PURE__ */ new Set();
|
|
6120
6276
|
const promises = functionCalls.map((func) => {
|
|
@@ -6123,6 +6279,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6123
6279
|
}
|
|
6124
6280
|
const promise = funcProc.execute(func, { sessionId, traceId, ai }).then((functionResult) => {
|
|
6125
6281
|
functionsExecuted.add(func.name.toLowerCase());
|
|
6282
|
+
if (span) {
|
|
6283
|
+
const eventData = {
|
|
6284
|
+
name: func.name
|
|
6285
|
+
};
|
|
6286
|
+
if (!excludeContentFromTelemetry) {
|
|
6287
|
+
eventData.args = func.args;
|
|
6288
|
+
eventData.result = functionResult ?? "";
|
|
6289
|
+
}
|
|
6290
|
+
span.addEvent("function.call", eventData);
|
|
6291
|
+
}
|
|
6126
6292
|
return {
|
|
6127
6293
|
role: "function",
|
|
6128
6294
|
result: functionResult ?? "",
|
|
@@ -6131,6 +6297,17 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
|
|
|
6131
6297
|
}).catch((e) => {
|
|
6132
6298
|
if (e instanceof FunctionError) {
|
|
6133
6299
|
const result = e.getFixingInstructions();
|
|
6300
|
+
if (span) {
|
|
6301
|
+
const errorEventData = {
|
|
6302
|
+
name: func.name,
|
|
6303
|
+
message: e.toString()
|
|
6304
|
+
};
|
|
6305
|
+
if (!excludeContentFromTelemetry) {
|
|
6306
|
+
errorEventData.args = func.args;
|
|
6307
|
+
errorEventData.fixing_instructions = result;
|
|
6308
|
+
}
|
|
6309
|
+
span.addEvent("function.error", errorEventData);
|
|
6310
|
+
}
|
|
6134
6311
|
mem.add(
|
|
6135
6312
|
{
|
|
6136
6313
|
role: "function",
|
|
@@ -6188,6 +6365,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6188
6365
|
functionsExecuted = /* @__PURE__ */ new Set();
|
|
6189
6366
|
fieldProcessors = [];
|
|
6190
6367
|
streamingFieldProcessors = [];
|
|
6368
|
+
values = {};
|
|
6369
|
+
excludeContentFromTrace = false;
|
|
6191
6370
|
constructor(signature, options) {
|
|
6192
6371
|
super(signature, { description: options?.description });
|
|
6193
6372
|
this.options = options;
|
|
@@ -6197,6 +6376,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6197
6376
|
);
|
|
6198
6377
|
this.asserts = this.options?.asserts ?? [];
|
|
6199
6378
|
this.streamingAsserts = this.options?.streamingAsserts ?? [];
|
|
6379
|
+
this.excludeContentFromTrace = options?.excludeContentFromTrace ?? false;
|
|
6200
6380
|
this.usage = [];
|
|
6201
6381
|
if (options?.functions) {
|
|
6202
6382
|
this.functions = parseFunctions(options.functions);
|
|
@@ -6245,7 +6425,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6245
6425
|
rateLimiter,
|
|
6246
6426
|
stream,
|
|
6247
6427
|
functions: _functions,
|
|
6248
|
-
functionCall: _functionCall
|
|
6428
|
+
functionCall: _functionCall,
|
|
6429
|
+
thinkingTokenBudget
|
|
6249
6430
|
} = options ?? {};
|
|
6250
6431
|
const chatPrompt = mem?.history(sessionId) ?? [];
|
|
6251
6432
|
if (chatPrompt.length === 0) {
|
|
@@ -6266,7 +6447,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6266
6447
|
traceId,
|
|
6267
6448
|
rateLimiter,
|
|
6268
6449
|
stream,
|
|
6269
|
-
debug: false
|
|
6450
|
+
debug: false,
|
|
6451
|
+
thinkingTokenBudget
|
|
6270
6452
|
}
|
|
6271
6453
|
);
|
|
6272
6454
|
return res;
|
|
@@ -6274,7 +6456,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6274
6456
|
async *forwardCore({
|
|
6275
6457
|
ai,
|
|
6276
6458
|
mem,
|
|
6277
|
-
options
|
|
6459
|
+
options,
|
|
6460
|
+
span
|
|
6278
6461
|
}) {
|
|
6279
6462
|
const { sessionId, traceId, functions: _functions } = options ?? {};
|
|
6280
6463
|
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
@@ -6294,7 +6477,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6294
6477
|
traceId,
|
|
6295
6478
|
sessionId,
|
|
6296
6479
|
functions,
|
|
6297
|
-
fastFail
|
|
6480
|
+
fastFail,
|
|
6481
|
+
span
|
|
6298
6482
|
});
|
|
6299
6483
|
} else {
|
|
6300
6484
|
yield await this.processResponse({
|
|
@@ -6304,7 +6488,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6304
6488
|
mem,
|
|
6305
6489
|
traceId,
|
|
6306
6490
|
sessionId,
|
|
6307
|
-
functions
|
|
6491
|
+
functions,
|
|
6492
|
+
span
|
|
6308
6493
|
});
|
|
6309
6494
|
}
|
|
6310
6495
|
}
|
|
@@ -6316,11 +6501,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6316
6501
|
sessionId,
|
|
6317
6502
|
traceId,
|
|
6318
6503
|
functions,
|
|
6319
|
-
fastFail
|
|
6504
|
+
fastFail,
|
|
6505
|
+
span
|
|
6320
6506
|
}) {
|
|
6321
6507
|
const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
|
|
6322
6508
|
const functionCalls = [];
|
|
6323
|
-
|
|
6509
|
+
this.values = {};
|
|
6324
6510
|
const xstate = {
|
|
6325
6511
|
extractedFields: [],
|
|
6326
6512
|
streamedIndex: {},
|
|
@@ -6347,6 +6533,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6347
6533
|
sessionId
|
|
6348
6534
|
);
|
|
6349
6535
|
} else if (result.content) {
|
|
6536
|
+
if (result.thought && result.thought.length > 0) {
|
|
6537
|
+
yield { thought: result.thought };
|
|
6538
|
+
}
|
|
6350
6539
|
content += result.content;
|
|
6351
6540
|
mem.updateResult(
|
|
6352
6541
|
{ name: result.name, content, delta: result.content },
|
|
@@ -6354,7 +6543,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6354
6543
|
);
|
|
6355
6544
|
const skip = streamingExtractValues(
|
|
6356
6545
|
this.signature,
|
|
6357
|
-
values,
|
|
6546
|
+
this.values,
|
|
6358
6547
|
xstate,
|
|
6359
6548
|
content,
|
|
6360
6549
|
streamingValidation
|
|
@@ -6375,12 +6564,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6375
6564
|
content,
|
|
6376
6565
|
xstate,
|
|
6377
6566
|
mem,
|
|
6378
|
-
values,
|
|
6567
|
+
this.values,
|
|
6379
6568
|
sessionId
|
|
6380
6569
|
);
|
|
6381
6570
|
}
|
|
6382
|
-
yield* streamValues(
|
|
6383
|
-
|
|
6571
|
+
yield* streamValues(
|
|
6572
|
+
this.signature,
|
|
6573
|
+
content,
|
|
6574
|
+
this.values,
|
|
6575
|
+
xstate
|
|
6576
|
+
);
|
|
6577
|
+
await assertAssertions(this.asserts, this.values);
|
|
6578
|
+
} else if (result.thought && result.thought.length > 0) {
|
|
6579
|
+
this.values.thought = this.values.thought ?? "" + result.thought;
|
|
6580
|
+
yield { thought: result.thought };
|
|
6384
6581
|
}
|
|
6385
6582
|
if (result.finishReason === "length") {
|
|
6386
6583
|
throw new Error(
|
|
@@ -6389,7 +6586,7 @@ Content: ${content}`
|
|
|
6389
6586
|
);
|
|
6390
6587
|
}
|
|
6391
6588
|
}
|
|
6392
|
-
const funcs = parseFunctionCalls(ai, functionCalls, values, model);
|
|
6589
|
+
const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
|
|
6393
6590
|
if (funcs) {
|
|
6394
6591
|
if (!functions) {
|
|
6395
6592
|
throw new Error("Functions are not defined");
|
|
@@ -6400,22 +6597,24 @@ Content: ${content}`
|
|
|
6400
6597
|
funcs,
|
|
6401
6598
|
mem,
|
|
6402
6599
|
sessionId,
|
|
6403
|
-
traceId
|
|
6600
|
+
traceId,
|
|
6601
|
+
span,
|
|
6602
|
+
this.excludeContentFromTrace
|
|
6404
6603
|
);
|
|
6405
6604
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6406
6605
|
} else {
|
|
6407
|
-
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
6606
|
+
streamingExtractFinalValue(this.signature, this.values, xstate, content);
|
|
6408
6607
|
await assertStreamingAssertions(
|
|
6409
6608
|
this.streamingAsserts,
|
|
6410
6609
|
xstate,
|
|
6411
6610
|
content,
|
|
6412
6611
|
true
|
|
6413
6612
|
);
|
|
6414
|
-
await assertAssertions(this.asserts, values);
|
|
6613
|
+
await assertAssertions(this.asserts, this.values);
|
|
6415
6614
|
if (this.fieldProcessors.length) {
|
|
6416
6615
|
await processFieldProcessors(
|
|
6417
6616
|
this.fieldProcessors,
|
|
6418
|
-
values,
|
|
6617
|
+
this.values,
|
|
6419
6618
|
mem,
|
|
6420
6619
|
sessionId
|
|
6421
6620
|
);
|
|
@@ -6426,12 +6625,17 @@ Content: ${content}`
|
|
|
6426
6625
|
content,
|
|
6427
6626
|
xstate,
|
|
6428
6627
|
mem,
|
|
6429
|
-
values,
|
|
6628
|
+
this.values,
|
|
6430
6629
|
sessionId,
|
|
6431
6630
|
true
|
|
6432
6631
|
);
|
|
6433
6632
|
}
|
|
6434
|
-
yield* streamValues(
|
|
6633
|
+
yield* streamValues(
|
|
6634
|
+
this.signature,
|
|
6635
|
+
content,
|
|
6636
|
+
this.values,
|
|
6637
|
+
xstate
|
|
6638
|
+
);
|
|
6435
6639
|
}
|
|
6436
6640
|
}
|
|
6437
6641
|
async processResponse({
|
|
@@ -6440,9 +6644,10 @@ Content: ${content}`
|
|
|
6440
6644
|
mem,
|
|
6441
6645
|
sessionId,
|
|
6442
6646
|
traceId,
|
|
6443
|
-
functions
|
|
6647
|
+
functions,
|
|
6648
|
+
span
|
|
6444
6649
|
}) {
|
|
6445
|
-
|
|
6650
|
+
this.values = {};
|
|
6446
6651
|
let results = res.results ?? [];
|
|
6447
6652
|
if (results.length > 1) {
|
|
6448
6653
|
results = results.filter((r) => r.functionCalls);
|
|
@@ -6453,7 +6658,7 @@ Content: ${content}`
|
|
|
6453
6658
|
}
|
|
6454
6659
|
mem.addResult(result, sessionId);
|
|
6455
6660
|
if (result.functionCalls?.length) {
|
|
6456
|
-
const funcs = parseFunctionCalls(ai, result.functionCalls, values);
|
|
6661
|
+
const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
|
|
6457
6662
|
if (funcs) {
|
|
6458
6663
|
if (!functions) {
|
|
6459
6664
|
throw new Error("Functions are not defined");
|
|
@@ -6464,17 +6669,22 @@ Content: ${content}`
|
|
|
6464
6669
|
funcs,
|
|
6465
6670
|
mem,
|
|
6466
6671
|
sessionId,
|
|
6467
|
-
traceId
|
|
6672
|
+
traceId,
|
|
6673
|
+
span,
|
|
6674
|
+
this.excludeContentFromTrace
|
|
6468
6675
|
);
|
|
6469
6676
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6470
6677
|
}
|
|
6471
6678
|
} else if (result.content) {
|
|
6472
|
-
|
|
6473
|
-
|
|
6679
|
+
if (result.thought && result.thought.length > 0) {
|
|
6680
|
+
this.values.thought = result.thought;
|
|
6681
|
+
}
|
|
6682
|
+
extractValues(this.signature, this.values, result.content);
|
|
6683
|
+
await assertAssertions(this.asserts, this.values);
|
|
6474
6684
|
if (this.fieldProcessors.length) {
|
|
6475
6685
|
await processFieldProcessors(
|
|
6476
6686
|
this.fieldProcessors,
|
|
6477
|
-
values,
|
|
6687
|
+
this.values,
|
|
6478
6688
|
mem,
|
|
6479
6689
|
sessionId
|
|
6480
6690
|
);
|
|
@@ -6487,13 +6697,12 @@ Content: ${result.content}`
|
|
|
6487
6697
|
);
|
|
6488
6698
|
}
|
|
6489
6699
|
}
|
|
6490
|
-
const publicValues = { ...values };
|
|
6491
6700
|
for (const field of this.signature.getOutputFields()) {
|
|
6492
6701
|
if (field.isInternal) {
|
|
6493
|
-
delete
|
|
6702
|
+
delete this.values[field.name];
|
|
6494
6703
|
}
|
|
6495
6704
|
}
|
|
6496
|
-
return { ...values };
|
|
6705
|
+
return { ...this.values };
|
|
6497
6706
|
}
|
|
6498
6707
|
async *_forward2(ai, values, options, span) {
|
|
6499
6708
|
const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
|
|
@@ -6519,7 +6728,7 @@ Content: ${result.content}`
|
|
|
6519
6728
|
multiStepLoop: for (let n = 0; n < maxSteps; n++) {
|
|
6520
6729
|
for (let errCount = 0; errCount < maxRetries; errCount++) {
|
|
6521
6730
|
try {
|
|
6522
|
-
const generator = this.forwardCore({ options, ai, mem });
|
|
6731
|
+
const generator = this.forwardCore({ options, ai, mem, span });
|
|
6523
6732
|
for await (const delta of generator) {
|
|
6524
6733
|
if (delta !== void 0) {
|
|
6525
6734
|
yield { version: errCount, delta };
|
|
@@ -6543,10 +6752,22 @@ Content: ${result.content}`
|
|
|
6543
6752
|
if (e instanceof ValidationError) {
|
|
6544
6753
|
errorFields = e.getFixingInstructions();
|
|
6545
6754
|
err = e;
|
|
6755
|
+
if (span) {
|
|
6756
|
+
span.addEvent("validation.error", {
|
|
6757
|
+
message: e.toString(),
|
|
6758
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6759
|
+
});
|
|
6760
|
+
}
|
|
6546
6761
|
} else if (e instanceof AxAssertionError) {
|
|
6547
6762
|
const e1 = e;
|
|
6548
6763
|
errorFields = e1.getFixingInstructions();
|
|
6549
6764
|
err = e;
|
|
6765
|
+
if (span) {
|
|
6766
|
+
span.addEvent("assertion.error", {
|
|
6767
|
+
message: e1.toString(),
|
|
6768
|
+
fixing_instructions: errorFields?.map((f) => f.title).join(", ") ?? ""
|
|
6769
|
+
});
|
|
6770
|
+
}
|
|
6550
6771
|
} else if (e instanceof AxAIServiceStreamTerminatedError) {
|
|
6551
6772
|
} else {
|
|
6552
6773
|
throw enhanceError(e, ai, this.signature);
|
|
@@ -6601,14 +6822,25 @@ Content: ${result.content}`
|
|
|
6601
6822
|
}
|
|
6602
6823
|
const funcNames = functions?.map((f) => f.name).join(",");
|
|
6603
6824
|
const attributes = {
|
|
6604
|
-
|
|
6605
|
-
|
|
6825
|
+
signature: JSON.stringify(this.signature.toJSON(), null, 2),
|
|
6826
|
+
...this.examples ? { examples: JSON.stringify(this.examples, null, 2) } : {},
|
|
6827
|
+
...funcNames ? { provided_functions: funcNames } : {},
|
|
6828
|
+
...options?.model ? { model: options.model } : {},
|
|
6829
|
+
...options?.thinkingTokenBudget ? { thinking_token_budget: options.thinkingTokenBudget } : {},
|
|
6830
|
+
...options?.maxSteps ? { max_steps: options.maxSteps } : {},
|
|
6831
|
+
...options?.maxRetries ? { max_retries: options.maxRetries } : {},
|
|
6832
|
+
...options?.fastFail ? { fast_fail: options.fastFail } : {}
|
|
6606
6833
|
};
|
|
6607
|
-
const
|
|
6834
|
+
const traceLabel = options.traceLabel ?? this.options?.traceLabel;
|
|
6835
|
+
const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
|
|
6836
|
+
const span = tracer.startSpan(spanName, {
|
|
6608
6837
|
kind: SpanKind2.SERVER,
|
|
6609
6838
|
attributes
|
|
6610
6839
|
});
|
|
6611
6840
|
try {
|
|
6841
|
+
if (!this.excludeContentFromTrace) {
|
|
6842
|
+
span.addEvent("input", { content: JSON.stringify(values, null, 2) });
|
|
6843
|
+
}
|
|
6612
6844
|
yield* this._forward2(
|
|
6613
6845
|
ai,
|
|
6614
6846
|
values,
|
|
@@ -6618,6 +6850,11 @@ Content: ${result.content}`
|
|
|
6618
6850
|
},
|
|
6619
6851
|
span
|
|
6620
6852
|
);
|
|
6853
|
+
if (!this.excludeContentFromTrace) {
|
|
6854
|
+
span.addEvent("output", {
|
|
6855
|
+
content: JSON.stringify(this.values, null, 2)
|
|
6856
|
+
});
|
|
6857
|
+
}
|
|
6621
6858
|
} finally {
|
|
6622
6859
|
span.end();
|
|
6623
6860
|
}
|