@ax-llm/ax 10.0.28 → 10.0.30

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
@@ -796,11 +796,11 @@ var logChatRequest = (req) => {
796
796
  return `${colorLog.blueBright("System:")}
797
797
  ${colorLog.whiteBright(msg.content)}`;
798
798
  case "function":
799
- return `${colorLog.blueBright("Function Result:")}
799
+ return `${colorLog.blueBright("\nFunction Result:")}
800
800
  ${colorLog.whiteBright(msg.result)}`;
801
801
  case "user": {
802
802
  if (typeof msg.content === "string") {
803
- return `${colorLog.blueBright("User:")}
803
+ return `${colorLog.blueBright("\nUser:")}
804
804
  ${colorLog.whiteBright(msg.content)}`;
805
805
  }
806
806
  const items2 = msg.content.map((v) => {
@@ -813,7 +813,7 @@ ${colorLog.whiteBright(msg.content)}`;
813
813
  throw new Error("Invalid content type");
814
814
  }
815
815
  });
816
- return `${colorLog.blueBright("User:")}
816
+ return `${colorLog.blueBright("\nUser:")}
817
817
  ${items2.join("\n")}`;
818
818
  }
819
819
  case "assistant": {
@@ -822,10 +822,10 @@ ${items2.join("\n")}`;
822
822
  const args = typeof fn.params !== "string" ? JSON.stringify(fn.params, null, 2) : fn.params;
823
823
  return `${fn.name}(${args})`;
824
824
  });
825
- return `${colorLog.blueBright("Functions:")}
825
+ return `${colorLog.blueBright("\nFunctions:")}
826
826
  ${colorLog.whiteBright(fns.join("\n"))}`;
827
827
  }
828
- return `${colorLog.blueBright("Assistant:")}
828
+ return `${colorLog.blueBright("\nAssistant:")}
829
829
  ${colorLog.whiteBright(msg.content ?? "<empty>")}`;
830
830
  }
831
831
  default:
@@ -4101,6 +4101,15 @@ var AxProgram = class {
4101
4101
  };
4102
4102
 
4103
4103
  // dsp/prompt.ts
4104
+ var formattingRules = `
4105
+ When providing responses:
4106
+ 1. Only output the exact requested content - no additional text, commentary, explanations, or clarifications
4107
+ 2. Each key's value must strictly adhere to the formatting rules specified in the reference documentation
4108
+ 3. Follow all formatting conventions precisely as defined
4109
+ 4. Do not add any extra content beyond what is explicitly requested
4110
+ 5. Match the exact structure and format specifications for each field
4111
+ 6. No preamble, postscript, or supplementary information
4112
+ 7. Pure output only - conforming exactly to the documented requirements`;
4104
4113
  var AxPromptTemplate = class {
4105
4114
  sig;
4106
4115
  fieldTemplates;
@@ -4111,43 +4120,39 @@ var AxPromptTemplate = class {
4111
4120
  const inArgs = this.renderDescFields(this.sig.getInputFields());
4112
4121
  const outArgs = this.renderDescFields(this.sig.getOutputFields());
4113
4122
  const task = [
4114
- `#Task
4115
- Given the fields ${inArgs}, produce the fields ${outArgs}.`
4123
+ `You will be provided with the following fields: ${inArgs}. Your task is to generate two new fields: ${outArgs}.`
4116
4124
  ];
4117
- const fnNames = functions?.map((f) => {
4118
- if ("toFunction" in f) {
4119
- return f.toFunction().name;
4120
- }
4121
- return f.name;
4122
- });
4123
- const funcList = fnNames?.map((fname) => `'${fname}'`).join(", ");
4125
+ const funcs = functions?.map(
4126
+ (f) => "toFunction" in f ? f.toFunction() : f
4127
+ );
4128
+ const funcList = funcs?.map((fn) => `\`${fn.name}\`: ${capitalizeFirstLetter(fn.description)}`).join("\n");
4124
4129
  if (funcList && funcList.length > 0) {
4130
+ task.push(`## Available Functions
4131
+ ${funcList}`);
4125
4132
  task.push(
4126
- `Use the following functions ${funcList} to complete the task. The functions must be used to resolve the output field values.`
4133
+ `Complete the task, using the functions defined earlier in this prompt, as needed. The output field values may be generated by applying these functions if appropriate for the task.`
4127
4134
  );
4128
4135
  }
