@digipair/skill-llm 0.8.37 → 0.8.40

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.
Files changed (5) hide show
  1. package/chat.cjs.js +310 -21
  2. package/chat.esm.js +308 -19
  3. package/index.cjs2.js +45144 -21071
  4. package/index.esm2.js +45144 -21071
  5. package/package.json +1 -1
package/chat.cjs.js CHANGED
@@ -2,6 +2,146 @@
2
2
 
3
3
  var index = require('./index.cjs2.js');
4
4
 
5
+ function _assert_this_initialized$2(self) {
6
+ if (self === void 0) {
7
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
8
+ }
9
+ return self;
10
+ }
11
+ function _class_call_check$2(instance, Constructor) {
12
+ if (!(instance instanceof Constructor)) {
13
+ throw new TypeError("Cannot call a class as a function");
14
+ }
15
+ }
16
+ function _defineProperties$2(target, props) {
17
+ for(var i = 0; i < props.length; i++){
18
+ var descriptor = props[i];
19
+ descriptor.enumerable = descriptor.enumerable || false;
20
+ descriptor.configurable = true;
21
+ if ("value" in descriptor) descriptor.writable = true;
22
+ Object.defineProperty(target, descriptor.key, descriptor);
23
+ }
24
+ }
25
+ function _create_class$2(Constructor, protoProps, staticProps) {
26
+ if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
27
+ if (staticProps) _defineProperties$2(Constructor, staticProps);
28
+ return Constructor;
29
+ }
30
+ function _get_prototype_of$2(o) {
31
+ _get_prototype_of$2 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
32
+ return o.__proto__ || Object.getPrototypeOf(o);
33
+ };
34
+ return _get_prototype_of$2(o);
35
+ }
36
+ function _inherits$2(subClass, superClass) {
37
+ if (typeof superClass !== "function" && superClass !== null) {
38
+ throw new TypeError("Super expression must either be null or a function");
39
+ }
40
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
41
+ constructor: {
42
+ value: subClass,
43
+ writable: true,
44
+ configurable: true
45
+ }
46
+ });
47
+ if (superClass) _set_prototype_of$2(subClass, superClass);
48
+ }
49
+ function _possible_constructor_return$2(self, call) {
50
+ if (call && (_type_of$2(call) === "object" || typeof call === "function")) {
51
+ return call;
52
+ }
53
+ return _assert_this_initialized$2(self);
54
+ }
55
+ function _set_prototype_of$2(o, p) {
56
+ _set_prototype_of$2 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
57
+ o.__proto__ = p;
58
+ return o;
59
+ };
60
+ return _set_prototype_of$2(o, p);
61
+ }
62
+ function _type_of$2(obj) {
63
+ "@swc/helpers - typeof";
64
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
65
+ }
66
+ function _is_native_reflect_construct$2() {
67
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
68
+ if (Reflect.construct.sham) return false;
69
+ if (typeof Proxy === "function") return true;
70
+ try {
71
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
72
+ return true;
73
+ } catch (e) {
74
+ return false;
75
+ }
76
+ }
77
+ function _create_super$2(Derived) {
78
+ var hasNativeReflectConstruct = _is_native_reflect_construct$2();
79
+ return function _createSuperInternal() {
80
+ var Super = _get_prototype_of$2(Derived), result;
81
+ if (hasNativeReflectConstruct) {
82
+ var NewTarget = _get_prototype_of$2(this).constructor;
83
+ result = Reflect.construct(Super, arguments, NewTarget);
84
+ } else {
85
+ result = Super.apply(this, arguments);
86
+ }
87
+ return _possible_constructor_return$2(this, result);
88
+ };
89
+ }
90
+ /**
91
+ * Represents a chat message in a conversation.
92
+ */ var ChatMessage = /*#__PURE__*/ function(BaseMessage) {
93
+ _inherits$2(ChatMessage, BaseMessage);
94
+ var _super = _create_super$2(ChatMessage);
95
+ function ChatMessage(fields, role) {
96
+ _class_call_check$2(this, ChatMessage);
97
+ var _this;
98
+ if (typeof fields === "string") {
99
+ // eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
100
+ fields = {
101
+ content: fields,
102
+ role: role
103
+ };
104
+ }
105
+ _this = _super.call(this, fields);
106
+ Object.defineProperty(_assert_this_initialized$2(_this), "role", {
107
+ enumerable: true,
108
+ configurable: true,
109
+ writable: true,
110
+ value: void 0
111
+ });
112
+ _this.role = fields.role;
113
+ return _this;
114
+ }
115
+ _create_class$2(ChatMessage, [
116
+ {
117
+ key: "_getType",
118
+ value: function _getType() {
119
+ return "generic";
120
+ }
121
+ }
122
+ ], [
123
+ {
124
+ key: "lc_name",
125
+ value: function lc_name() {
126
+ return "ChatMessage";
127
+ }
128
+ },
129
+ {
130
+ key: "_chatMessageClass",
131
+ value: function _chatMessageClass() {
132
+ return ChatMessage;
133
+ }
134
+ },
135
+ {
136
+ key: "isInstance",
137
+ value: function isInstance(message) {
138
+ return message._getType() === "generic";
139
+ }
140
+ }
141
+ ]);
142
+ return ChatMessage;
143
+ }(index.BaseMessage);
144
+
5
145
  function _array_like_to_array$1(arr, len) {
6
146
  if (len == null || len > arr.length) len = arr.length;
7
147
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
@@ -868,6 +1008,101 @@ function _ts_values(o) {
868
1008
  ]);
