@langchain/core 0.1.56 → 0.1.58

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.
@@ -32,7 +32,7 @@ class AIMessage extends base_js_1.BaseMessage {
32
32
  initParams = fields;
33
33
  const rawToolCalls = initParams.additional_kwargs?.tool_calls;
34
34
  const toolCalls = initParams.tool_calls;
35
- if (rawToolCalls !== undefined &&
35
+ if (!(rawToolCalls == null) &&
36
36
  rawToolCalls.length > 0 &&
37
37
  (toolCalls === undefined || toolCalls.length === 0)) {
38
38
  console.warn([
@@ -43,7 +43,7 @@ class AIMessage extends base_js_1.BaseMessage {
43
43
  ].join(" "));
44
44
  }
45
45
  try {
46
- if (rawToolCalls !== undefined && toolCalls === undefined) {
46
+ if (!(rawToolCalls == null) && toolCalls === undefined) {
47
47
  const [toolCalls, invalidToolCalls] = (0, tool_js_1.defaultToolCallParser)(rawToolCalls);
48
48
  initParams.tool_calls = toolCalls ?? [];
49
49
  initParams.invalid_tool_calls = invalidToolCalls ?? [];
@@ -29,7 +29,7 @@ export class AIMessage extends BaseMessage {
29
29
  initParams = fields;
30
30
  const rawToolCalls = initParams.additional_kwargs?.tool_calls;
31
31
  const toolCalls = initParams.tool_calls;
32
- if (rawToolCalls !== undefined &&
32
+ if (!(rawToolCalls == null) &&
33
33
  rawToolCalls.length > 0 &&
34
34
  (toolCalls === undefined || toolCalls.length === 0)) {
35
35
  console.warn([
@@ -40,7 +40,7 @@ export class AIMessage extends BaseMessage {
40
40
  ].join(" "));
41
41
  }
42
42
  try {
43
- if (rawToolCalls !== undefined && toolCalls === undefined) {
43
+ if (!(rawToolCalls == null) && toolCalls === undefined) {
44
44
  const [toolCalls, invalidToolCalls] = defaultToolCallParser(rawToolCalls);
45
45
  initParams.tool_calls = toolCalls ?? [];
46
46
  initParams.invalid_tool_calls = invalidToolCalls ?? [];
@@ -174,8 +174,10 @@ class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
174
174
  async format(values) {
175
175
  return new index_js_1.ChatMessage(await this.prompt.format(values), this.role);
176
176
  }
177
- static fromTemplate(template, role) {
178
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template), role);
177
+ static fromTemplate(template, role, options) {
178
+ return new this(prompt_js_1.PromptTemplate.fromTemplate(template, {
179
+ templateFormat: options?.templateFormat,
180
+ }), role);
179
181
  }
180
182
  }
181
183
  exports.ChatMessagePromptTemplate = ChatMessagePromptTemplate;
@@ -286,7 +288,7 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
286
288
  }
287
289
  static fromTemplate(template, additionalOptions) {
288
290
  if (typeof template === "string") {
289
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template));
291
+ return new this(prompt_js_1.PromptTemplate.fromTemplate(template, additionalOptions));
290
292
  }
291
293
  const prompt = [];
292
294
  for (const item of template) {
@@ -446,7 +448,7 @@ function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
446
448
  return (typeof baseMessagePromptTemplateLike
447
449
  .formatMessages === "function");
448
450
  }
449
- function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
451
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike, extra) {
450
452
  if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) ||
451
453
  (0, index_js_1.isBaseMessage)(messagePromptTemplateLike)) {
452
454
  return messagePromptTemplateLike;
@@ -477,21 +479,21 @@ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
477
479
  return { image_url: item.image_url };
478
480
  }
479
481
  else {
480
- throw new Error("Invalid message content");
482
+ return item;
481
483
  }
482
484
  });
483
485
  }
484
486
  if (message._getType() === "human") {
485
- return HumanMessagePromptTemplate.fromTemplate(templateData);
487
+ return HumanMessagePromptTemplate.fromTemplate(templateData, extra);
486
488
  }
487
489
  else if (message._getType() === "ai") {
488
- return AIMessagePromptTemplate.fromTemplate(templateData);
490
+ return AIMessagePromptTemplate.fromTemplate(templateData, extra);
489
491
  }
490
492
  else if (message._getType() === "system") {
491
- return SystemMessagePromptTemplate.fromTemplate(templateData);
493
+ return SystemMessagePromptTemplate.fromTemplate(templateData, extra);
492
494
  }
493
495
  else if (index_js_1.ChatMessage.isInstance(message)) {
494
- return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
496
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role, extra);
495
497
  }
496
498
  else {
497
499
  throw new Error(`Could not coerce message prompt template from input. Received message type: "${message._getType()}".`);
@@ -540,6 +542,17 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
540
542
  writable: true,
541
543
  value: true
542
544
  });
545
+ Object.defineProperty(this, "templateFormat", {
546
+ enumerable: true,
547
+ configurable: true,
548
+ writable: true,
549
+ value: "f-string"
550
+ });
551
+ // If input is mustache and validateTemplate is not defined, set it to false
552
+ if (input.templateFormat === "mustache" &&
553
+ input.validateTemplate === undefined) {
554
+ this.validateTemplate = false;
555
+ }
543
556
  Object.assign(this, input);
544
557
  if (this.validateTemplate) {
545
558
  const inputVariablesMessages = new Set();
@@ -641,11 +654,8 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
641
654
  };
642
655
  return new ChatPromptTemplate(promptDict);
643
656
  }
