@ax-llm/ax 11.0.9 → 11.0.12

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
@@ -1150,7 +1150,11 @@ var AxBaseAI = class {
1150
1150
  }
1151
1151
  };
1152
1152
  var logChatRequest = (req) => {
1153
+ const hasAssistant = req.chatPrompt?.some((msg) => msg.role === "assistant");
1153
1154
  const items = req.chatPrompt?.map((msg) => {
1155
+ if (hasAssistant && msg.role === "system") {
1156
+ return "";
1157
+ }
1154
1158
  switch (msg.role) {
1155
1159
  case "system":
1156
1160
  return `${colorLog.blueBright("System:")}
@@ -4822,16 +4826,17 @@ var AxProgram = class {
4822
4826
 
4823
4827
  // dsp/prompt.ts
4824
4828
  var functionCallInstructions = `
4825
- ## Function Call Instructions
4829
+ ### Function Call Instructions
4826
4830
  - Complete the task, using the functions defined earlier in this prompt.
4827
4831
  - Call functions step-by-step, using the output of one function as input to the next.
4828
4832
  - Use the function results to generate the output fields.`;
4829
4833
  var formattingRules = `
4830
- ## Output Formatting Rules
4831
- - 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.
4832
4836
  - Each output key, value must strictly adhere to the specified output field formatting rules.
4833
4837
  - No preamble, postscript, or supplementary information.
4834
- - Do not repeat output fields.`;
4838
+ - Do not repeat output fields.
4839
+ - Do not use JSON to format the output.`;
4835
4840
  var AxPromptTemplate = class {
4836
4841
  sig;
4837
4842
  fieldTemplates;
@@ -4839,11 +4844,18 @@ var AxPromptTemplate = class {
4839
4844
  constructor(sig, functions, fieldTemplates) {
4840
4845
  this.sig = sig;
4841
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
+ }
4842
4853
  const inArgs = this.renderDescFields(this.sig.getInputFields());
4843
4854
  const outArgs = this.renderDescFields(this.sig.getOutputFields());
4844
- const task = [
4845
- `You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
4846
- ];
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
+ );
4847
4859
  const funcs = functions?.map(
4848
4860
  (f) => "toFunction" in f ? f.toFunction() : f
4849
4861
  );
@@ -4851,27 +4863,19 @@ var AxPromptTemplate = class {
4851
4863
  (fn) => `- \`${fn.name}\`: ${capitalizeFirstLetter(fn.description)}.`
4852
4864
  ).join("\n");
4853
4865
  if (funcList && funcList.length > 0) {
4854
- task.push(`## Available Functions
4866
+ task.push(`### Available Functions
4855
4867
  ${funcList}`);
4856
4868
  }
4857
4869
  const inputFields = this.renderFields(this.sig.getInputFields());
4858
- task.push(`## Input Fields
4870
+ task.push(`### Input Fields
4859
4871
  ${inputFields}`);
4860
4872
  const outputFields = this.renderFields(this.sig.getOutputFields());
4861
- task.push(`## Output Fields
4873
+ task.push(`### Output Fields
4862
4874
  ${outputFields}`);
4863
4875
  if (funcList && funcList.length > 0) {
4864
4876
  task.push(functionCallInstructions.trim());
4865
4877
  }
4866
4878
  task.push(formattingRules.trim());
4867
- const desc = this.sig.getDescription();
4868
- if (desc) {
4869
- const capitalized = capitalizeFirstLetter(desc.trim());
4870
- task.push(
4871
- `## TASK DESCRIPTION
4872
- ${capitalized.endsWith(".") ? capitalized : capitalized + "."}`
4873
- );
4874
- }
4875
4879
  this.task = {
4876
4880
  type: "text",
4877
4881
  text: task.join("\n\n")
@@ -6151,6 +6155,12 @@ function processChildAgentFunction(childFunction, parentValues, parentInputKeys,
6151
6155
  }
6152
6156
  return processedFunction;
6153
6157
  }
6158
+ var descriptionError = new Error(
6159
+ "Agent description must be at least 20 characters (explain in detail what the agent does)"
6160
+ );
6161
+ var definitionError = new Error(
6162
+ "Agent definition is the prompt you give to the LLM for the agent. It must be detailed and at least 100 characters"
6163
+ );
6154
6164
  var AxAgent = class {
6155
6165
  ai;
6156
6166
  program;
@@ -6159,43 +6169,46 @@ var AxAgent = class {
6159
6169
  disableSmartModelRouting;
6160
6170
  excludeFieldsFromPassthrough;
6161
6171
  name;
6162
- subAgentList;
6172
+ // private subAgentList?: string
6163
6173
  func;
6164
6174
  constructor({
6165
6175
  ai,
6166
6176
  name,
6167
6177
  description,
6178
+ definition,
6168
6179
  signature,
6169
6180
  agents,
6170
6181
  functions
6171
6182
  }, options) {
6183
+ const { disableSmartModelRouting, excludeFieldsFromPassthrough } = options ?? {};
6172
6184
  this.ai = ai;
6173
6185
  this.agents = agents;
6174
6186
  this.functions = functions;
6175
- this.disableSmartModelRouting = options?.disableSmartModelRouting;
6176
- this.excludeFieldsFromPassthrough = options?.excludeFieldsFromPassthrough ?? [];
6177
- const sig = new AxSignature(signature);
6178
- sig.setDescription(description);
6187
+ this.disableSmartModelRouting = disableSmartModelRouting;
6188
+ this.excludeFieldsFromPassthrough = excludeFieldsFromPassthrough ?? [];
6179
6189
  if (!name || name.length < 5) {
6180
6190
  throw new Error(
6181
- `Agent name must be at least 10 characters (more descriptive): ${name}`
6191
+ `Agent name must be at least 10 characters (more descriptive)`
6182
6192
  );
6183
6193
  }
6184
6194
  if (!description || description.length < 20) {
6185
- throw new Error(
6186
- `Agent description must be at least 20 characters (explain in detail what the agent does): ${description}`
6187
- );
6195
+ throw descriptionError;
6196
+ }
6197
+ if (definition && definition.length < 100) {
6198
+ throw definitionError;
6188
6199
  }
6189
- this.program = new AxGen(sig, options);
6200
+ this.program = new AxGen(signature, {
6201
+ ...options,
6202
+ description: definition ?? description
6203
+ });
6190
6204
  for (const agent of agents ?? []) {
6191
6205
  this.program.register(agent);
6192
6206
  }
6193
6207
  this.name = name;
6194
- this.subAgentList = agents?.map((a) => a.getFunction().name).join(", ");
6195
6208
  this.func = {
6196
6209
  name: toCamelCase(this.name),
6197
6210
  description,
6198
- parameters: sig.toJSONSchema(),
6211
+ parameters: this.program.getSignature().toJSONSchema(),
6199
6212
  func: () => this.forward
6200
6213
  };
6201
6214
  const mm = ai?.getModelList();
@@ -6226,13 +6239,26 @@ var AxAgent = class {
6226
6239
  }
6227
6240
  getFunction() {
6228
6241
  const boundFunc = this.forward.bind(this);
6229
- const wrappedFunc = (valuesAndModel, options) => {
6242
+ const wrappedFunc = async (valuesAndModel, options) => {
6230
6243
  const { model, ...values } = valuesAndModel;
6231
6244
  const ai = this.ai ?? options?.ai;
6232
6245
  if (!ai) {
6233
6246
  throw new Error("AI service is required to run the agent");
6234
6247
  }
6235
- return boundFunc(ai, values, { ...options, model });
6248
+ const ret = await boundFunc(ai, values, {
6249
+ ...options,
6250
+ model
6251
+ });
6252
+ const sig = this.program.getSignature();
6253
+ const outFields = sig.getOutputFields();
6254
+ const result = Object.keys(ret).map((k) => {
6255
+ const field = outFields.find((f) => f.name === k);
6256
+ if (field) {
6257
+ return `${field.title}: ${ret[k]}`;
6258
+ }
6259
+ return `${k}: ${ret[k]}`;
6260
+ }).join("\n");
6261
+ return result;
6236
6262
  };
6237
6263
  return {
6238
6264
  ...this.func,
@@ -6295,13 +6321,17 @@ var AxAgent = class {
6295
6321
  */
6296
6322
  setDescription(description) {
6297
6323
  if (!description || description.length < 20) {
6298
- throw new Error(
6299
- "Agent description must be at least 20 characters (explain in detail what the agent does)"
6300
- );
6324
+ throw descriptionError;
6301
6325
  }
6302
6326
  this.program.getSignature().setDescription(description);
6303
6327
  this.func.description = description;
6304
6328
  }
6329
+ setDefinition(definition) {
6330
+ if (!definition || definition.length < 100) {
6331
+ throw definitionError;
6332
+ }
6333
+ this.program.getSignature().setDescription(definition);
6334
+ }
6305
6335
  };
6306
6336
  function toCamelCase(inputString) {
6307
6337
  const words = inputString.split(/[^a-zA-Z0-9]/);