4129
- const desc = this.sig.getDescription();
4130
- if (desc) {
4131
- task.push(desc.endsWith(".") ? desc : desc + ".");
4132
- }
4133
- task.push(
4134
- "Ensure the output strictly follows a plain text format, `key: value` separated by a new line."
4135
- );
4136
4136
  const inputFields = this.renderFields(this.sig.getInputFields());
4137
+ task.push(`## Input Fields
4138
+ ${inputFields}`);
4137
4139
  const outputFields = this.renderFields(this.sig.getOutputFields());
4140
+ task.push(`## Output Fields
4141
+ ${outputFields}`);
4138
4142
  task.push(
4139
- [
4140
- "\n",
4141
- "## Input Fields",
4142
- inputFields,
4143
- "\n",
4144
- "## Output Fields",
4145
- outputFields
4146
- ].join("\n")
4143
+ "Output must be in plain text, with each `key: value` pair on a new line. The format of each `value` should strictly adhere to the formatting instructions for its corresponding `key`, as defined earlier in this prompt."
4147
4144
  );
4145
+ task.push(formattingRules);
4146
+ const desc = this.sig.getDescription();
4147
+ if (desc) {
4148
+ task.push(
4149
+ `## TASK DESCRIPTION
4150
+ ${capitalizeFirstLetter(desc.endsWith(".") ? desc : desc + ".")}`
4151
+ );
4152
+ }
4148
4153
  this.task = {
4149
4154
  type: "text",
4150
- text: task.join(" ")
4155
+ text: task.join("\n\n")
4151
4156
  };
4152
4157
  }
