langchain 0.0.194 → 0.0.196

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 (241) hide show
  1. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  2. package/dist/base_language/count_tokens.cjs +5 -70
  3. package/dist/base_language/count_tokens.d.ts +1 -10
  4. package/dist/base_language/count_tokens.js +1 -65
  5. package/dist/base_language/index.cjs +6 -196
  6. package/dist/base_language/index.d.ts +1 -111
  7. package/dist/base_language/index.js +1 -191
  8. package/dist/cache/base.cjs +15 -37
  9. package/dist/cache/base.d.ts +1 -20
  10. package/dist/cache/base.js +1 -33
  11. package/dist/cache/index.cjs +2 -46
  12. package/dist/cache/index.d.ts +1 -29
  13. package/dist/cache/index.js +1 -45
  14. package/dist/callbacks/base.cjs +3 -139
  15. package/dist/callbacks/base.d.ts +1 -266
  16. package/dist/callbacks/base.js +1 -126
  17. package/dist/callbacks/handlers/console.cjs +14 -221
  18. package/dist/callbacks/handlers/console.d.ts +1 -117
  19. package/dist/callbacks/handlers/console.js +1 -217
  20. package/dist/callbacks/handlers/initialize.cjs +15 -30
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  22. package/dist/callbacks/handlers/initialize.js +1 -27
  23. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  25. package/dist/callbacks/handlers/log_stream.js +1 -289
  26. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  28. package/dist/callbacks/handlers/run_collector.js +1 -46
  29. package/dist/callbacks/handlers/tracer.cjs +15 -375
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  31. package/dist/callbacks/handlers/tracer.js +1 -373
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  38. package/dist/callbacks/manager.cjs +15 -676
  39. package/dist/callbacks/manager.d.ts +1 -180
  40. package/dist/callbacks/manager.js +1 -666
  41. package/dist/callbacks/promises.cjs +14 -42
  42. package/dist/callbacks/promises.d.ts +1 -11
  43. package/dist/callbacks/promises.js +1 -37
  44. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  45. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  46. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  47. package/dist/chains/openai_functions/structured_output.js +1 -1
  48. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  49. package/dist/chat_models/base.cjs +15 -296
  50. package/dist/chat_models/base.d.ts +1 -122
  51. package/dist/chat_models/base.js +1 -292
  52. package/dist/chat_models/bedrock/web.d.ts +1 -1
  53. package/dist/chat_models/fireworks.d.ts +1 -1
  54. package/dist/document.cjs +2 -24
  55. package/dist/document.d.ts +1 -12
  56. package/dist/document.js +1 -23
  57. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  58. package/dist/document_loaders/web/s3.d.ts +1 -1
  59. package/dist/embeddings/base.cjs +15 -22
  60. package/dist/embeddings/base.d.ts +1 -33
  61. package/dist/embeddings/base.js +1 -20
  62. package/dist/embeddings/cache_backed.cjs +2 -2
  63. package/dist/embeddings/cache_backed.js +1 -1
  64. package/dist/embeddings/hf.cjs +1 -2
  65. package/dist/embeddings/hf.js +1 -2
  66. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  67. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  68. package/dist/evaluation/qa/prompt.d.ts +2 -2
  69. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  70. package/dist/experimental/openai_assistant/index.cjs +2 -1
  71. package/dist/experimental/openai_assistant/index.d.ts +2 -1
  72. package/dist/experimental/openai_assistant/index.js +2 -1
  73. package/dist/experimental/openai_files/index.cjs +88 -0
  74. package/dist/experimental/openai_files/index.d.ts +79 -0
  75. package/dist/experimental/openai_files/index.js +84 -0
  76. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  77. package/dist/llms/base.cjs +15 -278
  78. package/dist/llms/base.d.ts +1 -115
  79. package/dist/llms/base.js +1 -275
  80. package/dist/llms/bedrock/web.d.ts +1 -1
  81. package/dist/llms/fireworks.d.ts +1 -1
  82. package/dist/load/import_constants.cjs +1 -0
  83. package/dist/load/import_constants.js +1 -0
  84. package/dist/load/import_map.cjs +4 -2
  85. package/dist/load/import_map.d.ts +2 -0
  86. package/dist/load/import_map.js +2 -0
  87. package/dist/load/index.cjs +7 -148
  88. package/dist/load/index.js +7 -148
  89. package/dist/load/map_keys.cjs +0 -24
  90. package/dist/load/map_keys.d.ts +0 -6
  91. package/dist/load/map_keys.js +1 -17
  92. package/dist/load/serializable.cjs +15 -178
  93. package/dist/load/serializable.d.ts +1 -66
  94. package/dist/load/serializable.js +1 -175
  95. package/dist/memory/base.cjs +17 -92
  96. package/dist/memory/base.d.ts +2 -68
  97. package/dist/memory/base.js +2 -87
  98. package/dist/output_parsers/openai_functions.cjs +2 -2
  99. package/dist/output_parsers/openai_functions.d.ts +1 -1
  100. package/dist/output_parsers/openai_functions.js +1 -1
  101. package/dist/output_parsers/regex.d.ts +1 -1
  102. package/dist/output_parsers/structured.d.ts +1 -1
  103. package/dist/prompts/base.cjs +8 -183
  104. package/dist/prompts/base.d.ts +3 -132
  105. package/dist/prompts/base.js +3 -178
  106. package/dist/prompts/chat.cjs +15 -477
  107. package/dist/prompts/chat.d.ts +1 -219
  108. package/dist/prompts/chat.js +1 -466
  109. package/dist/prompts/few_shot.cjs +15 -353
  110. package/dist/prompts/few_shot.d.ts +1 -192
  111. package/dist/prompts/few_shot.js +1 -350
  112. package/dist/prompts/index.cjs +3 -2
  113. package/dist/prompts/index.d.ts +2 -1
  114. package/dist/prompts/index.js +2 -1
  115. package/dist/prompts/pipeline.cjs +15 -142
  116. package/dist/prompts/pipeline.d.ts +1 -98
  117. package/dist/prompts/pipeline.js +1 -140
  118. package/dist/prompts/prompt.cjs +15 -146
  119. package/dist/prompts/prompt.d.ts +1 -92
  120. package/dist/prompts/prompt.js +1 -144
  121. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
  122. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  123. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  124. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  125. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  126. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  127. package/dist/prompts/selectors/conditional.cjs +15 -74
  128. package/dist/prompts/selectors/conditional.d.ts +1 -63
  129. package/dist/prompts/selectors/conditional.js +1 -69
  130. package/dist/prompts/serde.cjs +15 -0
  131. package/dist/prompts/serde.d.ts +1 -43
  132. package/dist/prompts/serde.js +1 -1
  133. package/dist/prompts/template.cjs +14 -88
  134. package/dist/prompts/template.d.ts +1 -36
  135. package/dist/prompts/template.js +1 -83
  136. package/dist/retrievers/chaindesk.cjs +9 -1
  137. package/dist/retrievers/chaindesk.d.ts +3 -1
  138. package/dist/retrievers/chaindesk.js +9 -1
  139. package/dist/retrievers/self_query/vectara.cjs +138 -0
  140. package/dist/retrievers/self_query/vectara.d.ts +41 -0
  141. package/dist/retrievers/self_query/vectara.js +134 -0
  142. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  143. package/dist/runnables/index.d.ts +1 -0
  144. package/dist/runnables/index.js +1 -0
  145. package/dist/schema/document.cjs +3 -34
  146. package/dist/schema/document.d.ts +2 -29
  147. package/dist/schema/document.js +2 -32
  148. package/dist/schema/index.cjs +37 -612
  149. package/dist/schema/index.d.ts +11 -311
  150. package/dist/schema/index.js +8 -583
  151. package/dist/schema/output_parser.cjs +15 -309
  152. package/dist/schema/output_parser.d.ts +1 -173
  153. package/dist/schema/output_parser.js +1 -301
  154. package/dist/schema/retriever.cjs +15 -77
  155. package/dist/schema/retriever.d.ts +1 -43
  156. package/dist/schema/retriever.js +1 -75
  157. package/dist/schema/runnable/base.cjs +10 -1072
  158. package/dist/schema/runnable/base.d.ts +1 -356
  159. package/dist/schema/runnable/base.js +1 -1060
  160. package/dist/schema/runnable/branch.cjs +2 -131
  161. package/dist/schema/runnable/branch.d.ts +1 -94
  162. package/dist/schema/runnable/branch.js +1 -130
  163. package/dist/schema/runnable/config.cjs +0 -6
  164. package/dist/schema/runnable/config.d.ts +1 -3
  165. package/dist/schema/runnable/config.js +1 -4
  166. package/dist/schema/runnable/index.cjs +15 -16
  167. package/dist/schema/runnable/index.d.ts +1 -5
  168. package/dist/schema/runnable/index.js +1 -4
  169. package/dist/schema/runnable/passthrough.cjs +3 -113
  170. package/dist/schema/runnable/passthrough.d.ts +1 -72
  171. package/dist/schema/runnable/passthrough.js +1 -111
  172. package/dist/schema/runnable/router.cjs +2 -71
  173. package/dist/schema/runnable/router.d.ts +1 -29
  174. package/dist/schema/runnable/router.js +1 -70
  175. package/dist/schema/storage.cjs +15 -8
  176. package/dist/schema/storage.d.ts +1 -57
  177. package/dist/schema/storage.js +1 -6
  178. package/dist/tools/bingserpapi.d.ts +1 -1
  179. package/dist/tools/searchapi.d.ts +1 -1
  180. package/dist/tools/serpapi.d.ts +1 -1
  181. package/dist/tools/serper.d.ts +1 -1
  182. package/dist/util/async_caller.cjs +14 -128
  183. package/dist/util/async_caller.d.ts +1 -45
  184. package/dist/util/async_caller.js +1 -124
  185. package/dist/vectorstores/vectara.cjs +77 -7
  186. package/dist/vectorstores/vectara.d.ts +9 -3
  187. package/dist/vectorstores/vectara.js +54 -7
  188. package/experimental/openai_files.cjs +1 -0
  189. package/experimental/openai_files.d.ts +1 -0
  190. package/experimental/openai_files.js +1 -0
  191. package/package.json +27 -5
  192. package/retrievers/self_query/vectara.cjs +1 -0
  193. package/retrievers/self_query/vectara.d.ts +1 -0
  194. package/retrievers/self_query/vectara.js +1 -0
  195. package/runnables.cjs +1 -0
  196. package/runnables.d.ts +1 -0
  197. package/runnables.js +1 -0
  198. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  199. package/dist/util/@cfworker/json-schema/index.js +0 -1
  200. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  201. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  202. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  203. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  204. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  205. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  206. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  207. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  208. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  209. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  210. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  211. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  212. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  213. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  214. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  215. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  216. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  217. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  218. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  219. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  220. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  221. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  222. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  223. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  224. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  225. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  226. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  227. package/dist/util/fast-json-patch/index.cjs +0 -49
  228. package/dist/util/fast-json-patch/index.d.ts +0 -22
  229. package/dist/util/fast-json-patch/index.js +0 -16
  230. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  231. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  232. package/dist/util/fast-json-patch/src/core.js +0 -459
  233. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  234. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  235. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  236. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  237. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  238. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  239. package/dist/util/js-sha1/hash.cjs +0 -358
  240. package/dist/util/js-sha1/hash.d.ts +0 -1
  241. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,178 +1,3 @@
