langchain 0.0.195 → 0.0.197-rc.0

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 (236) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,355 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FewShotChatMessagePromptTemplate = exports.FewShotPromptTemplate = void 0;
4
- const base_js_1 = require("./base.cjs");
5
- const template_js_1 = require("./template.cjs");
6
- const prompt_js_1 = require("./prompt.cjs");
7
- const chat_js_1 = require("./chat.cjs");
8
- /**
9
- * Prompt template that contains few-shot examples.
10
- * @augments BasePromptTemplate
11
- * @augments FewShotPromptTemplateInput
12
- * @example
13
- * ```typescript
14
- * const examplePrompt = PromptTemplate.fromTemplate(
15
- * "Input: {input}\nOutput: {output}",
16
- * );
17
- *
18
- * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
19
- * [
20
- * { input: "happy", output: "sad" },
21
- * { input: "tall", output: "short" },
22
- * { input: "energetic", output: "lethargic" },
23
- * { input: "sunny", output: "gloomy" },
24
- * { input: "windy", output: "calm" },
25
- * ],
26
- * new OpenAIEmbeddings(),
27
- * HNSWLib,
28
- * { k: 1 },
29
- * );
30
- *
31
- * const dynamicPrompt = new FewShotPromptTemplate({
32
- * exampleSelector,
33
- * examplePrompt,
34
- * prefix: "Give the antonym of every input",
35
- * suffix: "Input: {adjective}\nOutput:",
36
- * inputVariables: ["adjective"],
37
- * });
38
- *
39
- * // Format the dynamic prompt with the input 'rainy'
40
- * console.log(await dynamicPrompt.format({ adjective: "rainy" }));
41
- *
42
- * ```
43
- */
44
- class FewShotPromptTemplate extends base_js_1.BaseStringPromptTemplate {
45
- constructor(input) {
46
- super(input);
47
- Object.defineProperty(this, "lc_serializable", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: false
52
- });
53
- Object.defineProperty(this, "examples", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: void 0
58
- });
59
- Object.defineProperty(this, "exampleSelector", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- Object.defineProperty(this, "examplePrompt", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: void 0
70
- });
71
- Object.defineProperty(this, "suffix", {
72
- enumerable: true,
73
- configurable: true,
74
- writable: true,
75
- value: ""
76
- });
77
- Object.defineProperty(this, "exampleSeparator", {
78
- enumerable: true,
79
- configurable: true,
80
- writable: true,
81
- value: "\n\n"
82
- });
83
- Object.defineProperty(this, "prefix", {
84
- enumerable: true,
85
- configurable: true,
86
- writable: true,
87
- value: ""
88
- });
89
- Object.defineProperty(this, "templateFormat", {
90
- enumerable: true,
91
- configurable: true,
92
- writable: true,
93
- value: "f-string"
94
- });
95
- Object.defineProperty(this, "validateTemplate", {
96
- enumerable: true,
97
- configurable: true,
98
- writable: true,
99
- value: true
100
- });
101
- Object.assign(this, input);
102
- if (this.examples !== undefined && this.exampleSelector !== undefined) {
103
- throw new Error("Only one of 'examples' and 'example_selector' should be provided");
104
- }
105
- if (this.examples === undefined && this.exampleSelector === undefined) {
106
- throw new Error("One of 'examples' and 'example_selector' should be provided");
107
- }
108
- if (this.validateTemplate) {
109
- let totalInputVariables = this.inputVariables;
110
- if (this.partialVariables) {
111
- totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
112
- }
113
- (0, template_js_1.checkValidTemplate)(this.prefix + this.suffix, this.templateFormat, totalInputVariables);
114
- }
115
- }
116
- _getPromptType() {
117
- return "few_shot";
118
- }
119
- static lc_name() {
120
- return "FewShotPromptTemplate";
121
- }
122
- async getExamples(inputVariables) {
123
- if (this.examples !== undefined) {
124
- return this.examples;
125
- }
126
- if (this.exampleSelector !== undefined) {
127
- return this.exampleSelector.selectExamples(inputVariables);
128
- }
129
- throw new Error("One of 'examples' and 'example_selector' should be provided");
130
- }
131
- async partial(values) {
132
- const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
133
- const newPartialVariables = {
134
- ...(this.partialVariables ?? {}),
135
- ...values,
136
- };
137
- const promptDict = {
138
- ...this,
139
- inputVariables: newInputVariables,
140
- partialVariables: newPartialVariables,
141
- };
142
- return new FewShotPromptTemplate(promptDict);
143
- }
144
- /**
145
- * Formats the prompt with the given values.
146
- * @param values The values to format the prompt with.
147
- * @returns A promise that resolves to a string representing the formatted prompt.
148
- */
149
- async format(values) {
150
- const allValues = await this.mergePartialAndUserVariables(values);
151
- const examples = await this.getExamples(allValues);
152
- const exampleStrings = await Promise.all(examples.map((example) => this.examplePrompt.format(example)));
153
- const template = [this.prefix, ...exampleStrings, this.suffix].join(this.exampleSeparator);
154
- return (0, template_js_1.renderTemplate)(template, this.templateFormat, allValues);
155
- }
156
- serialize() {
157
- if (this.exampleSelector || !this.examples) {
158
- throw new Error("Serializing an example selector is not currently supported");
159
- }
160
- if (this.outputParser !== undefined) {
161
- throw new Error("Serializing an output parser is not currently supported");
162
- }
163
- return {
164
- _type: this._getPromptType(),
165
- input_variables: this.inputVariables,
166
- example_prompt: this.examplePrompt.serialize(),
167
- example_separator: this.exampleSeparator,
168
- suffix: this.suffix,
169
- prefix: this.prefix,
170
- template_format: this.templateFormat,
171
- examples: this.examples,
172
- };
173
- }
174
- static async deserialize(data) {
175
- const { example_prompt } = data;
176
- if (!example_prompt) {
177
- throw new Error("Missing example prompt");
178
- }
179
- const examplePrompt = await prompt_js_1.PromptTemplate.deserialize(example_prompt);
180
- let examples;
181
- if (Array.isArray(data.examples)) {
182
- examples = data.examples;
183
- }
184
- else {
185
- throw new Error("Invalid examples format. Only list or string are supported.");
186
- }
187
- return new FewShotPromptTemplate({
188
- inputVariables: data.input_variables,
189
- examplePrompt,
190
- examples,
191
- exampleSeparator: data.example_separator,
192
- prefix: data.prefix,
193
- suffix: data.suffix,
194
- templateFormat: data.template_format,
195
- });
196
- }
197
- }
198
- exports.FewShotPromptTemplate = FewShotPromptTemplate;
199
- /**
200
- * Chat prompt template that contains few-shot examples.
201
- * @augments BasePromptTemplateInput
202
- * @augments FewShotChatMessagePromptTemplateInput
203
- */
204
- class FewShotChatMessagePromptTemplate extends chat_js_1.BaseChatPromptTemplate {
205
- _getPromptType() {
206
- return "few_shot_chat";
207
- }
208
- static lc_name() {
209
- return "FewShotChatMessagePromptTemplate";
210
- }
211
- constructor(fields) {
212
- super(fields);
213
- Object.defineProperty(this, "lc_serializable", {
214
- enumerable: true,
215
- configurable: true,
216
- writable: true,
217
- value: true
218
- });
219
- Object.defineProperty(this, "examples", {
220
- enumerable: true,
221
- configurable: true,
222
- writable: true,
223
- value: void 0
224
- });
225
- Object.defineProperty(this, "exampleSelector", {
226
- enumerable: true,
227
- configurable: true,
228
- writable: true,
229
- value: void 0
230
- });
231
- Object.defineProperty(this, "examplePrompt", {
232
- enumerable: true,
233
- configurable: true,
234
- writable: true,
235
- value: void 0
236
- });
237
- Object.defineProperty(this, "suffix", {
238
- enumerable: true,
239
- configurable: true,
240
- writable: true,
241
- value: ""
242
- });
243
- Object.defineProperty(this, "exampleSeparator", {
244
- enumerable: true,
245
- configurable: true,
246
- writable: true,
247
- value: "\n\n"
248
- });
249
- Object.defineProperty(this, "prefix", {
250
- enumerable: true,
251
- configurable: true,
252
- writable: true,
253
- value: ""
254
- });
255
- Object.defineProperty(this, "templateFormat", {
256
- enumerable: true,
257
- configurable: true,
258
- writable: true,
259
- value: "f-string"
260
- });
261
- Object.defineProperty(this, "validateTemplate", {
262
- enumerable: true,
263
- configurable: true,
264
- writable: true,
265
- value: true
266
- });
267
- this.examples = fields.examples;
268
- this.examplePrompt = fields.examplePrompt;
269
- this.exampleSeparator = fields.exampleSeparator ?? "\n\n";
270
- this.exampleSelector = fields.exampleSelector;
271
- this.prefix = fields.prefix ?? "";
272
- this.suffix = fields.suffix ?? "";
273
- this.templateFormat = fields.templateFormat ?? "f-string";
274
- this.validateTemplate = fields.validateTemplate ?? true;
275
- if (this.examples !== undefined && this.exampleSelector !== undefined) {
276
- throw new Error("Only one of 'examples' and 'example_selector' should be provided");
277
- }
278
- if (this.examples === undefined && this.exampleSelector === undefined) {
279
- throw new Error("One of 'examples' and 'example_selector' should be provided");
280
- }
281
- if (this.validateTemplate) {
282
- let totalInputVariables = this.inputVariables;
283
- if (this.partialVariables) {
284
- totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
285
- }
286
- (0, template_js_1.checkValidTemplate)(this.prefix + this.suffix, this.templateFormat, totalInputVariables);
287
- }
288
- }
289
- async getExamples(inputVariables) {
290
- if (this.examples !== undefined) {
291
- return this.examples;
292
- }
293
- if (this.exampleSelector !== undefined) {
294
- return this.exampleSelector.selectExamples(inputVariables);
295
- }
296
- throw new Error("One of 'examples' and 'example_selector' should be provided");
297
- }
298
- /**
299
- * Formats the list of values and returns a list of formatted messages.
300
- * @param values The values to format the prompt with.
301
- * @returns A promise that resolves to a string representing the formatted prompt.
302
- */
303
- async formatMessages(values) {
304
- const allValues = await this.mergePartialAndUserVariables(values);
305
- let examples = await this.getExamples(allValues);
306
- examples = examples.map((example) => {
307
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
308
- const result = {};
309
- this.examplePrompt.inputVariables.forEach((inputVariable) => {
310
- result[inputVariable] = example[inputVariable];
311
- });
312
- return result;
313
- });
314
- const messages = [];
315
- for (const example of examples) {
316
- const exampleMessages = await this.examplePrompt.formatMessages(example);
317
- messages.push(...exampleMessages);
318
- }
319
- return messages;
320
- }
321
- /**
322
- * Formats the prompt with the given values.
323
- * @param values The values to format the prompt with.
324
- * @returns A promise that resolves to a string representing the formatted prompt.
325
- */
326
- async format(values) {
327
- const allValues = await this.mergePartialAndUserVariables(values);
328
- const examples = await this.getExamples(allValues);
329
- const exampleMessages = await Promise.all(examples.map((example) => this.examplePrompt.formatMessages(example)));
330
- const exampleStrings = exampleMessages
331
- .flat()
332
- .map((message) => message.content);
333
- const template = [this.prefix, ...exampleStrings, this.suffix].join(this.exampleSeparator);
334
- return (0, template_js_1.renderTemplate)(template, this.templateFormat, allValues);
335
- }
336
- /**
337
- * Partially formats the prompt with the given values.
338
- * @param values The values to partially format the prompt with.
339
- * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
340
- */
341
- async partial(values) {
342
- const newInputVariables = this.inputVariables.filter((variable) => !(variable in values));
343
- const newPartialVariables = {
344
- ...(this.partialVariables ?? {}),
345
- ...values,
346
- };
347
- const promptDict = {
348
- ...this,
349
- inputVariables: newInputVariables,
350
- partialVariables: newPartialVariables,
351
- };
352
- return new FewShotChatMessagePromptTemplate(promptDict);
353
- }
354
- }
355
- exports.FewShotChatMessagePromptTemplate = FewShotChatMessagePromptTemplate;
4
+ var prompts_1 = require("langchain-core/prompts");
5
+ Object.defineProperty(exports, "FewShotPromptTemplate", { enumerable: true, get: function () { return prompts_1.FewShotPromptTemplate; } });
6
+ Object.defineProperty(exports, "FewShotChatMessagePromptTemplate", { enumerable: true, get: function () { return prompts_1.FewShotChatMessagePromptTemplate; } });
@@ -1,192 +1 @@
1
- import { BaseStringPromptTemplate, BasePromptTemplateInput, BaseExampleSelector, TypedPromptInputValues } from "./base.js";
2
- import { TemplateFormat } from "./template.js";
3
- import { PromptTemplate } from "./prompt.js";
4
- import { SerializedFewShotTemplate } from "./serde.js";
5
- import { BaseMessage, Example, InputValues, PartialValues } from "../schema/index.js";
6
- import { BaseChatPromptTemplate, BaseMessagePromptTemplate } from "./chat.js";
7
- export interface FewShotPromptTemplateInput extends BasePromptTemplateInput<InputValues> {
8
- /**
9
- * Examples to format into the prompt. Exactly one of this or
10
- * {@link exampleSelector} must be
11
- * provided.
12
- */
13
- examples?: Example[];
14
- /**
15
- * An {@link BaseExampleSelector} Examples to format into the prompt. Exactly one of this or
16
- * {@link examples} must be
17
- * provided.
18
- */
19
- exampleSelector?: BaseExampleSelector;
20
- /**
21
- * An {@link PromptTemplate} used to format a single example.
22
- */
23
- examplePrompt: PromptTemplate;
24
- /**
25
- * String separator used to join the prefix, the examples, and suffix.
26
- */
27
- exampleSeparator?: string;
28
- /**
29
- * A prompt template string to put before the examples.
30
- *
31
- * @defaultValue `""`
32
- */
33
- prefix?: string;
34
- /**
35
- * A prompt template string to put after the examples.
36
- */
37
- suffix?: string;
38
- /**
39
- * The format of the prompt template. Options are: 'f-string'
40
- */
41
- templateFormat?: TemplateFormat;
42
- /**
43
- * Whether or not to try validating the template on initialization.
44
- */
45
- validateTemplate?: boolean;
46
- }
47
- /**
48
- * Prompt template that contains few-shot examples.
49
- * @augments BasePromptTemplate
50
- * @augments FewShotPromptTemplateInput
51
- * @example
52
- * ```typescript
53
- * const examplePrompt = PromptTemplate.fromTemplate(
54
- * "Input: {input}\nOutput: {output}",
55
- * );
56
- *
57
- * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
58
- * [
59
- * { input: "happy", output: "sad" },
60
- * { input: "tall", output: "short" },
61
- * { input: "energetic", output: "lethargic" },
62
- * { input: "sunny", output: "gloomy" },
63
- * { input: "windy", output: "calm" },
64
- * ],
65
- * new OpenAIEmbeddings(),
66
- * HNSWLib,
67
- * { k: 1 },
68
- * );
69
- *
70
- * const dynamicPrompt = new FewShotPromptTemplate({
71
- * exampleSelector,
72
- * examplePrompt,
73
- * prefix: "Give the antonym of every input",
74
- * suffix: "Input: {adjective}\nOutput:",
75
- * inputVariables: ["adjective"],
76
- * });
77
- *
78
- * // Format the dynamic prompt with the input 'rainy'
79
- * console.log(await dynamicPrompt.format({ adjective: "rainy" }));
80
- *
81
- * ```
82
- */
83
- export declare class FewShotPromptTemplate extends BaseStringPromptTemplate implements FewShotPromptTemplateInput {
84
- lc_serializable: boolean;
85
- examples?: InputValues[];
86
- exampleSelector?: BaseExampleSelector | undefined;
87
- examplePrompt: PromptTemplate;
88
- suffix: string;
89
- exampleSeparator: string;
90
- prefix: string;
91
- templateFormat: TemplateFormat;
92
- validateTemplate: boolean;
93
- constructor(input: FewShotPromptTemplateInput);
94
- _getPromptType(): "few_shot";
95
- static lc_name(): string;
96
- private getExamples;
97
- partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<FewShotPromptTemplate>;
98
- /**
99
- * Formats the prompt with the given values.
100
- * @param values The values to format the prompt with.
101
- * @returns A promise that resolves to a string representing the formatted prompt.
102
- */
103
- format(values: InputValues): Promise<string>;
104
- serialize(): SerializedFewShotTemplate;
105
- static deserialize(data: SerializedFewShotTemplate): Promise<FewShotPromptTemplate>;
106
- }
107
- export interface FewShotChatMessagePromptTemplateInput extends BasePromptTemplateInput<InputValues> {
108
- /**
109
- * Examples to format into the prompt. Exactly one of this or
110
- * {@link exampleSelector} must be
111
- * provided.
112
- */
113
- examples?: Example[];
114
- /**
115
- * An {@link BaseMessagePromptTemplate} | {@link BaseChatPromptTemplate} used to format a single example.
116
- */
117
- examplePrompt: BaseMessagePromptTemplate | BaseChatPromptTemplate;
118
- /**
119
- * String separator used to join the prefix, the examples, and suffix.
120
- *
121
- * @defaultValue `"\n\n"`
122
- */
123
- exampleSeparator?: string;
124
- /**
125
- * An {@link BaseExampleSelector} Examples to format into the prompt. Exactly one of this or
126
- * {@link examples} must be
127
- * provided.
128
- */
129
- exampleSelector?: BaseExampleSelector | undefined;
130
- /**
131
- * A prompt template string to put before the examples.
132
- *
133
- * @defaultValue `""`
134
- */
135
- prefix?: string;
136
- /**
137
- * A prompt template string to put after the examples.
138
- *
139
- * @defaultValue `""`
140
- */
141
- suffix?: string;
142
- /**
143
- * The format of the prompt template. Options are: 'f-string'
144
- *
145
- * @defaultValue `f-string`
146
- */
147
- templateFormat?: TemplateFormat;
148
- /**
149
- * Whether or not to try validating the template on initialization.
150
- *
151
- * @defaultValue `true`
152
- */
153
- validateTemplate?: boolean;
154
- }
155
- /**
156
- * Chat prompt template that contains few-shot examples.
157
- * @augments BasePromptTemplateInput
158
- * @augments FewShotChatMessagePromptTemplateInput
159
- */
160
- export declare class FewShotChatMessagePromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BaseChatPromptTemplate implements FewShotChatMessagePromptTemplateInput {
161
- lc_serializable: boolean;
162
- examples?: InputValues[];
163
- exampleSelector?: BaseExampleSelector | undefined;
164
- examplePrompt: BaseMessagePromptTemplate | BaseChatPromptTemplate;
165
- suffix: string;
166
- exampleSeparator: string;
167
- prefix: string;
168
- templateFormat: TemplateFormat;
169
- validateTemplate: boolean;
170
- _getPromptType(): "few_shot_chat";
171
- static lc_name(): string;
172
- constructor(fields: FewShotChatMessagePromptTemplateInput);
173
- private getExamples;
174
- /**
175
- * Formats the list of values and returns a list of formatted messages.
176
- * @param values The values to format the prompt with.
177
- * @returns A promise that resolves to a string representing the formatted prompt.
178
- */
179
- formatMessages(values: TypedPromptInputValues<RunInput>): Promise<BaseMessage[]>;
180
- /**
181
- * Formats the prompt with the given values.
182
- * @param values The values to format the prompt with.
183
- * @returns A promise that resolves to a string representing the formatted prompt.
184
- */
185
- format(values: TypedPromptInputValues<RunInput>): Promise<string>;
186
- /**
187
- * Partially formats the prompt with the given values.
188
- * @param values The values to partially format the prompt with.
189
- * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
190
- */
191
- partial(values: PartialValues<PartialVariableName>): Promise<FewShotChatMessagePromptTemplate<RunInput, PartialVariableName>>;
192
- }
1
+ export { type FewShotPromptTemplateInput, FewShotPromptTemplate, type FewShotChatMessagePromptTemplateInput, FewShotChatMessagePromptTemplate, } from "langchain-core/prompts";