@ax-llm/ax 11.0.29 → 11.0.31

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
@@ -42,6 +42,7 @@ __export(index_exports, {
42
42
  AxAIDeepSeekModel: () => AxAIDeepSeekModel,
43
43
  AxAIGoogleGemini: () => AxAIGoogleGemini,
44
44
  AxAIGoogleGeminiEmbedModel: () => AxAIGoogleGeminiEmbedModel,
45
+ AxAIGoogleGeminiEmbedTypes: () => AxAIGoogleGeminiEmbedTypes,
45
46
  AxAIGoogleGeminiModel: () => AxAIGoogleGeminiModel,
46
47
  AxAIGoogleGeminiSafetyCategory: () => AxAIGoogleGeminiSafetyCategory,
47
48
  AxAIGoogleGeminiSafetyThreshold: () => AxAIGoogleGeminiSafetyThreshold,
@@ -752,9 +753,12 @@ var ColorLog = class {
752
753
 
753
754
  // ai/debug.ts
754
755
  var colorLog = new ColorLog();
755
- var formatChatMessage = (msg, hideContent) => {
756
+ var formatChatMessage = (msg, hideContent, hideSystemPrompt) => {
756
757
  switch (msg.role) {
757
758
  case "system":
759
+ if (hideSystemPrompt) {
760
+ return "";
761
+ }
758
762
  return `
759
763
  ${colorLog.blueBright("System:")}
760
764
  ${colorLog.whiteBright(msg.content)}`;
@@ -800,13 +804,15 @@ ${hideContent ? "" : colorLog.whiteBright(msg.content ?? "<empty>")}`;
800
804
  throw new Error("Invalid role");
801
805
  }
802
806
  };
803
- var logChatRequestMessage = (msg) => {
804
- process.stdout.write(`${formatChatMessage(msg)}
807
+ var logChatRequestMessage = (msg, hideSystemPrompt) => {
808
+ process.stdout.write(`${formatChatMessage(msg, hideSystemPrompt)}
805
809
  `);
806
810
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
807
811
  };
808
- var logChatRequest = (chatPrompt) => {
809
- const items = chatPrompt?.map((msg) => formatChatMessage(msg));
812
+ var logChatRequest = (chatPrompt, hideSystemPrompt) => {
813
+ const items = chatPrompt?.map(
814
+ (msg) => formatChatMessage(msg, hideSystemPrompt)
815
+ );
810
816
  if (items) {
811
817
  process.stdout.write(items.join("\n"));
812
818
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
@@ -1126,7 +1132,7 @@ var AxBaseAI = class {
1126
1132
  return res2;
1127
1133
  };
1128
1134
  if (options?.debug ?? this.debug) {
1129
- logChatRequest(req.chatPrompt);
1135
+ logChatRequest(req.chatPrompt, options?.debugHideSystemPrompt);
1130
1136
  }
1131
1137
  const rt = options?.rateLimiter ?? this.rt;
1132
1138
  const rv = rt ? await rt(fn, { modelUsage: this.modelUsage }) : await fn();
@@ -1913,10 +1919,9 @@ var axAIOpenAIFastConfig = () => ({
1913
1919
  model: "gpt-4o-mini" /* GPT4OMini */
1914
1920
  });
1915
1921
  var AxAIOpenAIImpl = class {
1916
- constructor(config, streamingUsage, dimensions) {
1922
+ constructor(config, streamingUsage) {
1917
1923
  this.config = config;
1918
1924
  this.streamingUsage = streamingUsage;
1919
- this.dimensions = dimensions;
1920
1925
  }
1921
1926
  getModelConfig() {
1922
1927
  const { config } = this;
@@ -1988,7 +1993,7 @@ var AxAIOpenAIImpl = class {
1988
1993
  const reqValue = {
1989
1994
  model,
1990
1995
  input: req.texts,
1991
- dimensions: this.dimensions
1996
+ dimensions: this.config.dimensions
1992
1997
  };
1993
1998
  return [apiConfig, reqValue];
1994
1999
  }
@@ -2168,8 +2173,7 @@ var AxAIOpenAIBase = class extends AxBaseAI {
2168
2173
  }
2169
2174
  const aiImpl = new AxAIOpenAIImpl(
2170
2175
  config,
2171
- options?.streamingUsage ?? true,
2172
- config.dimensions
2176
+ options?.streamingUsage ?? true
2173
2177
  );
2174
2178
  super(aiImpl, {
2175
2179
  name: "OpenAI",
@@ -2663,6 +2667,7 @@ var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2663
2667
  return AxAIGoogleGeminiModel2;
2664
2668
  })(AxAIGoogleGeminiModel || {});
2665
2669
  var AxAIGoogleGeminiEmbedModel = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedModel2) => {
2670
+ AxAIGoogleGeminiEmbedModel2["GeminiEmbedding"] = "gemini-embedding-exp-03-07";
2666
2671
  AxAIGoogleGeminiEmbedModel2["TextEmbedding004"] = "text-embedding-004";
2667
2672
  AxAIGoogleGeminiEmbedModel2["TextEmbedding005"] = "text-embedding-005";
2668
2673
  return AxAIGoogleGeminiEmbedModel2;
@@ -2682,6 +2687,17 @@ var AxAIGoogleGeminiSafetyThreshold = /* @__PURE__ */ ((AxAIGoogleGeminiSafetyTh
2682
2687
  AxAIGoogleGeminiSafetyThreshold2["BlockDefault"] = "HARM_BLOCK_THRESHOLD_UNSPECIFIED";
2683
2688
  return AxAIGoogleGeminiSafetyThreshold2;
2684
2689
  })(AxAIGoogleGeminiSafetyThreshold || {});
2690
+ var AxAIGoogleGeminiEmbedTypes = /* @__PURE__ */ ((AxAIGoogleGeminiEmbedTypes2) => {
2691
+ AxAIGoogleGeminiEmbedTypes2["SemanticSimilarity"] = "SEMANTIC_SIMILARITY";
2692
+ AxAIGoogleGeminiEmbedTypes2["Classification"] = "CLASSIFICATION";
2693
+ AxAIGoogleGeminiEmbedTypes2["Clustering"] = "CLUSTERING";
2694
+ AxAIGoogleGeminiEmbedTypes2["RetrievalDocument"] = "RETRIEVAL_DOCUMENT";
2695
+ AxAIGoogleGeminiEmbedTypes2["RetrievalQuery"] = "RETRIEVAL_QUERY";
2696
+ AxAIGoogleGeminiEmbedTypes2["QuestionAnswering"] = "QUESTION_ANSWERING";
2697
+ AxAIGoogleGeminiEmbedTypes2["FactVerification"] = "FACT_VERIFICATION";
2698
+ AxAIGoogleGeminiEmbedTypes2["CodeRetrievalQuery"] = "CODE_RETRIEVAL_QUERY";
2699
+ return AxAIGoogleGeminiEmbedTypes2;
2700
+ })(AxAIGoogleGeminiEmbedTypes || {});
2685
2701
 
2686
2702
  // ai/google-gemini/info.ts
2687
2703
  var axModelInfoGoogleGemini = [
@@ -2986,7 +3002,9 @@ var AxAIGoogleGeminiImpl = class {
2986
3002
  reqValue = {
2987
3003
  requests: req.texts.map((text) => ({
2988
3004
  model: "models/" + model,
2989
- content: { parts: [{ text }] }
3005
+ content: { parts: [{ text }] },
3006
+ outputDimensionality: this.config.dimensions,
3007
+ taskType: this.config.embedType
2990
3008
  }))
2991
3009
  };
2992
3010
  }
@@ -3908,9 +3926,9 @@ function mergeFunctionCalls(functionCalls, functionCallDeltas) {
3908
3926
  // mem/memory.ts
3909
3927
  var defaultLimit = 1e4;
3910
3928
  var MemoryImpl = class {
3911
- constructor(limit = defaultLimit, debug = false) {
3929
+ constructor(limit = defaultLimit, options) {
3912
3930
  this.limit = limit;
3913
- this.debug = debug;
3931
+ this.options = options;
3914
3932
  if (limit <= 0) {
3915
3933
  throw Error("argument 'limit' must be greater than 0");
3916
3934
  }
@@ -3931,8 +3949,8 @@ var MemoryImpl = class {
3931
3949
  }
3932
3950
  add(value) {
3933
3951
  this.addMemory(value);
3934
- if (this.debug) {
3935
- debugRequest(value);
3952
+ if (this.options?.debug) {
3953
+ debugRequest(value, this.options?.debugHideSystemPrompt);
3936
3954
  }
3937
3955
  }
3938
3956
  addResultMessage({
@@ -3951,7 +3969,7 @@ var MemoryImpl = class {
3951
3969
  functionCalls
3952
3970
  }) {
3953
3971
  this.addResultMessage({ content, name, functionCalls });
3954
- if (this.debug) {
3972
+ if (this.options?.debug) {
3955
3973
  debugResponse({ content, name, functionCalls });
3956
3974
  }
3957
3975
  }
@@ -3975,7 +3993,7 @@ var MemoryImpl = class {
3975
3993
  lastItem.chat.functionCalls = functionCalls;
3976
3994
  }
3977
3995
  }
3978
- if (this.debug) {
3996
+ if (this.options?.debug) {
3979
3997
  if (delta && typeof delta === "string") {
3980
3998
  debugResponseDelta(delta);
3981
3999
  } else if (lastItem) {
@@ -4031,10 +4049,10 @@ var MemoryImpl = class {
4031
4049
  }
4032
4050
  };
4033
4051
  var AxMemory = class {
4034
- constructor(limit = defaultLimit, debug = false) {
4052
+ constructor(limit = defaultLimit, options) {
4035
4053
  this.limit = limit;
4036
- this.debug = debug;
4037
- this.defaultMemory = new MemoryImpl(limit, debug);
4054
+ this.options = options;
4055
+ this.defaultMemory = new MemoryImpl(limit, options);
4038
4056
  }
4039
4057
  memories = /* @__PURE__ */ new Map();
4040
4058
  defaultMemory;
@@ -4043,7 +4061,7 @@ var AxMemory = class {
4043
4061
  return this.defaultMemory;
4044
4062
  }
4045
4063
  if (!this.memories.has(sessionId)) {
4046
- this.memories.set(sessionId, new MemoryImpl(this.limit));
4064
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
4047
4065
  }
4048
4066
  return this.memories.get(sessionId);
4049
4067
  }
@@ -4072,15 +4090,15 @@ var AxMemory = class {
4072
4090
  if (!sessionId) {
4073
4091
  this.defaultMemory.reset();
4074
4092
  } else {
4075
- this.memories.set(sessionId, new MemoryImpl(this.limit));
4093
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
4076
4094
  }
4077
4095
  }
4078
4096
  };
4079
- function debugRequest(value) {
4097
+ function debugRequest(value, hideSystemPrompt) {
4080
4098
  if (Array.isArray(value)) {
4081
- logChatRequest(value);
4099
+ logChatRequest(value, hideSystemPrompt);
4082
4100
  } else {
4083
- logChatRequestMessage(value);
4101
+ logChatRequestMessage(value, hideSystemPrompt);
4084
4102
  }
4085
4103
  }
4086
4104
  function debugResponse(value) {
@@ -5565,10 +5583,13 @@ ${errors}
5565
5583
  }
5566
5584
 
5567
5585
  // dsp/datetime.ts
5568
- function parseLLMFriendlyDate(field, dateStr) {
5586
+ function parseLLMFriendlyDate(field, dateStr, required = false) {
5569
5587
  try {
5570
5588
  return _parseLLMFriendlyDate(dateStr);
5571
5589
  } catch (err) {
5590
+ if (field.isOptional && !required) {
5591
+ return;
5592
+ }
5572
5593
  const message = err.message;
5573
5594
  throw new ValidationError({ fields: [field], message, value: dateStr });
5574
5595
  }
@@ -5582,10 +5603,13 @@ function _parseLLMFriendlyDate(dateStr) {
5582
5603
  const date = import_moment_timezone.default.utc(dateStr, "YYYY-MM-DD").startOf("day");
5583
5604
  return date.toDate();
5584
5605
  }
5585
- function parseLLMFriendlyDateTime(field, dateStr) {
5606
+ function parseLLMFriendlyDateTime(field, dateStr, required = false) {
5586
5607
  try {
5587
5608
  return _parseLLMFriendlyDateTime(dateStr);
5588
5609
  } catch (err) {
5610
+ if (field.isOptional && !required) {
5611
+ return;
5612
+ }
5589
5613
  const message = err.message;
5590
5614
  throw new ValidationError({ fields: [field], message, value: dateStr });
5591
5615
  }
@@ -5654,8 +5678,9 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5654
5678
  if (field.name in values) {
5655
5679
  continue;
5656
5680
  }
5657
- const prefix = field.title + ":";
5658
- let e = matchesContent(content, prefix, xstate.s + 1);
5681
+ const isFirst = xstate.extractedFields.length === 0;
5682
+ const prefix = (isFirst ? "" : "\n") + field.title + ":";
5683
+ let e = matchesContent(content, prefix, xstate.s === 0 ? 0 : xstate.s + 1);
5659
5684
  switch (e) {
5660
5685
  case -1:
5661
5686
  if (streamingValidation && values.length == 0 && !field.isOptional) {
@@ -5683,7 +5708,11 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5683
5708
  if (parsedValue !== void 0) {
5684
5709
  values[xstate.currField.name] = parsedValue;
5685
5710
  }
5686
- xstate.prevField = { field: xstate.currField, s: xstate.s, e };
5711
+ if (xstate.prevFields) {
5712
+ xstate.prevFields?.push({ field: xstate.currField, s: xstate.s, e });
5713
+ } else {
5714
+ xstate.prevFields = [{ field: xstate.currField, s: xstate.s, e }];
5715
+ }
5687
5716
  }
5688
5717
  checkMissingRequiredFields(xstate, values, index);
5689
5718
  xstate.s = e + prefixLen;
@@ -5708,7 +5737,7 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
5708
5737
  const sigFields = sig.getOutputFields();
5709
5738
  checkMissingRequiredFields(xstate, values, sigFields.length);
5710
5739
  };
5711
- var convertValueToType = (field, val) => {
5740
+ var convertValueToType = (field, val, required = false) => {
5712
5741
  switch (field.type?.name) {
5713
5742
  case "code":
5714
5743
  return extractBlock(val);
@@ -5717,6 +5746,9 @@ var convertValueToType = (field, val) => {
5717
5746
  case "number": {
5718
5747
  const v = Number(val);
5719
5748
  if (Number.isNaN(v)) {
5749
+ if (field.isOptional && !required) {
5750
+ return;
5751
+ }
5720
5752
  throw new Error("Invalid number");
5721
5753
  }
5722
5754
  return v;
@@ -5731,16 +5763,22 @@ var convertValueToType = (field, val) => {
5731
5763
  } else if (v === "false") {
5732
5764
  return false;
5733
5765
  } else {
5766
+ if (field.isOptional && !required) {
5767
+ return;
5768
+ }
5734
5769
  throw new Error("Invalid boolean");
5735
5770
  }
5736
5771
  }
5737
5772
  case "date":
5738
- return parseLLMFriendlyDate(field, val);
5773
+ return parseLLMFriendlyDate(field, val, required);
5739
5774
  case "datetime":
5740
- return parseLLMFriendlyDateTime(field, val);
5775
+ return parseLLMFriendlyDateTime(field, val, required);
5741
5776
  case "class":
5742
5777
  const className = val;
5743
5778
  if (field.type.classes && !field.type.classes.includes(className)) {
5779
+ if (field.isOptional) {
5780
+ return;
5781
+ }
5744
5782
  throw new Error(
5745
5783
  `Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
5746
5784
  );
@@ -5776,11 +5814,11 @@ function* yieldDelta(content, field, s, e, xstate) {
5776
5814
  }
5777
5815
  }
5778
5816
  function* streamValues(sig, content, values, xstate) {
5779
- if (xstate.prevField && !xstate.prevField.field.isInternal) {
5780
- const { field, s, e } = xstate.prevField;
5817
+ for (const prevField of xstate.prevFields ?? []) {
5818
+ const { field, s, e } = prevField;
5781
5819
  yield* yieldDelta(content, field, s, e, xstate);
5782
- xstate.prevField = void 0;
5783
5820
  }
5821
+ xstate.prevFields = void 0;
5784
5822
  if (!xstate.currField || xstate.currField.isInternal) {
5785
5823
  return;
5786
5824
  }
@@ -5861,7 +5899,7 @@ function validateAndParseFieldValue(field, fieldValue) {
5861
5899
  for (const [index, item] of value.entries()) {
5862
5900
  if (item !== void 0) {
5863
5901
  const v = typeof item === "string" ? item.trim() : item;
5864
- value[index] = convertValueToType(field, v);
5902
+ value[index] = convertValueToType(field, v, true);
5865
5903
  }
5866
5904
  }
5867
5905
  } else {
@@ -6486,7 +6524,11 @@ var AxGen = class extends AxProgramWithSignature {
6486
6524
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
6487
6525
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
6488
6526
  const debug = options.debug ?? ai.getOptions().debug;
6489
- const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, debug);
6527
+ const memOptions = {
6528
+ debug: options.debug,
6529
+ debugHideSystemPrompt: options.debugHideSystemPrompt
6530
+ };
6531
+ const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
6490
6532
  let err;
6491
6533
  if (options?.functions && options.functions.length > 0) {
6492
6534
  const promptTemplate = this.options?.promptTemplate ?? AxPromptTemplate;
@@ -11273,6 +11315,7 @@ var AxRAG = class extends AxChainOfThought {
11273
11315
  AxAIDeepSeekModel,
11274
11316
  AxAIGoogleGemini,
11275
11317
  AxAIGoogleGeminiEmbedModel,
11318
+ AxAIGoogleGeminiEmbedTypes,
11276
11319
  AxAIGoogleGeminiModel,
11277
11320
  AxAIGoogleGeminiSafetyCategory,
11278
11321
  AxAIGoogleGeminiSafetyThreshold,