1
- // Default generic "any" values are for backwards compatibility.
2
- // Replace with "string" when we are comfortable with a breaking change.
3
- import { BasePromptValue, HumanMessage, } from "../schema/index.js";
4
- import { Serializable } from "../load/serializable.js";
5
- import { Runnable } from "../schema/runnable/index.js";
6
- /**
7
- * Represents a prompt value as a string. It extends the BasePromptValue
8
- * class and overrides the toString and toChatMessages methods.
9
- */
10
- export class StringPromptValue extends BasePromptValue {
11
- constructor(value) {
12
- super({ value });
13
- Object.defineProperty(this, "lc_namespace", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: ["langchain", "prompts", "base"]
18
- });
19
- Object.defineProperty(this, "value", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: void 0
24
- });
25
- this.value = value;
26
- }
27
- toString() {
28
- return this.value;
29
- }
30
- toChatMessages() {
31
- return [new HumanMessage(this.value)];
32
- }
33
- }
34
- /**
35
- * Base class for prompt templates. Exposes a format method that returns a
36
- * string prompt given a set of input values.
37
- */
38
- export class BasePromptTemplate extends Runnable {
39
- get lc_attributes() {
40
- return {
41
- partialVariables: undefined, // python doesn't support this yet
42
- };
43
- }
44
- constructor(input) {
45
- super(input);
46
- Object.defineProperty(this, "lc_serializable", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: true
51
- });
52
- Object.defineProperty(this, "lc_namespace", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: ["langchain", "prompts", this._getPromptType()]
57
- });
58
- Object.defineProperty(this, "inputVariables", {
59
- enumerable: true,
60
- configurable: true,
61
- writable: true,
62
- value: void 0
63
- });
64
- Object.defineProperty(this, "outputParser", {
65
- enumerable: true,
66
- configurable: true,
67
- writable: true,
68
- value: void 0
69
- });
70
- Object.defineProperty(this, "partialVariables", {
71
- enumerable: true,
72
- configurable: true,
73
- writable: true,
74
- value: void 0
75
- });
76
- const { inputVariables } = input;
77
- if (inputVariables.includes("stop")) {
78
- throw new Error("Cannot have an input variable named 'stop', as it is used internally, please rename.");
79
- }
80
- Object.assign(this, input);
81
- }
82
- /**
83
- * Merges partial variables and user variables.
84
- * @param userVariables The user variables to merge with the partial variables.
85
- * @returns A Promise that resolves to an object containing the merged variables.
86
- */
87
- async mergePartialAndUserVariables(userVariables) {
88
- const partialVariables = this.partialVariables ?? {};
89
- const partialValues = {};
90
- for (const [key, value] of Object.entries(partialVariables)) {
91
- if (typeof value === "string") {
92
- partialValues[key] = value;
93
- }
94
- else {
95
- partialValues[key] = await value();
96
- }
97
- }
98
- const allKwargs = {
99
- ...partialValues,
100
- ...userVariables,
101
- };
102
- return allKwargs;
103
- }
104
- /**
105
- * Invokes the prompt template with the given input and options.
106
- * @param input The input to invoke the prompt template with.
107
- * @param options Optional configuration for the callback.
108
- * @returns A Promise that resolves to the output of the prompt template.
109
- */
110
- async invoke(input, options) {
111
- return this._callWithConfig((input) => this.formatPromptValue(input), input, { ...options, runType: "prompt" });
112
- }
113
- /**
114
- * Return a json-like object representing this prompt template.
115
- * @deprecated
116
- */
117
- serialize() {
118
- throw new Error("Use .toJSON() instead");
119
- }
120
- /**
121
- * @deprecated
122
- * Load a prompt template from a json-like object describing it.
123
- *
124
- * @remarks
125
- * Deserializing needs to be async because templates (e.g. {@link FewShotPromptTemplate}) can
126
- * reference remote resources that we read asynchronously with a web
127
- * request.
128
- */
129
- static async deserialize(data) {
130
- switch (data._type) {
131
- case "prompt": {
132
- const { PromptTemplate } = await import("./prompt.js");
133
- return PromptTemplate.deserialize(data);
134
- }
135
- case undefined: {
136
- const { PromptTemplate } = await import("./prompt.js");
137
- return PromptTemplate.deserialize({ ...data, _type: "prompt" });
138
- }
139
- case "few_shot": {
140
- const { FewShotPromptTemplate } = await import("./few_shot.js");
141
- return FewShotPromptTemplate.deserialize(data);
142
- }
143
- default:
144
- throw new Error(`Invalid prompt type in config: ${data._type}`);
145
- }
146
- }
147
- }
148
- /**
149
- * Base class for string prompt templates. It extends the
150
- * BasePromptTemplate class and overrides the formatPromptValue method to
151
- * return a StringPromptValue.
152
- */
153
- export class BaseStringPromptTemplate extends BasePromptTemplate {
154
- /**
155
- * Formats the prompt given the input values and returns a formatted
156
- * prompt value.
157
- * @param values The input values to format the prompt.
158
- * @returns A Promise that resolves to a formatted prompt value.
159
- */
160
- async formatPromptValue(values) {
161
- const formattedPrompt = await this.format(values);
162
- return new StringPromptValue(formattedPrompt);
163
- }
164
- }
165
- /**
166
- * Base class for example selectors.
167
- */
168
- export class BaseExampleSelector extends Serializable {
169
- constructor() {
170
- super(...arguments);
171
- Object.defineProperty(this, "lc_namespace", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: ["langchain", "prompts", "selectors"]
176
- });
177
- }
178
- }
1
+ export { StringPromptValue, BaseStringPromptTemplate, } from "langchain-core/prompts/base";
2
+ export { BasePromptTemplate, } from "langchain-core/schema/prompt_template";
3
+ export { BaseExampleSelector } from "langchain-core/prompts/example_selector/base";
@@ -1,479 +1,17 @@
1
1
  "use strict";
