langchain 0.0.175 → 0.0.177

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 (89) hide show
  1. package/dist/chat_models/bedrock.cjs +25 -4
  2. package/dist/chat_models/bedrock.d.ts +2 -1
  3. package/dist/chat_models/bedrock.js +25 -4
  4. package/dist/chat_models/googlevertexai/common.cjs +46 -7
  5. package/dist/chat_models/googlevertexai/common.d.ts +7 -2
  6. package/dist/chat_models/googlevertexai/common.js +47 -8
  7. package/dist/chat_models/googlevertexai/index.cjs +4 -3
  8. package/dist/chat_models/googlevertexai/index.js +4 -3
  9. package/dist/chat_models/googlevertexai/web.cjs +2 -1
  10. package/dist/chat_models/googlevertexai/web.js +2 -1
  11. package/dist/chat_models/llama_cpp.cjs +31 -79
  12. package/dist/chat_models/llama_cpp.d.ts +15 -58
  13. package/dist/chat_models/llama_cpp.js +32 -80
  14. package/dist/chat_models/openai.cjs +91 -6
  15. package/dist/chat_models/openai.d.ts +10 -0
  16. package/dist/chat_models/openai.js +91 -6
  17. package/dist/embeddings/googlevertexai.cjs +1 -1
  18. package/dist/embeddings/googlevertexai.js +1 -1
  19. package/dist/embeddings/hf.cjs +10 -1
  20. package/dist/embeddings/hf.d.ts +4 -2
  21. package/dist/embeddings/hf.js +10 -1
  22. package/dist/embeddings/llama_cpp.cjs +67 -0
  23. package/dist/embeddings/llama_cpp.d.ts +26 -0
  24. package/dist/embeddings/llama_cpp.js +63 -0
  25. package/dist/embeddings/ollama.cjs +7 -1
  26. package/dist/embeddings/ollama.js +7 -1
  27. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +2 -2
  28. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
  29. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  30. package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
  31. package/dist/experimental/plan_and_execute/agent_executor.cjs +7 -4
  32. package/dist/experimental/plan_and_execute/agent_executor.d.ts +4 -3
  33. package/dist/experimental/plan_and_execute/agent_executor.js +8 -5
  34. package/dist/experimental/plan_and_execute/prompt.cjs +25 -9
  35. package/dist/experimental/plan_and_execute/prompt.d.ts +9 -1
  36. package/dist/experimental/plan_and_execute/prompt.js +23 -8
  37. package/dist/llms/bedrock.cjs +25 -3
  38. package/dist/llms/bedrock.d.ts +2 -1
  39. package/dist/llms/bedrock.js +25 -3
  40. package/dist/llms/googlevertexai/common.cjs +46 -13
  41. package/dist/llms/googlevertexai/common.d.ts +8 -3
  42. package/dist/llms/googlevertexai/common.js +46 -13
  43. package/dist/llms/googlevertexai/index.cjs +4 -3
  44. package/dist/llms/googlevertexai/index.js +4 -3
  45. package/dist/llms/googlevertexai/web.cjs +2 -1
  46. package/dist/llms/googlevertexai/web.js +2 -1
  47. package/dist/llms/hf.cjs +10 -1
  48. package/dist/llms/hf.d.ts +3 -0
  49. package/dist/llms/hf.js +10 -1
  50. package/dist/llms/llama_cpp.cjs +25 -65
  51. package/dist/llms/llama_cpp.d.ts +7 -43
  52. package/dist/llms/llama_cpp.js +25 -65
  53. package/dist/load/import_constants.cjs +1 -0
  54. package/dist/load/import_constants.js +1 -0
  55. package/dist/prompts/few_shot.cjs +162 -1
  56. package/dist/prompts/few_shot.d.ts +90 -2
  57. package/dist/prompts/few_shot.js +160 -0
  58. package/dist/prompts/index.cjs +2 -1
  59. package/dist/prompts/index.d.ts +1 -1
  60. package/dist/prompts/index.js +1 -1
  61. package/dist/retrievers/zep.cjs +26 -3
  62. package/dist/retrievers/zep.d.ts +11 -2
  63. package/dist/retrievers/zep.js +26 -3
  64. package/dist/types/googlevertexai-types.d.ts +12 -10
  65. package/dist/util/bedrock.d.ts +2 -0
  66. package/dist/util/googlevertexai-connection.cjs +298 -10
  67. package/dist/util/googlevertexai-connection.d.ts +76 -7
  68. package/dist/util/googlevertexai-connection.js +294 -9
  69. package/dist/util/googlevertexai-gauth.cjs +36 -0
  70. package/dist/util/googlevertexai-gauth.d.ts +8 -0
  71. package/dist/util/googlevertexai-gauth.js +32 -0
  72. package/dist/util/googlevertexai-webauth.cjs +38 -2
  73. package/dist/util/googlevertexai-webauth.d.ts +2 -6
  74. package/dist/util/googlevertexai-webauth.js +38 -2
  75. package/dist/util/llama_cpp.cjs +34 -0
  76. package/dist/util/llama_cpp.d.ts +46 -0
  77. package/dist/util/llama_cpp.js +28 -0
  78. package/dist/util/openai-format-fndef.cjs +81 -0
  79. package/dist/util/openai-format-fndef.d.ts +44 -0
  80. package/dist/util/openai-format-fndef.js +77 -0
  81. package/dist/util/openapi.d.ts +2 -2
  82. package/dist/vectorstores/googlevertexai.d.ts +4 -4
  83. package/dist/vectorstores/pinecone.cjs +5 -5
  84. package/dist/vectorstores/pinecone.d.ts +2 -2
  85. package/dist/vectorstores/pinecone.js +5 -5
  86. package/embeddings/llama_cpp.cjs +1 -0
  87. package/embeddings/llama_cpp.d.ts +1 -0
  88. package/embeddings/llama_cpp.js +1 -0
  89. package/package.json +13 -5
