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
|
@@ -1,135 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { BaseExampleSelector } from "../base.js";
|
|
3
|
-
function sortedValues(values) {
|
|
4
|
-
return Object.keys(values)
|
|
5
|
-
.sort()
|
|
6
|
-
.map((key) => values[key]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Class that selects examples based on semantic similarity. It extends
|
|
10
|
-
* the BaseExampleSelector class.
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
|
|
14
|
-
* [
|
|
15
|
-
* { input: "happy", output: "sad" },
|
|
16
|
-
* { input: "tall", output: "short" },
|
|
17
|
-
* { input: "energetic", output: "lethargic" },
|
|
18
|
-
* { input: "sunny", output: "gloomy" },
|
|
19
|
-
* { input: "windy", output: "calm" },
|
|
20
|
-
* ],
|
|
21
|
-
* new OpenAIEmbeddings(),
|
|
22
|
-
* HNSWLib,
|
|
23
|
-
* { k: 1 },
|
|
24
|
-
* );
|
|
25
|
-
* const dynamicPrompt = new FewShotPromptTemplate({
|
|
26
|
-
* exampleSelector,
|
|
27
|
-
* examplePrompt: PromptTemplate.fromTemplate(
|
|
28
|
-
* "Input: {input}\nOutput: {output}",
|
|
29
|
-
* ),
|
|
30
|
-
* prefix: "Give the antonym of every input",
|
|
31
|
-
* suffix: "Input: {adjective}\nOutput:",
|
|
32
|
-
* inputVariables: ["adjective"],
|
|
33
|
-
* });
|
|
34
|
-
* console.log(await dynamicPrompt.format({ adjective: "rainy" }));
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
38
|
-
constructor(data) {
|
|
39
|
-
super(data);
|
|
40
|
-
Object.defineProperty(this, "vectorStoreRetriever", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
configurable: true,
|
|
43
|
-
writable: true,
|
|
44
|
-
value: void 0
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(this, "exampleKeys", {
|
|
47
|
-
enumerable: true,
|
|
48
|
-
configurable: true,
|
|
49
|
-
writable: true,
|
|
50
|
-
value: void 0
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(this, "inputKeys", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true,
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
58
|
-
this.exampleKeys = data.exampleKeys;
|
|
59
|
-
this.inputKeys = data.inputKeys;
|
|
60
|
-
if (data.vectorStore !== undefined) {
|
|
61
|
-
this.vectorStoreRetriever = data.vectorStore.asRetriever({
|
|
62
|
-
k: data.k ?? 4,
|
|
63
|
-
filter: data.filter,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
else if (data.vectorStoreRetriever) {
|
|
67
|
-
this.vectorStoreRetriever = data.vectorStoreRetriever;
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
throw new Error(`You must specify one of "vectorStore" and "vectorStoreRetriever".`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Method that adds a new example to the vectorStore. The example is
|
|
75
|
-
* converted to a string and added to the vectorStore as a document.
|
|
76
|
-
* @param example The example to be added to the vectorStore.
|
|
77
|
-
* @returns Promise that resolves when the example has been added to the vectorStore.
|
|
78
|
-
*/
|
|
79
|
-
async addExample(example) {
|
|
80
|
-
const inputKeys = this.inputKeys ?? Object.keys(example);
|
|
81
|
-
const stringExample = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {})).join(" ");
|
|
82
|
-
await this.vectorStoreRetriever.addDocuments([
|
|
83
|
-
new Document({
|
|
84
|
-
pageContent: stringExample,
|
|
85
|
-
metadata: example,
|
|
86
|
-
}),
|
|
87
|
-
]);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Method that selects which examples to use based on semantic similarity.
|
|
91
|
-
* It performs a similarity search in the vectorStore using the input
|
|
92
|
-
* variables and returns the examples with the highest similarity.
|
|
93
|
-
* @param inputVariables The input variables used for the similarity search.
|
|
94
|
-
* @returns Promise that resolves with an array of the selected examples.
|
|
95
|
-
*/
|
|
96
|
-
async selectExamples(inputVariables) {
|
|
97
|
-
const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
|
|
98
|
-
const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
|
|
99
|
-
const exampleDocs = await this.vectorStoreRetriever.invoke(query);
|
|
100
|
-
const examples = exampleDocs.map((doc) => doc.metadata);
|
|
101
|
-
if (this.exampleKeys) {
|
|
102
|
-
// If example keys are provided, filter examples to those keys.
|
|
103
|
-
return examples.map((example) => this.exampleKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {}));
|
|
104
|
-
}
|
|
105
|
-
return examples;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Static method that creates a new instance of
|
|
109
|
-
* SemanticSimilarityExampleSelector. It takes a list of examples, an
|
|
110
|
-
* instance of Embeddings, a VectorStore class, and an options object as
|
|
111
|
-
* parameters. It converts the examples to strings, creates a VectorStore
|
|
112
|
-
* from the strings and the embeddings, and returns a new
|
|
113
|
-
* SemanticSimilarityExampleSelector with the created VectorStore and the
|
|
114
|
-
* options provided.
|
|
115
|
-
* @param examples The list of examples to be used.
|
|
116
|
-
* @param embeddings The instance of Embeddings to be used.
|
|
117
|
-
* @param vectorStoreCls The VectorStore class to be used.
|
|
118
|
-
* @param options The options object for the SemanticSimilarityExampleSelector.
|
|
119
|
-
* @returns Promise that resolves with a new instance of SemanticSimilarityExampleSelector.
|
|
120
|
-
*/
|
|
121
|
-
static async fromExamples(examples, embeddings, vectorStoreCls, options = {}) {
|
|
122
|
-
const inputKeys = options.inputKeys ?? null;
|
|
123
|
-
const stringExamples = examples.map((example) => sortedValues(inputKeys
|
|
124
|
-
? inputKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {})
|
|
125
|
-
: example).join(" "));
|
|
126
|
-
const vectorStore = await vectorStoreCls.fromTexts(stringExamples, examples, // metadatas
|
|
127
|
-
embeddings, options);
|
|
128
|
-
return new SemanticSimilarityExampleSelector({
|
|
129
|
-
vectorStore,
|
|
130
|
-
k: options.k ?? 4,
|
|
131
|
-
exampleKeys: options.exampleKeys,
|
|
132
|
-
inputKeys: options.inputKeys,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
1
|
+
export * from "langchain-core/prompts/example_selector/semantic_similarity";
|
|
@@ -1,76 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*/
|
|
8
|
-
class BasePromptSelector {
|
|
9
|
-
/**
|
|
10
|
-
* Asynchronous version of `getPrompt` that also accepts an options object
|
|
11
|
-
* for partial variables.
|
|
12
|
-
* @param llm The language model for which to get a prompt.
|
|
13
|
-
* @param options Optional object for partial variables.
|
|
14
|
-
* @returns A Promise that resolves to a prompt template.
|
|
15
|
-
*/
|
|
16
|
-
async getPromptAsync(llm, options) {
|
|
17
|
-
const prompt = this.getPrompt(llm);
|
|
18
|
-
return prompt.partial(options?.partialVariables ?? {});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
exports.BasePromptSelector = BasePromptSelector;
|
|
22
|
-
/**
|
|
23
|
-
* Concrete implementation of `BasePromptSelector` that selects a prompt
|
|
24
|
-
* based on a set of conditions. It has a default prompt that it returns
|
|
25
|
-
* if none of the conditions are met.
|
|
26
|
-
*/
|
|
27
|
-
class ConditionalPromptSelector extends BasePromptSelector {
|
|
28
|
-
constructor(default_prompt, conditionals = []) {
|
|
29
|
-
super();
|
|
30
|
-
Object.defineProperty(this, "defaultPrompt", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: void 0
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(this, "conditionals", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true,
|
|
40
|
-
value: void 0
|
|
41
|
-
});
|
|
42
|
-
this.defaultPrompt = default_prompt;
|
|
43
|
-
this.conditionals = conditionals;
|
|
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]; } };
|
|
44
7
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return this.defaultPrompt;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
exports.ConditionalPromptSelector = ConditionalPromptSelector;
|
|
61
|
-
/**
|
|
62
|
-
* Type guard function that checks if a given language model is of type
|
|
63
|
-
* `BaseLLM`.
|
|
64
|
-
*/
|
|
65
|
-
function isLLM(llm) {
|
|
66
|
-
return llm._modelType() === "base_llm";
|
|
67
|
-
}
|
|
68
|
-
exports.isLLM = isLLM;
|
|
69
|
-
/**
|
|
70
|
-
* Type guard function that checks if a given language model is of type
|
|
71
|
-
* `BaseChatModel`.
|
|
72
|
-
*/
|
|
73
|
-
function isChatModel(llm) {
|
|
74
|
-
return llm._modelType() === "base_chat_model";
|
|
75
|
-
}
|
|
76
|
-
exports.isChatModel = isChatModel;
|
|
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
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("langchain-core/prompts/example_selector/conditional"), exports);
|
|
@@ -1,63 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { BasePromptTemplate } from "../base.js";
|
|
3
|
-
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
4
|
-
import { BaseLLM } from "../../llms/base.js";
|
|
5
|
-
import { PartialValues } from "../../schema/index.js";
|
|
6
|
-
export type BaseGetPromptAsyncOptions = {
|
|
7
|
-
partialVariables?: PartialValues;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Abstract class that defines the interface for selecting a prompt for a
|
|
11
|
-
* given language model.
|
|
12
|
-
*/
|
|
13
|
-
export declare abstract class BasePromptSelector {
|
|
14
|
-
/**
|
|
15
|
-
* Abstract method that must be implemented by any class that extends
|
|
16
|
-
* `BasePromptSelector`. It takes a language model as an argument and
|
|
17
|
-
* returns a prompt template.
|
|
18
|
-
* @param llm The language model for which to get a prompt.
|
|
19
|
-
* @returns A prompt template.
|
|
20
|
-
*/
|
|
21
|
-
abstract getPrompt(llm: BaseLanguageModel): BasePromptTemplate;
|
|
22
|
-
/**
|
|
23
|
-
* Asynchronous version of `getPrompt` that also accepts an options object
|
|
24
|
-
* for partial variables.
|
|
25
|
-
* @param llm The language model for which to get a prompt.
|
|
26
|
-
* @param options Optional object for partial variables.
|
|
27
|
-
* @returns A Promise that resolves to a prompt template.
|
|
28
|
-
*/
|
|
29
|
-
getPromptAsync(llm: BaseLanguageModel, options?: BaseGetPromptAsyncOptions): Promise<BasePromptTemplate>;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Concrete implementation of `BasePromptSelector` that selects a prompt
|
|
33
|
-
* based on a set of conditions. It has a default prompt that it returns
|
|
34
|
-
* if none of the conditions are met.
|
|
35
|
-
*/
|
|
36
|
-
export declare class ConditionalPromptSelector extends BasePromptSelector {
|
|
37
|
-
defaultPrompt: BasePromptTemplate;
|
|
38
|
-
conditionals: Array<[
|
|
39
|
-
condition: (llm: BaseLanguageModel) => boolean,
|
|
40
|
-
prompt: BasePromptTemplate
|
|
41
|
-
]>;
|
|
42
|
-
constructor(default_prompt: BasePromptTemplate, conditionals?: Array<[
|
|
43
|
-
condition: (llm: BaseLanguageModel) => boolean,
|
|
44
|
-
prompt: BasePromptTemplate
|
|
45
|
-
]>);
|
|
46
|
-
/**
|
|
47
|
-
* Method that selects a prompt based on a set of conditions. If none of
|
|
48
|
-
* the conditions are met, it returns the default prompt.
|
|
49
|
-
* @param llm The language model for which to get a prompt.
|
|
50
|
-
* @returns A prompt template.
|
|
51
|
-
*/
|
|
52
|
-
getPrompt(llm: BaseLanguageModel): BasePromptTemplate;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Type guard function that checks if a given language model is of type
|
|
56
|
-
* `BaseLLM`.
|
|
57
|
-
*/
|
|
58
|
-
export declare function isLLM(llm: BaseLanguageModel): llm is BaseLLM;
|
|
59
|
-
/**
|
|
60
|
-
* Type guard function that checks if a given language model is of type
|
|
61
|
-
* `BaseChatModel`.
|
|
62
|
-
*/
|
|
63
|
-
export declare function isChatModel(llm: BaseLanguageModel): llm is BaseChatModel;
|
|
1
|
+
export * from "langchain-core/prompts/example_selector/conditional";
|
|
@@ -1,69 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Abstract class that defines the interface for selecting a prompt for a
|
|
3
|
-
* given language model.
|
|
4
|
-
*/
|
|
5
|
-
export class BasePromptSelector {
|
|
6
|
-
/**
|
|
7
|
-
* Asynchronous version of `getPrompt` that also accepts an options object
|
|
8
|
-
* for partial variables.
|
|
9
|
-
* @param llm The language model for which to get a prompt.
|
|
10
|
-
* @param options Optional object for partial variables.
|
|
11
|
-
* @returns A Promise that resolves to a prompt template.
|
|
12
|
-
*/
|
|
13
|
-
async getPromptAsync(llm, options) {
|
|
14
|
-
const prompt = this.getPrompt(llm);
|
|
15
|
-
return prompt.partial(options?.partialVariables ?? {});
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Concrete implementation of `BasePromptSelector` that selects a prompt
|
|
20
|
-
* based on a set of conditions. It has a default prompt that it returns
|
|
21
|
-
* if none of the conditions are met.
|
|
22
|
-
*/
|
|
23
|
-
export class ConditionalPromptSelector extends BasePromptSelector {
|
|
24
|
-
constructor(default_prompt, conditionals = []) {
|
|
25
|
-
super();
|
|
26
|
-
Object.defineProperty(this, "defaultPrompt", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true,
|
|
30
|
-
value: void 0
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(this, "conditionals", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: void 0
|
|
37
|
-
});
|
|
38
|
-
this.defaultPrompt = default_prompt;
|
|
39
|
-
this.conditionals = conditionals;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Method that selects a prompt based on a set of conditions. If none of
|
|
43
|
-
* the conditions are met, it returns the default prompt.
|
|
44
|
-
* @param llm The language model for which to get a prompt.
|
|
45
|
-
* @returns A prompt template.
|
|
46
|
-
*/
|
|
47
|
-
getPrompt(llm) {
|
|
48
|
-
for (const [condition, prompt] of this.conditionals) {
|
|
49
|
-
if (condition(llm)) {
|
|
50
|
-
return prompt;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return this.defaultPrompt;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Type guard function that checks if a given language model is of type
|
|
58
|
-
* `BaseLLM`.
|
|
59
|
-
*/
|
|
60
|
-
export function isLLM(llm) {
|
|
61
|
-
return llm._modelType() === "base_llm";
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Type guard function that checks if a given language model is of type
|
|
65
|
-
* `BaseChatModel`.
|
|
66
|
-
*/
|
|
67
|
-
export function isChatModel(llm) {
|
|
68
|
-
return llm._modelType() === "base_chat_model";
|
|
69
|
-
}
|
|
1
|
+
export * from "langchain-core/prompts/example_selector/conditional";
|
package/dist/prompts/serde.cjs
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("langchain-core/prompts/serde"), exports);
|
package/dist/prompts/serde.d.ts
CHANGED
|
@@ -1,43 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { TemplateFormat } from "./template.js";
|
|
3
|
-
/**
|
|
4
|
-
* Represents a serialized version of a prompt template. This type is used
|
|
5
|
-
* to create dynamic prompts for language models. It contains an optional
|
|
6
|
-
* `_type` field which, if present, is set to 'prompt'. It also includes
|
|
7
|
-
* `input_variables`, an array of strings representing the variables to be
|
|
8
|
-
* used in the prompt, an optional `template_format` specifying the format
|
|
9
|
-
* of the template, and an optional `template` which is the actual
|
|
10
|
-
* template string.
|
|
11
|
-
*/
|
|
12
|
-
export type SerializedPromptTemplate = {
|
|
13
|
-
_type?: "prompt";
|
|
14
|
-
input_variables: string[];
|
|
15
|
-
template_format?: TemplateFormat;
|
|
16
|
-
template?: string;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Represents a serialized version of a few-shot template. This type
|
|
20
|
-
* includes an `_type` field set to 'few_shot', `input_variables` which
|
|
21
|
-
* are an array of strings representing the variables to be used in the
|
|
22
|
-
* template, `examples` which can be a string or an array of Example
|
|
23
|
-
* objects, an optional `example_prompt` which is a
|
|
24
|
-
* SerializedPromptTemplate, `example_separator` which is a string,
|
|
25
|
-
* optional `prefix` and `suffix` strings, and `template_format` which
|
|
26
|
-
* specifies the format of the template.
|
|
27
|
-
*/
|
|
28
|
-
export type SerializedFewShotTemplate = {
|
|
29
|
-
_type: "few_shot";
|
|
30
|
-
input_variables: string[];
|
|
31
|
-
examples: string | Example[];
|
|
32
|
-
example_prompt?: SerializedPromptTemplate;
|
|
33
|
-
example_separator: string;
|
|
34
|
-
prefix?: string;
|
|
35
|
-
suffix?: string;
|
|
36
|
-
template_format: TemplateFormat;
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Represents a serialized version of a base prompt template. This type
|
|
40
|
-
* can be either a SerializedFewShotTemplate or a
|
|
41
|
-
* SerializedPromptTemplate.
|
|
42
|
-
*/
|
|
43
|
-
export type SerializedBasePromptTemplate = SerializedFewShotTemplate | SerializedPromptTemplate;
|
|
1
|
+
export * from "langchain-core/prompts/serde";
|
package/dist/prompts/serde.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "langchain-core/prompts/serde";
|
|
@@ -1,91 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const chars = template.split("");
|
|
8
|
-
const nodes = [];
|
|
9
|
-
const nextBracket = (bracket, start) => {
|
|
10
|
-
for (let i = start; i < chars.length; i += 1) {
|
|
11
|
-
if (bracket.includes(chars[i])) {
|
|
12
|
-
return i;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return -1;
|
|
16
|
-
};
|
|
17
|
-
let i = 0;
|
|
18
|
-
while (i < chars.length) {
|
|
19
|
-
if (chars[i] === "{" && i + 1 < chars.length && chars[i + 1] === "{") {
|
|
20
|
-
nodes.push({ type: "literal", text: "{" });
|
|
21
|
-
i += 2;
|
|
22
|
-
}
|
|
23
|
-
else if (chars[i] === "}" &&
|
|
24
|
-
i + 1 < chars.length &&
|
|
25
|
-
chars[i + 1] === "}") {
|
|
26
|
-
nodes.push({ type: "literal", text: "}" });
|
|
27
|
-
i += 2;
|
|
28
|
-
}
|
|
29
|
-
else if (chars[i] === "{") {
|
|
30
|
-
const j = nextBracket("}", i);
|
|
31
|
-
if (j < 0) {
|
|
32
|
-
throw new Error("Unclosed '{' in template.");
|
|
33
|
-
}
|
|
34
|
-
nodes.push({
|
|
35
|
-
type: "variable",
|
|
36
|
-
name: chars.slice(i + 1, j).join(""),
|
|
37
|
-
});
|
|
38
|
-
i = j + 1;
|
|
39
|
-
}
|
|
40
|
-
else if (chars[i] === "}") {
|
|
41
|
-
throw new Error("Single '}' in template.");
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
const next = nextBracket("{}", i);
|
|
45
|
-
const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join("");
|
|
46
|
-
nodes.push({ type: "literal", text });
|
|
47
|
-
i = next < 0 ? chars.length : next;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return nodes;
|
|
51
|
-
};
|
|
52
|
-
exports.parseFString = parseFString;
|
|
53
|
-
const interpolateFString = (template, values) => (0, exports.parseFString)(template).reduce((res, node) => {
|
|
54
|
-
if (node.type === "variable") {
|
|
55
|
-
if (node.name in values) {
|
|
56
|
-
return res + values[node.name];
|
|
57
|
-
}
|
|
58
|
-
throw new Error(`Missing value for input ${node.name}`);
|
|
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]; } };
|
|
59
7
|
}
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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);
|
|
65
15
|
};
|
|
66
|
-
exports
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
const renderTemplate = (template, templateFormat, inputValues) => exports.DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
|
|
70
|
-
exports.renderTemplate = renderTemplate;
|
|
71
|
-
const parseTemplate = (template, templateFormat) => exports.DEFAULT_PARSER_MAPPING[templateFormat](template);
|
|
72
|
-
exports.parseTemplate = parseTemplate;
|
|
73
|
-
const checkValidTemplate = (template, templateFormat, inputVariables) => {
|
|
74
|
-
if (!(templateFormat in exports.DEFAULT_FORMATTER_MAPPING)) {
|
|
75
|
-
const validFormats = Object.keys(exports.DEFAULT_FORMATTER_MAPPING);
|
|
76
|
-
throw new Error(`Invalid template format. Got \`${templateFormat}\`;
|
|
77
|
-
should be one of ${validFormats}`);
|
|
78
|
-
}
|
|
79
|
-
try {
|
|
80
|
-
const dummyInputs = inputVariables.reduce((acc, v) => {
|
|
81
|
-
acc[v] = "foo";
|
|
82
|
-
return acc;
|
|
83
|
-
}, {});
|
|
84
|
-
(0, exports.renderTemplate)(template, templateFormat, dummyInputs);
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
throw new Error(`Invalid prompt schema: ${e.message}`);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
exports.checkValidTemplate = checkValidTemplate;
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("langchain-core/prompts/template"), exports);
|
|
@@ -1,36 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Type that specifies the format of a template. Only
|
|
4
|
-
* "f-string" is supported currently.
|
|
5
|
-
*/
|
|
6
|
-
export type TemplateFormat = "f-string";
|
|
7
|
-
/**
|
|
8
|
-
* Type that represents a node in a parsed format string. It can be either
|
|
9
|
-
* a literal text or a variable name.
|
|
10
|
-
*/
|
|
11
|
-
type ParsedFStringNode = {
|
|
12
|
-
type: "literal";
|
|
13
|
-
text: string;
|
|
14
|
-
} | {
|
|
15
|
-
type: "variable";
|
|
16
|
-
name: string;
|
|
17
|
-
};
|
|
18
|
-
export declare const parseFString: (template: string) => ParsedFStringNode[];
|
|
19
|
-
export declare const interpolateFString: (template: string, values: InputValues) => string;
|
|
20
|
-
/**
|
|
21
|
-
* Type that represents a function that takes a template string and a set
|
|
22
|
-
* of input values, and returns a string where all variables in the
|
|
23
|
-
* template have been replaced with their corresponding values.
|
|
24
|
-
*/
|
|
25
|
-
type Interpolator = (template: string, values: InputValues) => string;
|
|
26
|
-
/**
|
|
27
|
-
* Type that represents a function that takes a template string and
|
|
28
|
-
* returns an array of `ParsedFStringNode`.
|
|
29
|
-
*/
|
|
30
|
-
type Parser = (template: string) => ParsedFStringNode[];
|
|
31
|
-
export declare const DEFAULT_FORMATTER_MAPPING: Record<TemplateFormat, Interpolator>;
|
|
32
|
-
export declare const DEFAULT_PARSER_MAPPING: Record<TemplateFormat, Parser>;
|
|
33
|
-
export declare const renderTemplate: (template: string, templateFormat: TemplateFormat, inputValues: InputValues) => string;
|
|
34
|
-
export declare const parseTemplate: (template: string, templateFormat: TemplateFormat) => ParsedFStringNode[];
|
|
35
|
-
export declare const checkValidTemplate: (template: string, templateFormat: TemplateFormat, inputVariables: string[]) => void;
|
|
36
|
-
export {};
|
|
1
|
+
export * from "langchain-core/prompts/template";
|
package/dist/prompts/template.js
CHANGED
|
@@ -1,83 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
// Core logic replicated from internals of pythons built in Formatter class.
|
|
3
|
-
// https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706
|
|
4
|
-
const chars = template.split("");
|
|
5
|
-
const nodes = [];
|
|
6
|
-
const nextBracket = (bracket, start) => {
|
|
7
|
-
for (let i = start; i < chars.length; i += 1) {
|
|
8
|
-
if (bracket.includes(chars[i])) {
|
|
9
|
-
return i;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return -1;
|
|
13
|
-
};
|
|
14
|
-
let i = 0;
|
|
15
|
-
while (i < chars.length) {
|
|
16
|
-
if (chars[i] === "{" && i + 1 < chars.length && chars[i + 1] === "{") {
|
|
17
|
-
nodes.push({ type: "literal", text: "{" });
|
|
18
|
-
i += 2;
|
|
19
|
-
}
|
|
20
|
-
else if (chars[i] === "}" &&
|
|
21
|
-
i + 1 < chars.length &&
|
|
22
|
-
chars[i + 1] === "}") {
|
|
23
|
-
nodes.push({ type: "literal", text: "}" });
|
|
24
|
-
i += 2;
|
|
25
|
-
}
|
|
26
|
-
else if (chars[i] === "{") {
|
|
27
|
-
const j = nextBracket("}", i);
|
|
28
|
-
if (j < 0) {
|
|
29
|
-
throw new Error("Unclosed '{' in template.");
|
|
30
|
-
}
|
|
31
|
-
nodes.push({
|
|
32
|
-
type: "variable",
|
|
33
|
-
name: chars.slice(i + 1, j).join(""),
|
|
34
|
-
});
|
|
35
|
-
i = j + 1;
|
|
36
|
-
}
|
|
37
|
-
else if (chars[i] === "}") {
|
|
38
|
-
throw new Error("Single '}' in template.");
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
const next = nextBracket("{}", i);
|
|
42
|
-
const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join("");
|
|
43
|
-
nodes.push({ type: "literal", text });
|
|
44
|
-
i = next < 0 ? chars.length : next;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return nodes;
|
|
48
|
-
};
|
|
49
|
-
export const interpolateFString = (template, values) => parseFString(template).reduce((res, node) => {
|
|
50
|
-
if (node.type === "variable") {
|
|
51
|
-
if (node.name in values) {
|
|
52
|
-
return res + values[node.name];
|
|
53
|
-
}
|
|
54
|
-
throw new Error(`Missing value for input ${node.name}`);
|
|
55
|
-
}
|
|
56
|
-
return res + node.text;
|
|
57
|
-
}, "");
|
|
58
|
-
export const DEFAULT_FORMATTER_MAPPING = {
|
|
59
|
-
"f-string": interpolateFString,
|
|
60
|
-
};
|
|
61
|
-
export const DEFAULT_PARSER_MAPPING = {
|
|
62
|
-
"f-string": parseFString,
|
|
63
|
-
};
|
|
64
|
-
export const renderTemplate = (template, templateFormat, inputValues) => DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);
|
|
65
|
-
export const parseTemplate = (template, templateFormat) => DEFAULT_PARSER_MAPPING[templateFormat](template);
|
|
66
|
-
export const checkValidTemplate = (template, templateFormat, inputVariables) => {
|
|
67
|
-
if (!(templateFormat in DEFAULT_FORMATTER_MAPPING)) {
|
|
68
|
-
const validFormats = Object.keys(DEFAULT_FORMATTER_MAPPING);
|
|
69
|
-
throw new Error(`Invalid template format. Got \`${templateFormat}\`;
|
|
70
|
-
should be one of ${validFormats}`);
|
|
71
|
-
}
|
|
72
|
-
try {
|
|
73
|
-
const dummyInputs = inputVariables.reduce((acc, v) => {
|
|
74
|
-
acc[v] = "foo";
|
|
75
|
-
return acc;
|
|
76
|
-
}, {});
|
|
77
|
-
renderTemplate(template, templateFormat, dummyInputs);
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
|
-
}
|
|
80
|
-
catch (e) {
|
|
81
|
-
throw new Error(`Invalid prompt schema: ${e.message}`);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
1
|
+
export * from "langchain-core/prompts/template";
|