644
- /**
645
- * Load prompt template from a template f-string
646
- */
647
- static fromTemplate(template) {
648
- const prompt = prompt_js_1.PromptTemplate.fromTemplate(template);
657
+ static fromTemplate(template, options) {
658
+ const prompt = prompt_js_1.PromptTemplate.fromTemplate(template, options);
649
659
  const humanTemplate = new HumanMessagePromptTemplate({ prompt });
650
660
  return this.fromMessages([humanTemplate]);
651
661
  }
@@ -660,7 +670,9 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
660
670
  // eslint-disable-next-line no-instanceof/no-instanceof
661
671
  promptMessage instanceof ChatPromptTemplate
662
672
  ? promptMessage.promptMessages
663
- : [_coerceMessagePromptTemplateLike(promptMessage)]), []);
673
+ : [
674
+ _coerceMessagePromptTemplateLike(promptMessage, extra),
675
+ ]), []);
664
676
  const flattenedPartialVariables = promptMessages.reduce((acc, promptMessage) =>
665
677
  // eslint-disable-next-line no-instanceof/no-instanceof
666
678
  promptMessage instanceof ChatPromptTemplate
@@ -683,6 +695,7 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
683
695
  inputVariables: [...inputVariables],
684
696
  promptMessages: flattenedMessages,
685
697
  partialVariables: flattenedPartialVariables,
698
+ templateFormat: extra?.templateFormat,
686
699
  });
687
700
  }
688
701
  /** @deprecated Renamed to .fromMessages */
@@ -5,8 +5,9 @@ import type { InputValues, PartialValues } from "../utils/types/index.js";
5
5
  import { Runnable } from "../runnables/base.js";
6
6
  import { BaseStringPromptTemplate } from "./string.js";
7
7
  import { BasePromptTemplate, type BasePromptTemplateInput, type TypedPromptInputValues } from "./base.js";
8
- import { type ParamsFromFString } from "./prompt.js";
8
+ import { PromptTemplateInput, ExtractedFStringParams } from "./prompt.js";
9
9
  import { ImagePromptTemplate } from "./image.js";