@@ -1,4 +1,4 @@
1
- import { LlamaModel, LlamaContext, LlamaChatSession } from "node-llama-cpp";
1
+ import { createLlamaModel, createLlamaContext, createLlamaSession, } from "../util/llama_cpp.js";
2
2
  import { LLM } from "./base.js";
3
3
  /**
4
4
  * To use this model you need to have the `node-llama-cpp` module installed.
@@ -12,73 +12,31 @@ export class LlamaCpp extends LLM {
12
12
  }
13
13
  constructor(inputs) {
14
14
  super(inputs);
15
- Object.defineProperty(this, "batchSize", {
15
+ Object.defineProperty(this, "maxTokens", {
16
16
  enumerable: true,
17
17
  configurable: true,
18
18
  writable: true,
19
19
  value: void 0
20
20
  });
21
- Object.defineProperty(this, "contextSize", {
21
+ Object.defineProperty(this, "temperature", {
22
22
  enumerable: true,
23
23
  configurable: true,
24
24
  writable: true,
25
25
  value: void 0
26
26
  });
27
- Object.defineProperty(this, "embedding", {
27
+ Object.defineProperty(this, "topK", {
28
28
  enumerable: true,
29
29
  configurable: true,
30
30
  writable: true,
31
31
  value: void 0
32
32
  });
33
- Object.defineProperty(this, "f16Kv", {
33
+ Object.defineProperty(this, "topP", {
34
34
  enumerable: true,
35
35
  configurable: true,
36
36
  writable: true,
37
37
  value: void 0
38
38
  });
39
- Object.defineProperty(this, "gpuLayers", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: void 0
44
- });
45
- Object.defineProperty(this, "logitsAll", {
46
- enumerable: true,
47
- configurable: true,
48
- writable: true,
49
- value: void 0
50
- });
51
- Object.defineProperty(this, "lowVram", {
52
- enumerable: true,
53
- configurable: true,
54
- writable: true,
55
- value: void 0
56
- });
57
- Object.defineProperty(this, "seed", {
58
- enumerable: true,
59
- configurable: true,
60
- writable: true,
61
- value: void 0
62
- });
63
- Object.defineProperty(this, "useMlock", {
64
- enumerable: true,
65
- configurable: true,
66
- writable: true,
67
- value: void 0
68
- });
69
- Object.defineProperty(this, "useMmap", {
70
- enumerable: true,
71
- configurable: true,
72
- writable: true,
73
- value: void 0
74
- });
75
- Object.defineProperty(this, "vocabOnly", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: void 0
80
- });
81
- Object.defineProperty(this, "modelPath", {
39
+ Object.defineProperty(this, "trimWhitespaceSuffix", {
82
40
  enumerable: true,
83
41
  configurable: true,
84
42
  writable: true,
@@ -102,29 +60,31 @@ export class LlamaCpp extends LLM {
102
60
  writable: true,
103
61
  value: void 0
104
62
  });
105
- this.batchSize = inputs.batchSize;
106
- this.contextSize = inputs.contextSize;
107
- this.embedding = inputs.embedding;
108
- this.f16Kv = inputs.f16Kv;
109
- this.gpuLayers = inputs.gpuLayers;
110
- this.logitsAll = inputs.logitsAll;
111
- this.lowVram = inputs.lowVram;
112
- this.modelPath = inputs.modelPath;
113
- this.seed = inputs.seed;
114
- this.useMlock = inputs.useMlock;
115
- this.useMmap = inputs.useMmap;
116
- this.vocabOnly = inputs.vocabOnly;
117
- this._model = new LlamaModel(inputs);
118
- this._context = new LlamaContext({ model: this._model });
119
- this._session = new LlamaChatSession({ context: this._context });
63
+ this.maxTokens = inputs?.maxTokens;
64
+ this.temperature = inputs?.temperature;
65
+ this.topK = inputs?.topK;
66
+ this.topP = inputs?.topP;
67
+ this.trimWhitespaceSuffix = inputs?.trimWhitespaceSuffix;
68
+ this._model = createLlamaModel(inputs);
69
+ this._context = createLlamaContext(this._model, inputs);
70
+ this._session = createLlamaSession(this._context);
120
71
  }
121
72
  _llmType() {
122
73
  return "llama2_cpp";
123
74
  }
124
75
  /** @ignore */