2
- // Default generic "any" values are for backwards compatibility.
3
- // Replace with "string" when we are comfortable with a breaking change.
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
4
16
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.ChatPromptTemplate = exports.SystemMessagePromptTemplate = exports.AIMessagePromptTemplate = exports.HumanMessagePromptTemplate = exports.ChatMessagePromptTemplate = exports.BaseChatPromptTemplate = exports.BaseMessageStringPromptTemplate = exports.MessagesPlaceholder = exports.ChatPromptValue = exports.BaseMessagePromptTemplate = void 0;
6
- const base_js_1 = require("../memory/base.cjs");
7
- const index_js_1 = require("../schema/index.cjs");
8
- const index_js_2 = require("../schema/runnable/index.cjs");
9
- const base_js_2 = require("./base.cjs");
10
- const prompt_js_1 = require("./prompt.cjs");
11
- /**
12
- * Abstract class that serves as a base for creating message prompt
13
- * templates. It defines how to format messages for different roles in a
14
- * conversation.
15
- */
16
- class BaseMessagePromptTemplate extends index_js_2.Runnable {
17
- constructor() {
18
- super(...arguments);
19
- Object.defineProperty(this, "lc_namespace", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: ["langchain", "prompts", "chat"]
24
- });
25
- Object.defineProperty(this, "lc_serializable", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: true
30
- });
31
- }
32
- /**
33
- * Calls the formatMessages method with the provided input and options.
34
- * @param input Input for the formatMessages method
35
- * @param options Optional BaseCallbackConfig
36
- * @returns Formatted output messages
37
- */
38
- async invoke(input, options) {
39
- return this._callWithConfig((input) => this.formatMessages(input), input, { ...options, runType: "prompt" });
40
- }
41
- }
42
- exports.BaseMessagePromptTemplate = BaseMessagePromptTemplate;
43
- /**
44
- * Class that represents a chat prompt value. It extends the
45
- * BasePromptValue and includes an array of BaseMessage instances.
46
- */
47
- class ChatPromptValue extends index_js_1.BasePromptValue {
48
- static lc_name() {
49
- return "ChatPromptValue";
50
- }
51
- constructor(fields) {
52
- if (Array.isArray(fields)) {
53
- // eslint-disable-next-line no-param-reassign
54
- fields = { messages: fields };
55
- }
56
- super(fields);
57
- Object.defineProperty(this, "lc_namespace", {
58
- enumerable: true,
59
- configurable: true,
60
- writable: true,
61
- value: ["langchain", "prompts", "chat"]
62
- });
63
- Object.defineProperty(this, "lc_serializable", {
64
- enumerable: true,
65
- configurable: true,
66
- writable: true,
67
- value: true
68
- });
69
- Object.defineProperty(this, "messages", {
70
- enumerable: true,
71
- configurable: true,
72
- writable: true,
73
- value: void 0
74
- });
75
- this.messages = fields.messages;
76
- }
77
- toString() {
78
- return (0, base_js_1.getBufferString)(this.messages);
79
- }
80
- toChatMessages() {
81
- return this.messages;
82
- }
83
- }
84
- exports.ChatPromptValue = ChatPromptValue;
85
- /**
86
- * Class that represents a placeholder for messages in a chat prompt. It
87
- * extends the BaseMessagePromptTemplate.
88
- */
89
- class MessagesPlaceholder extends BaseMessagePromptTemplate {
90
- static lc_name() {
91
- return "MessagesPlaceholder";
92
- }
93
- constructor(fields) {
94
- if (typeof fields === "string") {
95
- // eslint-disable-next-line no-param-reassign
96
- fields = { variableName: fields };
97
- }
98
- super(fields);
99
- Object.defineProperty(this, "variableName", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: void 0
104
- });
105
- this.variableName = fields.variableName;
106
- }
107
- get inputVariables() {
108
- return [this.variableName];
109
- }
110
- validateInputOrThrow(input, variableName) {
111
- let isInputBaseMessage = false;
112
- if (Array.isArray(input)) {
113
- isInputBaseMessage = input.every((message) => (0, index_js_1.isBaseMessage)(message));
114
- }
115
- else {
116
- isInputBaseMessage = (0, index_js_1.isBaseMessage)(input);
117
- }
118
- if (!isInputBaseMessage) {
119
- const readableInput = typeof input === "string" ? input : JSON.stringify(input, null, 2);
120
- const error = new Error(`Error: Field "${variableName}" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: ${readableInput}`);
121
- error.name = "InputFormatError";
122
- throw error;
123
- }
124
- return true;
125
- }
126
- async formatMessages(values) {
127
- this.validateInputOrThrow(values[this.variableName], this.variableName);
128
- return values[this.variableName];
129
- }
130
- }
131
- exports.MessagesPlaceholder = MessagesPlaceholder;
132
- /**
133
- * Abstract class that serves as a base for creating message string prompt
134
- * templates. It extends the BaseMessagePromptTemplate.
135
- */
136
- class BaseMessageStringPromptTemplate extends BaseMessagePromptTemplate {
137
- constructor(fields) {
138
- if (!("prompt" in fields)) {
139
- // eslint-disable-next-line no-param-reassign
140
- fields = { prompt: fields };
141
- }
142
- super(fields);
143
- Object.defineProperty(this, "prompt", {
144
- enumerable: true,
145
- configurable: true,
146
- writable: true,
147
- value: void 0
148
- });
149
- this.prompt = fields.prompt;
150
- }
151
- get inputVariables() {
152
- return this.prompt.inputVariables;
153
- }
154
- async formatMessages(values) {
155
- return [await this.format(values)];
156
- }
157
- }
158
- exports.BaseMessageStringPromptTemplate = BaseMessageStringPromptTemplate;
159
- /**
160
- * Abstract class that serves as a base for creating chat prompt
161
- * templates. It extends the BasePromptTemplate.
162
- */
163
- class BaseChatPromptTemplate extends base_js_2.BasePromptTemplate {
164
- constructor(input) {
165
- super(input);
166
- }
167
- async format(values) {
168
- return (await this.formatPromptValue(values)).toString();
169
- }
170
- async formatPromptValue(values) {
171
- const resultMessages = await this.formatMessages(values);
172
- return new ChatPromptValue(resultMessages);
173
- }
174
- }
175
- exports.BaseChatPromptTemplate = BaseChatPromptTemplate;
176
- /**
177
- * Class that represents a chat message prompt template. It extends the
178
- * BaseMessageStringPromptTemplate.
179
- */
180
- class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
181
- static lc_name() {
182
- return "ChatMessagePromptTemplate";
183
- }
184
- constructor(fields, role) {
185
- if (!("prompt" in fields)) {
186
- // eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
187
- fields = { prompt: fields, role: role };
188
- }
189
- super(fields);
190
- Object.defineProperty(this, "role", {
191
- enumerable: true,
192
- configurable: true,
193
- writable: true,
194
- value: void 0
195
- });
196
- this.role = fields.role;
197
- }
198
- async format(values) {
199
- return new index_js_1.ChatMessage(await this.prompt.format(values), this.role);
200
- }
201
- static fromTemplate(template, role) {
202
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template), role);
203
- }
204
- }
205
- exports.ChatMessagePromptTemplate = ChatMessagePromptTemplate;
206
- /**
207
- * Class that represents a human message prompt template. It extends the
208
- * BaseMessageStringPromptTemplate.
209
- * @example
210
- * ```typescript
211
- * const message = HumanMessagePromptTemplate.fromTemplate("{text}");
212
- * const formatted = await message.format({ text: "Hello world!" });
213
- *
214
- * const chatPrompt = ChatPromptTemplate.fromMessages([message]);
215
- * const formattedChatPrompt = await chatPrompt.invoke({
216
- * text: "Hello world!",
217
- * });
218
- * ```
219
- */
220
- class HumanMessagePromptTemplate extends BaseMessageStringPromptTemplate {
221
- static lc_name() {
222
- return "HumanMessagePromptTemplate";
223
- }
224
- async format(values) {
225
- return new index_js_1.HumanMessage(await this.prompt.format(values));
226
- }
227
- static fromTemplate(template) {
228
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template));
229
- }
230
- }
231
- exports.HumanMessagePromptTemplate = HumanMessagePromptTemplate;
232
- /**
233
- * Class that represents an AI message prompt template. It extends the
234
- * BaseMessageStringPromptTemplate.
235
- */
236
- class AIMessagePromptTemplate extends BaseMessageStringPromptTemplate {
237
- static lc_name() {
238
- return "AIMessagePromptTemplate";
239
- }
240
- async format(values) {
241
- return new index_js_1.AIMessage(await this.prompt.format(values));
242
- }
243
- static fromTemplate(template) {
244
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template));
245
- }
246
- }
247
- exports.AIMessagePromptTemplate = AIMessagePromptTemplate;
248
- /**
249
- * Class that represents a system message prompt template. It extends the
250
- * BaseMessageStringPromptTemplate.
251
- * @example
252
- * ```typescript
253
- * const message = SystemMessagePromptTemplate.fromTemplate("{text}");
254
- * const formatted = await message.format({ text: "Hello world!" });
255
- *
256
- * const chatPrompt = ChatPromptTemplate.fromMessages([message]);
257
- * const formattedChatPrompt = await chatPrompt.invoke({
258
- * text: "Hello world!",
259
- * });
260
- * ```
261
- */
262
- class SystemMessagePromptTemplate extends BaseMessageStringPromptTemplate {
263
- static lc_name() {
264
- return "SystemMessagePromptTemplate";
265
- }
266
- async format(values) {
267
- return new index_js_1.SystemMessage(await this.prompt.format(values));
268
- }
269
- static fromTemplate(template) {
270
- return new this(prompt_js_1.PromptTemplate.fromTemplate(template));
271
- }
272
- }
273
- exports.SystemMessagePromptTemplate = SystemMessagePromptTemplate;
274
- function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
275
- return (typeof baseMessagePromptTemplateLike
276
- .formatMessages === "function");
277
- }
278
- function _coerceMessagePromptTemplateLike(messagePromptTemplateLike) {
279
- if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) ||
280
- (0, index_js_1.isBaseMessage)(messagePromptTemplateLike)) {
281
- return messagePromptTemplateLike;
282
- }
283
- const message = (0, index_js_1.coerceMessageLikeToMessage)(messagePromptTemplateLike);
284
- if (message._getType() === "human") {
285
- return HumanMessagePromptTemplate.fromTemplate(message.content);
286
- }
287
- else if (message._getType() === "ai") {
288
- return AIMessagePromptTemplate.fromTemplate(message.content);
289
- }
290
- else if (message._getType() === "system") {
291
- return SystemMessagePromptTemplate.fromTemplate(message.content);
292
- }
293
- else if (index_js_1.ChatMessage.isInstance(message)) {
294
- return ChatMessagePromptTemplate.fromTemplate(message.content, message.role);
295
- }
296
- else {
297
- throw new Error(`Could not coerce message prompt template from input. Received message type: "${message._getType()}".`);
298
- }
299
- }
300
- /**
301
- * Class that represents a chat prompt. It extends the
302
- * BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate
303
- * instances to format a series of messages for a conversation.
304
- * @example
305
- * ```typescript
306
- * const message = SystemMessagePromptTemplate.fromTemplate("{text}");
307
- * const chatPrompt = ChatPromptTemplate.fromMessages([
308
- * ["ai", "You are a helpful assistant."],
309
- * message,
310
- * ]);
311
- * const formattedChatPrompt = await chatPrompt.invoke({
312
- * text: "Hello world!",
313
- * });
314
- * ```
315
- */
316
- class ChatPromptTemplate extends BaseChatPromptTemplate {
317
- static lc_name() {
318
- return "ChatPromptTemplate";
319
- }
320
- get lc_aliases() {
321
- return {
322
- promptMessages: "messages",
323
- };
324
- }
325
- constructor(input) {
326
- super(input);
327
- Object.defineProperty(this, "promptMessages", {
328
- enumerable: true,
329
- configurable: true,
330
- writable: true,
331
- value: void 0
332
- });
333
- Object.defineProperty(this, "validateTemplate", {
334
- enumerable: true,
335
- configurable: true,
336
- writable: true,
337
- value: true
338
- });
339
- Object.assign(this, input);
340
- if (this.validateTemplate) {
341
- const inputVariablesMessages = new Set();
342
- for (const promptMessage of this.promptMessages) {
343
- // eslint-disable-next-line no-instanceof/no-instanceof
344
- if (promptMessage instanceof index_js_1.BaseMessage)
345
- continue;
346
- for (const inputVariable of promptMessage.inputVariables) {
347
- inputVariablesMessages.add(inputVariable);
348
- }
349
- }
350
- const totalInputVariables = this.inputVariables;
351
- const inputVariablesInstance = new Set(this.partialVariables
352
- ? totalInputVariables.concat(Object.keys(this.partialVariables))
353
- : totalInputVariables);
354
- const difference = new Set([...inputVariablesInstance].filter((x) => !inputVariablesMessages.has(x)));
355
- if (difference.size > 0) {
356
- throw new Error(`Input variables \`${[
357
- ...difference,
358
- ]}\` are not used in any of the prompt messages.`);
359
- }
360
- const otherDifference = new Set([...inputVariablesMessages].filter((x) => !inputVariablesInstance.has(x)));
361
- if (otherDifference.size > 0) {
362
- throw new Error(`Input variables \`${[
363
- ...otherDifference,
364
- ]}\` are used in prompt messages but not in the prompt template.`);
365
- }
366
- }
367
- }
368
- _getPromptType() {
369
- return "chat";
370
- }
371
- async _parseImagePrompts(message, inputValues) {
372
- if (typeof message.content === "string") {
373
- return message;
374
- }
375
- const formattedMessageContent = await Promise.all(message.content.map(async (item) => {
376
- if (item.type !== "image_url" ||
377
- typeof item.image_url === "string" ||
378
- !item.image_url?.url) {
379
- return item;
380
- }
381
- const imageUrl = item.image_url.url;
382
- const promptTemplatePlaceholder = prompt_js_1.PromptTemplate.fromTemplate(imageUrl);
383
- const formattedUrl = await promptTemplatePlaceholder.format(inputValues);
384
- // eslint-disable-next-line no-param-reassign
385
- item.image_url.url = formattedUrl;
386
- return item;
387
- }));
388
- // eslint-disable-next-line no-param-reassign
389
- message.content = formattedMessageContent;
390
- return message;
391
- }
392
- async formatMessages(values) {
393
- const allValues = await this.mergePartialAndUserVariables(values);
394
- let resultMessages = [];
395
- for (const promptMessage of this.promptMessages) {
396
- // eslint-disable-next-line no-instanceof/no-instanceof
397
- if (promptMessage instanceof index_js_1.BaseMessage) {
398
- resultMessages.push(await this._parseImagePrompts(promptMessage, allValues));
399
- }
400
- else {
401
- const inputValues = promptMessage.inputVariables.reduce((acc, inputVariable) => {
402
- if (!(inputVariable in allValues)) {
403
- throw new Error(`Missing value for input variable \`${inputVariable.toString()}\``);
404
- }
405
- acc[inputVariable] = allValues[inputVariable];
406
- return acc;
407
- }, {});
408
- const message = await promptMessage.formatMessages(inputValues);
409
- resultMessages = resultMessages.concat(message);
410
- }
411
- }
412
- return resultMessages;
413
- }
414
- async partial(values) {
415
- // This is implemented in a way it doesn't require making
416
- // BaseMessagePromptTemplate aware of .partial()
417
- const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
418
- const newPartialVariables = {
419
- ...(this.partialVariables ?? {}),
420
- ...values,
421
- };
422
- const promptDict = {
423
- ...this,
424
- inputVariables: newInputVariables,
425
- partialVariables: newPartialVariables,
426
- };
427
- return new ChatPromptTemplate(promptDict);
428
- }
429
- /**
430
- * Load prompt template from a template f-string
431
- */
432
- static fromTemplate(template) {
433
- const prompt = prompt_js_1.PromptTemplate.fromTemplate(template);
434
- const humanTemplate = new HumanMessagePromptTemplate({ prompt });
435
- return this.fromMessages([humanTemplate]);
436
- }
437
- /**
438
- * Create a chat model-specific prompt from individual chat messages
439
- * or message-like tuples.
440
- * @param promptMessages Messages to be passed to the chat model
441
- * @returns A new ChatPromptTemplate
442
- */
443
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
444
- static fromMessages(promptMessages) {
445
- const flattenedMessages = promptMessages.reduce((acc, promptMessage) => acc.concat(
446
- // eslint-disable-next-line no-instanceof/no-instanceof
447
- promptMessage instanceof ChatPromptTemplate
448
- ? promptMessage.promptMessages
449
- : [_coerceMessagePromptTemplateLike(promptMessage)]), []);
450
- const flattenedPartialVariables = promptMessages.reduce((acc, promptMessage) =>
451
- // eslint-disable-next-line no-instanceof/no-instanceof
452
- promptMessage instanceof ChatPromptTemplate
453
- ? Object.assign(acc, promptMessage.partialVariables)
454
- : acc, Object.create(null));
455
- const inputVariables = new Set();
456
- for (const promptMessage of flattenedMessages) {
457
- // eslint-disable-next-line no-instanceof/no-instanceof
458
- if (promptMessage instanceof index_js_1.BaseMessage)
459
- continue;
460
- for (const inputVariable of promptMessage.inputVariables) {
461
- if (inputVariable in flattenedPartialVariables) {
462
- continue;
463
- }
464
- inputVariables.add(inputVariable);
465
- }
466
- }
467
- return new ChatPromptTemplate({
468
- inputVariables: [...inputVariables],
469
- promptMessages: flattenedMessages,
470
- partialVariables: flattenedPartialVariables,
471
- });
472
- }
473
- /** @deprecated Renamed to .fromMessages */
474
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
475
- static fromPromptMessages(promptMessages) {
476
- return this.fromMessages(promptMessages);
477
- }
478
- }
479
- exports.ChatPromptTemplate = ChatPromptTemplate;
17
+ __exportStar(require("langchain-core/prompts/chat"), exports);