@ax-llm/ax 11.0.8 → 11.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -905,7 +905,8 @@ var AxBaseAI = class {
905
905
  const startTime = performance.now();
906
906
  let isError = false;
907
907
  try {
908
- return this._chat1(req, options);
908
+ const result = await this._chat1(req, options);
909
+ return result;
909
910
  } catch (error) {
910
911
  isError = true;
911
912
  throw error;
@@ -958,13 +959,31 @@ var AxBaseAI = class {
958
959
  }
959
960
  return await this._chat2(model, modelConfig, req, options);
960
961
  }
962
+ cleanupFunctionSchema(fn) {
963
+ const cleanFn = { ...fn };
964
+ if (cleanFn.parameters) {
965
+ const cleanParams = { ...cleanFn.parameters };
966
+ if (Array.isArray(cleanParams.required) && cleanParams.required.length === 0) {
967
+ delete cleanParams.required;
968
+ }
969
+ if (cleanParams.properties && Object.keys(cleanParams.properties).length === 0) {
970
+ delete cleanParams.properties;
971
+ }
972
+ if (Object.keys(cleanParams).length === 0 || Object.keys(cleanParams).length === 1 && cleanParams.type === "object") {
973
+ delete cleanFn.parameters;
974
+ } else {
975
+ cleanFn.parameters = cleanParams;
976
+ }
977
+ }
978
+ return cleanFn;
979
+ }
961
980
  async _chat2(model, modelConfig, chatReq, options, span) {
962
981
  if (!this.aiImpl.createChatReq) {
963
982
  throw new Error("generateChatReq not implemented");
964
983
  }
965
984
  let functions;
966
985
  if (chatReq.functions && chatReq.functions.length > 0) {
967
- functions = chatReq.functions;
986
+ functions = chatReq.functions.map((fn2) => this.cleanupFunctionSchema(fn2));
968
987
  }
969
988
  const req = {
970
989
  ...chatReq,
@@ -1131,7 +1150,11 @@ var AxBaseAI = class {
1131
1150
  }
1132
1151
  };
1133
1152
  var logChatRequest = (req) => {
1153
+ const hasAssistant = req.chatPrompt?.some((msg) => msg.role === "assistant");
1134
1154
  const items = req.chatPrompt?.map((msg) => {
1155
+ if (hasAssistant && msg.role === "system") {
1156
+ return "";
1157
+ }
1135
1158
  switch (msg.role) {
1136
1159
  case "system":
1137
1160
  return `${colorLog.blueBright("System:")}
@@ -4803,16 +4826,17 @@ var AxProgram = class {
4803
4826
 
4804
4827
  // dsp/prompt.ts
4805
4828
  var functionCallInstructions = `
4806
- ## Function Call Instructions
4829
+ ### Function Call Instructions
4807
4830
  - Complete the task, using the functions defined earlier in this prompt.
4808
4831
  - Call functions step-by-step, using the output of one function as input to the next.
4809
4832
  - Use the function results to generate the output fields.`;
4810
4833
  var formattingRules = `
4811
- ## Output Formatting Rules
4812
- - Output must strictly follow the defined plaintext \`key: value\` field format.
4834
+ ### Output Formatting Rules
4835
+ - Output must strictly follow the defined plain-text \`key: value\` field format.
4813
4836
  - Each output key, value must strictly adhere to the specified output field formatting rules.
4814
4837
  - No preamble, postscript, or supplementary information.
4815
- - Do not repeat output fields.`;
4838
+ - Do not repeat output fields.
4839
+ - Do not use JSON to format the output.`;
4816
4840
  var AxPromptTemplate = class {
4817
4841
  sig;
4818
4842
  fieldTemplates;
@@ -4820,11 +4844,18 @@ var AxPromptTemplate = class {
4820
4844
  constructor(sig, functions, fieldTemplates) {
4821
4845
  this.sig = sig;
4822
4846
  this.fieldTemplates = fieldTemplates;
4847
+ const task = [];
4848
+ const desc = this.sig.getDescription();
4849
+ if (desc) {
4850
+ const capitalized = capitalizeFirstLetter(desc.trim());
4851
+ task.push(capitalized.endsWith(".") ? capitalized : capitalized + ".");
4852
+ }
4823
4853
  const inArgs = this.renderDescFields(this.sig.getInputFields());
4824
4854
  const outArgs = this.renderDescFields(this.sig.getOutputFields());
4825
- const task = [
4826
- `You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
4827
- ];
4855
+ task.push(
4856
+ `## Processing Instructions
4857
+ You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
4858
+ );
4828
4859
  const funcs = functions?.map(
4829
4860
  (f) => "toFunction" in f ? f.toFunction() : f
4830
4861
  );
@@ -4832,27 +4863,19 @@ var AxPromptTemplate = class {
4832
4863
  (fn) => `- \`${fn.name}\`: ${capitalizeFirstLetter(fn.description)}.`
4833
4864
  ).join("\n");
4834
4865
  if (funcList && funcList.length > 0) {
4835
- task.push(`## Available Functions
4866
+ task.push(`### Available Functions
4836
4867
  ${funcList}`);
4837
4868
  }
4838
4869
  const inputFields = this.renderFields(this.sig.getInputFields());
4839
- task.push(`## Input Fields
4870
+ task.push(`### Input Fields
4840
4871
  ${inputFields}`);
4841
4872
  const outputFields = this.renderFields(this.sig.getOutputFields());
4842
- task.push(`## Output Fields
4873
+ task.push(`### Output Fields
4843
4874
  ${outputFields}`);
4844
4875
  if (funcList && funcList.length > 0) {
4845
4876
  task.push(functionCallInstructions.trim());
4846
4877
  }
4847
4878
  task.push(formattingRules.trim());
4848
- const desc = this.sig.getDescription();
4849
- if (desc) {
4850
- const capitalized = capitalizeFirstLetter(desc.trim());
4851
- task.push(
4852
- `## TASK DESCRIPTION
4853
- ${capitalized.endsWith(".") ? capitalized : capitalized + "."}`
4854
- );
4855
- }
4856
4879
  this.task = {
4857
4880
  type: "text",
4858
4881
  text: task.join("\n\n")
@@ -6134,31 +6157,29 @@ function processChildAgentFunction(childFunction, parentValues, parentInputKeys,
6134
6157
  }
6135
6158
  var AxAgent = class {
6136
6159
  ai;
6137
- signature;
6138
6160
  program;
6139
6161
  functions;
6140
6162
  agents;
6141
6163
  disableSmartModelRouting;
6142
6164
  excludeFieldsFromPassthrough;
6143
6165
  name;
6144
- description;
6145
- subAgentList;
6166
+ // private subAgentList?: string
6146
6167
  func;
6147
6168
  constructor({
6148
6169
  ai,
6149
6170
  name,
6150
6171
  description,
6172
+ definition,
6151
6173
  signature,
6152
6174
  agents,
6153
6175
  functions
6154
6176
  }, options) {
6177
+ const { disableSmartModelRouting, excludeFieldsFromPassthrough } = options ?? {};
6155
6178
  this.ai = ai;
6156
6179
  this.agents = agents;
6157
6180
  this.functions = functions;
6158
- this.disableSmartModelRouting = options?.disableSmartModelRouting;
6159
- this.excludeFieldsFromPassthrough = options?.excludeFieldsFromPassthrough ?? [];
6160
- this.signature = new AxSignature(signature);
6161
- this.signature.setDescription(description);
6181
+ this.disableSmartModelRouting = disableSmartModelRouting;
6182
+ this.excludeFieldsFromPassthrough = excludeFieldsFromPassthrough ?? [];
6162
6183
  if (!name || name.length < 5) {
6163
6184
  throw new Error(
6164
6185
  `Agent name must be at least 10 characters (more descriptive): ${name}`
@@ -6169,17 +6190,23 @@ var AxAgent = class {
6169
6190
  `Agent description must be at least 20 characters (explain in detail what the agent does): ${description}`
6170
6191
  );
6171
6192
  }
6172
- this.program = new AxGen(this.signature, options);
6193
+ if (definition && definition.length < 100) {
6194
+ throw new Error(
6195
+ `Agent definition is the prompt you give to the LLM for the agent. It must be detailed and at least 100 characters`
6196
+ );
6197
+ }
6198
+ this.program = new AxGen(signature, {
6199
+ ...options,
6200
+ description: definition
6201
+ });
6173
6202
  for (const agent of agents ?? []) {
6174
6203
  this.program.register(agent);
6175
6204
  }
6176
6205
  this.name = name;
6177
- this.description = description;
6178
- this.subAgentList = agents?.map((a) => a.getFunction().name).join(", ");
6179
6206
  this.func = {
6180
6207
  name: toCamelCase(this.name),
6181
- description: this.description,
6182
- parameters: this.signature.toJSONSchema(),
6208
+ description,
6209
+ parameters: this.program.getSignature().toJSONSchema(),
6183
6210
  func: () => this.forward
6184
6211
  };
6185
6212
  const mm = ai?.getModelList();
@@ -6210,13 +6237,26 @@ var AxAgent = class {
6210
6237
  }
6211
6238
  getFunction() {
6212
6239
  const boundFunc = this.forward.bind(this);
6213
- const wrappedFunc = (valuesAndModel, options) => {
6240
+ const wrappedFunc = async (valuesAndModel, options) => {
6214
6241
  const { model, ...values } = valuesAndModel;
6215
6242
  const ai = this.ai ?? options?.ai;
6216
6243
  if (!ai) {
6217
6244
  throw new Error("AI service is required to run the agent");
6218
6245
  }
6219
- return boundFunc(ai, values, { ...options, model });
6246
+ const ret = await boundFunc(ai, values, {
6247
+ ...options,
6248
+ model
6249
+ });
6250
+ const sig = this.program.getSignature();
6251
+ const outFields = sig.getOutputFields();
6252
+ const result = Object.keys(ret).map((k) => {
6253
+ const field = outFields.find((f) => f.name === k);
6254
+ if (field) {
6255
+ return `${field.title}: ${ret[k]}`;
6256
+ }
6257
+ return `${k}: ${ret[k]}`;
6258
+ }).join("\n");
6259
+ return result;
6220
6260
  };
6221
6261
  return {
6222
6262
  ...this.func,
@@ -6235,7 +6275,7 @@ var AxAgent = class {
6235
6275
  init(parentAi, values, options) {
6236
6276
  const ai = this.ai ?? parentAi;
6237
6277
  const mm = ai?.getModelList();
6238
- const parentSchema = this.signature.getInputFields();
6278
+ const parentSchema = this.program.getSignature().getInputFields();
6239
6279
  const parentKeys = parentSchema.map((p) => p.name);
6240
6280
  const agentFuncs = this.agents?.map((agent) => {
6241
6281
  const f = agent.getFeatures();
@@ -6283,8 +6323,7 @@ var AxAgent = class {
6283
6323
  "Agent description must be at least 20 characters (explain in detail what the agent does)"
6284
6324
  );
6285
6325
  }
6286
- this.description = description;
6287
- this.signature.setDescription(description);
6326
+ this.program.getSignature().setDescription(description);
6288
6327
  this.func.description = description;
6289
6328
  }
6290
6329
  };