10
+ import { TemplateFormat } from "./template.js";
10
11
  /**
11
12
  * Abstract class that serves as a base for creating message prompt
12
13
  * templates. It defines how to format messages for different roles in a
@@ -96,7 +97,9 @@ export declare class ChatMessagePromptTemplate<RunInput extends InputValues = an
96
97
  constructor(prompt: BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>>, role: string);
97
98
  constructor(fields: ChatMessagePromptTemplateFields<InputValues<Extract<keyof RunInput, string>>>);
98
99
  format(values: RunInput): Promise<BaseMessage>;
99
- static fromTemplate(template: string, role: string): ChatMessagePromptTemplate<any>;
100
+ static fromTemplate(template: string, role: string, options?: {
101
+ templateFormat?: TemplateFormat;
102
+ }): ChatMessagePromptTemplate<any>;
100
103
  }
101
104
  interface _TextTemplateParam {
102
105
  text?: string | Record<string, any>;
@@ -106,21 +109,24 @@ interface _ImageTemplateParam {
106
109
  }
107
110
  type MessageClass = typeof HumanMessage | typeof AIMessage | typeof SystemMessage;
108
111
  type ChatMessageClass = typeof ChatMessage;
112
+ interface _StringImageMessagePromptTemplateOptions<Format extends TemplateFormat = TemplateFormat> extends Record<string, unknown> {
113
+ templateFormat?: Format;
114
+ }
109
115
  declare class _StringImageMessagePromptTemplate<RunInput extends InputValues = any, RunOutput extends BaseMessage[] = BaseMessage[]> extends BaseMessagePromptTemplate<RunInput, RunOutput> {
110
116
  lc_namespace: string[];
111
117
  lc_serializable: boolean;
112
118
  inputVariables: Array<Extract<keyof RunInput, string>>;
113
- additionalOptions: Record<string, unknown>;
119
+ additionalOptions: _StringImageMessagePromptTemplateOptions;
114
120
  prompt: BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | Array<BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | ImagePromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | MessageStringPromptTemplateFields<InputValues<Extract<keyof RunInput, string>>>>;
115
121
  protected messageClass?: MessageClass;
116
122
  static _messageClass(): MessageClass;
117
123
  protected chatMessageClass?: ChatMessageClass;
118
124
  constructor(
119
125
  /** @TODO When we come up with a better way to type prompt templates, fix this */
120
- fields: any, additionalOptions?: Record<string, unknown>);
126
+ fields: any, additionalOptions?: _StringImageMessagePromptTemplateOptions);
121
127
  createMessage(content: MessageContent): any;
122
128
  getRoleFromMessageClass(name: string): "human" | "ai" | "system" | "chat";
123
- static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam>, additionalOptions?: Record<string, unknown>): _StringImageMessagePromptTemplate<any, BaseMessage[]>;
129
+ static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam>, additionalOptions?: _StringImageMessagePromptTemplateOptions): _StringImageMessagePromptTemplate<any, BaseMessage[]>;
124
130
  format(input: TypedPromptInputValues<RunInput>): Promise<BaseMessage>;
125
131
  formatMessages(values: RunInput): Promise<RunOutput>;
126
132
  }
@@ -182,6 +188,11 @@ export interface ChatPromptTemplateInput<RunInput extends InputValues = any, Par
182
188
  * @defaultValue `true`
183
189
  */
184
190
  validateTemplate?: boolean;
191
+ /**
192
+ * The formatting method to use on the prompt.
193
+ * @default "f-string"
194
+ */
195
+ templateFormat?: TemplateFormat;
185
196
  }
186
197
  export type BaseMessagePromptTemplateLike = BaseMessagePromptTemplate | BaseMessageLike;
187
198
  /**
@@ -205,6 +216,7 @@ export declare class ChatPromptTemplate<RunInput extends InputValues = any, Part
205
216
  get lc_aliases(): Record<string, string>;
206
217
  promptMessages: Array<BaseMessagePromptTemplate | BaseMessage>;
207
218
  validateTemplate: boolean;
219
+ templateFormat: TemplateFormat;
208
220
  constructor(input: ChatPromptTemplateInput<RunInput, PartialVariableName>);
209
221
  _getPromptType(): "chat";
210
222
  private _parseImagePrompts;
@@ -213,7 +225,9 @@ export declare class ChatPromptTemplate<RunInput extends InputValues = any, Part
213
225
  /**
214
226
  * Load prompt template from a template f-string
215
227
  */