4153
4158
  render = (values, {
@@ -4155,21 +4160,22 @@ Given the fields ${inArgs}, produce the fields ${outArgs}.`
4155
4160
  demos
4156
4161
  }) => {
4157
4162
  const renderedExamples = examples ? [
4158
- { type: "text", text: "Examples:\n" },
4163
+ { type: "text", text: "## Examples:\n" },
4159
4164
  ...this.renderExamples(examples)
4160
4165
  ] : [];
4161
4166
  const renderedDemos = demos ? this.renderDemos(demos) : [];
4162
4167
  const completion = this.renderInputFields(values);
4163
4168
  const allTextExamples = renderedExamples.every((v) => v.type === "text");
4164
4169
  const allTextDemos = renderedDemos.every((v) => v.type === "text");
4170
+ const examplesInSystemPrompt = allTextExamples && allTextDemos;
4165
4171
  let systemContent = this.task.text;
4166
- if (allTextExamples && allTextDemos) {
4172
+ if (examplesInSystemPrompt) {
4167
4173
  const combinedItems = [
4168
- { type: "text", text: this.task.text },
4174
+ { type: "text", text: systemContent + "\n\n" },
4169
4175
  ...renderedExamples,
4170
4176
  ...renderedDemos
4171
4177
  ];
4172
- combinedItems.reduce(combineConsecutiveStrings("\n"), []);
4178
+ combinedItems.reduce(combineConsecutiveStrings(""), []);
4173
4179
  if (combinedItems && combinedItems[0]) {
4174
4180
  systemContent = combinedItems[0].text;
4175
4181
  }
@@ -4178,7 +4184,7 @@ Given the fields ${inArgs}, produce the fields ${outArgs}.`
4178
4184
  role: "system",
4179
4185
  content: systemContent
4180
4186
  };
4181
- const promptList = allTextExamples && allTextDemos ? completion : [...renderedExamples, ...renderedDemos, ...completion];
4187
+ const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
4182
4188
  const prompt = promptList.filter((v) => v !== void 0);
4183
4189
  const userContent = prompt.every((v) => v.type === "text") ? prompt.map((v) => v.text).join("\n") : prompt.reduce(combineConsecutiveStrings("\n"), []);
4184
4190
  const userPrompt = {
@@ -4213,16 +4219,13 @@ Given the fields ${inArgs}, produce the fields ${outArgs}.`
4213
4219
  const renderedItem = [...renderedInputItem, ...renderedOutputItem];
4214
4220
  renderedItem.forEach((v) => {
4215
4221
  if ("text" in v) {
4216
- v.text = v.text;
4222
+ v.text = v.text + "\n";
4217
4223
  }
4218
4224
  if ("image" in v) {
4219
4225
  v.image = v.image;
4220
4226
  }
4221
4227
  list.push(v);
4222
4228
  });
4223
- if (renderedItem.length > 0) {
4224
- list.push({ type: "text", text: "\n" });
4225
- }
4226
4229
  }
4227
4230
  return list;
4228
4231
  };
@@ -4233,23 +4236,20 @@ Given the fields ${inArgs}, produce the fields ${outArgs}.`
4233
4236
  const renderedItem = fields.map((field) => this.renderInField(field, item, true)).filter((v) => v !== void 0).flat();
4234
4237
  renderedItem.slice(0, -1).forEach((v) => {
4235
4238
  if ("text" in v) {
4236
- v.text = v.text;
4239
+ v.text = v.text + "\n";
4237
4240
  }
4238
4241
  if ("image" in v) {
4239
4242
  v.image = v.image;
4240
4243
  }
4241
4244
  list.push(v);
4242
4245
  });
4243
- if (renderedItem.length > 0) {
4244
- list.push({ type: "text", text: "\n" });
4245
- }
4246
4246
  }
4247
4247
  return list;
4248
4248
  };
4249
4249
  renderInputFields = (values) => {
4250
4250
  const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values)).filter((v) => v !== void 0).flat();
4251
4251
  renderedItems.filter((v) => v.type === "text").forEach((v) => {
4252
- v.text = v.text + "\n\n";
4252
+ v.text = v.text + "\n";
4253
4253
  });
4254
4254
  return renderedItems;
4255
4255
  };
@@ -4363,16 +4363,14 @@ Given the fields ${inArgs}, produce the fields ${outArgs}.`
4363
4363
  };
4364
4364
  renderDescFields = (list) => list.map((v) => `\`${v.title}\``).join(", ");
4365
4365
  renderFields = (fields) => {
4366
- const header = "Field Name | Field Type | Required/Optional | Description";
4367
- const separator = "|";
4368
4366
  const rows = fields.map((field) => {
4369
4367
  const name = field.title;
4370
4368
  const type = field.type?.name ? toFieldType(field.type) : "string";
4371
4369
  const required = field.isOptional ? "optional" : "required";
4372
- const description = field.description ?? "";
4373
- return [name, type, required, description].join(` ${separator} `).trim();
4370
+ const description = field.description ? `: ${capitalizeFirstLetter(field.description)}` : "";
4371
+ return `- \`${name}\` (${type}, ${required})${description}`.trim();
4374
4372
  });
4375
- return [header, ...rows].join("\n");
4373
+ return rows.join("\n");
4376
4374
  };
4377
4375
  };
4378
4376
  var processValue = (field, value) => {
@@ -4447,6 +4445,12 @@ var isEmptyValue = (field, value) => {
4447
4445
  }
4448
4446
  return false;
4449
4447
  };
4448
+ function capitalizeFirstLetter(str) {
4449
+ if (str.length === 0) {
4450
+ return "";
4451
+ }
4452
+ return `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
4453
+ }
4450
4454
 
4451
4455
  // dsp/validate.ts
4452
4456
  var AxValidationError = class extends Error {