@ax-llm/ax 11.0.50 → 11.0.52
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 +177 -86
- package/index.cjs.map +1 -1
- package/index.d.cts +33 -18
- package/index.d.ts +33 -18
- package/index.js +184 -93
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// ai/base.ts
|
|
2
|
+
import crypto2 from "crypto";
|
|
2
3
|
import { context, SpanKind } from "@opentelemetry/api";
|
|
3
4
|
|
|
4
5
|
// trace/trace.ts
|
|
@@ -66,6 +67,7 @@ var AxSpanKindValues = /* @__PURE__ */ ((AxSpanKindValues2) => {
|
|
|
66
67
|
})(AxSpanKindValues || {});
|
|
67
68
|
|
|
68
69
|
// util/apicall.ts
|
|
70
|
+
import crypto from "crypto";
|
|
69
71
|
import {
|
|
70
72
|
ReadableStream as ReadableStream2,
|
|
71
73
|
TextDecoderStream as TextDecoderStreamNative,
|
|
@@ -718,13 +720,11 @@ var logResponseDelta = (delta) => {
|
|
|
718
720
|
|
|
719
721
|
// ai/base.ts
|
|
720
722
|
var axBaseAIDefaultConfig = () => structuredClone({
|
|
721
|
-
maxTokens: 2e3,
|
|
722
723
|
temperature: 0,
|
|
723
724
|
topK: 40,
|
|
724
725
|
topP: 0.9
|
|
725
726
|
});
|
|
726
727
|
var axBaseAIDefaultCreativeConfig = () => structuredClone({
|
|
727
|
-
maxTokens: 2e3,
|
|
728
728
|
temperature: 0.4,
|
|
729
729
|
topP: 0.7,
|
|
730
730
|
frequencyPenalty: 0.2
|
|
@@ -748,7 +748,7 @@ var AxBaseAI = class {
|
|
|
748
748
|
this.tracer = options.tracer;
|
|
749
749
|
this.modelInfo = modelInfo;
|
|
750
750
|
this.models = models;
|
|
751
|
-
this.id =
|
|
751
|
+
this.id = crypto2.randomUUID();
|
|
752
752
|
const model = this.getModel(defaults.model) ?? defaults.model;
|
|
753
753
|
const embedModel = this.getEmbedModel(defaults.embedModel) ?? defaults.embedModel;
|
|
754
754
|
this.defaults = { model, embedModel };
|
|
@@ -947,14 +947,14 @@ var AxBaseAI = class {
|
|
|
947
947
|
[axSpanAttributes.LLM_SYSTEM]: this.name,
|
|
948
948
|
[axSpanAttributes.LLM_OPERATION_NAME]: "chat",
|
|
949
949
|
[axSpanAttributes.LLM_REQUEST_MODEL]: model,
|
|
950
|
-
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens,
|
|
950
|
+
[axSpanAttributes.LLM_REQUEST_MAX_TOKENS]: modelConfig.maxTokens ?? "Not set",
|
|
951
951
|
[axSpanAttributes.LLM_REQUEST_TEMPERATURE]: modelConfig.temperature,
|
|
952
|
-
[axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP,
|
|
953
|
-
[axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK,
|
|
954
|
-
[axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty,
|
|
955
|
-
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty,
|
|
956
|
-
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", "),
|
|
957
|
-
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream
|
|
952
|
+
[axSpanAttributes.LLM_REQUEST_TOP_P]: modelConfig.topP ?? "Not set",
|
|
953
|
+
[axSpanAttributes.LLM_REQUEST_TOP_K]: modelConfig.topK ?? "Not set",
|
|
954
|
+
[axSpanAttributes.LLM_REQUEST_FREQUENCY_PENALTY]: modelConfig.frequencyPenalty ?? "Not set",
|
|
955
|
+
[axSpanAttributes.LLM_REQUEST_PRESENCE_PENALTY]: modelConfig.presencePenalty ?? "Not set",
|
|
956
|
+
[axSpanAttributes.LLM_REQUEST_STOP_SEQUENCES]: modelConfig.stopSequences?.join(", ") ?? "Not set",
|
|
957
|
+
[axSpanAttributes.LLM_REQUEST_LLM_IS_STREAMING]: modelConfig.stream ?? "Not set"
|
|
958
958
|
}
|
|
959
959
|
},
|
|
960
960
|
options?.traceContext ?? context.active(),
|
|
@@ -2053,7 +2053,7 @@ var AxAIOpenAIImpl = class {
|
|
|
2053
2053
|
response_format: this.config?.responseFormat ? { type: this.config.responseFormat } : void 0,
|
|
2054
2054
|
tools,
|
|
2055
2055
|
tool_choice: toolsChoice,
|
|
2056
|
-
max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens
|
|
2056
|
+
max_completion_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
2057
2057
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
2058
2058
|
top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
|
|
2059
2059
|
n: req.modelConfig?.n ?? this.config.n,
|
|
@@ -2097,6 +2097,9 @@ var AxAIOpenAIImpl = class {
|
|
|
2097
2097
|
}
|
|
2098
2098
|
if (config.thinkingTokenBudget) {
|
|
2099
2099
|
switch (config.thinkingTokenBudget) {
|
|
2100
|
+
case "none":
|
|
2101
|
+
reqValue.reasoning_effort = void 0;
|
|
2102
|
+
break;
|
|
2100
2103
|
case "minimal":
|
|
2101
2104
|
reqValue.reasoning_effort = "low";
|
|
2102
2105
|
break;
|
|
@@ -3158,6 +3161,9 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
3158
3161
|
}
|
|
3159
3162
|
if (config.thinkingTokenBudget) {
|
|
3160
3163
|
switch (config.thinkingTokenBudget) {
|
|
3164
|
+
case "none":
|
|
3165
|
+
thinkingConfig.thinkingBudget = 0;
|
|
3166
|
+
break;
|
|
3161
3167
|
case "minimal":
|
|
3162
3168
|
thinkingConfig.thinkingBudget = 200;
|
|
3163
3169
|
break;
|
|
@@ -4603,7 +4609,6 @@ var axAIOpenAIResponsesDefaultConfig = () => ({
|
|
|
4603
4609
|
model: "gpt-4o" /* GPT4O */,
|
|
4604
4610
|
embedModel: "text-embedding-ada-002" /* TextEmbeddingAda002 */,
|
|
4605
4611
|
temperature: 0.7,
|
|
4606
|
-
maxTokens: 2048,
|
|
4607
4612
|
topP: 1,
|
|
4608
4613
|
stream: true
|
|
4609
4614
|
// reasoningEffort: 'medium',
|
|
@@ -4770,7 +4775,7 @@ var AxAIRekaImpl = class {
|
|
|
4770
4775
|
const reqValue = {
|
|
4771
4776
|
model,
|
|
4772
4777
|
messages,
|
|
4773
|
-
max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens
|
|
4778
|
+
max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
|
|
4774
4779
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
4775
4780
|
top_k: req.modelConfig?.n ?? this.config.n,
|
|
4776
4781
|
top_p: req.modelConfig?.topP ?? this.config.topP ?? 1,
|
|
@@ -5170,7 +5175,7 @@ var AxAIGrok = class extends AxAIOpenAIBase {
|
|
|
5170
5175
|
};
|
|
5171
5176
|
|
|
5172
5177
|
// dsp/generate.ts
|
|
5173
|
-
import { ReadableStream as ReadableStream3 } from "
|
|
5178
|
+
import { ReadableStream as ReadableStream3 } from "stream/web";
|
|
5174
5179
|
import {
|
|
5175
5180
|
context as context2,
|
|
5176
5181
|
SpanKind as SpanKind2,
|
|
@@ -5753,16 +5758,20 @@ var AxPromptTemplate = class {
|
|
|
5753
5758
|
sig;
|
|
5754
5759
|
fieldTemplates;
|
|
5755
5760
|
task;
|
|
5756
|
-
|
|
5761
|
+
thoughtFieldName;
|
|
5762
|
+
functions;
|
|
5763
|
+
constructor(sig, options, fieldTemplates) {
|
|
5757
5764
|
this.sig = sig;
|
|
5758
5765
|
this.fieldTemplates = fieldTemplates;
|
|
5766
|
+
this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
|
|
5767
|
+
this.functions = options?.functions;
|
|
5759
5768
|
const task = [];
|
|
5760
5769
|
const inArgs = renderDescFields(this.sig.getInputFields());
|
|
5761
5770
|
const outArgs = renderDescFields(this.sig.getOutputFields());
|
|
5762
5771
|
task.push(
|
|
5763
5772
|
`You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
|
|
5764
5773
|
);
|
|
5765
|
-
const funcs = functions?.map((f) => "toFunction" in f ? f.toFunction() : f)?.flat();
|
|
5774
|
+
const funcs = this.functions?.map((f) => "toFunction" in f ? f.toFunction() : f)?.flat();
|
|
5766
5775
|
const funcList = funcs?.map((fn) => `- \`${fn.name}\`: ${formatDescription(fn.description)}`).join("\n");
|
|
5767
5776
|
if (funcList && funcList.length > 0) {
|
|
5768
5777
|
task.push(`## Available Functions
|
|
@@ -5797,7 +5806,6 @@ ${outputFields}`);
|
|
|
5797
5806
|
...this.renderExamples(examples)
|
|
5798
5807
|
] : [];
|
|
5799
5808
|
const renderedDemos = demos ? this.renderDemos(demos) : [];
|
|
5800
|
-
const completion = this.renderInputFields(values);
|
|
5801
5809
|
const allTextExamples = renderedExamples.every((v) => v.type === "text");
|
|
5802
5810
|
const allTextDemos = renderedDemos.every((v) => v.type === "text");
|
|
5803
5811
|
const examplesInSystemPrompt = allTextExamples && allTextDemos;
|
|
@@ -5817,14 +5825,73 @@ ${outputFields}`);
|
|
|
5817
5825
|
role: "system",
|
|
5818
5826
|
content: systemContent
|
|
5819
5827
|
};
|
|
5820
|
-
|
|
5821
|
-
|
|
5822
|
-
|
|
5823
|
-
|
|
5824
|
-
|
|
5825
|
-
|
|
5826
|
-
|
|
5827
|
-
|
|
5828
|
+
let userMessages = [];
|
|
5829
|
+
if (Array.isArray(values)) {
|
|
5830
|
+
const history = values;
|
|
5831
|
+
let lastRole = void 0;
|
|
5832
|
+
for (const message of history) {
|
|
5833
|
+
let messageContent = "";
|
|
5834
|
+
if (message.role === "user") {
|
|
5835
|
+
const userMsgParts = this.renderInputFields(
|
|
5836
|
+
message.values
|
|
5837
|
+
// Cast message.values (AxGenIn) to T (which extends AxGenIn)
|
|
5838
|
+
);
|
|
5839
|
+
messageContent = userMsgParts.map((part) => part.type === "text" ? part.text : "").join("").trim();
|
|
5840
|
+
} else if (message.role === "assistant") {
|
|
5841
|
+
const assistantValues = message.values;
|
|
5842
|
+
let assistantContentParts = [];
|
|
5843
|
+
const outputFields = this.sig.getOutputFields();
|
|
5844
|
+
for (const field of outputFields) {
|
|
5845
|
+
const value = assistantValues[field.name];
|
|
5846
|
+
if (value !== void 0 && value !== null && (typeof value === "string" ? value !== "" : true)) {
|
|
5847
|
+
const renderedValue = processValue(field, value);
|
|
5848
|
+
assistantContentParts.push(`${field.name}: ${renderedValue}`);
|
|
5849
|
+
} else {
|
|
5850
|
+
const isThoughtField = field.name === this.thoughtFieldName;
|
|
5851
|
+
if (!field.isOptional && !field.isInternal && !isThoughtField) {
|
|
5852
|
+
throw new Error(
|
|
5853
|
+
`Value for output field '${field.name}' ('${field.title}') is required in assistant message history but was not found or was empty.`
|
|
5854
|
+
);
|
|
5855
|
+
}
|
|
5856
|
+
}
|
|
5857
|
+
}
|
|
5858
|
+
messageContent = assistantContentParts.join("\n");
|
|
5859
|
+
}
|
|
5860
|
+
if (messageContent) {
|
|
5861
|
+
if (lastRole === message.role && userMessages.length > 0) {
|
|
5862
|
+
const lastMessage = userMessages[userMessages.length - 1];
|
|
5863
|
+
if (lastMessage) {
|
|
5864
|
+
lastMessage.content += "\n" + messageContent;
|
|
5865
|
+
}
|
|
5866
|
+
} else {
|
|
5867
|
+
if (message.role === "user") {
|
|
5868
|
+
userMessages.push({ role: "user", content: messageContent });
|
|
5869
|
+
} else if (message.role === "assistant") {
|
|
5870
|
+
userMessages.push({ role: "assistant", content: messageContent });
|
|
5871
|
+
}
|
|
5872
|
+
}
|
|
5873
|
+
lastRole = message.role;
|
|
5874
|
+
}
|
|
5875
|
+
}
|
|
5876
|
+
} else {
|
|
5877
|
+
const currentValues = values;
|
|
5878
|
+
const completion = this.renderInputFields(currentValues);
|
|
5879
|
+
const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
|
|
5880
|
+
const promptFilter = promptList.filter((v) => v !== void 0);
|
|
5881
|
+
let userContent;
|
|
5882
|
+
if (promptFilter.every((v) => v.type === "text")) {
|
|
5883
|
+
userContent = promptFilter.map((v) => v.text).join("\n");
|
|
5884
|
+
} else {
|
|
5885
|
+
userContent = promptFilter.map((part) => {
|
|
5886
|
+
if (part.type === "text") return part.text;
|
|
5887
|
+
if (part.type === "image") return "[IMAGE]";
|
|
5888
|
+
if (part.type === "audio") return "[AUDIO]";
|
|
5889
|
+
return "";
|
|
5890
|
+
}).join("\n").trim();
|
|
5891
|
+
}
|
|
5892
|
+
userMessages.push({ role: "user", content: userContent });
|
|
5893
|
+
}
|
|
5894
|
+
return [systemPrompt, ...userMessages];
|
|
5828
5895
|
};
|
|
5829
5896
|
renderExtraFields = (extraFields) => {
|
|
5830
5897
|
const prompt = [];
|
|
@@ -5868,8 +5935,8 @@ ${outputFields}`);
|
|
|
5868
5935
|
renderExamples = (data) => {
|
|
5869
5936
|
const list = [];
|
|
5870
5937
|
for (const [index, item] of data.entries()) {
|
|
5871
|
-
const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item
|
|
5872
|
-
const renderedOutputItem = this.sig.getOutputFields().map((field) => this.renderInField(field, item
|
|
5938
|
+
const renderedInputItem = this.sig.getInputFields().map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
|
|
5939
|
+
const renderedOutputItem = this.sig.getOutputFields().map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
|
|
5873
5940
|
if (renderedOutputItem.length === 0) {
|
|
5874
5941
|
throw new Error(
|
|
5875
5942
|
`Output fields are required in examples: index: ${index}, data: ${JSON.stringify(item)}`
|
|
@@ -5895,7 +5962,7 @@ ${outputFields}`);
|
|
|
5895
5962
|
const list = [];
|
|
5896
5963
|
const fields = [...this.sig.getInputFields(), ...this.sig.getOutputFields()];
|
|
5897
5964
|
for (const item of data) {
|
|
5898
|
-
const renderedItem = fields.map((field) => this.renderInField(field, item
|
|
5965
|
+
const renderedItem = fields.map((field) => this.renderInField(field, item)).filter((v) => v !== void 0).flat();
|
|
5899
5966
|
renderedItem.slice(0, -1).forEach((v) => {
|
|
5900
5967
|
if ("text" in v) {
|
|
5901
5968
|
v.text = v.text + "\n";
|
|
@@ -5915,11 +5982,8 @@ ${outputFields}`);
|
|
|
5915
5982
|
});
|
|
5916
5983
|
return renderedItems;
|
|
5917
5984
|
};
|
|
5918
|
-
renderInField = (field, values
|
|
5985
|
+
renderInField = (field, values) => {
|
|
5919
5986
|
const value = values[field.name];
|
|
5920
|
-
if (skipMissing && !value) {
|
|
5921
|
-
return;
|
|
5922
|
-
}
|
|
5923
5987
|
if (isEmptyValue(field, value)) {
|
|
5924
5988
|
return;
|
|
5925
5989
|
}
|
|
@@ -5956,20 +6020,20 @@ ${outputFields}`);
|
|
|
5956
6020
|
}
|
|
5957
6021
|
result = result.concat(
|
|
5958
6022
|
value.map((v) => {
|
|
5959
|
-
|
|
6023
|
+
const validated = validateImage(v);
|
|
5960
6024
|
return {
|
|
5961
6025
|
type: "image",
|
|
5962
|
-
mimeType:
|
|
5963
|
-
image:
|
|
6026
|
+
mimeType: validated.mimeType,
|
|
6027
|
+
image: validated.data
|
|
5964
6028
|
};
|
|
5965
6029
|
})
|
|
5966
6030
|
);
|
|
5967
6031
|
} else {
|
|
5968
|
-
const
|
|
6032
|
+
const validated = validateImage(value);
|
|
5969
6033
|
result.push({
|
|
5970
6034
|
type: "image",
|
|
5971
|
-
mimeType:
|
|
5972
|
-
image:
|
|
6035
|
+
mimeType: validated.mimeType,
|
|
6036
|
+
image: validated.data
|
|
5973
6037
|
});
|
|
5974
6038
|
}
|
|
5975
6039
|
return result;
|
|
@@ -5992,24 +6056,24 @@ ${outputFields}`);
|
|
|
5992
6056
|
];
|
|
5993
6057
|
if (field.type.isArray) {
|
|
5994
6058
|
if (!Array.isArray(value)) {
|
|
5995
|
-
throw new Error("
|
|
6059
|
+
throw new Error("Audio field value must be an array.");
|
|
5996
6060
|
}
|
|
5997
6061
|
result = result.concat(
|
|
5998
6062
|
value.map((v) => {
|
|
5999
|
-
|
|
6063
|
+
const validated = validateAudio(v);
|
|
6000
6064
|
return {
|
|
6001
6065
|
type: "audio",
|
|
6002
|
-
format:
|
|
6003
|
-
data:
|
|
6066
|
+
format: validated.format ?? "wav",
|
|
6067
|
+
data: validated.data
|
|
6004
6068
|
};
|
|
6005
6069
|
})
|
|
6006
6070
|
);
|
|
6007
6071
|
} else {
|
|
6008
|
-
const
|
|
6072
|
+
const validated = validateAudio(value);
|
|
6009
6073
|
result.push({
|
|
6010
6074
|
type: "audio",
|
|
6011
|
-
format:
|
|
6012
|
-
data:
|
|
6075
|
+
format: validated.format ?? "wav",
|
|
6076
|
+
data: validated.data
|
|
6013
6077
|
});
|
|
6014
6078
|
}
|
|
6015
6079
|
return result;
|
|
@@ -6109,7 +6173,7 @@ var isEmptyValue = (field, value) => {
|
|
|
6109
6173
|
return false;
|
|
6110
6174
|
}
|
|
6111
6175
|
if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
|
|
6112
|
-
if (field.isOptional) {
|
|
6176
|
+
if (field.isOptional || field.isInternal) {
|
|
6113
6177
|
return true;
|
|
6114
6178
|
}
|
|
6115
6179
|
throw new Error(`Value for input field '${field.name}' is required.`);
|
|
@@ -7563,12 +7627,18 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
7563
7627
|
streamingFieldProcessors = [];
|
|
7564
7628
|
values = {};
|
|
7565
7629
|
excludeContentFromTrace = false;
|
|
7630
|
+
thoughtFieldName;
|
|
7566
7631
|
constructor(signature, options) {
|
|
7567
7632
|
super(signature, { description: options?.description });
|
|
7568
7633
|
this.options = options;
|
|
7634
|
+
this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
|
|
7635
|
+
const promptTemplateOptions = {
|
|
7636
|
+
functions: options?.functions,
|
|
7637
|
+
thoughtFieldName: this.thoughtFieldName
|
|
7638
|
+
};
|
|
7569
7639
|
this.promptTemplate = new (options?.promptTemplate ?? AxPromptTemplate)(
|
|
7570
7640
|
this.signature,
|
|
7571
|
-
|
|
7641
|
+
promptTemplateOptions
|
|
7572
7642
|
);
|
|
7573
7643
|
this.asserts = this.options?.asserts ?? [];
|
|
7574
7644
|
this.streamingAsserts = this.options?.streamingAsserts ?? [];
|
|
@@ -7740,7 +7810,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
7740
7810
|
);
|
|
7741
7811
|
} else if (result.content && result.content.length > 0) {
|
|
7742
7812
|
if (result.thought && result.thought.length > 0) {
|
|
7743
|
-
yield {
|
|
7813
|
+
yield {
|
|
7814
|
+
[this.thoughtFieldName]: result.thought
|
|
7815
|
+
};
|
|
7744
7816
|
}
|
|
7745
7817
|
content += result.content;
|
|
7746
7818
|
mem.updateResult(
|
|
@@ -7782,8 +7854,10 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
7782
7854
|
);
|
|
7783
7855
|
await assertAssertions(this.asserts, this.values);
|
|
7784
7856
|
} else if (result.thought && result.thought.length > 0) {
|
|
7785
|
-
this.values.
|
|
7786
|
-
yield {
|
|
7857
|
+
this.values[this.thoughtFieldName] = (this.values[this.thoughtFieldName] ?? "") + result.thought;
|
|
7858
|
+
yield {
|
|
7859
|
+
[this.thoughtFieldName]: result.thought
|
|
7860
|
+
};
|
|
7787
7861
|
}
|
|
7788
7862
|
if (result.finishReason === "length") {
|
|
7789
7863
|
throw new Error(
|
|
@@ -7883,7 +7957,7 @@ Content: ${content}`
|
|
|
7883
7957
|
}
|
|
7884
7958
|
} else if (result.content) {
|
|
7885
7959
|
if (result.thought && result.thought.length > 0) {
|
|
7886
|
-
this.values.
|
|
7960
|
+
this.values[this.thoughtFieldName] = result.thought;
|
|
7887
7961
|
}
|
|
7888
7962
|
extractValues(this.signature, this.values, result.content);
|
|
7889
7963
|
await assertAssertions(this.asserts, this.values);
|
|
@@ -7920,16 +7994,29 @@ Content: ${result.content}`
|
|
|
7920
7994
|
const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
|
|
7921
7995
|
let err;
|
|
7922
7996
|
if (options?.functions && options.functions.length > 0) {
|
|
7923
|
-
const
|
|
7924
|
-
|
|
7997
|
+
const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
|
|
7998
|
+
const currentPromptTemplateOptions = {
|
|
7999
|
+
functions: options.functions,
|
|
8000
|
+
thoughtFieldName: this.thoughtFieldName
|
|
8001
|
+
};
|
|
8002
|
+
this.promptTemplate = new promptTemplateClass(
|
|
7925
8003
|
this.signature,
|
|
7926
|
-
|
|
8004
|
+
currentPromptTemplateOptions
|
|
7927
8005
|
);
|
|
7928
8006
|
}
|
|
7929
|
-
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
|
|
8007
|
+
let prompt;
|
|
8008
|
+
if (Array.isArray(values)) {
|
|
8009
|
+
prompt = this.promptTemplate.render(values, {
|
|
8010
|
+
examples: this.examples,
|
|
8011
|
+
demos: this.demos
|
|
8012
|
+
});
|
|
8013
|
+
} else {
|
|
8014
|
+
prompt = this.promptTemplate.render(values, {
|
|
8015
|
+
// Cast if necessary
|
|
8016
|
+
examples: this.examples,
|
|
8017
|
+
demos: this.demos
|
|
8018
|
+
});
|
|
8019
|
+
}
|
|
7933
8020
|
mem.add(prompt, options?.sessionId);
|
|
7934
8021
|
multiStepLoop: for (let n = 0; n < maxSteps; n++) {
|
|
7935
8022
|
const firstStep = n === 0;
|
|
@@ -8429,7 +8516,7 @@ function pick(obj, keys) {
|
|
|
8429
8516
|
}
|
|
8430
8517
|
|
|
8431
8518
|
// docs/tika.ts
|
|
8432
|
-
import { createReadStream } from "
|
|
8519
|
+
import { createReadStream } from "fs";
|
|
8433
8520
|
var AxApacheTika = class {
|
|
8434
8521
|
tikaUrl;
|
|
8435
8522
|
fetch;
|
|
@@ -8791,7 +8878,8 @@ var AxBootstrapFewShot = class {
|
|
|
8791
8878
|
if (this.costMonitoring) {
|
|
8792
8879
|
this.stats.estimatedTokenUsage += JSON.stringify(ex).length / 4 + JSON.stringify(res).length / 4;
|
|
8793
8880
|
}
|
|
8794
|
-
const
|
|
8881
|
+
const score = metricFn({ prediction: res, example: ex });
|
|
8882
|
+
const success = score >= 0.5;
|
|
8795
8883
|
if (success) {
|
|
8796
8884
|
this.traces = [...this.traces, ...this.program.getTraces()];
|
|
8797
8885
|
this.stats.successfulDemos++;
|
|
@@ -9966,13 +10054,13 @@ var AxHFDataLoader = class {
|
|
|
9966
10054
|
};
|
|
9967
10055
|
|
|
9968
10056
|
// funcs/code.ts
|
|
9969
|
-
import * as _crypto from "
|
|
9970
|
-
import * as _fs from "
|
|
9971
|
-
import * as _http from "
|
|
9972
|
-
import * as _https from "
|
|
9973
|
-
import * as _os from "
|
|
9974
|
-
import * as _process from "
|
|
9975
|
-
import { runInNewContext } from "
|
|
10057
|
+
import * as _crypto from "crypto";
|
|
10058
|
+
import * as _fs from "fs";
|
|
10059
|
+
import * as _http from "http";
|
|
10060
|
+
import * as _https from "https";
|
|
10061
|
+
import * as _os from "os";
|
|
10062
|
+
import * as _process from "process";
|
|
10063
|
+
import { runInNewContext } from "vm";
|
|
9976
10064
|
var AxJSInterpreterPermission = /* @__PURE__ */ ((AxJSInterpreterPermission2) => {
|
|
9977
10065
|
AxJSInterpreterPermission2["FS"] = "node:fs";
|
|
9978
10066
|
AxJSInterpreterPermission2["NET"] = "net";
|
|
@@ -10779,19 +10867,20 @@ ${dataContext}
|
|
|
10779
10867
|
}
|
|
10780
10868
|
evalSet = minibatchEvalSet;
|
|
10781
10869
|
}
|
|
10782
|
-
let
|
|
10870
|
+
let sumOfScores = 0;
|
|
10783
10871
|
for (const example of evalSet) {
|
|
10784
10872
|
try {
|
|
10785
10873
|
const prediction = await this.program.forward(this.ai, example);
|
|
10786
|
-
const
|
|
10787
|
-
|
|
10874
|
+
const score = metricFn({ prediction, example });
|
|
10875
|
+
sumOfScores += score;
|
|
10788
10876
|
} catch (err) {
|
|
10789
10877
|
if (this.verbose) {
|
|
10790
10878
|
console.error("Error evaluating example:", err);
|
|
10791
10879
|
}
|
|
10792
10880
|
}
|
|
10793
10881
|
}
|
|
10794
|
-
|
|
10882
|
+
if (evalSet.length === 0) return 0;
|
|
10883
|
+
return sumOfScores / evalSet.length;
|
|
10795
10884
|
}
|
|
10796
10885
|
/**
|
|
10797
10886
|
* Run full evaluation on the entire validation set
|
|
@@ -10803,19 +10892,20 @@ ${dataContext}
|
|
|
10803
10892
|
bootstrappedDemos,
|
|
10804
10893
|
labeledExamples
|
|
10805
10894
|
);
|
|
10806
|
-
let
|
|
10895
|
+
let sumOfScores = 0;
|
|
10807
10896
|
for (const example of valset) {
|
|
10808
10897
|
try {
|
|
10809
10898
|
const prediction = await this.program.forward(this.ai, example);
|
|
10810
|
-
const
|
|
10811
|
-
|
|
10899
|
+
const score = metricFn({ prediction, example });
|
|
10900
|
+
sumOfScores += score;
|
|
10812
10901
|
} catch (err) {
|
|
10813
10902
|
if (this.verbose) {
|
|
10814
10903
|
console.error("Error evaluating example:", err);
|
|
10815
10904
|
}
|
|
10816
10905
|
}
|
|
10817
10906
|
}
|
|
10818
|
-
|
|
10907
|
+
if (valset.length === 0) return 0;
|
|
10908
|
+
return sumOfScores / valset.length;
|
|
10819
10909
|
}
|
|
10820
10910
|
/**
|
|
10821
10911
|
* Implements a Bayesian-inspired selection of the next configuration to try
|
|
@@ -10987,10 +11077,11 @@ ${dataContext}
|
|
|
10987
11077
|
};
|
|
10988
11078
|
|
|
10989
11079
|
// ai/mock/api.ts
|
|
11080
|
+
import crypto3 from "crypto";
|
|
10990
11081
|
var AxMockAIService = class {
|
|
10991
11082
|
constructor(config = {}) {
|
|
10992
11083
|
this.config = config;
|
|
10993
|
-
this.config.id = this.config.id ??
|
|
11084
|
+
this.config.id = this.config.id ?? crypto3.randomUUID();
|
|
10994
11085
|
}
|
|
10995
11086
|
metrics = {
|
|
10996
11087
|
latency: {
|
|
@@ -11201,27 +11292,26 @@ var AxTestPrompt = class {
|
|
|
11201
11292
|
async run(metricFn) {
|
|
11202
11293
|
const st = (/* @__PURE__ */ new Date()).getTime();
|
|
11203
11294
|
const total = this.examples.length;
|
|
11204
|
-
let
|
|
11295
|
+
let sumOfScores = 0;
|
|
11205
11296
|
for (let i = 0; i < total; i++) {
|
|
11206
11297
|
const ex = this.examples[i];
|
|
11207
11298
|
if (!ex) {
|
|
11208
11299
|
throw new Error("Invalid example");
|
|
11209
11300
|
}
|
|
11210
11301
|
const res = await this.program.forward(this.ai, ex);
|
|
11211
|
-
const
|
|
11212
|
-
|
|
11213
|
-
successCount++;
|
|
11214
|
-
}
|
|
11302
|
+
const score = metricFn({ prediction: res, example: ex });
|
|
11303
|
+
sumOfScores += score;
|
|
11215
11304
|
const et = (/* @__PURE__ */ new Date()).getTime() - st;
|
|
11216
|
-
updateProgressBar(i, total,
|
|
11305
|
+
updateProgressBar(i, total, sumOfScores, et, "Testing Prompt", 30);
|
|
11217
11306
|
}
|
|
11307
|
+
const averageScore = total > 0 ? sumOfScores / total : 0;
|
|
11218
11308
|
console.log(
|
|
11219
11309
|
"\nPerformance: ",
|
|
11220
|
-
|
|
11310
|
+
sumOfScores,
|
|
11221
11311
|
"/",
|
|
11222
11312
|
total,
|
|
11223
|
-
"
|
|
11224
|
-
|
|
11313
|
+
"Average Score: ",
|
|
11314
|
+
averageScore,
|
|
11225
11315
|
"\n"
|
|
11226
11316
|
);
|
|
11227
11317
|
}
|
|
@@ -11235,7 +11325,8 @@ var AxChainOfThought = class extends AxGen {
|
|
|
11235
11325
|
sig.setOutputFields([
|
|
11236
11326
|
{
|
|
11237
11327
|
name: "reason",
|
|
11238
|
-
description
|
|
11328
|
+
description,
|
|
11329
|
+
isInternal: options?.setVisibleReasoning !== true
|
|
11239
11330
|
},
|
|
11240
11331
|
...sig.getOutputFields()
|
|
11241
11332
|
]);
|
|
@@ -12564,7 +12655,7 @@ function normalizeText(s) {
|
|
|
12564
12655
|
return s.toLowerCase();
|
|
12565
12656
|
}
|
|
12566
12657
|
function emScore(prediction, groundTruth) {
|
|
12567
|
-
return normalizeText(prediction) === normalizeText(groundTruth);
|
|
12658
|
+
return normalizeText(prediction) === normalizeText(groundTruth) ? 1 : 0;
|
|
12568
12659
|
}
|
|
12569
12660
|
function f1Score(prediction, groundTruth) {
|
|
12570
12661
|
const predictionTokens = normalizeText(prediction).split(" ");
|
|
@@ -12604,12 +12695,12 @@ var AxEvalUtil = {
|
|
|
12604
12695
|
};
|
|
12605
12696
|
|
|
12606
12697
|
// ../../node_modules/uuid/dist/esm-node/rng.js
|
|
12607
|
-
import
|
|
12698
|
+
import crypto4 from "crypto";
|
|
12608
12699
|
var rnds8Pool = new Uint8Array(256);
|
|
12609
12700
|
var poolPtr = rnds8Pool.length;
|
|
12610
12701
|
function rng() {
|
|
12611
12702
|
if (poolPtr > rnds8Pool.length - 16) {
|
|
12612
|
-
|
|
12703
|
+
crypto4.randomFillSync(rnds8Pool);
|
|
12613
12704
|
poolPtr = 0;
|
|
12614
12705
|
}
|
|
12615
12706
|
return rnds8Pool.slice(poolPtr, poolPtr += 16);
|
|
@@ -12625,9 +12716,9 @@ function unsafeStringify(arr, offset = 0) {
|
|
|
12625
12716
|
}
|
|
12626
12717
|
|
|
12627
12718
|
// ../../node_modules/uuid/dist/esm-node/native.js
|
|
12628
|
-
import
|
|
12719
|
+
import crypto5 from "crypto";
|
|
12629
12720
|
var native_default = {
|
|
12630
|
-
randomUUID:
|
|
12721
|
+
randomUUID: crypto5.randomUUID
|
|
12631
12722
|
};
|
|
12632
12723
|
|
|
12633
12724
|
// ../../node_modules/uuid/dist/esm-node/v4.js
|
|
@@ -12821,8 +12912,8 @@ ${JSON.stringify(res, null, 2)}`
|
|
|
12821
12912
|
};
|
|
12822
12913
|
|
|
12823
12914
|
// mcp/stdioTransport.ts
|
|
12824
|
-
import { spawn } from "
|
|
12825
|
-
import readline from "
|
|
12915
|
+
import { spawn } from "child_process";
|
|
12916
|
+
import readline from "readline";
|
|
12826
12917
|
var AxMCPStdioTransport = class {
|
|
12827
12918
|
process;
|
|
12828
12919
|
rl;
|