216
- static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T): ChatPromptTemplate<RunInput extends Symbol ? ParamsFromFString<T> : RunInput, any>;
228
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "f-string">, "template" | "inputVariables">): ChatPromptTemplate<ExtractedFStringParams<T, RunInput>>;
229
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): ChatPromptTemplate<ExtractedFStringParams<T, RunInput>>;
230
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "mustache">, "template" | "inputVariables">): ChatPromptTemplate<InputValues>;
217
231
  /**
218
232
  * Create a chat model-specific prompt from individual chat messages
219
233
  * or message-like tuples.
@@ -5,7 +5,7 @@ import { ChatPromptValue, } from "../prompt_values.js";
5
5
  import { Runnable } from "../runnables/base.js";
6
6
  import { BaseStringPromptTemplate } from "./string.js";
7
7
  import { BasePromptTemplate, } from "./base.js";
8
- import { PromptTemplate } from "./prompt.js";
8
+ import { PromptTemplate, } from "./prompt.js";
9
9
  import { ImagePromptTemplate } from "./image.js";
10
10
  import { parseFString } from "./template.js";
11
11
  /**
@@ -167,8 +167,10 @@ export class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
167
167
  async format(values) {
168
168
  return new ChatMessage(await this.prompt.format(values), this.role);
169
169
  }
170
- static fromTemplate(template, role) {
171
- return new this(PromptTemplate.fromTemplate(template), role);
170
+ static fromTemplate(template, role, options) {
171
+ return new this(PromptTemplate.fromTemplate(template, {
172
+ templateFormat: options?.templateFormat,
173
+ }), role);
172
174
  }
173
175
  }
174
176
  class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
@@ -278,7 +280,7 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
278
280
  }
279
281
  static fromTemplate(template, additionalOptions) {
280
282
  if (typeof template === "string") {
281
- return new this(PromptTemplate.fromTemplate(template));
283
+ return new this(PromptTemplate.fromTemplate(template, additionalOptions));
282
284
  }
283
285
  const prompt = [];
284
286
  for (const item of template) {
@@ -435,7 +437,7 @@ function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
435
437
  return (typeof baseMessagePromptTemplateLike
436
438
  .formatMessages === "function");
437
439
  }
438
- function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
440
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike, extra) {
439
441
  if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) ||
440
442
  isBaseMessage(messagePromptTemplateLike)) {
441
443
  return messagePromptTemplateLike;
@@ -466,21 +468,21 @@ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
466
468
  return { image_url: item.image_url };
467
469
  }
468
470
  else {
469
- throw new Error("Invalid message content");
471
+ return item;
470
472
  }
471
473
  });
472
474
  }
473
475
  if (message._getType() === "human") {
474
- return HumanMessagePromptTemplate.fromTemplate(templateData);
476
+ return HumanMessagePromptTemplate.fromTemplate(templateData, extra);
475
477
  }
476
478
  else if (message._getType() === "ai") {
477
- return AIMessagePromptTemplate.fromTemplate(templateData);
479
+ return AIMessagePromptTemplate.fromTemplate(templateData, extra);
478
480
  }
479
481
  else if (message._getType() === "system") {
480
- return SystemMessagePromptTemplate.fromTemplate(templateData);
482
+ return SystemMessagePromptTemplate.fromTemplate(templateData, extra);
481
483
  }
482
484
  else if (ChatMessage.isInstance(message)) {
483
- return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
485
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role, extra);
484
486
  }
485
487
  else {
486
488
  throw new Error(`Could not coerce message prompt template from input. Received message type: "${message._getType()}".`);
@@ -529,6 +531,17 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
529
531
  writable: true,
530
532
  value: true
531
533
  });
534
+ Object.defineProperty(this, "templateFormat", {
535
+ enumerable: true,
536
+ configurable: true,
537
+ writable: true,
538
+ value: "f-string"
539
+ });
540
+ // If input is mustache and validateTemplate is not defined, set it to false
541
+ if (input.templateFormat === "mustache" &&
542
+ input.validateTemplate === undefined) {
543
+ this.validateTemplate = false;
544
+ }
532
545
  Object.assign(this, input);
533
546
  if (this.validateTemplate) {
534
547
  const inputVariablesMessages = new Set();
@@ -630,11 +643,8 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
630
643
  };
631
644
  return new ChatPromptTemplate(promptDict);
632
645
  }
633
- /**
634
- * Load prompt template from a template f-string
635
- */
636
- static fromTemplate(template) {
637
- const prompt = PromptTemplate.fromTemplate(template);
646
+ static fromTemplate(template, options) {
647
+ const prompt = PromptTemplate.fromTemplate(template, options);
638
648
  const humanTemplate = new HumanMessagePromptTemplate({ prompt });
639
649
  return this.fromMessages([humanTemplate]);
640
650
  }
@@ -649,7 +659,9 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
649
659
  // eslint-disable-next-line no-instanceof/no-instanceof
650
660
  promptMessage instanceof ChatPromptTemplate
651
661
  ? promptMessage.promptMessages
652
- : [_coerceMessagePromptTemplateLike(promptMessage)]), []);
662
+ : [
663
+ _coerceMessagePromptTemplateLike(promptMessage, extra),
664
+ ]), []);
653
665
  const flattenedPartialVariables = promptMessages.reduce((acc, promptMessage) =>
654
666
  // eslint-disable-next-line no-instanceof/no-instanceof
655
667
  promptMessage instanceof ChatPromptTemplate
@@ -672,6 +684,7 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
672
684
  inputVariables: [...inputVariables],
673
685
  promptMessages: flattenedMessages,
674
686
  partialVariables: flattenedPartialVariables,
687
+ templateFormat: extra?.templateFormat,
675
688
  });
