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.
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
- package/dist/base_language/count_tokens.cjs +5 -70
- package/dist/base_language/count_tokens.d.ts +1 -10
- package/dist/base_language/count_tokens.js +1 -65
- package/dist/base_language/index.cjs +6 -196
- package/dist/base_language/index.d.ts +1 -111
- package/dist/base_language/index.js +1 -191
- package/dist/cache/base.cjs +15 -37
- package/dist/cache/base.d.ts +1 -20
- package/dist/cache/base.js +1 -33
- package/dist/cache/index.cjs +2 -46
- package/dist/cache/index.d.ts +1 -29
- package/dist/cache/index.js +1 -45
- package/dist/callbacks/base.cjs +3 -139
- package/dist/callbacks/base.d.ts +1 -266
- package/dist/callbacks/base.js +1 -126
- package/dist/callbacks/handlers/console.cjs +14 -221
- package/dist/callbacks/handlers/console.d.ts +1 -117
- package/dist/callbacks/handlers/console.js +1 -217
- package/dist/callbacks/handlers/initialize.cjs +15 -30
- package/dist/callbacks/handlers/initialize.d.ts +1 -16
- package/dist/callbacks/handlers/initialize.js +1 -27
- package/dist/callbacks/handlers/log_stream.cjs +15 -293
- package/dist/callbacks/handlers/log_stream.d.ts +1 -100
- package/dist/callbacks/handlers/log_stream.js +1 -289
- package/dist/callbacks/handlers/run_collector.cjs +15 -48
- package/dist/callbacks/handlers/run_collector.d.ts +1 -26
- package/dist/callbacks/handlers/run_collector.js +1 -46
- package/dist/callbacks/handlers/tracer.cjs +15 -375
- package/dist/callbacks/handlers/tracer.d.ts +1 -70
- package/dist/callbacks/handlers/tracer.js +1 -373
- package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
- package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
- package/dist/callbacks/handlers/tracer_langchain.js +1 -102
- package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
- package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
- package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
- package/dist/callbacks/manager.cjs +15 -676
- package/dist/callbacks/manager.d.ts +1 -180
- package/dist/callbacks/manager.js +1 -666
- package/dist/callbacks/promises.cjs +14 -42
- package/dist/callbacks/promises.d.ts +1 -11
- package/dist/callbacks/promises.js +1 -37
- package/dist/chains/graph_qa/prompts.d.ts +1 -1
- package/dist/chains/openai_functions/structured_output.cjs +2 -2
- package/dist/chains/openai_functions/structured_output.d.ts +1 -1
- package/dist/chains/openai_functions/structured_output.js +1 -1
- package/dist/chat_models/baiduwenxin.d.ts +1 -1
- package/dist/chat_models/base.cjs +15 -296
- package/dist/chat_models/base.d.ts +1 -122
- package/dist/chat_models/base.js +1 -292
- package/dist/chat_models/bedrock/web.d.ts +1 -1
- package/dist/chat_models/fireworks.d.ts +1 -1
- package/dist/document.cjs +2 -24
- package/dist/document.d.ts +1 -12
- package/dist/document.js +1 -23
- package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
- package/dist/document_loaders/web/s3.d.ts +1 -1
- package/dist/embeddings/base.cjs +15 -22
- package/dist/embeddings/base.d.ts +1 -33
- package/dist/embeddings/base.js +1 -20
- package/dist/embeddings/cache_backed.cjs +2 -2
- package/dist/embeddings/cache_backed.js +1 -1
- package/dist/embeddings/hf.cjs +1 -2
- package/dist/embeddings/hf.js +1 -2
- package/dist/evaluation/agents/trajectory.d.ts +1 -1
- package/dist/evaluation/criteria/prompt.d.ts +2 -2
- package/dist/evaluation/qa/prompt.d.ts +2 -2
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.cjs +2 -1
- package/dist/experimental/openai_assistant/index.d.ts +2 -1
- package/dist/experimental/openai_assistant/index.js +2 -1
- package/dist/experimental/openai_files/index.cjs +88 -0
- package/dist/experimental/openai_files/index.d.ts +79 -0
- package/dist/experimental/openai_files/index.js +84 -0
- package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
- package/dist/llms/base.cjs +15 -278
- package/dist/llms/base.d.ts +1 -115
- package/dist/llms/base.js +1 -275
- package/dist/llms/bedrock/web.d.ts +1 -1
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +4 -2
- package/dist/load/import_map.d.ts +2 -0
- package/dist/load/import_map.js +2 -0
- package/dist/load/index.cjs +7 -148
- package/dist/load/index.js +7 -148
- package/dist/load/map_keys.cjs +0 -24
- package/dist/load/map_keys.d.ts +0 -6
- package/dist/load/map_keys.js +1 -17
- package/dist/load/serializable.cjs +15 -178
- package/dist/load/serializable.d.ts +1 -66
- package/dist/load/serializable.js +1 -175
- package/dist/memory/base.cjs +17 -92
- package/dist/memory/base.d.ts +2 -68
- package/dist/memory/base.js +2 -87
- package/dist/output_parsers/openai_functions.cjs +2 -2
- package/dist/output_parsers/openai_functions.d.ts +1 -1
- package/dist/output_parsers/openai_functions.js +1 -1
- package/dist/output_parsers/regex.d.ts +1 -1
- package/dist/output_parsers/structured.d.ts +1 -1
- package/dist/prompts/base.cjs +8 -183
- package/dist/prompts/base.d.ts +3 -132
- package/dist/prompts/base.js +3 -178
- package/dist/prompts/chat.cjs +15 -477
- package/dist/prompts/chat.d.ts +1 -219
- package/dist/prompts/chat.js +1 -466
- package/dist/prompts/few_shot.cjs +15 -353
- package/dist/prompts/few_shot.d.ts +1 -192
- package/dist/prompts/few_shot.js +1 -350
- package/dist/prompts/index.cjs +3 -2
- package/dist/prompts/index.d.ts +2 -1
- package/dist/prompts/index.js +2 -1
- package/dist/prompts/pipeline.cjs +15 -142
- package/dist/prompts/pipeline.d.ts +1 -98
- package/dist/prompts/pipeline.js +1 -140
- package/dist/prompts/prompt.cjs +15 -146
- package/dist/prompts/prompt.d.ts +1 -92
- package/dist/prompts/prompt.js +1 -144
- package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
- package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
- package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
- package/dist/prompts/selectors/conditional.cjs +15 -74
- package/dist/prompts/selectors/conditional.d.ts +1 -63
- package/dist/prompts/selectors/conditional.js +1 -69
- package/dist/prompts/serde.cjs +15 -0
- package/dist/prompts/serde.d.ts +1 -43
- package/dist/prompts/serde.js +1 -1
- package/dist/prompts/template.cjs +14 -88
- package/dist/prompts/template.d.ts +1 -36
- package/dist/prompts/template.js +1 -83
- package/dist/retrievers/chaindesk.cjs +9 -1
- package/dist/retrievers/chaindesk.d.ts +3 -1
- package/dist/retrievers/chaindesk.js +9 -1
- package/dist/retrievers/self_query/vectara.cjs +138 -0
- package/dist/retrievers/self_query/vectara.d.ts +41 -0
- package/dist/retrievers/self_query/vectara.js +134 -0
- package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
- package/dist/runnables/index.d.ts +1 -0
- package/dist/runnables/index.js +1 -0
- package/dist/schema/document.cjs +3 -34
- package/dist/schema/document.d.ts +2 -29
- package/dist/schema/document.js +2 -32
- package/dist/schema/index.cjs +37 -612
- package/dist/schema/index.d.ts +11 -311
- package/dist/schema/index.js +8 -583
- package/dist/schema/output_parser.cjs +15 -309
- package/dist/schema/output_parser.d.ts +1 -173
- package/dist/schema/output_parser.js +1 -301
- package/dist/schema/retriever.cjs +15 -77
- package/dist/schema/retriever.d.ts +1 -43
- package/dist/schema/retriever.js +1 -75
- package/dist/schema/runnable/base.cjs +10 -1072
- package/dist/schema/runnable/base.d.ts +1 -356
- package/dist/schema/runnable/base.js +1 -1060
- package/dist/schema/runnable/branch.cjs +2 -131
- package/dist/schema/runnable/branch.d.ts +1 -94
- package/dist/schema/runnable/branch.js +1 -130
- package/dist/schema/runnable/config.cjs +0 -6
- package/dist/schema/runnable/config.d.ts +1 -3
- package/dist/schema/runnable/config.js +1 -4
- package/dist/schema/runnable/index.cjs +15 -16
- package/dist/schema/runnable/index.d.ts +1 -5
- package/dist/schema/runnable/index.js +1 -4
- package/dist/schema/runnable/passthrough.cjs +3 -113
- package/dist/schema/runnable/passthrough.d.ts +1 -72
- package/dist/schema/runnable/passthrough.js +1 -111
- package/dist/schema/runnable/router.cjs +2 -71
- package/dist/schema/runnable/router.d.ts +1 -29
- package/dist/schema/runnable/router.js +1 -70
- package/dist/schema/storage.cjs +15 -8
- package/dist/schema/storage.d.ts +1 -57
- package/dist/schema/storage.js +1 -6
- package/dist/tools/bingserpapi.d.ts +1 -1
- package/dist/tools/searchapi.d.ts +1 -1
- package/dist/tools/serpapi.d.ts +1 -1
- package/dist/tools/serper.d.ts +1 -1
- package/dist/util/async_caller.cjs +14 -128
- package/dist/util/async_caller.d.ts +1 -45
- package/dist/util/async_caller.js +1 -124
- package/dist/vectorstores/vectara.cjs +77 -7
- package/dist/vectorstores/vectara.d.ts +9 -3
- package/dist/vectorstores/vectara.js +54 -7
- package/experimental/openai_files.cjs +1 -0
- package/experimental/openai_files.d.ts +1 -0
- package/experimental/openai_files.js +1 -0
- package/package.json +27 -5
- package/retrievers/self_query/vectara.cjs +1 -0
- package/retrievers/self_query/vectara.d.ts +1 -0
- package/retrievers/self_query/vectara.js +1 -0
- package/runnables.cjs +1 -0
- package/runnables.d.ts +1 -0
- package/runnables.js +1 -0
- package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
- package/dist/util/@cfworker/json-schema/index.js +0 -1
- package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
- package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
- package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
- package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
- package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
- package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
- package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
- package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
- package/dist/util/@cfworker/json-schema/src/format.js +0 -136
- package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
- package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
- package/dist/util/@cfworker/json-schema/src/index.js +0 -8
- package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
- package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
- package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
- package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
- package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
- package/dist/util/@cfworker/json-schema/src/types.js +0 -1
- package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
- package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
- package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
- package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
- package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
- package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
- package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
- package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
- package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
- package/dist/util/fast-json-patch/index.cjs +0 -49
- package/dist/util/fast-json-patch/index.d.ts +0 -22
- package/dist/util/fast-json-patch/index.js +0 -16
- package/dist/util/fast-json-patch/src/core.cjs +0 -469
- package/dist/util/fast-json-patch/src/core.d.ts +0 -111
- package/dist/util/fast-json-patch/src/core.js +0 -459
- package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
- package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
- package/dist/util/fast-json-patch/src/duplex.js +0 -230
- package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
- package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
- package/dist/util/fast-json-patch/src/helpers.js +0 -181
- package/dist/util/js-sha1/hash.cjs +0 -358
- package/dist/util/js-sha1/hash.d.ts +0 -1
- package/dist/util/js-sha1/hash.js +0 -355
package/dist/prompts/pipeline.js
CHANGED
|
@@ -1,140 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { ChatPromptTemplate } from "./chat.js";
|
|
3
|
-
/**
|
|
4
|
-
* Class that handles a sequence of prompts, each of which may require
|
|
5
|
-
* different input variables. Includes methods for formatting these
|
|
6
|
-
* prompts, extracting required input values, and handling partial
|
|
7
|
-
* prompts.
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const composedPrompt = new PipelinePromptTemplate({
|
|
11
|
-
* pipelinePrompts: [
|
|
12
|
-
* {
|
|
13
|
-
* name: "introduction",
|
|
14
|
-
* prompt: PromptTemplate.fromTemplate(`You are impersonating {person}.`),
|
|
15
|
-
* },
|
|
16
|
-
* {
|
|
17
|
-
* name: "example",
|
|
18
|
-
* prompt: PromptTemplate.fromTemplate(
|
|
19
|
-
* `Here's an example of an interaction:
|
|
20
|
-
* Q: {example_q}
|
|
21
|
-
* A: {example_a}`,
|
|
22
|
-
* ),
|
|
23
|
-
* },
|
|
24
|
-
* {
|
|
25
|
-
* name: "start",
|
|
26
|
-
* prompt: PromptTemplate.fromTemplate(
|
|
27
|
-
* `Now, do this for real!
|
|
28
|
-
* Q: {input}
|
|
29
|
-
* A:`,
|
|
30
|
-
* ),
|
|
31
|
-
* },
|
|
32
|
-
* ],
|
|
33
|
-
* finalPrompt: PromptTemplate.fromTemplate(
|
|
34
|
-
* `{introduction}
|
|
35
|
-
* {example}
|
|
36
|
-
* {start}`,
|
|
37
|
-
* ),
|
|
38
|
-
* });
|
|
39
|
-
*
|
|
40
|
-
* const formattedPrompt = await composedPrompt.format({
|
|
41
|
-
* person: "Elon Musk",
|
|
42
|
-
* example_q: `What's your favorite car?`,
|
|
43
|
-
* example_a: "Tesla",
|
|
44
|
-
* input: `What's your favorite social media site?`,
|
|
45
|
-
* });
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
export class PipelinePromptTemplate extends BasePromptTemplate {
|
|
49
|
-
static lc_name() {
|
|
50
|
-
return "PipelinePromptTemplate";
|
|
51
|
-
}
|
|
52
|
-
constructor(input) {
|
|
53
|
-
super({ ...input, inputVariables: [] });
|
|
54
|
-
Object.defineProperty(this, "pipelinePrompts", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: void 0
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(this, "finalPrompt", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: void 0
|
|
65
|
-
});
|
|
66
|
-
this.pipelinePrompts = input.pipelinePrompts;
|
|
67
|
-
this.finalPrompt = input.finalPrompt;
|
|
68
|
-
this.inputVariables = this.computeInputValues();
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Computes the input values required by the pipeline prompts.
|
|
72
|
-
* @returns Array of input values required by the pipeline prompts.
|
|
73
|
-
*/
|
|
74
|
-
computeInputValues() {
|
|
75
|
-
const intermediateValues = this.pipelinePrompts.map((pipelinePrompt) => pipelinePrompt.name);
|
|
76
|
-
const inputValues = this.pipelinePrompts
|
|
77
|
-
.map((pipelinePrompt) => pipelinePrompt.prompt.inputVariables.filter((inputValue) => !intermediateValues.includes(inputValue)))
|
|
78
|
-
.flat();
|
|
79
|
-
return [...new Set(inputValues)];
|
|
80
|
-
}
|
|
81
|
-
static extractRequiredInputValues(allValues, requiredValueNames) {
|
|
82
|
-
return requiredValueNames.reduce((requiredValues, valueName) => {
|
|
83
|
-
// eslint-disable-next-line no-param-reassign
|
|
84
|
-
requiredValues[valueName] = allValues[valueName];
|
|
85
|
-
return requiredValues;
|
|
86
|
-
}, {});
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Formats the pipeline prompts based on the provided input values.
|
|
90
|
-
* @param values Input values to format the pipeline prompts.
|
|
91
|
-
* @returns Promise that resolves with the formatted input values.
|
|
92
|
-
*/
|
|
93
|
-
async formatPipelinePrompts(values) {
|
|
94
|
-
const allValues = await this.mergePartialAndUserVariables(values);
|
|
95
|
-
for (const { name: pipelinePromptName, prompt: pipelinePrompt } of this
|
|
96
|
-
.pipelinePrompts) {
|
|
97
|
-
const pipelinePromptInputValues = PipelinePromptTemplate.extractRequiredInputValues(allValues, pipelinePrompt.inputVariables);
|
|
98
|
-
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
99
|
-
if (pipelinePrompt instanceof ChatPromptTemplate) {
|
|
100
|
-
allValues[pipelinePromptName] = await pipelinePrompt.formatMessages(pipelinePromptInputValues);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
allValues[pipelinePromptName] = await pipelinePrompt.format(pipelinePromptInputValues);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return PipelinePromptTemplate.extractRequiredInputValues(allValues, this.finalPrompt.inputVariables);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Formats the final prompt value based on the provided input values.
|
|
110
|
-
* @param values Input values to format the final prompt value.
|
|
111
|
-
* @returns Promise that resolves with the formatted final prompt value.
|
|
112
|
-
*/
|
|
113
|
-
async formatPromptValue(values) {
|
|
114
|
-
return this.finalPrompt.formatPromptValue(await this.formatPipelinePrompts(values));
|
|
115
|
-
}
|
|
116
|
-
async format(values) {
|
|
117
|
-
return this.finalPrompt.format(await this.formatPipelinePrompts(values));
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Handles partial prompts, which are prompts that have been partially
|
|
121
|
-
* filled with input values.
|
|
122
|
-
* @param values Partial input values.
|
|
123
|
-
* @returns Promise that resolves with a new PipelinePromptTemplate instance with updated input variables.
|
|
124
|
-
*/
|
|
125
|
-
async partial(values) {
|
|
126
|
-
const promptDict = { ...this };
|
|
127
|
-
promptDict.inputVariables = this.inputVariables.filter((iv) => !(iv in values));
|
|
128
|
-
promptDict.partialVariables = {
|
|
129
|
-
...(this.partialVariables ?? {}),
|
|
130
|
-
...values,
|
|
131
|
-
};
|
|
132
|
-
return new PipelinePromptTemplate(promptDict);
|
|
133
|
-
}
|
|
134
|
-
serialize() {
|
|
135
|
-
throw new Error("Not implemented.");
|
|
136
|
-
}
|
|
137
|
-
_getPromptType() {
|
|
138
|
-
return "pipeline";
|
|
139
|
-
}
|
|
140
|
-
}
|
|
1
|
+
export * from "langchain-core/prompts/pipeline";
|
package/dist/prompts/prompt.cjs
CHANGED
|
@@ -1,148 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
6
|
-
const base_js_1 = require("./base.cjs");
|
|
7
|
-
const template_js_1 = require("./template.cjs");
|
|
8
|
-
/**
|
|
9
|
-
* Schema to represent a basic prompt for an LLM.
|
|
10
|
-
* @augments BasePromptTemplate
|
|
11
|
-
* @augments PromptTemplateInput
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* import { PromptTemplate } from "langchain/prompts";
|
|
16
|
-
*
|
|
17
|
-
* const prompt = new PromptTemplate({
|
|
18
|
-
* inputVariables: ["foo"],
|
|
19
|
-
* template: "Say {foo}",
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
class PromptTemplate extends base_js_1.BaseStringPromptTemplate {
|
|
24
|
-
static lc_name() {
|
|
25
|
-
return "PromptTemplate";
|
|
26
|
-
}
|
|
27
|
-
constructor(input) {
|
|
28
|
-
super(input);
|
|
29
|
-
Object.defineProperty(this, "template", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: void 0
|
|
34
|
-
});
|
|
35
|
-
Object.defineProperty(this, "templateFormat", {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
configurable: true,
|
|
38
|
-
writable: true,
|
|
39
|
-
value: "f-string"
|
|
40
|
-
});
|
|
41
|
-
Object.defineProperty(this, "validateTemplate", {
|
|
42
|
-
enumerable: true,
|
|
43
|
-
configurable: true,
|
|
44
|
-
writable: true,
|
|
45
|
-
value: true
|
|
46
|
-
});
|
|
47
|
-
Object.assign(this, input);
|
|
48
|
-
if (this.validateTemplate) {
|
|
49
|
-
let totalInputVariables = this.inputVariables;
|
|
50
|
-
if (this.partialVariables) {
|
|
51
|
-
totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
|
|
52
|
-
}
|
|
53
|
-
(0, template_js_1.checkValidTemplate)(this.template, this.templateFormat, totalInputVariables);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
_getPromptType() {
|
|
57
|
-
return "prompt";
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Formats the prompt template with the provided values.
|
|
61
|
-
* @param values The values to be used to format the prompt template.
|
|
62
|
-
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
63
|
-
*/
|
|
64
|
-
async format(values) {
|
|
65
|
-
const allValues = await this.mergePartialAndUserVariables(values);
|
|
66
|
-
return (0, template_js_1.renderTemplate)(this.template, this.templateFormat, allValues);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Take examples in list format with prefix and suffix to create a prompt.
|
|
70
|
-
*
|
|
71
|
-
* Intended to be used a a way to dynamically create a prompt from examples.
|
|
72
|
-
*
|
|
73
|
-
* @param examples - List of examples to use in the prompt.
|
|
74
|
-
* @param suffix - String to go after the list of examples. Should generally set up the user's input.
|
|
75
|
-
* @param inputVariables - A list of variable names the final prompt template will expect
|
|
76
|
-
* @param exampleSeparator - The separator to use in between examples
|
|
77
|
-
* @param prefix - String that should go before any examples. Generally includes examples.
|
|
78
|
-
*
|
|
79
|
-
* @returns The final prompt template generated.
|
|
80
|
-
*/
|
|
81
|
-
static fromExamples(examples, suffix, inputVariables, exampleSeparator = "\n\n", prefix = "") {
|
|
82
|
-
const template = [prefix, ...examples, suffix].join(exampleSeparator);
|
|
83
|
-
return new PromptTemplate({
|
|
84
|
-
inputVariables,
|
|
85
|
-
template,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Load prompt template from a template f-string
|
|
90
|
-
*/
|
|
91
|
-
static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
|
|
92
|
-
const names = new Set();
|
|
93
|
-
(0, template_js_1.parseTemplate)(template, templateFormat).forEach((node) => {
|
|
94
|
-
if (node.type === "variable") {
|
|
95
|
-
names.add(node.name);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return new PromptTemplate({
|
|
99
|
-
// Rely on extracted types
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
-
inputVariables: [...names],
|
|
102
|
-
templateFormat,
|
|
103
|
-
template,
|
|
104
|
-
...rest,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Partially applies values to the prompt template.
|
|
109
|
-
* @param values The values to be partially applied to the prompt template.
|
|
110
|
-
* @returns A new instance of PromptTemplate with the partially applied values.
|
|
111
|
-
*/
|
|
112
|
-
async partial(values) {
|
|
113
|
-
const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
|
|
114
|
-
const newPartialVariables = {
|
|
115
|
-
...(this.partialVariables ?? {}),
|
|
116
|
-
...values,
|
|
117
|
-
};
|
|
118
|
-
const promptDict = {
|
|
119
|
-
...this,
|
|
120
|
-
inputVariables: newInputVariables,
|
|
121
|
-
partialVariables: newPartialVariables,
|
|
122
|
-
};
|
|
123
|
-
return new PromptTemplate(promptDict);
|
|
124
|
-
}
|
|
125
|
-
serialize() {
|
|
126
|
-
if (this.outputParser !== undefined) {
|
|
127
|
-
throw new Error("Cannot serialize a prompt template with an output parser");
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
_type: this._getPromptType(),
|
|
131
|
-
input_variables: this.inputVariables,
|
|
132
|
-
template: this.template,
|
|
133
|
-
template_format: this.templateFormat,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
static async deserialize(data) {
|
|
137
|
-
if (!data.template) {
|
|
138
|
-
throw new Error("Prompt template must have a template");
|
|
139
|
-
}
|
|
140
|
-
const res = new PromptTemplate({
|
|
141
|
-
inputVariables: data.input_variables,
|
|
142
|
-
template: data.template,
|
|
143
|
-
templateFormat: data.template_format,
|
|
144
|
-
});
|
|
145
|
-
return res;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
exports.PromptTemplate = PromptTemplate;
|
|
17
|
+
__exportStar(require("langchain-core/prompts"), exports);
|
package/dist/prompts/prompt.d.ts
CHANGED
|
@@ -1,92 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { TemplateFormat } from "./template.js";
|
|
3
|
-
import { SerializedPromptTemplate } from "./serde.js";
|
|
4
|
-
import { InputValues, PartialValues } from "../schema/index.js";
|
|
5
|
-
/**
|
|
6
|
-
* Inputs to create a {@link PromptTemplate}
|
|
7
|
-
* @augments BasePromptTemplateInput
|
|
8
|
-
*/
|
|
9
|
-
export interface PromptTemplateInput<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BasePromptTemplateInput<RunInput, PartialVariableName> {
|
|
10
|
-
/**
|
|
11
|
-
* The prompt template
|
|
12
|
-
*/
|
|
13
|
-
template: string;
|
|
14
|
-
/**
|
|
15
|
-
* The format of the prompt template. Options are 'f-string'
|
|
16
|
-
*
|
|
17
|
-
* @defaultValue 'f-string'
|
|
18
|
-
*/
|
|
19
|
-
templateFormat?: TemplateFormat;
|
|
20
|
-
/**
|
|
21
|
-
* Whether or not to try validating the template on initialization
|
|
22
|
-
*
|
|
23
|
-
* @defaultValue `true`
|
|
24
|
-
*/
|
|
25
|
-
validateTemplate?: boolean;
|
|
26
|
-
}
|
|
27
|
-
type NonAlphanumeric = " " | "\t" | "\n" | "\r" | '"' | "'" | "{" | "[" | "(" | "`" | ":" | ";";
|
|
28
|
-
/**
|
|
29
|
-
* Recursive type to extract template parameters from a string.
|
|
30
|
-
* @template T - The input string.
|
|
31
|
-
* @template Result - The resulting array of extracted template parameters.
|
|
32
|
-
*/
|
|
33
|
-
type ExtractTemplateParamsRecursive<T extends string, Result extends string[] = []> = T extends `${string}{${infer Param}}${infer Rest}` ? Param extends `${NonAlphanumeric}${string}` ? ExtractTemplateParamsRecursive<Rest, Result> : ExtractTemplateParamsRecursive<Rest, [...Result, Param]> : Result;
|
|
34
|
-
export type ParamsFromFString<T extends string> = {
|
|
35
|
-
[Key in ExtractTemplateParamsRecursive<T>[number] | (string & Record<never, never>)]: string;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Schema to represent a basic prompt for an LLM.
|
|
39
|
-
* @augments BasePromptTemplate
|
|
40
|
-
* @augments PromptTemplateInput
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* import { PromptTemplate } from "langchain/prompts";
|
|
45
|
-
*
|
|
46
|
-
* const prompt = new PromptTemplate({
|
|
47
|
-
* inputVariables: ["foo"],
|
|
48
|
-
* template: "Say {foo}",
|
|
49
|
-
* });
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export declare class PromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BaseStringPromptTemplate<RunInput, PartialVariableName> implements PromptTemplateInput<RunInput, PartialVariableName> {
|
|
53
|
-
static lc_name(): string;
|
|
54
|
-
template: string;
|
|
55
|
-
templateFormat: TemplateFormat;
|
|
56
|
-
validateTemplate: boolean;
|
|
57
|
-
constructor(input: PromptTemplateInput<RunInput, PartialVariableName>);
|
|
58
|
-
_getPromptType(): "prompt";
|
|
59
|
-
/**
|
|
60
|
-
* Formats the prompt template with the provided values.
|
|
61
|
-
* @param values The values to be used to format the prompt template.
|
|
62
|
-
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
63
|
-
*/
|
|
64
|
-
format(values: TypedPromptInputValues<RunInput>): Promise<string>;
|
|
65
|
-
/**
|
|
66
|
-
* Take examples in list format with prefix and suffix to create a prompt.
|
|
67
|
-
*
|
|
68
|
-
* Intended to be used a a way to dynamically create a prompt from examples.
|
|
69
|
-
*
|
|
70
|
-
* @param examples - List of examples to use in the prompt.
|
|
71
|
-
* @param suffix - String to go after the list of examples. Should generally set up the user's input.
|
|
72
|
-
* @param inputVariables - A list of variable names the final prompt template will expect
|
|
73
|
-
* @param exampleSeparator - The separator to use in between examples
|
|
74
|
-
* @param prefix - String that should go before any examples. Generally includes examples.
|
|
75
|
-
*
|
|
76
|
-
* @returns The final prompt template generated.
|
|
77
|
-
*/
|
|
78
|
-
static fromExamples(examples: string[], suffix: string, inputVariables: string[], exampleSeparator?: string, prefix?: string): PromptTemplate<any, any>;
|
|
79
|
-
/**
|
|
80
|
-
* Load prompt template from a template f-string
|
|
81
|
-
*/
|
|
82
|
-
static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, { templateFormat, ...rest }?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): PromptTemplate<RunInput extends Symbol ? ParamsFromFString<T> : RunInput, any>;
|
|
83
|
-
/**
|
|
84
|
-
* Partially applies values to the prompt template.
|
|
85
|
-
* @param values The values to be partially applied to the prompt template.
|
|
86
|
-
* @returns A new instance of PromptTemplate with the partially applied values.
|
|
87
|
-
*/
|
|
88
|
-
partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<PromptTemplate<InputValues<Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>>, any>>;
|
|
89
|
-
serialize(): SerializedPromptTemplate;
|
|
90
|
-
static deserialize(data: SerializedPromptTemplate): Promise<PromptTemplate>;
|
|
91
|
-
}
|
|
92
|
-
export {};
|
|
1
|
+
export * from "langchain-core/prompts";
|
package/dist/prompts/prompt.js
CHANGED
|
@@ -1,144 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// Replace with "string" when we are comfortable with a breaking change.
|
|
3
|
-
import { BaseStringPromptTemplate, } from "./base.js";
|
|
4
|
-
import { checkValidTemplate, parseTemplate, renderTemplate, } from "./template.js";
|
|
5
|
-
/**
|
|
6
|
-
* Schema to represent a basic prompt for an LLM.
|
|
7
|
-
* @augments BasePromptTemplate
|
|
8
|
-
* @augments PromptTemplateInput
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* import { PromptTemplate } from "langchain/prompts";
|
|
13
|
-
*
|
|
14
|
-
* const prompt = new PromptTemplate({
|
|
15
|
-
* inputVariables: ["foo"],
|
|
16
|
-
* template: "Say {foo}",
|
|
17
|
-
* });
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export class PromptTemplate extends BaseStringPromptTemplate {
|
|
21
|
-
static lc_name() {
|
|
22
|
-
return "PromptTemplate";
|
|
23
|
-
}
|
|
24
|
-
constructor(input) {
|
|
25
|
-
super(input);
|
|
26
|
-
Object.defineProperty(this, "template", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true,
|
|
30
|
-
value: void 0
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(this, "templateFormat", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: "f-string"
|
|
37
|
-
});
|
|
38
|
-
Object.defineProperty(this, "validateTemplate", {
|
|
39
|
-
enumerable: true,
|
|
40
|
-
configurable: true,
|
|
41
|
-
writable: true,
|
|
42
|
-
value: true
|
|
43
|
-
});
|
|
44
|
-
Object.assign(this, input);
|
|
45
|
-
if (this.validateTemplate) {
|
|
46
|
-
let totalInputVariables = this.inputVariables;
|
|
47
|
-
if (this.partialVariables) {
|
|
48
|
-
totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
|
|
49
|
-
}
|
|
50
|
-
checkValidTemplate(this.template, this.templateFormat, totalInputVariables);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
_getPromptType() {
|
|
54
|
-
return "prompt";
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Formats the prompt template with the provided values.
|
|
58
|
-
* @param values The values to be used to format the prompt template.
|
|
59
|
-
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
60
|
-
*/
|
|
61
|
-
async format(values) {
|
|
62
|
-
const allValues = await this.mergePartialAndUserVariables(values);
|
|
63
|
-
return renderTemplate(this.template, this.templateFormat, allValues);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Take examples in list format with prefix and suffix to create a prompt.
|
|
67
|
-
*
|
|
68
|
-
* Intended to be used a a way to dynamically create a prompt from examples.
|
|
69
|
-
*
|
|
70
|
-
* @param examples - List of examples to use in the prompt.
|
|
71
|
-
* @param suffix - String to go after the list of examples. Should generally set up the user's input.
|
|
72
|
-
* @param inputVariables - A list of variable names the final prompt template will expect
|
|
73
|
-
* @param exampleSeparator - The separator to use in between examples
|
|
74
|
-
* @param prefix - String that should go before any examples. Generally includes examples.
|
|
75
|
-
*
|
|
76
|
-
* @returns The final prompt template generated.
|
|
77
|
-
*/
|
|
78
|
-
static fromExamples(examples, suffix, inputVariables, exampleSeparator = "\n\n", prefix = "") {
|
|
79
|
-
const template = [prefix, ...examples, suffix].join(exampleSeparator);
|
|
80
|
-
return new PromptTemplate({
|
|
81
|
-
inputVariables,
|
|
82
|
-
template,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Load prompt template from a template f-string
|
|
87
|
-
*/
|
|
88
|
-
static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
|
|
89
|
-
const names = new Set();
|
|
90
|
-
parseTemplate(template, templateFormat).forEach((node) => {
|
|
91
|
-
if (node.type === "variable") {
|
|
92
|
-
names.add(node.name);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
return new PromptTemplate({
|
|
96
|
-
// Rely on extracted types
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
-
inputVariables: [...names],
|
|
99
|
-
templateFormat,
|
|
100
|
-
template,
|
|
101
|
-
...rest,
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Partially applies values to the prompt template.
|
|
106
|
-
* @param values The values to be partially applied to the prompt template.
|
|
107
|
-
* @returns A new instance of PromptTemplate with the partially applied values.
|
|
108
|
-
*/
|
|
109
|
-
async partial(values) {
|
|
110
|
-
const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
|
|
111
|
-
const newPartialVariables = {
|
|
112
|
-
...(this.partialVariables ?? {}),
|
|
113
|
-
...values,
|
|
114
|
-
};
|
|
115
|
-
const promptDict = {
|
|
116
|
-
...this,
|
|
117
|
-
inputVariables: newInputVariables,
|
|
118
|
-
partialVariables: newPartialVariables,
|
|
119
|
-
};
|
|
120
|
-
return new PromptTemplate(promptDict);
|
|
121
|
-
}
|
|
122
|
-
serialize() {
|
|
123
|
-
if (this.outputParser !== undefined) {
|
|
124
|
-
throw new Error("Cannot serialize a prompt template with an output parser");
|
|
125
|
-
}
|
|
126
|
-
return {
|
|
127
|
-
_type: this._getPromptType(),
|
|
128
|
-
input_variables: this.inputVariables,
|
|
129
|
-
template: this.template,
|
|
130
|
-
template_format: this.templateFormat,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
static async deserialize(data) {
|
|
134
|
-
if (!data.template) {
|
|
135
|
-
throw new Error("Prompt template must have a template");
|
|
136
|
-
}
|
|
137
|
-
const res = new PromptTemplate({
|
|
138
|
-
inputVariables: data.input_variables,
|
|
139
|
-
template: data.template,
|
|
140
|
-
templateFormat: data.template_format,
|
|
141
|
-
});
|
|
142
|
-
return res;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
1
|
+
export * from "langchain-core/prompts";
|