125
- async _call(prompt, options) {
76
+ async _call(prompt,
77
+ // @ts-expect-error - TS6133: 'options' is declared but its value is never read.
78
+ options) {
126
79
  try {
127
- const completion = await this._session.prompt(prompt, options);
80
+ const promptOptions = {
81
+ maxTokens: this?.maxTokens,
82
+ temperature: this?.temperature,
83
+ topK: this?.topK,
84
+ topP: this?.topP,
85
+ trimWhitespaceSuffix: this?.trimWhitespaceSuffix,
86
+ };
87
+ const completion = await this._session.prompt(prompt, promptOptions);
128
88
  return completion;
129
89
  }
130
90
  catch (e) {
@@ -25,6 +25,7 @@ exports.optionalImportEntrypoints = [
25
25
  "langchain/embeddings/hf_transformers",
26
26
  "langchain/embeddings/googlevertexai",
27
27
  "langchain/embeddings/googlepalm",
28
+ "langchain/embeddings/llama_cpp",
28
29
  "langchain/llms/load",
29
30
  "langchain/llms/cohere",
30
31
  "langchain/llms/hf",
@@ -22,6 +22,7 @@ export const optionalImportEntrypoints = [
22
22
  "langchain/embeddings/hf_transformers",
23
23
  "langchain/embeddings/googlevertexai",
24
24
  "langchain/embeddings/googlepalm",
25
+ "langchain/embeddings/llama_cpp",
25
26
  "langchain/llms/load",
26
27
  "langchain/llms/cohere",
27
28
  "langchain/llms/hf",
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FewShotPromptTemplate = void 0;
3
+ exports.FewShotChatMessagePromptTemplate = exports.FewShotPromptTemplate = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
5
  const template_js_1 = require("./template.cjs");
6
6
  const prompt_js_1 = require("./prompt.cjs");
7
+ const chat_js_1 = require("./chat.cjs");
7
8
  /**
8
9
  * Prompt template that contains few-shot examples.
9
10
  * @augments BasePromptTemplate
@@ -84,6 +85,9 @@ class FewShotPromptTemplate extends base_js_1.BaseStringPromptTemplate {
84
85
  _getPromptType() {
85
86
  return "few_shot";
86
87
  }
88
+ static lc_name() {
89
+ return "FewShotPromptTemplate";
90
+ }
87
91
  async getExamples(inputVariables) {
88
92
  if (this.examples !== undefined) {
89
93
  return this.examples;
@@ -161,3 +165,160 @@ class FewShotPromptTemplate extends base_js_1.BaseStringPromptTemplate {
161
165
  }
162
166
  }
163
167
  exports.FewShotPromptTemplate = FewShotPromptTemplate;
168
+ /**
169
+ * Chat prompt template that contains few-shot examples.
170
+ * @augments BasePromptTemplateInput
171
+ * @augments FewShotChatMessagePromptTemplateInput
172
+ */
173
+ class FewShotChatMessagePromptTemplate extends chat_js_1.BaseChatPromptTemplate {
174
+ _getPromptType() {
175
+ return "few_shot_chat";
176
+ }
177
+ static lc_name() {
178
+ return "FewShotChatMessagePromptTemplate";
179
+ }
180
+ constructor(fields) {
181
+ super(fields);
182
+ Object.defineProperty(this, "lc_serializable", {
183
+ enumerable: true,
184
+ configurable: true,
185
+ writable: true,
186
+ value: true
187
+ });
188
+ Object.defineProperty(this, "examples", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: void 0
193
+ });
194
+ Object.defineProperty(this, "exampleSelector", {
195
+ enumerable: true,
196
+ configurable: true,
197
+ writable: true,
198
+ value: void 0
199
+ });
200
+ Object.defineProperty(this, "examplePrompt", {
201
+ enumerable: true,
202
+ configurable: true,
203
+ writable: true,
204
+ value: void 0
205
+ });
206
+ Object.defineProperty(this, "suffix", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: ""
211
+ });
212
+ Object.defineProperty(this, "exampleSeparator", {
213
+ enumerable: true,
214
+ configurable: true,
215
+ writable: true,
216
+ value: "\n\n"
217
+ });
218
+ Object.defineProperty(this, "prefix", {
219
+ enumerable: true,
220
+ configurable: true,
221
+ writable: true,
222
+ value: ""
223
+ });
224
+ Object.defineProperty(this, "templateFormat", {
225
+ enumerable: true,
226
+ configurable: true,
227
+ writable: true,
228
+ value: "f-string"
229
+ });
230
+ Object.defineProperty(this, "validateTemplate", {
231
+ enumerable: true,
232
+ configurable: true,
233
+ writable: true,
234
+ value: true
235
+ });
236
+ this.examples = fields.examples;
237
+ this.examplePrompt = fields.examplePrompt;
238
+ this.exampleSeparator = fields.exampleSeparator ?? "\n\n";
239
+ this.exampleSelector = fields.exampleSelector;
240
+ this.prefix = fields.prefix ?? "";
241
+ this.suffix = fields.suffix ?? "";
242
+ this.templateFormat = fields.templateFormat ?? "f-string";
243
+ this.validateTemplate = fields.validateTemplate ?? true;
244
+ if (this.examples !== undefined && this.exampleSelector !== undefined) {
245
+ throw new Error("Only one of 'examples' and 'example_selector' should be provided");
246
+ }
247
+ if (this.examples === undefined && this.exampleSelector === undefined) {
248
+ throw new Error("One of 'examples' and 'example_selector' should be provided");
249
+ }
250
+ if (this.validateTemplate) {
251
+ let totalInputVariables = this.inputVariables;
252
+ if (this.partialVariables) {
253
+ totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
254
+ }
255
+ (0, template_js_1.checkValidTemplate)(this.prefix + this.suffix, this.templateFormat, totalInputVariables);
256
+ }
257
+ }
258
+ async getExamples(inputVariables) {
259
+ if (this.examples !== undefined) {
260
+ return this.examples;
261
+ }
262
+ if (this.exampleSelector !== undefined) {
263
+ return this.exampleSelector.selectExamples(inputVariables);
264
+ }
265
+ throw new Error("One of 'examples' and 'example_selector' should be provided");
266
+ }
267
+ /**
268
+ * Formats the list of values and returns a list of formatted messages.
269
+ * @param values The values to format the prompt with.
270
+ * @returns A promise that resolves to a string representing the formatted prompt.
271
+ */
272
+ async formatMessages(values) {
273
+ const allValues = await this.mergePartialAndUserVariables(values);
274
+ let examples = await this.getExamples(allValues);
275
+ examples = examples.map((example) => {
276
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
277
+ const result = {};
278
+ this.examplePrompt.inputVariables.forEach((inputVariable) => {
279
+ result[inputVariable] = example[inputVariable];
280
+ });
281
+ return result;
282
+ });
283
+ const messages = [];
284
+ for (const example of examples) {
285
+ const exampleMessages = await this.examplePrompt.formatMessages(example);
286
+ messages.push(...exampleMessages);
287
+ }
288
+ return messages;
289
+ }
290
+ /**
291
+ * Formats the prompt with the given values.
292
+ * @param values The values to format the prompt with.
293
+ * @returns A promise that resolves to a string representing the formatted prompt.
294
+ */
295
+ async format(values) {
296
+ const allValues = await this.mergePartialAndUserVariables(values);
297
+ const examples = await this.getExamples(allValues);
298
+ const exampleMessages = await Promise.all(examples.map((example) => this.examplePrompt.formatMessages(example)));
299
+ const exampleStrings = exampleMessages
300
+ .flat()
301
+ .map((message) => message.content);
302
+ const template = [this.prefix, ...exampleStrings, this.suffix].join(this.exampleSeparator);
303
+ return (0, template_js_1.renderTemplate)(template, this.templateFormat, allValues);
304
+ }
305
+ /**
306
+ * Partially formats the prompt with the given values.
307
+ * @param values The values to partially format the prompt with.
308
+ * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
309
+ */
310
+ async partial(values) {
311
+ const newInputVariables = this.inputVariables.filter((variable) => !(variable in values));
312
+ const newPartialVariables = {
313
+ ...(this.partialVariables ?? {}),
314
+ ...values,
315
+ };
316
+ const promptDict = {
317
+ ...this,
318
+ inputVariables: newInputVariables,
319
+ partialVariables: newPartialVariables,
320
+ };
321
+ return new FewShotChatMessagePromptTemplate(promptDict);
322
+ }
323
+ }
324
+ exports.FewShotChatMessagePromptTemplate = FewShotChatMessagePromptTemplate;
@@ -1,8 +1,9 @@
1
- import { BaseStringPromptTemplate, BasePromptTemplateInput, BaseExampleSelector } from "./base.js";
1
+ import { BaseStringPromptTemplate, BasePromptTemplateInput, BaseExampleSelector, TypedPromptInputValues } from "./base.js";
2
2
  import { TemplateFormat } from "./template.js";
3
3
  import { PromptTemplate } from "./prompt.js";
4
4
  import { SerializedFewShotTemplate } from "./serde.js";
5
- import { Example, InputValues, PartialValues } from "../schema/index.js";
5
+ import { BaseMessage, Example, InputValues, PartialValues } from "../schema/index.js";
6
+ import { BaseChatPromptTemplate, BaseMessagePromptTemplate } from "./chat.js";
6
7
  export interface FewShotPromptTemplateInput extends BasePromptTemplateInput<InputValues> {
7
8
  /**
8
9
  * Examples to format into the prompt. Exactly one of this or
@@ -60,6 +61,7 @@ export declare class FewShotPromptTemplate extends BaseStringPromptTemplate impl
60
61
  validateTemplate: boolean;
61
62
  constructor(input: FewShotPromptTemplateInput);
62
63
  _getPromptType(): "few_shot";
64
+ static lc_name(): string;
63
65
  private getExamples;
64
66
  partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<FewShotPromptTemplate>;
65
67
  /**
@@ -71,3 +73,89 @@ export declare class FewShotPromptTemplate extends BaseStringPromptTemplate impl
71
73
  serialize(): SerializedFewShotTemplate;
72
74
  static deserialize(data: SerializedFewShotTemplate): Promise<FewShotPromptTemplate>;
73
75
  }
76
+ export interface FewShotChatMessagePromptTemplateInput extends BasePromptTemplateInput<InputValues> {
77
+ /**
78
+ * Examples to format into the prompt. Exactly one of this or
79
+ * {@link exampleSelector} must be
80
+ * provided.
81
+ */
82
+ examples?: Example[];
83
+ /**
84
+ * An {@link BaseMessagePromptTemplate} | {@link BaseChatPromptTemplate} used to format a single example.
85
+ */
86
+ examplePrompt: BaseMessagePromptTemplate | BaseChatPromptTemplate;
87
+ /**
88
+ * String separator used to join the prefix, the examples, and suffix.
89
+ *
90
+ * @defaultValue `"\n\n"`
91
+ */
92
+ exampleSeparator?: string;
93
+ /**
94
+ * An {@link BaseExampleSelector} Examples to format into the prompt. Exactly one of this or
95
+ * {@link examples} must be
96
+ * provided.
97
+ */
98
+ exampleSelector?: BaseExampleSelector | undefined;
99
+ /**
100
+ * A prompt template string to put before the examples.
101
+ *
102
+ * @defaultValue `""`
103
+ */
104
+ prefix?: string;
105
+ /**
106
+ * A prompt template string to put after the examples.
107
+ *
108
+ * @defaultValue `""`
109
+ */
110
+ suffix?: string;
111
+ /**
112
+ * The format of the prompt template. Options are: 'f-string'
113
+ *
114
+ * @defaultValue `f-string`
115
+ */
116
+ templateFormat?: TemplateFormat;
117
+ /**
118
+ * Whether or not to try validating the template on initialization.
119
+ *
120
+ * @defaultValue `true`
121
+ */
122
+ validateTemplate?: boolean;
123
+ }
124
+ /**
125
+ * Chat prompt template that contains few-shot examples.
126
+ * @augments BasePromptTemplateInput
127
+ * @augments FewShotChatMessagePromptTemplateInput
128
+ */
129
+ export declare class FewShotChatMessagePromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BaseChatPromptTemplate implements FewShotChatMessagePromptTemplateInput {
130
+ lc_serializable: boolean;
131
+ examples?: InputValues[];
132
+ exampleSelector?: BaseExampleSelector | undefined;
133
+ examplePrompt: BaseMessagePromptTemplate | BaseChatPromptTemplate;
134
+ suffix: string;
135
+ exampleSeparator: string;
136
+ prefix: string;
137
+ templateFormat: TemplateFormat;
138
+ validateTemplate: boolean;
139
+ _getPromptType(): "few_shot_chat";
140
+ static lc_name(): string;
141
+ constructor(fields: FewShotChatMessagePromptTemplateInput);
142
+ private getExamples;
143
+ /**
144
+ * Formats the list of values and returns a list of formatted messages.
145
+ * @param values The values to format the prompt with.
146
+ * @returns A promise that resolves to a string representing the formatted prompt.
147
+ */
148
+ formatMessages(values: TypedPromptInputValues<RunInput>): Promise<BaseMessage[]>;
149
+ /**
150
+ * Formats the prompt with the given values.
151
+ * @param values The values to format the prompt with.
152
+ * @returns A promise that resolves to a string representing the formatted prompt.
153
+ */
154
+ format(values: TypedPromptInputValues<RunInput>): Promise<string>;
155
+ /**
156
+ * Partially formats the prompt with the given values.
157
+ * @param values The values to partially format the prompt with.
158
+ * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
159
+ */
160
+ partial(values: PartialValues<PartialVariableName>): Promise<FewShotChatMessagePromptTemplate<RunInput, PartialVariableName>>;
161
+ }
@@ -1,6 +1,7 @@
1
1
  import { BaseStringPromptTemplate, } from "./base.js";
2
2
  import { checkValidTemplate, renderTemplate, } from "./template.js";
3
3
  import { PromptTemplate } from "./prompt.js";
4
+ import { BaseChatPromptTemplate } from "./chat.js";
4
5
  /**
5
6
  * Prompt template that contains few-shot examples.
6
7
  * @augments BasePromptTemplate
@@ -81,6 +82,9 @@ export class FewShotPromptTemplate extends BaseStringPromptTemplate {
81
82
  _getPromptType() {
82
83
  return "few_shot";
83
84
  }
85
+ static lc_name() {
86
+ return "FewShotPromptTemplate";
87
+ }
84
88
  async getExamples(inputVariables) {
85
89
  if (this.examples !== undefined) {
86
90
  return this.examples;
@@ -157,3 +161,159 @@ export class FewShotPromptTemplate extends BaseStringPromptTemplate {
157
161
  });
158
162
  }
159
163
  }
164
+ /**
165
+ * Chat prompt template that contains few-shot examples.
166
+ * @augments BasePromptTemplateInput
167
+ * @augments FewShotChatMessagePromptTemplateInput
168
+ */
169
+ export class FewShotChatMessagePromptTemplate extends BaseChatPromptTemplate {
170
+ _getPromptType() {
171
+ return "few_shot_chat";
172
+ }
173
+ static lc_name() {
174
+ return "FewShotChatMessagePromptTemplate";
175
+ }
176
+ constructor(fields) {
177
+ super(fields);
178
+ Object.defineProperty(this, "lc_serializable", {
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true,
182
+ value: true
183
+ });
184
+ Object.defineProperty(this, "examples", {
185
+ enumerable: true,
186
+ configurable: true,
187
+ writable: true,
188
+ value: void 0
189
+ });
190
+ Object.defineProperty(this, "exampleSelector", {
191
+ enumerable: true,
192
+ configurable: true,
193
+ writable: true,
194
+ value: void 0
195
+ });
196
+ Object.defineProperty(this, "examplePrompt", {
197
+ enumerable: true,
198
+ configurable: true,
199
+ writable: true,
200
+ value: void 0
201
+ });
202
+ Object.defineProperty(this, "suffix", {
203
+ enumerable: true,
204
+ configurable: true,
205
+ writable: true,
206
+ value: ""
207
+ });
208
+ Object.defineProperty(this, "exampleSeparator", {
209
+ enumerable: true,
210
+ configurable: true,
211
+ writable: true,
212
+ value: "\n\n"
213
+ });
214
+ Object.defineProperty(this, "prefix", {
215
+ enumerable: true,
216
+ configurable: true,
217
+ writable: true,
218
+ value: ""
219
+ });
220
+ Object.defineProperty(this, "templateFormat", {
221
+ enumerable: true,
222
+ configurable: true,
223
+ writable: true,
224
+ value: "f-string"
225
+ });
226
+ Object.defineProperty(this, "validateTemplate", {
227
+ enumerable: true,
228
+ configurable: true,
229
+ writable: true,
230
+ value: true
231
+ });
232
+ this.examples = fields.examples;
233
+ this.examplePrompt = fields.examplePrompt;
234
+ this.exampleSeparator = fields.exampleSeparator ?? "\n\n";
235
+ this.exampleSelector = fields.exampleSelector;
236
+ this.prefix = fields.prefix ?? "";
237
+ this.suffix = fields.suffix ?? "";
238
+ this.templateFormat = fields.templateFormat ?? "f-string";
239
+ this.validateTemplate = fields.validateTemplate ?? true;
240
+ if (this.examples !== undefined && this.exampleSelector !== undefined) {
241
+ throw new Error("Only one of 'examples' and 'example_selector' should be provided");
242
+ }
243
+ if (this.examples === undefined && this.exampleSelector === undefined) {
244
+ throw new Error("One of 'examples' and 'example_selector' should be provided");
245
+ }
246
+ if (this.validateTemplate) {
247
+ let totalInputVariables = this.inputVariables;
248
+ if (this.partialVariables) {
249
+ totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
250
+ }
251
+ checkValidTemplate(this.prefix + this.suffix, this.templateFormat, totalInputVariables);
252
+ }
253
+ }
254
+ async getExamples(inputVariables) {
255
+ if (this.examples !== undefined) {
256
+ return this.examples;
257
+ }
258
+ if (this.exampleSelector !== undefined) {
259
+ return this.exampleSelector.selectExamples(inputVariables);
260
+ }
261
+ throw new Error("One of 'examples' and 'example_selector' should be provided");
262
+ }
263
+ /**
264
+ * Formats the list of values and returns a list of formatted messages.
265
+ * @param values The values to format the prompt with.
266
+ * @returns A promise that resolves to a string representing the formatted prompt.
267
+ */
268
+ async formatMessages(values) {
269
+ const allValues = await this.mergePartialAndUserVariables(values);
270
+ let examples = await this.getExamples(allValues);
271
+ examples = examples.map((example) => {
272
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
273
+ const result = {};
274
+ this.examplePrompt.inputVariables.forEach((inputVariable) => {
275
+ result[inputVariable] = example[inputVariable];
276
+ });
277
+ return result;
278
+ });
279
+ const messages = [];
280
+ for (const example of examples) {
281
+ const exampleMessages = await this.examplePrompt.formatMessages(example);
282
+ messages.push(...exampleMessages);
283
+ }
284
+ return messages;
285
+ }
286
+ /**
287
+ * Formats the prompt with the given values.
288
+ * @param values The values to format the prompt with.
289
+ * @returns A promise that resolves to a string representing the formatted prompt.
290
+ */
291
+ async format(values) {
292
+ const allValues = await this.mergePartialAndUserVariables(values);
293
+ const examples = await this.getExamples(allValues);
294
+ const exampleMessages = await Promise.all(examples.map((example) => this.examplePrompt.formatMessages(example)));
295
+ const exampleStrings = exampleMessages
296
+ .flat()
297
+ .map((message) => message.content);
298
+ const template = [this.prefix, ...exampleStrings, this.suffix].join(this.exampleSeparator);
299
+ return renderTemplate(template, this.templateFormat, allValues);
300
+ }
301
+ /**
302
+ * Partially formats the prompt with the given values.
303
+ * @param values The values to partially format the prompt with.
304
+ * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
305
+ */
306
+ async partial(values) {
307
+ const newInputVariables = this.inputVariables.filter((variable) => !(variable in values));
308
+ const newPartialVariables = {
309
+ ...(this.partialVariables ?? {}),
310
+ ...values,
311
+ };
312
+ const promptDict = {
313
+ ...this,
314
+ inputVariables: newInputVariables,
315
+ partialVariables: newPartialVariables,
316
+ };
317
+ return new FewShotChatMessagePromptTemplate(promptDict);
318
+ }
319
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PipelinePromptTemplate = exports.checkValidTemplate = exports.renderTemplate = exports.parseTemplate = exports.BaseChatPromptTemplate = exports.MessagesPlaceholder = exports.ChatMessagePromptTemplate = exports.SystemMessagePromptTemplate = exports.AIMessagePromptTemplate = exports.HumanMessagePromptTemplate = exports.ChatPromptTemplate = exports.FewShotPromptTemplate = exports.SemanticSimilarityExampleSelector = exports.LengthBasedExampleSelector = exports.isLLM = exports.isChatModel = exports.ConditionalPromptSelector = exports.BasePromptSelector = exports.PromptTemplate = exports.BaseStringPromptTemplate = exports.StringPromptValue = exports.BasePromptTemplate = exports.BaseExampleSelector = void 0;
3
+ exports.PipelinePromptTemplate = exports.checkValidTemplate = exports.renderTemplate = exports.parseTemplate = exports.BaseChatPromptTemplate = exports.MessagesPlaceholder = exports.ChatMessagePromptTemplate = exports.SystemMessagePromptTemplate = exports.AIMessagePromptTemplate = exports.HumanMessagePromptTemplate = exports.ChatPromptTemplate = exports.FewShotChatMessagePromptTemplate = exports.FewShotPromptTemplate = exports.SemanticSimilarityExampleSelector = exports.LengthBasedExampleSelector = exports.isLLM = exports.isChatModel = exports.ConditionalPromptSelector = exports.BasePromptSelector = exports.PromptTemplate = exports.BaseStringPromptTemplate = exports.StringPromptValue = exports.BasePromptTemplate = exports.BaseExampleSelector = void 0;
4
4
  var base_js_1 = require("./base.cjs");
5
5
  Object.defineProperty(exports, "BaseExampleSelector", { enumerable: true, get: function () { return base_js_1.BaseExampleSelector; } });
6
6
  Object.defineProperty(exports, "BasePromptTemplate", { enumerable: true, get: function () { return base_js_1.BasePromptTemplate; } });
@@ -19,6 +19,7 @@ var SemanticSimilarityExampleSelector_js_1 = require("./selectors/SemanticSimila
19
19
  Object.defineProperty(exports, "SemanticSimilarityExampleSelector", { enumerable: true, get: function () { return SemanticSimilarityExampleSelector_js_1.SemanticSimilarityExampleSelector; } });
20
20
  var few_shot_js_1 = require("./few_shot.cjs");
21
21
  Object.defineProperty(exports, "FewShotPromptTemplate", { enumerable: true, get: function () { return few_shot_js_1.FewShotPromptTemplate; } });
22
+ Object.defineProperty(exports, "FewShotChatMessagePromptTemplate", { enumerable: true, get: function () { return few_shot_js_1.FewShotChatMessagePromptTemplate; } });
22
23
  var chat_js_1 = require("./chat.cjs");
23
24
  Object.defineProperty(exports, "ChatPromptTemplate", { enumerable: true, get: function () { return chat_js_1.ChatPromptTemplate; } });
24
25
  Object.defineProperty(exports, "HumanMessagePromptTemplate", { enumerable: true, get: function () { return chat_js_1.HumanMessagePromptTemplate; } });
@@ -3,7 +3,7 @@ export { PromptTemplate, type PromptTemplateInput } from "./prompt.js";
3
3
  export { BasePromptSelector, ConditionalPromptSelector, isChatModel, isLLM, } from "./selectors/conditional.js";
4
4
  export { LengthBasedExampleSelector, type LengthBasedExampleSelectorInput, } from "./selectors/LengthBasedExampleSelector.js";
5
5
  export { SemanticSimilarityExampleSelector, type SemanticSimilarityExampleSelectorInput, } from "./selectors/SemanticSimilarityExampleSelector.js";
6
- export { FewShotPromptTemplate, type FewShotPromptTemplateInput, } from "./few_shot.js";
6
+ export { FewShotPromptTemplate, type FewShotPromptTemplateInput, type FewShotChatMessagePromptTemplateInput, FewShotChatMessagePromptTemplate, } from "./few_shot.js";
7
7
  export { ChatPromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, SystemMessagePromptTemplate, ChatMessagePromptTemplate, MessagesPlaceholder, BaseChatPromptTemplate, } from "./chat.js";
8
8
  export { type SerializedPromptTemplate, type SerializedBasePromptTemplate, type SerializedFewShotTemplate, } from "./serde.js";
9
9
  export { parseTemplate, renderTemplate, checkValidTemplate, type TemplateFormat, } from "./template.js";
@@ -3,7 +3,7 @@ export { PromptTemplate } from "./prompt.js";
3
3
  export { BasePromptSelector, ConditionalPromptSelector, isChatModel, isLLM, } from "./selectors/conditional.js";
4
4
  export { LengthBasedExampleSelector, } from "./selectors/LengthBasedExampleSelector.js";
5
5
  export { SemanticSimilarityExampleSelector, } from "./selectors/SemanticSimilarityExampleSelector.js";
6
- export { FewShotPromptTemplate, } from "./few_shot.js";
6
+ export { FewShotPromptTemplate, FewShotChatMessagePromptTemplate, } from "./few_shot.js";
7
7
  export { ChatPromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, SystemMessagePromptTemplate, ChatMessagePromptTemplate, MessagesPlaceholder, BaseChatPromptTemplate, } from "./chat.js";
8
8
  export { parseTemplate, renderTemplate, checkValidTemplate, } from "./template.js";
9
9
  export { PipelinePromptTemplate, } from "./pipeline.js";