676
689
  }
677
690
  /** @deprecated Renamed to .fromMessages */
@@ -44,8 +44,16 @@ class PromptTemplate extends string_js_1.BaseStringPromptTemplate {
44
44
  writable: true,
45
45
  value: true
46
46
  });
47
+ // If input is mustache and validateTemplate is not defined, set it to false
48
+ if (input.templateFormat === "mustache" &&
49
+ input.validateTemplate === undefined) {
50
+ this.validateTemplate = false;
51
+ }
47
52
  Object.assign(this, input);
48
53
  if (this.validateTemplate) {
54
+ if (this.templateFormat === "mustache") {
55
+ throw new Error("Mustache templates cannot be validated.");
56
+ }
49
57
  let totalInputVariables = this.inputVariables;
50
58
  if (this.partialVariables) {
51
59
  totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
@@ -85,10 +93,8 @@ class PromptTemplate extends string_js_1.BaseStringPromptTemplate {
85
93
  template,
86
94
  });
87
95
  }
88
- /**
89
- * Load prompt template from a template f-string
90
- */
91
- static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
96
+ static fromTemplate(template, options) {
97
+ const { templateFormat = "f-string", ...rest } = options ?? {};
92
98
  const names = new Set();
93
99
  (0, template_js_1.parseTemplate)(template, templateFormat).forEach((node) => {
94
100
  if (node.type === "variable") {
@@ -8,17 +8,15 @@ import { MessageContent } from "../messages/index.js";
8
8
  * Inputs to create a {@link PromptTemplate}
9
9
  * @augments BasePromptTemplateInput
10
10
  */
11
- export interface PromptTemplateInput<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BasePromptTemplateInput<RunInput, PartialVariableName> {
11
+ export interface PromptTemplateInput<RunInput extends InputValues = any, PartialVariableName extends string = any, Format extends TemplateFormat = TemplateFormat> extends BasePromptTemplateInput<RunInput, PartialVariableName> {
12
12
  /**
13
13
  * The prompt template
14
14
  */
15
15
  template: MessageContent;
16
16
  /**
17
- * The format of the prompt template. Options are 'f-string'
18
- *
19
- * @defaultValue 'f-string'
17
+ * The format of the prompt template. Options are "f-string" and "mustache"
20
18
  */
21
- templateFormat?: TemplateFormat;
19
+ templateFormat?: Format;
22
20
  /**
23
21
  * Whether or not to try validating the template on initialization
24
22
  *
@@ -36,6 +34,7 @@ type ExtractTemplateParamsRecursive<T extends string, Result extends string[] =
36
34
  export type ParamsFromFString<T extends string> = {
37
35
  [Key in ExtractTemplateParamsRecursive<T>[number] | (string & Record<never, never>)]: string;
38
36
  };
37
+ export type ExtractedFStringParams<T extends string, RunInput extends InputValues = Symbol> = RunInput extends Symbol ? ParamsFromFString<T> : RunInput;
39
38
  /**
40
39
  * Schema to represent a basic prompt for an LLM.
41
40
  * @augments BasePromptTemplate
@@ -81,7 +80,9 @@ export declare class PromptTemplate<RunInput extends InputValues = any, PartialV
81
80
  /**
82
81
  * Load prompt template from a template f-string
83
82
  */
84
- static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, { templateFormat, ...rest }?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): PromptTemplate<RunInput extends Symbol ? ParamsFromFString<T> : RunInput, any>;
83
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "f-string">, "template" | "inputVariables">): PromptTemplate<ExtractedFStringParams<T, RunInput>>;
84
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): PromptTemplate<ExtractedFStringParams<T, RunInput>>;
85
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, options?: Omit<PromptTemplateInput<RunInput, string, "mustache">, "template" | "inputVariables">): PromptTemplate<InputValues>;
85
86
  /**
86
87
  * Partially applies values to the prompt template.
87
88
  * @param values The values to be partially applied to the prompt template.
@@ -41,8 +41,16 @@ export class PromptTemplate extends BaseStringPromptTemplate {
41
41
  writable: true,
42
42
  value: true
43
43
  });
44
+ // If input is mustache and validateTemplate is not defined, set it to false
45
+ if (input.templateFormat === "mustache" &&
46
+ input.validateTemplate === undefined) {
47
+ this.validateTemplate = false;
48
+ }
44
49
  Object.assign(this, input);
45
50
  if (this.validateTemplate) {
51
+ if (this.templateFormat === "mustache") {
52
+ throw new Error("Mustache templates cannot be validated.");
53
+ }
46
54
  let totalInputVariables = this.inputVariables;
47
55
  if (this.partialVariables) {
48
56
  totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
@@ -82,10 +90,8 @@ export class PromptTemplate extends BaseStringPromptTemplate {
82
90
  template,
83
91
  });
84
92
  }
85
- /**
86
- * Load prompt template from a template f-string
87
- */
88
- static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
93
+ static fromTemplate(template, options) {
94
+ const { templateFormat = "f-string", ...rest } = options ?? {};
89
95
  const names = new Set();
90
96
  parseTemplate(template, templateFormat).forEach((node) => {
91
97
  if (node.type === "variable") {
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkValidTemplate = exports.parseTemplate = exports.renderTemplate = exports.DEFAULT_PARSER_MAPPING = exports.DEFAULT_FORMATTER_MAPPING = exports.interpolateFString = exports.parseFString = void 0;
6
+ exports.checkValidTemplate = exports.parseTemplate = exports.renderTemplate = exports.DEFAULT_PARSER_MAPPING = exports.DEFAULT_FORMATTER_MAPPING = exports.interpolateMustache = exports.interpolateFString = exports.parseMustache = exports.parseFString = void 0;
7
+ const mustache_1 = __importDefault(require("mustache"));
4
8
  const parseFString = (template) => {
5
9
  // Core logic replicated from internals of pythons built in Formatter class.
6
10
  // https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706
@@ -50,21 +54,49 @@ const parseFString = (template) => {
50
54
  return nodes;
51
55
  };
52
56
  exports.parseFString = parseFString;
57
+ /**
58
+ * Convert the result of mustache.parse into an array of ParsedTemplateNode,
59
+ * to make it compatible with other LangChain string parsing template formats.
60
+ *
61
+ * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.
62
+ * @returns {ParsedTemplateNode[]}
63
+ */
64
+ const mustacheTemplateToNodes = (template) => template.map((temp) => {
65
+ if (temp[0] === "name") {
66
+ const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
67
+ return { type: "variable", name };
68
+ }
69
+ else if (temp[0] === "#") {
70
+ return { type: "variable", name: temp[1] };
71
+ }
72
+ else {
73
+ return { type: "literal", text: temp[1] };
74
+ }
75
+ });
76
+ const parseMustache = (template) => {
77
+ const parsed = mustache_1.default.parse(template);
78
+ return mustacheTemplateToNodes(parsed);
79
+ };
80
+ exports.parseMustache = parseMustache;
53
81
  const interpolateFString = (template, values) => (0, exports.parseFString)(template).reduce((res, node) => {
54
82
  if (node.type === "variable") {
55
83
  if (node.name in values) {
56
84
  return res + values[node.name];
57
85
  }
58
- throw new Error(`Missing value for input ${node.name}`);
86
+ throw new Error(`(f-string) Missing value for input ${node.name}`);
59
87
  }
60
88
  return res + node.text;
61
89
  }, "");
62
90
  exports.interpolateFString = interpolateFString;
91
+ const interpolateMustache = (template, values) => mustache_1.default.render(template, values);
92
+ exports.interpolateMustache = interpolateMustache;
63
93
  exports.DEFAULT_FORMATTER_MAPPING = {
64
94
  "f-string": exports.interpolateFString,
95
+ mustache: exports.interpolateMustache,
65
96
  };
66
97
  exports.DEFAULT_PARSER_MAPPING = {
67
98
  "f-string": exports.parseFString,
99
+ mustache: exports.parseMustache,
68
100
  };
69
101
  const renderTemplate = (template, templateFormat, inputValues) => exports.DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
70
102
  exports.renderTemplate = renderTemplate;
@@ -1,23 +1,29 @@
1
1
  import { MessageContent } from "../messages/index.js";
2
2
  import type { InputValues } from "../utils/types/index.js";
3
3
  /**
4
- * Type that specifies the format of a template. Only
5
- * "f-string" is supported currently.
4
+ * Type that specifies the format of a template.
6
5
  */
7
- export type TemplateFormat = "f-string";
6
+ export type TemplateFormat = "f-string" | "mustache";
8
7
  /**
9
8
  * Type that represents a node in a parsed format string. It can be either
10
9
  * a literal text or a variable name.
11
10
  */
12
- export type ParsedFStringNode = {
11
+ export type ParsedTemplateNode = {
13
12
  type: "literal";
14
13
  text: string;
15
14
  } | {
16
15
  type: "variable";
17
16
  name: string;
18
17
  };
19
- export declare const parseFString: (template: string) => ParsedFStringNode[];
18
+ /**
19
+ * Alias for `ParsedTemplateNode` since it is the same for
20
+ * both f-string and mustache templates.
21
+ */
22
+ export type ParsedFStringNode = ParsedTemplateNode;
23
+ export declare const parseFString: (template: string) => ParsedTemplateNode[];
24
+ export declare const parseMustache: (template: string) => ParsedTemplateNode[];
20
25
  export declare const interpolateFString: (template: string, values: InputValues) => string;
26
+ export declare const interpolateMustache: (template: string, values: InputValues) => string;
21
27
  /**
22
28
  * Type that represents a function that takes a template string and a set
23
29
  * of input values, and returns a string where all variables in the
@@ -26,12 +32,12 @@ export declare const interpolateFString: (template: string, values: InputValues)
26
32
  type Interpolator = (template: string, values: InputValues) => string;
27
33
  /**
28
34
  * Type that represents a function that takes a template string and
29
- * returns an array of `ParsedFStringNode`.
35
+ * returns an array of `ParsedTemplateNode`.
30
36
  */
31
- type Parser = (template: string) => ParsedFStringNode[];
37
+ type Parser = (template: string) => ParsedTemplateNode[];
32
38
  export declare const DEFAULT_FORMATTER_MAPPING: Record<TemplateFormat, Interpolator>;
33
39
  export declare const DEFAULT_PARSER_MAPPING: Record<TemplateFormat, Parser>;
34
40
  export declare const renderTemplate: (template: string, templateFormat: TemplateFormat, inputValues: InputValues) => string;
35
- export declare const parseTemplate: (template: string, templateFormat: TemplateFormat) => ParsedFStringNode[];
41
+ export declare const parseTemplate: (template: string, templateFormat: TemplateFormat) => ParsedTemplateNode[];
36
42
  export declare const checkValidTemplate: (template: MessageContent, templateFormat: TemplateFormat, inputVariables: string[]) => void;
37
43
  export {};
@@ -1,3 +1,4 @@
1
+ import mustache from "mustache";
1
2
  export const parseFString = (template) => {
2
3
  // Core logic replicated from internals of pythons built in Formatter class.
3
4
  // https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706
@@ -46,20 +47,46 @@ export const parseFString = (template) => {
46
47
  }
47
48
  return nodes;
48
49
  };
50
+ /**
51
+ * Convert the result of mustache.parse into an array of ParsedTemplateNode,
52
+ * to make it compatible with other LangChain string parsing template formats.
53
+ *
54
+ * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.
55
+ * @returns {ParsedTemplateNode[]}
56
+ */
57
+ const mustacheTemplateToNodes = (template) => template.map((temp) => {
58
+ if (temp[0] === "name") {
59
+ const name = temp[1].includes(".") ? temp[1].split(".")[0] : temp[1];
60
+ return { type: "variable", name };
61
+ }
62
+ else if (temp[0] === "#") {
63
+ return { type: "variable", name: temp[1] };
64
+ }
65
+ else {
66
+ return { type: "literal", text: temp[1] };
67
+ }
68
+ });
69
+ export const parseMustache = (template) => {
70
+ const parsed = mustache.parse(template);
71
+ return mustacheTemplateToNodes(parsed);
72
+ };
49
73
  export const interpolateFString = (template, values) => parseFString(template).reduce((res, node) => {
50
74
  if (node.type === "variable") {
51
75
  if (node.name in values) {
52
76
  return res + values[node.name];
53
77
  }
54
- throw new Error(`Missing value for input ${node.name}`);
78
+ throw new Error(`(f-string) Missing value for input ${node.name}`);
55
79
  }
56
80
  return res + node.text;
57
81
  }, "");
82
+ export const interpolateMustache = (template, values) => mustache.render(template, values);
58
83
  export const DEFAULT_FORMATTER_MAPPING = {
59
84
  "f-string": interpolateFString,
85
+ mustache: interpolateMustache,
60
86
  };
61
87
  export const DEFAULT_PARSER_MAPPING = {
62
88
  "f-string": parseFString,
89
+ mustache: parseMustache,
63
90
  };
64
91
  export const renderTemplate = (template, templateFormat, inputValues) => DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
65
92
  export const parseTemplate = (template, templateFormat) => DEFAULT_PARSER_MAPPING[templateFormat](template);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.56",
3
+ "version": "0.1.58",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -46,6 +46,7 @@
46
46
  "js-tiktoken": "^1.0.8",
47
47
  "langsmith": "~0.1.7",
48
48
  "ml-distance": "^4.0.0",
49
+ "mustache": "^4.2.0",
49
50
  "p-queue": "^6.6.2",
50
51
  "p-retry": "4",
51
52
  "uuid": "^9.0.0",
@@ -57,6 +58,7 @@
57
58
  "@langchain/scripts": "~0.0",
58
59
  "@swc/core": "^1.3.90",
59
60
  "@swc/jest": "^0.2.29",
61
+ "@types/mustache": "^4",
60
62
  "dpdm": "^3.12.0",
61
63
  "eslint": "^8.33.0",
62
64
  "eslint-config-airbnb-base": "^15.0.0",