@ax-llm/ax 11.0.51 → 11.0.53
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 +305 -66
- package/index.cjs.map +1 -1
- package/index.d.cts +101 -9
- package/index.d.ts +101 -9
- package/index.js +314 -77
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -66,6 +66,7 @@ __export(index_exports, {
|
|
|
66
66
|
AxAIOpenAIResponsesImpl: () => AxAIOpenAIResponsesImpl,
|
|
67
67
|
AxAIReka: () => AxAIReka,
|
|
68
68
|
AxAIRekaModel: () => AxAIRekaModel,
|
|
69
|
+
AxAIServiceAbortedError: () => AxAIServiceAbortedError,
|
|
69
70
|
AxAIServiceAuthenticationError: () => AxAIServiceAuthenticationError,
|
|
70
71
|
AxAIServiceError: () => AxAIServiceError,
|
|
71
72
|
AxAIServiceNetworkError: () => AxAIServiceNetworkError,
|
|
@@ -74,6 +75,7 @@ __export(index_exports, {
|
|
|
74
75
|
AxAIServiceStreamTerminatedError: () => AxAIServiceStreamTerminatedError,
|
|
75
76
|
AxAIServiceTimeoutError: () => AxAIServiceTimeoutError,
|
|
76
77
|
AxAITogether: () => AxAITogether,
|
|
78
|
+
AxAbortableAI: () => AxAbortableAI,
|
|
77
79
|
AxAgent: () => AxAgent,
|
|
78
80
|
AxApacheTika: () => AxApacheTika,
|
|
79
81
|
AxAssertionError: () => AxAssertionError,
|
|
@@ -459,10 +461,25 @@ var AxAIServiceStreamTerminatedError = class extends AxAIServiceError {
|
|
|
459
461
|
};
|
|
460
462
|
var AxAIServiceTimeoutError = class extends AxAIServiceError {
|
|
461
463
|
constructor(url, timeoutMs, requestBody, context3) {
|
|
462
|
-
super(
|
|
463
|
-
timeoutMs
|
|
464
|
-
|
|
465
|
-
|
|
464
|
+
super(
|
|
465
|
+
`Request timed out after ${timeoutMs}ms`,
|
|
466
|
+
url,
|
|
467
|
+
requestBody,
|
|
468
|
+
void 0,
|
|
469
|
+
{ timeoutMs, ...context3 }
|
|
470
|
+
);
|
|
471
|
+
this.name = this.constructor.name;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var AxAIServiceAbortedError = class extends AxAIServiceError {
|
|
475
|
+
constructor(url, reason, requestBody, context3) {
|
|
476
|
+
super(
|
|
477
|
+
`Request aborted${reason ? `: ${reason}` : ""}`,
|
|
478
|
+
url,
|
|
479
|
+
requestBody,
|
|
480
|
+
void 0,
|
|
481
|
+
{ abortReason: reason, ...context3 }
|
|
482
|
+
);
|
|
466
483
|
this.name = this.constructor.name;
|
|
467
484
|
}
|
|
468
485
|
};
|
|
@@ -522,9 +539,34 @@ var apiCall = async (api, json) => {
|
|
|
522
539
|
});
|
|
523
540
|
let attempt = 0;
|
|
524
541
|
while (true) {
|
|
525
|
-
const
|
|
542
|
+
const combinedAbortController = new AbortController();
|
|
543
|
+
if (api.abortSignal) {
|
|
544
|
+
if (api.abortSignal.aborted) {
|
|
545
|
+
throw new AxAIServiceAbortedError(
|
|
546
|
+
apiUrl.href,
|
|
547
|
+
api.abortSignal.reason,
|
|
548
|
+
json,
|
|
549
|
+
{ metrics }
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
const userAbortHandler = () => {
|
|
553
|
+
combinedAbortController.abort(
|
|
554
|
+
api.abortSignal.reason || "User aborted request"
|
|
555
|
+
);
|
|
556
|
+
};
|
|
557
|
+
api.abortSignal.addEventListener("abort", userAbortHandler, {
|
|
558
|
+
once: true
|
|
559
|
+
});
|
|
560
|
+
const originalAbort = combinedAbortController.abort.bind(
|
|
561
|
+
combinedAbortController
|
|
562
|
+
);
|
|
563
|
+
combinedAbortController.abort = (reason) => {
|
|
564
|
+
api.abortSignal.removeEventListener("abort", userAbortHandler);
|
|
565
|
+
originalAbort(reason);
|
|
566
|
+
};
|
|
567
|
+
}
|
|
526
568
|
timeoutId = setTimeout(() => {
|
|
527
|
-
|
|
569
|
+
combinedAbortController.abort("Request timeout");
|
|
528
570
|
}, timeoutMs);
|
|
529
571
|
try {
|
|
530
572
|
const res = await (api.fetch ?? fetch)(apiUrl, {
|
|
@@ -536,7 +578,7 @@ var apiCall = async (api, json) => {
|
|
|
536
578
|
...api.headers
|
|
537
579
|
},
|
|
538
580
|
body: JSON.stringify(json),
|
|
539
|
-
signal:
|
|
581
|
+
signal: combinedAbortController.signal
|
|
540
582
|
});
|
|
541
583
|
clearTimeout(timeoutId);
|
|
542
584
|
if (res.status === 401 || res.status === 403) {
|
|
@@ -599,12 +641,12 @@ var apiCall = async (api, json) => {
|
|
|
599
641
|
let lastChunk;
|
|
600
642
|
let chunkCount = 0;
|
|
601
643
|
const trackingStream = new import_web3.TransformStream({
|
|
602
|
-
transform(chunk,
|
|
644
|
+
transform(chunk, controller) {
|
|
603
645
|
lastChunk = chunk;
|
|
604
646
|
chunkCount++;
|
|
605
647
|
metrics.streamChunks = chunkCount;
|
|
606
648
|
metrics.lastChunkTime = Date.now();
|
|
607
|
-
|
|
649
|
+
controller.enqueue(chunk);
|
|
608
650
|
api.span?.addEvent("stream.chunk", {
|
|
609
651
|
"stream.chunks": chunkCount,
|
|
610
652
|
"stream.duration": Date.now() - metrics.startTime,
|
|
@@ -614,7 +656,7 @@ var apiCall = async (api, json) => {
|
|
|
614
656
|
});
|
|
615
657
|
let closed = false;
|
|
616
658
|
return new import_web3.ReadableStream({
|
|
617
|
-
start(
|
|
659
|
+
start(controller) {
|
|
618
660
|
const reader = res.body.pipeThrough(new textDecoderStream()).pipeThrough(new SSEParser()).pipeThrough(trackingStream).getReader();
|
|
619
661
|
async function read() {
|
|
620
662
|
try {
|
|
@@ -623,12 +665,12 @@ var apiCall = async (api, json) => {
|
|
|
623
665
|
if (done) {
|
|
624
666
|
if (!closed) {
|
|
625
667
|
closed = true;
|
|
626
|
-
|
|
668
|
+
controller.close();
|
|
627
669
|
}
|
|
628
670
|
break;
|
|
629
671
|
}
|
|
630
672
|
if (closed) break;
|
|
631
|
-
|
|
673
|
+
controller.enqueue(value);
|
|
632
674
|
}
|
|
633
675
|
} catch (e) {
|
|
634
676
|
const error = e;
|
|
@@ -637,7 +679,7 @@ var apiCall = async (api, json) => {
|
|
|
637
679
|
streamDuration: Date.now() - metrics.startTime
|
|
638
680
|
};
|
|
639
681
|
if (error.name === "AbortError" || error.message?.includes("aborted")) {
|
|
640
|
-
|
|
682
|
+
controller.error(
|
|
641
683
|
new AxAIServiceStreamTerminatedError(
|
|
642
684
|
apiUrl.href,
|
|
643
685
|
json,
|
|
@@ -646,7 +688,7 @@ var apiCall = async (api, json) => {
|
|
|
646
688
|
)
|
|
647
689
|
);
|
|
648
690
|
} else if (error instanceof TypeError && error.message.includes("cancelled")) {
|
|
649
|
-
|
|
691
|
+
controller.error(
|
|
650
692
|
new AxAIServiceStreamTerminatedError(
|
|
651
693
|
apiUrl.href,
|
|
652
694
|
json,
|
|
@@ -658,7 +700,7 @@ var apiCall = async (api, json) => {
|
|
|
658
700
|
)
|
|
659
701
|
);
|
|
660
702
|
} else {
|
|
661
|
-
|
|
703
|
+
controller.error(
|
|
662
704
|
new AxAIServiceNetworkError(
|
|
663
705
|
error,
|
|
664
706
|
apiUrl.href,
|
|
@@ -685,9 +727,18 @@ var apiCall = async (api, json) => {
|
|
|
685
727
|
});
|
|
686
728
|
} catch (error) {
|
|
687
729
|
if (error instanceof Error && error.name === "AbortError") {
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
730
|
+
if (api.abortSignal?.aborted) {
|
|
731
|
+
throw new AxAIServiceAbortedError(
|
|
732
|
+
apiUrl.href,
|
|
733
|
+
api.abortSignal.reason,
|
|
734
|
+
json,
|
|
735
|
+
{ metrics }
|
|
736
|
+
);
|
|
737
|
+
} else {
|
|
738
|
+
throw new AxAIServiceTimeoutError(apiUrl.href, timeoutMs, json, {
|
|
739
|
+
metrics
|
|
740
|
+
});
|
|
741
|
+
}
|
|
691
742
|
}
|
|
692
743
|
if (api.span?.isRecording()) {
|
|
693
744
|
api.span.recordException(error);
|
|
@@ -885,13 +936,11 @@ var logResponseDelta = (delta) => {
|
|
|
885
936
|
|
|
886
937
|
// ai/base.ts
|
|
887
938
|
var axBaseAIDefaultConfig = () => structuredClone({
|
|
888
|
-
maxTokens: 2e3,
|
|
889
939
|
temperature: 0,
|
|
890
940
|
topK: 40,
|
|
891
941
|
topP: 0.9
|
|
892
942
|
});
|
|
893
943
|
var axBaseAIDefaultCreativeConfig = () => structuredClone({
|
|
894
|
-
maxTokens: 2e3,
|
|
895
944
|
temperature: 0.4,
|
|
896
945
|
topP: 0.7,
|
|
897
946
|
frequencyPenalty: 0.2
|
|
@@ -934,6 +983,7 @@ var AxBaseAI = class {
|
|
|
934
983
|
timeout;
|
|
935
984
|
excludeContentFromTrace;
|
|
936
985
|
models;
|
|
986
|
+
abortSignal;
|
|
937
987
|
modelInfo;
|
|
938
988
|
modelUsage;
|
|
939
989
|
embedModelUsage;
|
|
@@ -994,6 +1044,7 @@ var AxBaseAI = class {
|
|
|
994
1044
|
this.timeout = options.timeout;
|
|
995
1045
|
this.tracer = options.tracer;
|
|
996
1046
|
this.excludeContentFromTrace = options.excludeContentFromTrace;
|
|
1047
|
+
this.abortSignal = options.abortSignal;
|
|
997
1048
|
}
|
|
998
1049
|
getOptions() {
|
|
999
1050
|
return {
|
|
@@ -1002,7 +1053,8 @@ var AxBaseAI = class {
|
|
|
1002
1053
|
fetch: this.fetch,
|
|
1003
1054
|
tracer: this.tracer,
|
|
1004
1055
|
timeout: this.timeout,
|
|
1005
|
-
excludeContentFromTrace: this.excludeContentFromTrace
|
|
1056
|
+
excludeContentFromTrace: this.excludeContentFromTrace,
|
|
1057
|
+
abortSignal: this.abortSignal
|
|
1006
1058
|
};
|
|
1007
1059
|
}
|
|
1008
1060
|
getModelList() {
|
|
@@ -1114,14 +1166,14 @@ var AxBaseAI = class {
|
|
|
1114
1166
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
1115
1167
|
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
1116
1168
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
1117
|
-
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
1169
|
+
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens ?? "Not set",
|
|
1118
1170
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
1119
|
-
[axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP,
|
|
1120
|
-
[axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK,
|
|
1121
|
-
[axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty,
|
|
1122
|
-
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
1123
|
-
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
1124
|
-
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
1171
|
+
[axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP ?? "Not set",
|
|
1172
|
+
[axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK ?? "Not set",
|
|
1173
|
+
[axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty ?? "Not set",
|
|
1174
|
+
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty ?? "Not set",
|
|
1175
|
+
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", ") ?? "Not set",
|
|
1176
|
+
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream ?? "Not set"
|
|
1125
1177
|
}
|
|
1126
1178
|
},
|
|
1127
1179
|
options?.traceContext ?? import_api2.context.active(),
|
|
@@ -1184,7 +1236,8 @@ var AxBaseAI = class {
|
|
|
1184
1236
|
timeout: this.timeout,
|
|
1185
1237
|
debug,
|
|
1186
1238
|
fetch: this.fetch,
|
|
1187
|
-
span
|
|
1239
|
+
span,
|
|
1240
|
+
abortSignal: options?.abortSignal ?? this.abortSignal
|
|
1188
1241
|
},
|
|
1189
1242
|
reqValue
|
|
1190
1243
|
);
|
|
@@ -1341,7 +1394,8 @@ var AxBaseAI = class {
|
|
|
1341
1394
|
debug,
|
|
1342
1395
|
fetch: this.fetch,
|
|
1343
1396
|
timeout: this.timeout,
|
|
1344
|
-
span
|
|
1397
|
+
span,
|
|
1398
|
+
abortSignal: options?.abortSignal ?? this.abortSignal
|
|
1345
1399
|
},
|
|
1346
1400
|
reqValue
|
|
1347
1401
|
);
|
|
@@ -2220,7 +2274,7 @@ var AxAIOpenAIImpl = class {
|
|
|
2220
2274
|
response_format: this.config?.responseFormat ? { type: this.config.responseFormat } : void 0,
|
|
2221
2275
|
tools,
|
|
2222
2276
|
tool_choice: toolsChoice,
|
|
2223
|
-
max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens
|
|
2277
|
+
max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
2224
2278
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
2225
2279
|
top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
|
|
2226
2280
|
n: req.modelConfig?.n ?? this.config.n,
|
|
@@ -4776,7 +4830,6 @@ var axAIOpenAIResponsesDefaultConfig = () => ({
|
|
|
4776
4830
|
model: "gpt-4o" /* GPT4O */,
|
|
4777
4831
|
embedModel: "text-embedding-ada-002" /* TextEmbeddingAda002 */,
|
|
4778
4832
|
temperature: 0.7,
|
|
4779
|
-
maxTokens: 2048,
|
|
4780
4833
|
topP: 1,
|
|
4781
4834
|
stream: true
|
|
4782
4835
|
// reasoningEffort: 'medium',
|
|
@@ -4943,7 +4996,7 @@ var AxAIRekaImpl = class {
|
|
|
4943
4996
|
const reqValue = {
|
|
4944
4997
|
model,
|
|
4945
4998
|
messages,
|
|
4946
|
-
max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens
|
|
4999
|
+
max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
4947
5000
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
4948
5001
|
top_k: req.modelConfig?.n ?? this.config.n,
|
|
4949
5002
|
top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
|
|
@@ -5634,6 +5687,8 @@ var validateValue = (field, value) => {
|
|
|
5634
5687
|
const ft = field.type ?? { name: "string", isArray: false };
|
|
5635
5688
|
const validateSingleValue = (expectedType, val) => {
|
|
5636
5689
|
switch (expectedType) {
|
|
5690
|
+
case "class":
|
|
5691
|
+
return typeof val === "string";
|
|
5637
5692
|
case "code":
|
|
5638
5693
|
return typeof val === "string";
|
|
5639
5694
|
case "string":
|
|
@@ -5672,7 +5727,7 @@ var validateValue = (field, value) => {
|
|
|
5672
5727
|
}
|
|
5673
5728
|
if (msg) {
|
|
5674
5729
|
throw new Error(
|
|
5675
|
-
`Validation failed: Expected '${field.name}' to be
|
|
5730
|
+
`Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
|
|
5676
5731
|
);
|
|
5677
5732
|
}
|
|
5678
5733
|
return;
|
|
@@ -5697,7 +5752,7 @@ var validateValue = (field, value) => {
|
|
|
5697
5752
|
}
|
|
5698
5753
|
if (msg) {
|
|
5699
5754
|
throw new Error(
|
|
5700
|
-
`Validation failed: Expected '${field.name}' to be
|
|
5755
|
+
`Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
|
|
5701
5756
|
);
|
|
5702
5757
|
}
|
|
5703
5758
|
return;
|
|
@@ -5718,8 +5773,9 @@ var validateValue = (field, value) => {
|
|
|
5718
5773
|
isValid = validateSingleValue(ft.name, value);
|
|
5719
5774
|
}
|
|
5720
5775
|
if (!isValid) {
|
|
5776
|
+
const gotType = Array.isArray(value) ? "array" : typeof value;
|
|
5721
5777
|
throw new Error(
|
|
5722
|
-
`Validation failed: Expected '${field.name}' to be a ${field.type?.isArray ? "an array of " : ""}${ft.name} instead got '${
|
|
5778
|
+
`Validation failed: Expected '${field.name}' to be a ${field.type?.isArray ? "an array of " : ""}${ft.name} instead got '${gotType}' (${JSON.stringify(value)})`
|
|
5723
5779
|
);
|
|
5724
5780
|
}
|
|
5725
5781
|
};
|
|
@@ -5924,11 +5980,15 @@ var AxPromptTemplate = class {
|
|
|
5924
5980
|
task;
|
|
5925
5981
|
thoughtFieldName;
|
|
5926
5982
|
functions;
|
|
5983
|
+
strictExamples;
|
|
5984
|
+
optionalOutputFields;
|
|
5927
5985
|
constructor(sig, options, fieldTemplates) {
|
|
5928
5986
|
this.sig = sig;
|
|
5929
5987
|
this.fieldTemplates = fieldTemplates;
|
|
5930
5988
|
this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
|
|
5931
5989
|
this.functions = options?.functions;
|
|
5990
|
+
this.strictExamples = options?.strictExamples ?? false;
|
|
5991
|
+
this.optionalOutputFields = options?.optionalOutputFields ?? [];
|
|
5932
5992
|
const task = [];
|
|
5933
5993
|
const inArgs = renderDescFields(this.sig.getInputFields());
|
|
5934
5994
|
const outArgs = renderDescFields(this.sig.getOutputFields());
|
|
@@ -6098,13 +6158,32 @@ ${outputFields}`);
|
|
|
6098
6158
|
};
|
|
6099
6159
|
renderExamples = (data) => {
|
|
6100
6160
|
const list = [];
|
|
6161
|
+
const inputExampleContext = {
|
|
6162
|
+
isExample: true,
|
|
6163
|
+
strictExamples: this.strictExamples,
|
|
6164
|
+
optionalOutputFields: this.optionalOutputFields,
|
|
6165
|
+
isInputField: true
|
|
6166
|
+
};
|
|
6167
|
+
const outputExampleContext = {
|
|
6168
|
+
isExample: true,
|
|
6169
|
+
strictExamples: this.strictExamples,
|
|
6170
|
+
optionalOutputFields: this.optionalOutputFields,
|
|
6171
|
+
isInputField: false
|
|
6172
|
+
};
|
|
6101
6173
|
for (const [index, item] of data.entries()) {
|
|
6102
|
-
const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
|
|
6103
|
-
const
|
|
6174
|
+
const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item, inputExampleContext)).filter((v) => v !== void 0).flat();
|
|
6175
|
+
const outputFields = this.sig.getOutputFields();
|
|
6176
|
+
const renderedOutputItem = outputFields.map((field) => this.renderInField(field, item, outputExampleContext)).filter((v) => v !== void 0).flat();
|
|
6104
6177
|
if (renderedOutputItem.length === 0) {
|
|
6105
|
-
|
|
6106
|
-
|
|
6178
|
+
const missingFields = outputFields.filter((field) => !item[field.name]);
|
|
6179
|
+
const allMissingFieldsAreOptional = missingFields.every(
|
|
6180
|
+
(field) => this.optionalOutputFields.includes(field.name)
|
|
6107
6181
|
);
|
|
6182
|
+
if (!allMissingFieldsAreOptional) {
|
|
6183
|
+
throw new Error(
|
|
6184
|
+
`Output fields are required in examples: index: ${index}, data: ${JSON.stringify(item)}`
|
|
6185
|
+
);
|
|
6186
|
+
}
|
|
6108
6187
|
}
|
|
6109
6188
|
const renderedItem = [...renderedInputItem, ...renderedOutputItem];
|
|
6110
6189
|
if (index > 0 && renderedItem.length > 0 && renderedItem[0]?.type === "text") {
|
|
@@ -6124,9 +6203,26 @@ ${outputFields}`);
|
|
|
6124
6203
|
};
|
|
6125
6204
|
renderDemos = (data) => {
|
|
6126
6205
|
const list = [];
|
|
6127
|
-
const
|
|
6206
|
+
const inputFields = this.sig.getInputFields();
|
|
6207
|
+
const outputFields = this.sig.getOutputFields();
|
|
6128
6208
|
for (const item of data) {
|
|
6129
|
-
const
|
|
6209
|
+
const inputRenderedItems = inputFields.map(
|
|
6210
|
+
(field) => this.renderInField(field, item, {
|
|
6211
|
+
isExample: true,
|
|
6212
|
+
strictExamples: this.strictExamples,
|
|
6213
|
+
optionalOutputFields: this.optionalOutputFields,
|
|
6214
|
+
isInputField: true
|
|
6215
|
+
})
|
|
6216
|
+
).filter((v) => v !== void 0).flat();
|
|
6217
|
+
const outputRenderedItems = outputFields.map(
|
|
6218
|
+
(field) => this.renderInField(field, item, {
|
|
6219
|
+
isExample: true,
|
|
6220
|
+
strictExamples: this.strictExamples,
|
|
6221
|
+
optionalOutputFields: this.optionalOutputFields,
|
|
6222
|
+
isInputField: false
|
|
6223
|
+
})
|
|
6224
|
+
).filter((v) => v !== void 0).flat();
|
|
6225
|
+
const renderedItem = [...inputRenderedItems, ...outputRenderedItems];
|
|
6130
6226
|
renderedItem.slice(0, -1).forEach((v) => {
|
|
6131
6227
|
if ("text" in v) {
|
|
6132
6228
|
v.text = v.text + "\n";
|
|
@@ -6140,15 +6236,15 @@ ${outputFields}`);
|
|
|
6140
6236
|
return list;
|
|
6141
6237
|
};
|
|
6142
6238
|
renderInputFields = (values) => {
|
|
6143
|
-
const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values)).filter((v) => v !== void 0).flat();
|
|
6239
|
+
const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values, void 0)).filter((v) => v !== void 0).flat();
|
|
6144
6240
|
renderedItems.filter((v) => v.type === "text").forEach((v) => {
|
|
6145
6241
|
v.text = v.text + "\n";
|
|
6146
6242
|
});
|
|
6147
6243
|
return renderedItems;
|
|
6148
6244
|
};
|
|
6149
|
-
renderInField = (field, values) => {
|
|
6245
|
+
renderInField = (field, values, context3) => {
|
|
6150
6246
|
const value = values[field.name];
|
|
6151
|
-
if (isEmptyValue(field, value)) {
|
|
6247
|
+
if (isEmptyValue(field, value, context3)) {
|
|
6152
6248
|
return;
|
|
6153
6249
|
}
|
|
6154
6250
|
if (field.type) {
|
|
@@ -6332,15 +6428,34 @@ function combineConsecutiveStrings(separator) {
|
|
|
6332
6428
|
return acc;
|
|
6333
6429
|
};
|
|
6334
6430
|
}
|
|
6335
|
-
var isEmptyValue = (field, value) => {
|
|
6431
|
+
var isEmptyValue = (field, value, context3) => {
|
|
6336
6432
|
if (typeof value === "boolean") {
|
|
6337
6433
|
return false;
|
|
6338
6434
|
}
|
|
6339
6435
|
if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
|
|
6340
|
-
if (
|
|
6436
|
+
if (context3?.isExample) {
|
|
6437
|
+
const isInputField = context3?.isInputField ?? true;
|
|
6438
|
+
if (isInputField) {
|
|
6439
|
+
if (!context3?.strictExamples) {
|
|
6440
|
+
return true;
|
|
6441
|
+
} else {
|
|
6442
|
+
if (field.isOptional || field.isInternal) {
|
|
6443
|
+
return true;
|
|
6444
|
+
}
|
|
6445
|
+
throw new Error(`Value for input field '${field.name}' is required.`);
|
|
6446
|
+
}
|
|
6447
|
+
} else {
|
|
6448
|
+
if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
|
|
6449
|
+
return true;
|
|
6450
|
+
}
|
|
6451
|
+
throw new Error(`Value for output field '${field.name}' is required.`);
|
|
6452
|
+
}
|
|
6453
|
+
}
|
|
6454
|
+
if (field.isOptional || field.isInternal || context3?.optionalOutputFields?.includes(field.name)) {
|
|
6341
6455
|
return true;
|
|
6342
6456
|
}
|
|
6343
|
-
|
|
6457
|
+
const fieldType = context3?.isInputField !== false ? "input" : "output";
|
|
6458
|
+
throw new Error(`Value for ${fieldType} field '${field.name}' is required.`);
|
|
6344
6459
|
}
|
|
6345
6460
|
return false;
|
|
6346
6461
|
};
|
|
@@ -7600,6 +7715,7 @@ var AxProgramWithSignature = class {
|
|
|
7600
7715
|
signature;
|
|
7601
7716
|
sigHash;
|
|
7602
7717
|
examples;
|
|
7718
|
+
examplesOptions;
|
|
7603
7719
|
demos;
|
|
7604
7720
|
trace;
|
|
7605
7721
|
usage = [];
|
|
@@ -7641,16 +7757,16 @@ var AxProgramWithSignature = class {
|
|
|
7641
7757
|
this.key.id = [parentId, this.key.id].join("/");
|
|
7642
7758
|
}
|
|
7643
7759
|
}
|
|
7644
|
-
setExamples(examples) {
|
|
7645
|
-
this._setExamples(examples);
|
|
7760
|
+
setExamples(examples, options) {
|
|
7761
|
+
this._setExamples(examples, options);
|
|
7646
7762
|
if (!("programId" in examples)) {
|
|
7647
7763
|
return;
|
|
7648
7764
|
}
|
|
7649
7765
|
for (const child of this.children) {
|
|
7650
|
-
child.setExamples(examples);
|
|
7766
|
+
child.setExamples(examples, options);
|
|
7651
7767
|
}
|
|
7652
7768
|
}
|
|
7653
|
-
_setExamples(examples) {
|
|
7769
|
+
_setExamples(examples, options) {
|
|
7654
7770
|
let traces = [];
|
|
7655
7771
|
if ("programId" in examples && examples.programId === this.key.id) {
|
|
7656
7772
|
traces = examples.traces;
|
|
@@ -7659,6 +7775,7 @@ var AxProgramWithSignature = class {
|
|
|
7659
7775
|
traces = examples;
|
|
7660
7776
|
}
|
|
7661
7777
|
if (traces) {
|
|
7778
|
+
this.examplesOptions = options;
|
|
7662
7779
|
const sig = this.signature;
|
|
7663
7780
|
const fields = [...sig.getInputFields(), ...sig.getOutputFields()];
|
|
7664
7781
|
this.examples = traces.map((e) => {
|
|
@@ -7739,12 +7856,12 @@ var AxProgram = class {
|
|
|
7739
7856
|
this.key.id = [parentId, this.key.id].join("/");
|
|
7740
7857
|
}
|
|
7741
7858
|
}
|
|
7742
|
-
setExamples(examples) {
|
|
7859
|
+
setExamples(examples, options) {
|
|
7743
7860
|
if (!("programId" in examples)) {
|
|
7744
7861
|
return;
|
|
7745
7862
|
}
|
|
7746
7863
|
for (const child of this.children) {
|
|
7747
|
-
child.setExamples(examples);
|
|
7864
|
+
child.setExamples(examples, options);
|
|
7748
7865
|
}
|
|
7749
7866
|
}
|
|
7750
7867
|
getTraces() {
|
|
@@ -7798,7 +7915,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
7798
7915
|
this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
|
|
7799
7916
|
const promptTemplateOptions = {
|
|
7800
7917
|
functions: options?.functions,
|
|
7801
|
-
thoughtFieldName: this.thoughtFieldName
|
|
7918
|
+
thoughtFieldName: this.thoughtFieldName,
|
|
7919
|
+
strictExamples: options?.strictExamples,
|
|
7920
|
+
optionalOutputFields: options?.optionalOutputFields
|
|
7802
7921
|
};
|
|
7803
7922
|
this.promptTemplate = new (options?.promptTemplate ?? AxPromptTemplate)(
|
|
7804
7923
|
this.signature,
|
|
@@ -7884,7 +8003,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
7884
8003
|
stream,
|
|
7885
8004
|
debug: false,
|
|
7886
8005
|
thinkingTokenBudget,
|
|
7887
|
-
traceContext
|
|
8006
|
+
traceContext,
|
|
8007
|
+
abortSignal: options?.abortSignal
|
|
7888
8008
|
}
|
|
7889
8009
|
);
|
|
7890
8010
|
return res;
|
|
@@ -8161,7 +8281,9 @@ Content: ${result.content}`
|
|
|
8161
8281
|
const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
|
|
8162
8282
|
const currentPromptTemplateOptions = {
|
|
8163
8283
|
functions: options.functions,
|
|
8164
|
-
thoughtFieldName: this.thoughtFieldName
|
|
8284
|
+
thoughtFieldName: this.thoughtFieldName,
|
|
8285
|
+
strictExamples: this.options?.strictExamples,
|
|
8286
|
+
optionalOutputFields: this.options?.optionalOutputFields
|
|
8165
8287
|
};
|
|
8166
8288
|
this.promptTemplate = new promptTemplateClass(
|
|
8167
8289
|
this.signature,
|
|
@@ -8347,6 +8469,22 @@ Content: ${result.content}`
|
|
|
8347
8469
|
stream: true
|
|
8348
8470
|
});
|
|
8349
8471
|
}
|
|
8472
|
+
setExamples(examples, options) {
|
|
8473
|
+
super.setExamples(examples, options);
|
|
8474
|
+
if (options?.optionalOutputFields) {
|
|
8475
|
+
const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
|
|
8476
|
+
const currentPromptTemplateOptions = {
|
|
8477
|
+
functions: this.functions,
|
|
8478
|
+
thoughtFieldName: this.thoughtFieldName,
|
|
8479
|
+
strictExamples: this.options?.strictExamples,
|
|
8480
|
+
optionalOutputFields: options.optionalOutputFields
|
|
8481
|
+
};
|
|
8482
|
+
this.promptTemplate = new promptTemplateClass(
|
|
8483
|
+
this.signature,
|
|
8484
|
+
currentPromptTemplateOptions
|
|
8485
|
+
);
|
|
8486
|
+
}
|
|
8487
|
+
}
|
|
8350
8488
|
};
|
|
8351
8489
|
var AxGenerateError = class extends Error {
|
|
8352
8490
|
details;
|
|
@@ -8477,8 +8615,8 @@ var AxAgent = class {
|
|
|
8477
8615
|
this.func.parameters = addModelParameter(this.func.parameters, mm);
|
|
8478
8616
|
}
|
|
8479
8617
|
}
|
|
8480
|
-
setExamples(examples) {
|
|
8481
|
-
this.program.setExamples(examples);
|
|
8618
|
+
setExamples(examples, options) {
|
|
8619
|
+
this.program.setExamples(examples, options);
|
|
8482
8620
|
}
|
|
8483
8621
|
setId(id) {
|
|
8484
8622
|
this.program.setId(id);
|
|
@@ -9772,7 +9910,12 @@ var AxDBManager = class {
|
|
|
9772
9910
|
const bs = options?.batchSize ?? 10;
|
|
9773
9911
|
for (let i = 0; i < chunks.length; i += bs) {
|
|
9774
9912
|
const batch = chunks.slice(i, i + bs);
|
|
9775
|
-
const ret = await this.ai.embed(
|
|
9913
|
+
const ret = await this.ai.embed(
|
|
9914
|
+
{ texts: batch },
|
|
9915
|
+
{
|
|
9916
|
+
abortSignal: options?.abortSignal
|
|
9917
|
+
}
|
|
9918
|
+
);
|
|
9776
9919
|
const embeddings = ret.embeddings.map((embedding, index) => ({
|
|
9777
9920
|
id: `chunk_${Date.now() + index}`,
|
|
9778
9921
|
// Unique ID for each chunk, adjusted by index
|
|
@@ -9788,7 +9931,10 @@ var AxDBManager = class {
|
|
|
9788
9931
|
throw new Error(`Error processing text: ${error}`);
|
|
9789
9932
|
}
|
|
9790
9933
|
};
|
|
9791
|
-
query = async (query, {
|
|
9934
|
+
query = async (query, {
|
|
9935
|
+
topPercent,
|
|
9936
|
+
abortSignal
|
|
9937
|
+
} = {}) => {
|
|
9792
9938
|
const texts = Array.isArray(query) ? query : [query];
|
|
9793
9939
|
if (typeof texts[0] === "string" && this.rewriter) {
|
|
9794
9940
|
for (const [i, text] of texts.entries()) {
|
|
@@ -9800,7 +9946,12 @@ var AxDBManager = class {
|
|
|
9800
9946
|
}
|
|
9801
9947
|
let queries;
|
|
9802
9948
|
if (typeof texts[0] === "string") {
|
|
9803
|
-
const embedResults = await this.ai.embed(
|
|
9949
|
+
const embedResults = await this.ai.embed(
|
|
9950
|
+
{ texts },
|
|
9951
|
+
{
|
|
9952
|
+
abortSignal
|
|
9953
|
+
}
|
|
9954
|
+
);
|
|
9804
9955
|
queries = embedResults.embeddings.map(
|
|
9805
9956
|
(values) => this.db.query({ table, values })
|
|
9806
9957
|
);
|
|
@@ -11395,9 +11546,14 @@ var AxSimpleClassifier = class {
|
|
|
11395
11546
|
setState(state) {
|
|
11396
11547
|
this.db.setDB(state);
|
|
11397
11548
|
}
|
|
11398
|
-
setClasses = async (classes) => {
|
|
11549
|
+
setClasses = async (classes, options) => {
|
|
11399
11550
|
for (const c of classes) {
|
|
11400
|
-
const ret = await this.ai.embed(
|
|
11551
|
+
const ret = await this.ai.embed(
|
|
11552
|
+
{ texts: c.getContext() },
|
|
11553
|
+
{
|
|
11554
|
+
abortSignal: options?.abortSignal
|
|
11555
|
+
}
|
|
11556
|
+
);
|
|
11401
11557
|
await this.db.upsert({
|
|
11402
11558
|
id: c.getName(),
|
|
11403
11559
|
table: "classes",
|
|
@@ -11406,7 +11562,12 @@ var AxSimpleClassifier = class {
|
|
|
11406
11562
|
}
|
|
11407
11563
|
};
|
|
11408
11564
|
async forward(text, options) {
|
|
11409
|
-
const { embeddings } = await this.ai.embed(
|
|
11565
|
+
const { embeddings } = await this.ai.embed(
|
|
11566
|
+
{ texts: [text] },
|
|
11567
|
+
{
|
|
11568
|
+
abortSignal: options?.abortSignal
|
|
11569
|
+
}
|
|
11570
|
+
);
|
|
11410
11571
|
const matches = await this.db.query({
|
|
11411
11572
|
table: "classes",
|
|
11412
11573
|
values: embeddings[0]
|
|
@@ -11481,6 +11642,79 @@ var AxTestPrompt = class {
|
|
|
11481
11642
|
}
|
|
11482
11643
|
};
|
|
11483
11644
|
|
|
11645
|
+
// ai/abortable.ts
|
|
11646
|
+
var AxAbortableAI = class {
|
|
11647
|
+
abortController;
|
|
11648
|
+
ai;
|
|
11649
|
+
constructor(ai) {
|
|
11650
|
+
this.ai = ai;
|
|
11651
|
+
this.abortController = new AbortController();
|
|
11652
|
+
}
|
|
11653
|
+
/**
|
|
11654
|
+
* Get the current abort signal
|
|
11655
|
+
*/
|
|
11656
|
+
get signal() {
|
|
11657
|
+
return this.abortController.signal;
|
|
11658
|
+
}
|
|
11659
|
+
/**
|
|
11660
|
+
* Check if the request has been aborted
|
|
11661
|
+
*/
|
|
11662
|
+
get aborted() {
|
|
11663
|
+
return this.abortController.signal.aborted;
|
|
11664
|
+
}
|
|
11665
|
+
/**
|
|
11666
|
+
* Abort the ongoing request
|
|
11667
|
+
* @param reason Optional reason for the abort
|
|
11668
|
+
*/
|
|
11669
|
+
abort(reason) {
|
|
11670
|
+
this.abortController.abort(reason);
|
|
11671
|
+
}
|
|
11672
|
+
/**
|
|
11673
|
+
* Reset the abort controller to allow new requests
|
|
11674
|
+
* This creates a new AbortController, allowing fresh requests
|
|
11675
|
+
*/
|
|
11676
|
+
reset() {
|
|
11677
|
+
this.abortController = new AbortController();
|
|
11678
|
+
}
|
|
11679
|
+
/**
|
|
11680
|
+
* Send a chat request with abort support
|
|
11681
|
+
*/
|
|
11682
|
+
async chat(req, options) {
|
|
11683
|
+
return this.ai.chat(req, {
|
|
11684
|
+
...options,
|
|
11685
|
+
abortSignal: this.abortController.signal
|
|
11686
|
+
});
|
|
11687
|
+
}
|
|
11688
|
+
/**
|
|
11689
|
+
* Send an embed request with abort support
|
|
11690
|
+
*/
|
|
11691
|
+
async embed(req, options) {
|
|
11692
|
+
return this.ai.embed(req, {
|
|
11693
|
+
...options,
|
|
11694
|
+
abortSignal: this.abortController.signal
|
|
11695
|
+
});
|
|
11696
|
+
}
|
|
11697
|
+
/**
|
|
11698
|
+
* Create a timeout-based abort after specified milliseconds
|
|
11699
|
+
* @param timeoutMs Timeout in milliseconds
|
|
11700
|
+
* @param reason Optional reason for the timeout abort
|
|
11701
|
+
* @returns Timeout ID that can be cleared
|
|
11702
|
+
*/
|
|
11703
|
+
abortAfter(timeoutMs, reason = "Request timeout") {
|
|
11704
|
+
return setTimeout(() => {
|
|
11705
|
+
this.abort(reason);
|
|
11706
|
+
}, timeoutMs);
|
|
11707
|
+
}
|
|
11708
|
+
/**
|
|
11709
|
+
* Add an event listener for abort events
|
|
11710
|
+
*/
|
|
11711
|
+
onAbort(callback) {
|
|
11712
|
+
this.abortController.signal.addEventListener("abort", () => {
|
|
11713
|
+
callback(this.abortController.signal.reason);
|
|
11714
|
+
});
|
|
11715
|
+
}
|
|
11716
|
+
};
|
|
11717
|
+
|
|
11484
11718
|
// prompts/cot.ts
|
|
11485
11719
|
var AxChainOfThought = class extends AxGen {
|
|
11486
11720
|
constructor(signature, options) {
|
|
@@ -11607,7 +11841,10 @@ var AxEmbeddingAdapter = class {
|
|
|
11607
11841
|
async embedAdapter(text, extra) {
|
|
11608
11842
|
const embedRes = await this.aiService.embed(
|
|
11609
11843
|
{ texts: [text] },
|
|
11610
|
-
{
|
|
11844
|
+
{
|
|
11845
|
+
sessionId: extra?.sessionId,
|
|
11846
|
+
abortSignal: extra?.abortSignal
|
|
11847
|
+
}
|
|
11611
11848
|
);
|
|
11612
11849
|
const embeds = embedRes.embeddings.at(0);
|
|
11613
11850
|
if (!embeds) {
|
|
@@ -13371,6 +13608,7 @@ var AxRAG = class extends AxChainOfThought {
|
|
|
13371
13608
|
AxAIOpenAIResponsesImpl,
|
|
13372
13609
|
AxAIReka,
|
|
13373
13610
|
AxAIRekaModel,
|
|
13611
|
+
AxAIServiceAbortedError,
|
|
13374
13612
|
AxAIServiceAuthenticationError,
|
|
13375
13613
|
AxAIServiceError,
|
|
13376
13614
|
AxAIServiceNetworkError,
|
|
@@ -13379,6 +13617,7 @@ var AxRAG = class extends AxChainOfThought {
|
|
|
13379
13617
|
AxAIServiceStreamTerminatedError,
|
|
13380
13618
|
AxAIServiceTimeoutError,
|
|
13381
13619
|
AxAITogether,
|
|
13620
|
+
AxAbortableAI,
|
|
13382
13621
|
AxAgent,
|
|
13383
13622
|
AxApacheTika,
|
|
13384
13623
|
AxAssertionError,
|