@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.d.cts CHANGED
@@ -326,6 +326,7 @@ declare class AxBaseAI<TModel, TEmbedModel, TChatRequest, TEmbedRequest, TChatRe
326
326
  getMetrics(): AxAIServiceMetrics;
327
327
  chat(req: Readonly<AxChatRequest<TModel>>, options?: Readonly<AxAIPromptConfig & AxAIServiceActionOptions<TModel, TEmbedModel>>): Promise<AxChatResponse | ReadableStream$1<AxChatResponse>>;
328
328
  private _chat1;
329
+ private cleanupFunctionSchema;
329
330
  private _chat2;
330
331
  embed(req: Readonly<AxEmbedRequest<TEmbedModel>>, options?: Readonly<AxAIServiceActionOptions<TModel, TEmbedModel>>): Promise<AxEmbedResponse>;
331
332
  private _embed1;
@@ -1637,20 +1638,18 @@ interface AxAgentFeatures {
1637
1638
  */
1638
1639
  declare class AxAgent<IN extends AxGenIn, OUT extends AxGenOut = AxGenOut> implements AxAgentic {
1639
1640
  private ai?;
1640
- private signature;
1641
1641
  private program;
1642
1642
  private functions?;
1643
1643
  private agents?;
1644
1644
  private disableSmartModelRouting?;
1645
1645
  private excludeFieldsFromPassthrough;
1646
1646
  private name;
1647
- private description;
1648
- private subAgentList?;
1649
1647
  private func;
1650
- constructor({ ai, name, description, signature, agents, functions, }: Readonly<{
1648
+ constructor({ ai, name, description, definition, signature, agents, functions, }: Readonly<{
1651
1649
  ai?: Readonly<AxAIService>;
1652
1650
  name: string;
1653
1651
  description: string;
1652
+ definition?: string;
1654
1653
  signature: AxSignature | string;
1655
1654
  agents?: AxAgentic[];
1656
1655
  functions?: AxFunction[];
package/index.d.ts CHANGED
@@ -326,6 +326,7 @@ declare class AxBaseAI<TModel, TEmbedModel, TChatRequest, TEmbedRequest, TChatRe
326
326
  getMetrics(): AxAIServiceMetrics;
327
327
  chat(req: Readonly<AxChatRequest<TModel>>, options?: Readonly<AxAIPromptConfig & AxAIServiceActionOptions<TModel, TEmbedModel>>): Promise<AxChatResponse | ReadableStream$1<AxChatResponse>>;
328
328
  private _chat1;
329
+ private cleanupFunctionSchema;
329
330
  private _chat2;
330
331
  embed(req: Readonly<AxEmbedRequest<TEmbedModel>>, options?: Readonly<AxAIServiceActionOptions<TModel, TEmbedModel>>): Promise<AxEmbedResponse>;
331
332
  private _embed1;
@@ -1637,20 +1638,18 @@ interface AxAgentFeatures {
1637
1638
  */
1638
1639
  declare class AxAgent<IN extends AxGenIn, OUT extends AxGenOut = AxGenOut> implements AxAgentic {
1639
1640
  private ai?;
1640
- private signature;
1641
1641
  private program;
1642
1642
  private functions?;
1643
1643
  private agents?;
1644
1644
  private disableSmartModelRouting?;
1645
1645
  private excludeFieldsFromPassthrough;
1646
1646
  private name;
1647
- private description;
1648
- private subAgentList?;
1649
1647
  private func;
1650
- constructor({ ai, name, description, signature, agents, functions, }: Readonly<{
1648
+ constructor({ ai, name, description, definition, signature, agents, functions, }: Readonly<{
1651
1649
  ai?: Readonly<AxAIService>;
1652
1650
  name: string;
1653
1651
  description: string;
1652
+ definition?: string;
1654
1653
  signature: AxSignature | string;
1655
1654
  agents?: AxAgentic[];
1656
1655
  functions?: AxFunction[];
package/index.js CHANGED
@@ -803,7 +803,8 @@ var AxBaseAI = class {
803
803
  const startTime = performance.now();
804
804
  let isError = false;
805
805
  try {
806
- return this._chat1(req, options);
806
+ const result = await this._chat1(req, options);
807
+ return result;
807
808
  } catch (error) {
808
809
  isError = true;
809
810
  throw error;
@@ -856,13 +857,31 @@ var AxBaseAI = class {
856
857
  }
857
858
  return await this._chat2(model, modelConfig, req, options);
858
859
  }
860
+ cleanupFunctionSchema(fn) {
861
+ const cleanFn = { ...fn };
862
+ if (cleanFn.parameters) {
863
+ const cleanParams = { ...cleanFn.parameters };
864
+ if (Array.isArray(cleanParams.required) && cleanParams.required.length === 0) {
865
+ delete cleanParams.required;
866
+ }
867
+ if (cleanParams.properties && Object.keys(cleanParams.properties).length === 0) {
868
+ delete cleanParams.properties;
869
+ }
870
+ if (Object.keys(cleanParams).length === 0 || Object.keys(cleanParams).length === 1 && cleanParams.type === "object") {
871
+ delete cleanFn.parameters;
872
+ } else {
873
+ cleanFn.parameters = cleanParams;
874
+ }
875
+ }
876
+ return cleanFn;
877
+ }
859
878
  async _chat2(model, modelConfig, chatReq, options, span) {
860
879
  if (!this.aiImpl.createChatReq) {
861
880
  throw new Error("generateChatReq not implemented");
862
881
  }
863
882
  let functions;
864
883
  if (chatReq.functions && chatReq.functions.length > 0) {
865
- functions = chatReq.functions;
884
+ functions = chatReq.functions.map((fn2) => this.cleanupFunctionSchema(fn2));
866
885
  }
867
886
  const req = {
868
887
  ...chatReq,
@@ -1029,7 +1048,11 @@ var AxBaseAI = class {
1029
1048
  }
1030
1049
  };
1031
1050
  var logChatRequest = (req) => {
1051
+ const hasAssistant = req.chatPrompt?.some((msg) => msg.role === "assistant");
1032
1052
  const items = req.chatPrompt?.map((msg) => {
1053
+ if (hasAssistant && msg.role === "system") {
1054
+ return "";
1055
+ }
1033
1056
  switch (msg.role) {
1034
1057
  case "system":
1035
1058
  return `${colorLog.blueBright("System:")}
@@ -4701,16 +4724,17 @@ var AxProgram = class {
4701
4724
 
4702
4725
  // dsp/prompt.ts
4703
4726
  var functionCallInstructions = `
4704
- ## Function Call Instructions
4727
+ ### Function Call Instructions
4705
4728
  - Complete the task, using the functions defined earlier in this prompt.
4706
4729
  - Call functions step-by-step, using the output of one function as input to the next.
4707
4730
  - Use the function results to generate the output fields.`;
4708
4731
  var formattingRules = `
4709
- ## Output Formatting Rules
4710
- - Output must strictly follow the defined plaintext \`key: value\` field format.
4732
+ ### Output Formatting Rules
4733
+ - Output must strictly follow the defined plain-text \`key: value\` field format.
4711
4734
  - Each output key, value must strictly adhere to the specified output field formatting rules.
4712
4735
  - No preamble, postscript, or supplementary information.
4713
- - Do not repeat output fields.`;
4736
+ - Do not repeat output fields.
4737
+ - Do not use JSON to format the output.`;
4714
4738
  var AxPromptTemplate = class {
4715
4739
  sig;
4716
4740
  fieldTemplates;
@@ -4718,11 +4742,18 @@ var AxPromptTemplate = class {
4718
4742
  constructor(sig, functions, fieldTemplates) {
4719
4743
  this.sig = sig;
4720
4744
  this.fieldTemplates = fieldTemplates;
4745
+ const task = [];
4746
+ const desc = this.sig.getDescription();
4747
+ if (desc) {
4748
+ const capitalized = capitalizeFirstLetter(desc.trim());
4749
+ task.push(capitalized.endsWith(".") ? capitalized : capitalized + ".");
4750
+ }
4721
4751
  const inArgs = this.renderDescFields(this.sig.getInputFields());
4722
4752
  const outArgs = this.renderDescFields(this.sig.getOutputFields());
4723
- const task = [
4724
- `You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
4725
- ];
4753
+ task.push(
4754
+ `## Processing Instructions
4755
+ You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
4756
+ );
4726
4757
  const funcs = functions?.map(
4727
4758
  (f) => "toFunction" in f ? f.toFunction() : f
4728
4759
  );
@@ -4730,27 +4761,19 @@ var AxPromptTemplate = class {
4730
4761
  (fn) => `- \`${fn.name}\`: ${capitalizeFirstLetter(fn.description)}.`
4731
4762
  ).join("\n");
4732
4763
  if (funcList && funcList.length > 0) {
4733
- task.push(`## Available Functions
4764
+ task.push(`### Available Functions
4734
4765
  ${funcList}`);
4735
4766
  }
4736
4767
  const inputFields = this.renderFields(this.sig.getInputFields());
4737
- task.push(`## Input Fields
4768
+ task.push(`### Input Fields
4738
4769
  ${inputFields}`);
4739
4770
  const outputFields = this.renderFields(this.sig.getOutputFields());
4740
- task.push(`## Output Fields
4771
+ task.push(`### Output Fields
4741
4772
  ${outputFields}`);
4742
4773
  if (funcList && funcList.length > 0) {
4743
4774
  task.push(functionCallInstructions.trim());
4744
4775
  }
4745
4776
  task.push(formattingRules.trim());
4746
- const desc = this.sig.getDescription();
4747
- if (desc) {
4748
- const capitalized = capitalizeFirstLetter(desc.trim());
4749
- task.push(
4750
- `## TASK DESCRIPTION
4751
- ${capitalized.endsWith(".") ? capitalized : capitalized + "."}`
4752
- );
4753
- }
4754
4777
  this.task = {
4755
4778
  type: "text",
4756
4779
  text: task.join("\n\n")
@@ -6032,31 +6055,29 @@ function processChildAgentFunction(childFunction, parentValues, parentInputKeys,
6032
6055
  }
6033
6056
  var AxAgent = class {
6034
6057
  ai;
6035
- signature;
6036
6058
  program;
6037
6059
  functions;
6038
6060
  agents;
6039
6061
  disableSmartModelRouting;
6040
6062
  excludeFieldsFromPassthrough;
6041
6063
  name;
6042
- description;
6043
- subAgentList;
6064
+ // private subAgentList?: string
6044
6065
  func;
6045
6066
  constructor({
6046
6067
  ai,
6047
6068
  name,
6048
6069
  description,
6070
+ definition,
6049
6071
  signature,
6050
6072
  agents,
6051
6073
  functions
6052
6074
  }, options) {
6075
+ const { disableSmartModelRouting, excludeFieldsFromPassthrough } = options ?? {};
6053
6076
  this.ai = ai;
6054
6077
  this.agents = agents;
6055
6078
  this.functions = functions;
6056
- this.disableSmartModelRouting = options?.disableSmartModelRouting;
6057
- this.excludeFieldsFromPassthrough = options?.excludeFieldsFromPassthrough ?? [];
6058
- this.signature = new AxSignature(signature);
6059
- this.signature.setDescription(description);
6079
+ this.disableSmartModelRouting = disableSmartModelRouting;
6080
+ this.excludeFieldsFromPassthrough = excludeFieldsFromPassthrough ?? [];
6060
6081
  if (!name || name.length < 5) {
6061
6082
  throw new Error(
6062
6083
  `Agent name must be at least 10 characters (more descriptive): ${name}`
@@ -6067,17 +6088,23 @@ var AxAgent = class {
6067
6088
  `Agent description must be at least 20 characters (explain in detail what the agent does): ${description}`
6068
6089
  );
6069
6090
  }
6070
- this.program = new AxGen(this.signature, options);
6091
+ if (definition && definition.length < 100) {
6092
+ throw new Error(
6093
+ `Agent definition is the prompt you give to the LLM for the agent. It must be detailed and at least 100 characters`
6094
+ );
6095
+ }
6096
+ this.program = new AxGen(signature, {
6097
+ ...options,
6098
+ description: definition
6099
+ });
6071
6100
  for (const agent of agents ?? []) {
6072
6101
  this.program.register(agent);
6073
6102
  }
6074
6103
  this.name = name;
6075
- this.description = description;
6076
- this.subAgentList = agents?.map((a) => a.getFunction().name).join(", ");
6077
6104
  this.func = {
6078
6105
  name: toCamelCase(this.name),
6079
- description: this.description,
6080
- parameters: this.signature.toJSONSchema(),
6106
+ description,
6107
+ parameters: this.program.getSignature().toJSONSchema(),
6081
6108
  func: () => this.forward
6082
6109
  };
6083
6110
  const mm = ai?.getModelList();
@@ -6108,13 +6135,26 @@ var AxAgent = class {
6108
6135
  }
6109
6136
  getFunction() {
6110
6137
  const boundFunc = this.forward.bind(this);
6111
- const wrappedFunc = (valuesAndModel, options) => {
6138
+ const wrappedFunc = async (valuesAndModel, options) => {
6112
6139
  const { model, ...values } = valuesAndModel;
6113
6140
  const ai = this.ai ?? options?.ai;
6114
6141
  if (!ai) {
6115
6142
  throw new Error("AI service is required to run the agent");
6116
6143
  }
6117
- return boundFunc(ai, values, { ...options, model });
6144
+ const ret = await boundFunc(ai, values, {
6145
+ ...options,
6146
+ model
6147
+ });
6148
+ const sig = this.program.getSignature();
6149
+ const outFields = sig.getOutputFields();
6150
+ const result = Object.keys(ret).map((k) => {
6151
+ const field = outFields.find((f) => f.name === k);
6152
+ if (field) {
6153
+ return `${field.title}: ${ret[k]}`;
6154
+ }
6155
+ return `${k}: ${ret[k]}`;
6156
+ }).join("\n");
6157
+ return result;
6118
6158
  };
6119
6159
  return {
6120
6160
  ...this.func,
@@ -6133,7 +6173,7 @@ var AxAgent = class {
6133
6173
  init(parentAi, values, options) {
6134
6174
  const ai = this.ai ?? parentAi;
6135
6175
  const mm = ai?.getModelList();
6136
- const parentSchema = this.signature.getInputFields();
6176
+ const parentSchema = this.program.getSignature().getInputFields();
6137
6177
  const parentKeys = parentSchema.map((p) => p.name);
6138
6178
  const agentFuncs = this.agents?.map((agent) => {
6139
6179
  const f = agent.getFeatures();
@@ -6181,8 +6221,7 @@ var AxAgent = class {
6181
6221
  "Agent description must be at least 20 characters (explain in detail what the agent does)"
6182
6222
  );
6183
6223
  }
6184
- this.description = description;
6185
- this.signature.setDescription(description);
6224
+ this.program.getSignature().setDescription(description);
6186
6225
  this.func.description = description;
6187
6226
  }
6188
6227
  };