869
1009
  return BaseMessagePromptTemplate;
870
1010
  }(index.Runnable);
1011
+ /**
1012
+ * Class that represents a placeholder for messages in a chat prompt. It
1013
+ * extends the BaseMessagePromptTemplate.
1014
+ */ var MessagesPlaceholder = /*#__PURE__*/ function(BaseMessagePromptTemplate) {
1015
+ _inherits(MessagesPlaceholder, BaseMessagePromptTemplate);
1016
+ var _super = _create_super(MessagesPlaceholder);
1017
+ function MessagesPlaceholder(fields) {
1018
+ _class_call_check(this, MessagesPlaceholder);
1019
+ var _this;
1020
+ if (typeof fields === "string") {
1021
+ // eslint-disable-next-line no-param-reassign
1022
+ fields = {
1023
+ variableName: fields
1024
+ };
1025
+ }
1026
+ _this = _super.call(this, fields);
1027
+ Object.defineProperty(_assert_this_initialized(_this), "variableName", {
1028
+ enumerable: true,
1029
+ configurable: true,
1030
+ writable: true,
1031
+ value: void 0
1032
+ });
1033
+ Object.defineProperty(_assert_this_initialized(_this), "optional", {
1034
+ enumerable: true,
1035
+ configurable: true,
1036
+ writable: true,
1037
+ value: void 0
1038
+ });
1039
+ _this.variableName = fields.variableName;
1040
+ var _fields_optional;
1041
+ _this.optional = (_fields_optional = fields.optional) !== null && _fields_optional !== void 0 ? _fields_optional : false;
1042
+ return _this;
1043
+ }
1044
+ _create_class(MessagesPlaceholder, [
1045
+ {
1046
+ key: "inputVariables",
1047
+ get: function get() {
1048
+ return [
1049
+ this.variableName
1050
+ ];
1051
+ }
1052
+ },
1053
+ {
1054
+ key: "validateInputOrThrow",
1055
+ value: function validateInputOrThrow(input, variableName) {
1056
+ if (this.optional && !input) {
1057
+ return false;
1058
+ } else if (!input) {
1059
+ var error = new Error('Error: Field "'.concat(variableName, '" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: undefined'));
1060
+ error.name = "InputFormatError";
1061
+ throw error;
1062
+ }
1063
+ var isInputBaseMessage = false;
1064
+ if (Array.isArray(input)) {
1065
+ isInputBaseMessage = input.every(function(message) {
1066
+ return index.isBaseMessage(message);
1067
+ });
1068
+ } else {
1069
+ isInputBaseMessage = index.isBaseMessage(input);
1070
+ }
1071
+ if (!isInputBaseMessage) {
1072
+ var readableInput = typeof input === "string" ? input : JSON.stringify(input, null, 2);
1073
+ var error1 = new Error('Error: Field "'.concat(variableName, '" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: ').concat(readableInput));
1074
+ error1.name = "InputFormatError";
1075
+ throw error1;
1076
+ }
1077
+ return true;
1078
+ }
1079
+ },
1080
+ {
1081
+ key: "formatMessages",
1082
+ value: function formatMessages(values) {
1083
+ var _this = this;
1084
+ return _async_to_generator(function() {
1085
+ var _values_this_variableName;
1086
+ return _ts_generator(this, function(_state) {
1087
+ _this.validateInputOrThrow(values[_this.variableName], _this.variableName);
1088
+ return [
1089
+ 2,
1090
+ (_values_this_variableName = values[_this.variableName]) !== null && _values_this_variableName !== void 0 ? _values_this_variableName : []
1091
+ ];
1092
+ });
1093
+ })();
1094
+ }
1095
+ }
1096
+ ], [
1097
+ {
1098
+ key: "lc_name",
1099
+ value: function lc_name() {
1100
+ return "MessagesPlaceholder";
1101
+ }
1102
+ }
1103
+ ]);
1104
+ return MessagesPlaceholder;
1105
+ }(BaseMessagePromptTemplate);
871
1106
  /**
872
1107
  * Abstract class that serves as a base for creating message string prompt
873
1108
  * templates. It extends the BaseMessagePromptTemplate.
@@ -1023,7 +1258,7 @@ function _ts_values(o) {
1023
1258
  return _ts_generator(this, function(_state) {
1024
1259
  switch(_state.label){
1025
1260
  case 0:
1026
- _ = index.ChatMessage.bind;
1261
+ _ = ChatMessage.bind;
1027
1262
  return [
1028
1263
  4,
1029
1264
  _this.prompt.format(values)
@@ -1031,7 +1266,7 @@ function _ts_values(o) {
1031
1266
  case 1:
1032
1267
  return [
1033
1268
  2,
1034
- new (_.apply(index.ChatMessage, [
1269
+ new (_.apply(ChatMessage, [
1035
1270
  void 0,
1036
1271
  _state.sent(),
1037
1272
  _this.role
@@ -1051,8 +1286,10 @@ function _ts_values(o) {
1051
1286
  },
1052
1287
  {
1053
1288
  key: "fromTemplate",
1054
- value: function fromTemplate(template, role) {
1055
- return new this(index.PromptTemplate.fromTemplate(template), role);
1289
+ value: function fromTemplate(template, role, options) {
1290
+ return new this(index.PromptTemplate.fromTemplate(template, {
1291
+ templateFormat: options === null || options === void 0 ? void 0 : options.templateFormat
1292
+ }), role);
1056
1293
  }
1057
1294
  }
1058
1295
  ]);
@@ -1360,7 +1597,7 @@ var _StringImageMessagePromptTemplate = /*#__PURE__*/ function(BaseMessagePrompt
1360
1597
  key: "fromTemplate",
1361
1598
  value: function fromTemplate(template, additionalOptions) {
1362
1599
  if (typeof template === "string") {
1363
- return new this(index.PromptTemplate.fromTemplate(template));
1600
+ return new this(index.PromptTemplate.fromTemplate(template, additionalOptions));
1364
1601
  }
1365
1602
  var prompt = [];
1366
1603
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
@@ -1375,14 +1612,19 @@ var _StringImageMessagePromptTemplate = /*#__PURE__*/ function(BaseMessagePrompt
1375
1612
  var _item_text;
1376
1613
  text = (_item_text = item.text) !== null && _item_text !== void 0 ? _item_text : "";
1377
1614
  }
1378
- prompt.push(index.PromptTemplate.fromTemplate(text));
1615
+ prompt.push(index.PromptTemplate.fromTemplate(text, additionalOptions));
1379
1616
  } else if (typeof item === "object" && "image_url" in item) {
1380
1617
  var _item_image_url;
1381
1618
  var imgTemplate = (_item_image_url = item.image_url) !== null && _item_image_url !== void 0 ? _item_image_url : "";
1382
1619
  var imgTemplateObject = void 0;
1383
1620
  var inputVariables = [];
1384
1621
  if (typeof imgTemplate === "string") {
1385
- var parsedTemplate = index.parseFString(imgTemplate);
1622
+ var parsedTemplate = void 0;
1623
+ if ((additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat) === "mustache") {
1624
+ parsedTemplate = index.parseMustache(imgTemplate);
1625
+ } else {
1626
+ parsedTemplate = index.parseFString(imgTemplate);
1627
+ }
1386
1628
  var variables = parsedTemplate.flatMap(function(item) {
1387
1629
  return item.type === "variable" ? [
1388
1630
  item.name
@@ -1408,7 +1650,12 @@ var _StringImageMessagePromptTemplate = /*#__PURE__*/ function(BaseMessagePrompt
1408
1650
  });
1409
1651
  } else if (typeof imgTemplate === "object") {
1410
1652
  if ("url" in imgTemplate) {
1411
- var parsedTemplate1 = index.parseFString(imgTemplate.url);
1653
+ var parsedTemplate1 = void 0;
1654
+ if ((additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat) === "mustache") {
1655
+ parsedTemplate1 = index.parseMustache(imgTemplate.url);
1656
+ } else {
1657
+ parsedTemplate1 = index.parseFString(imgTemplate.url);
1658
+ }
1412
1659
  inputVariables = parsedTemplate1.flatMap(function(item) {
1413
1660
  return item.type === "variable" ? [
1414
1661
  item.name
@@ -1551,19 +1798,49 @@ var _StringImageMessagePromptTemplate = /*#__PURE__*/ function(BaseMessagePrompt
1551
1798
  function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
1552
1799
  return typeof baseMessagePromptTemplateLike.formatMessages === "function";
1553
1800
  }
1554
- function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
1801
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike, extra) {
1555
1802
  if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) || index.isBaseMessage(messagePromptTemplateLike)) {
1556
1803
  return messagePromptTemplateLike;
1557
1804
  }
1805
+ if (Array.isArray(messagePromptTemplateLike) && messagePromptTemplateLike[0] === "placeholder") {
1806
+ var messageContent = messagePromptTemplateLike[1];
1807
+ if (typeof messageContent !== "string" || messageContent[0] !== "{" || messageContent[messageContent.length - 1] !== "}") {
1808
+ throw new Error('Invalid placeholder template: "'.concat(messagePromptTemplateLike[1], '". Expected a variable name surrounded by curly braces.'));
1809
+ }
1810
+ var variableName = messageContent.slice(1, -1);
1811
+ return new MessagesPlaceholder({
1812
+ variableName: variableName,
1813
+ optional: true
1814
+ });
1815
+ }
1558
1816
  var message = index.coerceMessageLikeToMessage(messagePromptTemplateLike);
1817
+ var templateData;
1818
+ if (typeof message.content === "string") {
1819
+ templateData = message.content;
1820
+ } else {
1821
+ // Assuming message.content is an array of complex objects, transform it.
1822
+ templateData = message.content.map(function(item) {
1823
+ if ("text" in item) {
1824
+ return {
1825
+ text: item.text
1826
+ };
1827
+ } else if ("image_url" in item) {
1828
+ return {
1829
+ image_url: item.image_url
1830
+ };
1831
+ } else {
1832
+ return item;
1833
+ }
1834
+ });
1835
+ }
1559
1836
  if (message._getType() === "human") {
1560
- return HumanMessagePromptTemplate.fromTemplate(message.content);
1837
+ return HumanMessagePromptTemplate.fromTemplate(templateData, extra);
1561
1838
  } else if (message._getType() === "ai") {
1562
- return AIMessagePromptTemplate.fromTemplate(message.content);
1839
+ return AIMessagePromptTemplate.fromTemplate(templateData, extra);
1563
1840
  } else if (message._getType() === "system") {
1564
- return SystemMessagePromptTemplate.fromTemplate(message.content);
1565
- } else if (index.ChatMessage.isInstance(message)) {
1566
- return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
1841
+ return SystemMessagePromptTemplate.fromTemplate(templateData, extra);
1842
+ } else if (ChatMessage.isInstance(message)) {
1843
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role, extra);
1567
1844
  } else {
1568
1845
  throw new Error('Could not coerce message prompt template from input. Received message type: "'.concat(message._getType(), '".'));
1569
1846
  }
@@ -1606,6 +1883,16 @@ function isMessagesPlaceholder(x) {
1606
1883
  writable: true,
1607
1884
  value: true
1608
1885
  });
1886
+ Object.defineProperty(_assert_this_initialized(_this), "templateFormat", {
1887
+ enumerable: true,
1888
+ configurable: true,
1889
+ writable: true,
1890
+ value: "f-string"
1891
+ });
1892
+ // If input is mustache and validateTemplate is not defined, set it to false
1893
+ if (input.templateFormat === "mustache" && input.validateTemplate === undefined) {
1894
+ _this.validateTemplate = false;
1895
+ }
1609
1896
  Object.assign(_assert_this_initialized(_this), input);
1610
1897
  if (_this.validateTemplate) {
1611
1898
  var inputVariablesMessages = new Set();
@@ -1685,6 +1972,7 @@ function isMessagesPlaceholder(x) {
1685
1972
  {
1686
1973
  key: "_parseImagePrompts",
1687
1974
  value: function _parseImagePrompts(message, inputValues) {
1975
+ var _this = this;
1688
1976
  return _async_to_generator(function() {
1689
1977
  var formattedMessageContent;
1690
1978
  return _ts_generator(this, function(_state) {
@@ -1716,7 +2004,9 @@ function isMessagesPlaceholder(x) {
1716
2004
  } else {
1717
2005
  imageUrl = item.image_url.url;
1718
2006
  }
1719
- promptTemplatePlaceholder = index.PromptTemplate.fromTemplate(imageUrl);
2007
+ promptTemplatePlaceholder = index.PromptTemplate.fromTemplate(imageUrl, {
2008
+ templateFormat: _this.templateFormat
2009
+ });
1720
2010
  return [
1721
2011
  4,
1722
2012
  promptTemplatePlaceholder.format(inputValues)
@@ -1916,10 +2206,8 @@ function isMessagesPlaceholder(x) {
1916
2206
  },
1917
2207
  {
1918
2208
  key: "fromTemplate",
1919
- value: /**
1920
- * Load prompt template from a template f-string
1921
- */ function fromTemplate(template) {
1922
- var prompt = index.PromptTemplate.fromTemplate(template);
2209
+ value: function fromTemplate(template, options) {
2210
+ var prompt = index.PromptTemplate.fromTemplate(template, options);
1923
2211
  var humanTemplate = new HumanMessagePromptTemplate({
1924
2212
  prompt: prompt
1925
2213
  });
@@ -1939,7 +2227,7 @@ function isMessagesPlaceholder(x) {
1939
2227
  var flattenedMessages = promptMessages.reduce(function(acc, promptMessage) {
1940
2228
  return acc.concat(// eslint-disable-next-line no-instanceof/no-instanceof
1941
2229
  _instanceof(promptMessage, ChatPromptTemplate) ? promptMessage.promptMessages : [
1942
- _coerceMessagePromptTemplateLike(promptMessage)
2230
+ _coerceMessagePromptTemplateLike(promptMessage, extra)
1943
2231
  ]);
1944
2232
  }, []);
1945
2233
  var flattenedPartialVariables = promptMessages.reduce(function(acc, promptMessage) {
@@ -1994,7 +2282,8 @@ function isMessagesPlaceholder(x) {
1994
2282
  return new this(_object_spread_props(_object_spread({}, extra), {
1995
2283
  inputVariables: _to_consumable_array(inputVariables),
1996
2284
  promptMessages: flattenedMessages,
1997
- partialVariables: flattenedPartialVariables
2285
+ partialVariables: flattenedPartialVariables,
2286
+ templateFormat: extra === null || extra === void 0 ? void 0 : extra.templateFormat
1998
2287
  }));
1999
2288
  }
2000
2289
  },