@langchain/core 0.3.45 → 0.3.46

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.
@@ -12,6 +12,7 @@ const prompt_js_1 = require("./prompt.cjs");
12
12
  const image_js_1 = require("./image.cjs");
13
13
  const template_js_1 = require("./template.cjs");
14
14
  const index_js_2 = require("../errors/index.cjs");
15
+ const dict_js_1 = require("./dict.cjs");
15
16
  /**
16
17
  * Abstract class that serves as a base for creating message prompt
17
18
  * templates. It defines how to format messages for different roles in a
@@ -188,6 +189,25 @@ class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
188
189
  }
189
190
  }
190
191
  exports.ChatMessagePromptTemplate = ChatMessagePromptTemplate;
192
+ function isTextTemplateParam(param) {
193
+ if (param === null || typeof param !== "object" || Array.isArray(param)) {
194
+ return false;
195
+ }
196
+ return (Object.keys(param).length === 1 &&
197
+ "text" in param &&
198
+ typeof param.text === "string");
199
+ }
200
+ function isImageTemplateParam(param) {
201
+ if (param === null || typeof param !== "object" || Array.isArray(param)) {
202
+ return false;
203
+ }
204
+ return ("image_url" in param &&
205
+ (typeof param.image_url === "string" ||
206
+ (typeof param.image_url === "object" &&
207
+ param.image_url !== null &&
208
+ "url" in param.image_url &&
209
+ typeof param.image_url.url === "string")));
210
+ }
191
211
  class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
192
212
  static _messageClass() {
193
213
  throw new Error("Can not invoke _messageClass from inside _StringImageMessagePromptTemplate");
@@ -299,24 +319,25 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
299
319
  }
300
320
  const prompt = [];
301
321
  for (const item of template) {
302
- if (typeof item === "string" ||
303
- (typeof item === "object" && "text" in item)) {
322
+ // handle string cases
323
+ if (typeof item === "string") {
324
+ prompt.push(prompt_js_1.PromptTemplate.fromTemplate(item, additionalOptions));
325
+ }
326
+ else if (item === null) {
327
+ // pass
328
+ }
329
+ else if (isTextTemplateParam(item)) {
304
330
  let text = "";
305
- if (typeof item === "string") {
306
- text = item;
307
- }
308
- else if (typeof item.text === "string") {
331
+ if (typeof item.text === "string") {
309
332
  text = item.text ?? "";
310
333
  }
311
334
  const options = {
312
335
  ...additionalOptions,
313
- ...(typeof item !== "string"
314
- ? { additionalContentFields: item }
315
- : {}),
336
+ additionalContentFields: item,
316
337
  };
317
338
  prompt.push(prompt_js_1.PromptTemplate.fromTemplate(text, options));
318
339
  }
319
- else if (typeof item === "object" && "image_url" in item) {
340
+ else if (isImageTemplateParam(item)) {
320
341
  let imgTemplate = item.image_url ?? "";
321
342
  let imgTemplateObject;
322
343
  let inputVariables = [];
@@ -372,6 +393,12 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
372
393
  }
373
394
  prompt.push(imgTemplateObject);
374
395
  }
396
+ else if (typeof item === "object") {
397
+ prompt.push(new dict_js_1.DictPromptTemplate({
398
+ template: item,
399
+ templateFormat: additionalOptions?.templateFormat,
400
+ }));
401
+ }
375
402
  }
376
403
  return new this({ prompt, additionalOptions });
377
404
  }
@@ -423,6 +450,19 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
423
450
  type: "image_url",
424
451
  image_url: formatted,
425
452
  });
453
+ // eslint-disable-next-line no-instanceof/no-instanceof
454
+ }
455
+ else if (prompt instanceof dict_js_1.DictPromptTemplate) {
456
+ const formatted = await prompt.format(inputs);
457
+ let additionalContentFields;
458
+ if ("additionalContentFields" in prompt) {
459
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
460
+ additionalContentFields = prompt.additionalContentFields;
461
+ }
462
+ content.push({
463
+ ...additionalContentFields,
464
+ ...formatted,
465
+ });
426
466
  }
427
467
  }
428
468
  return this.createMessage(content);
@@ -8,6 +8,7 @@ import { BasePromptTemplate, type BasePromptTemplateInput, type TypedPromptInput
8
8
  import { PromptTemplateInput, ExtractedFStringParams } from "./prompt.js";
9
9
  import { ImagePromptTemplate } from "./image.js";
10
10
  import { TemplateFormat } from "./template.js";
11
+ import { DictPromptTemplate } from "./dict.js";
11
12
  /**
12
13
  * Abstract class that serves as a base for creating message prompt
13
14
  * templates. It defines how to format messages for different roles in a
@@ -116,7 +117,7 @@ declare class _StringImageMessagePromptTemplate<RunInput extends InputValues = a
116
117
  lc_serializable: boolean;
117
118
  inputVariables: Array<Extract<keyof RunInput, string>>;
118
119
  additionalOptions: _StringImageMessagePromptTemplateOptions;
119
- 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>>>>;
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>>> | DictPromptTemplate<InputValues<Extract<keyof RunInput, string>>>>;
120
121
  protected messageClass?: MessageClass;
121
122
  static _messageClass(): MessageClass;
122
123
  protected chatMessageClass?: ChatMessageClass;
@@ -125,7 +126,7 @@ declare class _StringImageMessagePromptTemplate<RunInput extends InputValues = a
125
126
  fields: any, additionalOptions?: _StringImageMessagePromptTemplateOptions);
126
127
  createMessage(content: MessageContent): any;
127
128
  getRoleFromMessageClass(name: string): "human" | "ai" | "system" | "chat";
128
- static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam>, additionalOptions?: _StringImageMessagePromptTemplateOptions): _StringImageMessagePromptTemplate<any, BaseMessage[]>;
129
+ static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam | Record<string, unknown>>, additionalOptions?: _StringImageMessagePromptTemplateOptions): _StringImageMessagePromptTemplate<any, BaseMessage[]>;
129
130
  format(input: TypedPromptInputValues<RunInput>): Promise<BaseMessage>;
130
131
  formatMessages(values: RunInput): Promise<RunOutput>;
131
132
  }
@@ -9,6 +9,7 @@ import { PromptTemplate, } from "./prompt.js";
9
9
  import { ImagePromptTemplate } from "./image.js";
10
10
  import { parseFString, parseMustache, } from "./template.js";
11
11
  import { addLangChainErrorFields } from "../errors/index.js";
12
+ import { DictPromptTemplate } from "./dict.js";
12
13
  /**
13
14
  * Abstract class that serves as a base for creating message prompt
14
15
  * templates. It defines how to format messages for different roles in a
@@ -180,6 +181,25 @@ export class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
180
181
  }), role);
181
182
  }
182
183
  }
184
+ function isTextTemplateParam(param) {
185
+ if (param === null || typeof param !== "object" || Array.isArray(param)) {
186
+ return false;
187
+ }
188
+ return (Object.keys(param).length === 1 &&
189
+ "text" in param &&
190
+ typeof param.text === "string");
191
+ }
192
+ function isImageTemplateParam(param) {
193
+ if (param === null || typeof param !== "object" || Array.isArray(param)) {
194
+ return false;
195
+ }
196
+ return ("image_url" in param &&
197
+ (typeof param.image_url === "string" ||
198
+ (typeof param.image_url === "object" &&
199
+ param.image_url !== null &&
200
+ "url" in param.image_url &&
201
+ typeof param.image_url.url === "string")));
202
+ }
183
203
  class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
184
204
  static _messageClass() {
185
205
  throw new Error("Can not invoke _messageClass from inside _StringImageMessagePromptTemplate");
@@ -291,24 +311,25 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
291
311
  }
292
312
  const prompt = [];
293
313
  for (const item of template) {
294
- if (typeof item === "string" ||
295
- (typeof item === "object" && "text" in item)) {
314
+ // handle string cases
315
+ if (typeof item === "string") {
316
+ prompt.push(PromptTemplate.fromTemplate(item, additionalOptions));
317
+ }
318
+ else if (item === null) {
319
+ // pass
320
+ }
321
+ else if (isTextTemplateParam(item)) {
296
322
  let text = "";
297
- if (typeof item === "string") {
298
- text = item;
299
- }
300
- else if (typeof item.text === "string") {
323
+ if (typeof item.text === "string") {
301
324
  text = item.text ?? "";
302
325
  }
303
326
  const options = {
304
327
  ...additionalOptions,
305
- ...(typeof item !== "string"
306
- ? { additionalContentFields: item }
307
- : {}),
328
+ additionalContentFields: item,
308
329
  };
309
330
  prompt.push(PromptTemplate.fromTemplate(text, options));
310
331
  }
311
- else if (typeof item === "object" && "image_url" in item) {
332
+ else if (isImageTemplateParam(item)) {
312
333
  let imgTemplate = item.image_url ?? "";
313
334
  let imgTemplateObject;
314
335
  let inputVariables = [];
@@ -364,6 +385,12 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
364
385
  }
365
386
  prompt.push(imgTemplateObject);
366
387
  }
388
+ else if (typeof item === "object") {
389
+ prompt.push(new DictPromptTemplate({
390
+ template: item,
391
+ templateFormat: additionalOptions?.templateFormat,
392
+ }));
393
+ }
367
394
  }
368
395
  return new this({ prompt, additionalOptions });
369
396
  }
@@ -415,6 +442,19 @@ class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
415
442
  type: "image_url",
416
443
  image_url: formatted,
417
444
  });
445
+ // eslint-disable-next-line no-instanceof/no-instanceof
446
+ }
447
+ else if (prompt instanceof DictPromptTemplate) {
448
+ const formatted = await prompt.format(inputs);
449
+ let additionalContentFields;
450
+ if ("additionalContentFields" in prompt) {
451
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
452
+ additionalContentFields = prompt.additionalContentFields;
453
+ }
454
+ content.push({
455
+ ...additionalContentFields,
456
+ ...formatted,
457
+ });
418
458
  }
419
459
  }
420
460
  return this.createMessage(content);
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DictPromptTemplate = void 0;
4
+ const base_js_1 = require("../runnables/base.cjs");
5
+ const template_js_1 = require("./template.cjs");
6
+ class DictPromptTemplate extends base_js_1.Runnable {
7
+ constructor(fields) {
8
+ super(fields);
9
+ Object.defineProperty(this, "lc_namespace", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: ["langchain_core", "prompts", "dict"]
14
+ });
15
+ Object.defineProperty(this, "lc_serializable", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: true
20
+ });
21
+ Object.defineProperty(this, "template", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "templateFormat", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: "f-string"
32
+ });
33
+ this.template = fields.template;
34
+ this.templateFormat = fields.templateFormat ?? this.templateFormat;
35
+ }
36
+ get inputVariables() {
37
+ return _getInputVariables(this.template, this.templateFormat);
38
+ }
39
+ async format(values) {
40
+ return _insertInputVariables(this.template, values, this.templateFormat);
41
+ }
42
+ async invoke(values) {
43
+ return await this._callWithConfig(this.format.bind(this), values, {
44
+ runType: "prompt",
45
+ });
46
+ }
47
+ }
48
+ exports.DictPromptTemplate = DictPromptTemplate;
49
+ function _getInputVariables(template, templateFormat) {
50
+ const inputVariables = [];
51
+ for (const v of Object.values(template)) {
52
+ if (typeof v === "string") {
53
+ (0, template_js_1.parseTemplate)(v, templateFormat).forEach((t) => {
54
+ if (t.type === "variable") {
55
+ inputVariables.push(t.name);
56
+ }
57
+ });
58
+ }
59
+ else if (Array.isArray(v)) {
60
+ for (const x of v) {
61
+ if (typeof x === "string") {
62
+ (0, template_js_1.parseTemplate)(x, templateFormat).forEach((t) => {
63
+ if (t.type === "variable") {
64
+ inputVariables.push(t.name);
65
+ }
66
+ });
67
+ }
68
+ else if (typeof x === "object") {
69
+ inputVariables.push(..._getInputVariables(x, templateFormat));
70
+ }
71
+ }
72
+ }
73
+ else if (typeof v === "object" && v !== null) {
74
+ inputVariables.push(..._getInputVariables(v, templateFormat));
75
+ }
76
+ }
77
+ return Array.from(new Set(inputVariables));
78
+ }
79
+ function _insertInputVariables(template, inputs, templateFormat) {
80
+ const formatted = {};
81
+ for (const [k, v] of Object.entries(template)) {
82
+ if (typeof v === "string") {
83
+ formatted[k] = (0, template_js_1.renderTemplate)(v, templateFormat, inputs);
84
+ }
85
+ else if (Array.isArray(v)) {
86
+ const formattedV = [];
87
+ for (const x of v) {
88
+ if (typeof x === "string") {
89
+ formattedV.push((0, template_js_1.renderTemplate)(x, templateFormat, inputs));
90
+ }
91
+ else if (typeof x === "object") {
92
+ formattedV.push(_insertInputVariables(x, inputs, templateFormat));
93
+ }
94
+ }
95
+ formatted[k] = formattedV;
96
+ }
97
+ else if (typeof v === "object" && v !== null) {
98
+ formatted[k] = _insertInputVariables(v, inputs, templateFormat);
99
+ }
100
+ else {
101
+ formatted[k] = v;
102
+ }
103
+ }
104
+ return formatted;
105
+ }
@@ -0,0 +1,17 @@
1
+ import { Runnable } from "../runnables/base.js";
2
+ import type { InputValues } from "../utils/types/index.js";
3
+ import { TypedPromptInputValues } from "./base.js";
4
+ import { TemplateFormat } from "./template.js";
5
+ export declare class DictPromptTemplate<RunInput extends InputValues = InputValues, RunOutput extends Record<string, unknown> = Record<string, unknown>> extends Runnable<TypedPromptInputValues<RunInput>, RunOutput> {
6
+ lc_namespace: string[];
7
+ lc_serializable: boolean;
8
+ template: Record<string, unknown>;
9
+ templateFormat: TemplateFormat;
10
+ constructor(fields: {
11
+ template: Record<string, unknown>;
12
+ templateFormat?: TemplateFormat;
13
+ });
14
+ get inputVariables(): Array<Extract<keyof RunInput, string>>;
15
+ format(values: TypedPromptInputValues<RunInput>): Promise<RunOutput>;
16
+ invoke(values: TypedPromptInputValues<InputValues>): Promise<RunOutput>;
17
+ }
@@ -0,0 +1,101 @@
1
+ import { Runnable } from "../runnables/base.js";
2
+ import { parseTemplate, renderTemplate } from "./template.js";
3
+ export class DictPromptTemplate extends Runnable {
4
+ constructor(fields) {
5
+ super(fields);
6
+ Object.defineProperty(this, "lc_namespace", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: ["langchain_core", "prompts", "dict"]
11
+ });
12
+ Object.defineProperty(this, "lc_serializable", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: true
17
+ });
18
+ Object.defineProperty(this, "template", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "templateFormat", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: "f-string"
29
+ });
30
+ this.template = fields.template;
31
+ this.templateFormat = fields.templateFormat ?? this.templateFormat;
32
+ }
33
+ get inputVariables() {
34
+ return _getInputVariables(this.template, this.templateFormat);
35
+ }
36
+ async format(values) {
37
+ return _insertInputVariables(this.template, values, this.templateFormat);
38
+ }
39
+ async invoke(values) {
40
+ return await this._callWithConfig(this.format.bind(this), values, {
41
+ runType: "prompt",
42
+ });
43
+ }
44
+ }
45
+ function _getInputVariables(template, templateFormat) {
46
+ const inputVariables = [];
47
+ for (const v of Object.values(template)) {
48
+ if (typeof v === "string") {
49
+ parseTemplate(v, templateFormat).forEach((t) => {
50
+ if (t.type === "variable") {
51
+ inputVariables.push(t.name);
52
+ }
53
+ });
54
+ }
55
+ else if (Array.isArray(v)) {
56
+ for (const x of v) {
57
+ if (typeof x === "string") {
58
+ parseTemplate(x, templateFormat).forEach((t) => {
59
+ if (t.type === "variable") {
60
+ inputVariables.push(t.name);
61
+ }
62
+ });
63
+ }
64
+ else if (typeof x === "object") {
65
+ inputVariables.push(..._getInputVariables(x, templateFormat));
66
+ }
67
+ }
68
+ }
69
+ else if (typeof v === "object" && v !== null) {
70
+ inputVariables.push(..._getInputVariables(v, templateFormat));
71
+ }
72
+ }
73
+ return Array.from(new Set(inputVariables));
74
+ }
75
+ function _insertInputVariables(template, inputs, templateFormat) {
76
+ const formatted = {};
77
+ for (const [k, v] of Object.entries(template)) {
78
+ if (typeof v === "string") {
79
+ formatted[k] = renderTemplate(v, templateFormat, inputs);
80
+ }
81
+ else if (Array.isArray(v)) {
82
+ const formattedV = [];
83
+ for (const x of v) {
84
+ if (typeof x === "string") {
85
+ formattedV.push(renderTemplate(x, templateFormat, inputs));
86
+ }
87
+ else if (typeof x === "object") {
88
+ formattedV.push(_insertInputVariables(x, inputs, templateFormat));
89
+ }
90
+ }
91
+ formatted[k] = formattedV;
92
+ }
93
+ else if (typeof v === "object" && v !== null) {
94
+ formatted[k] = _insertInputVariables(v, inputs, templateFormat);
95
+ }
96
+ else {
97
+ formatted[k] = v;
98
+ }
99
+ }
100
+ return formatted;
101
+ }
@@ -24,3 +24,4 @@ __exportStar(require("./string.cjs"), exports);
24
24
  __exportStar(require("./template.cjs"), exports);
25
25
  __exportStar(require("./image.cjs"), exports);
26
26
  __exportStar(require("./structured.cjs"), exports);
27
+ __exportStar(require("./dict.cjs"), exports);
@@ -8,3 +8,4 @@ export * from "./string.js";
8
8
  export * from "./template.js";
9
9
  export * from "./image.js";
10
10
  export * from "./structured.js";
11
+ export * from "./dict.js";
@@ -8,3 +8,4 @@ export * from "./string.js";
8
8
  export * from "./template.js";
9
9
  export * from "./image.js";
10
10
  export * from "./structured.js";
11
+ export * from "./dict.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.45",
3
+ "version": "0.3.46",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {