-
Notifications
You must be signed in to change notification settings - Fork 2k
/
pipeline.ts
197 lines (183 loc) Β· 5.94 KB
/
pipeline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import type { InputValues, PartialValues } from "../utils/types.js";
import type { SerializedBasePromptTemplate } from "./serde.js";
import { BasePromptTemplate, type BasePromptTemplateInput } from "./base.js";
import { ChatPromptTemplate } from "./chat.js";
/**
* Type that includes the name of the prompt and the prompt itself.
*/
export type PipelinePromptParams<
PromptTemplateType extends BasePromptTemplate
> = {
name: string;
prompt: PromptTemplateType;
};
/**
* Type that extends the BasePromptTemplateInput type, excluding the
* inputVariables property. It includes an array of pipelinePrompts and a
* finalPrompt.
*/
export type PipelinePromptTemplateInput<
PromptTemplateType extends BasePromptTemplate
> = Omit<BasePromptTemplateInput, "inputVariables"> & {
pipelinePrompts: PipelinePromptParams<PromptTemplateType>[];
finalPrompt: PromptTemplateType;
};
/**
* Class that handles a sequence of prompts, each of which may require
* different input variables. Includes methods for formatting these
* prompts, extracting required input values, and handling partial
* prompts.
* @example
* ```typescript
* const composedPrompt = new PipelinePromptTemplate({
* pipelinePrompts: [
* {
* name: "introduction",
* prompt: PromptTemplate.fromTemplate(`You are impersonating {person}.`),
* },
* {
* name: "example",
* prompt: PromptTemplate.fromTemplate(
* `Here's an example of an interaction:
* Q: {example_q}
* A: {example_a}`,
* ),
* },
* {
* name: "start",
* prompt: PromptTemplate.fromTemplate(
* `Now, do this for real!
* Q: {input}
* A:`,
* ),
* },
* ],
* finalPrompt: PromptTemplate.fromTemplate(
* `{introduction}
* {example}
* {start}`,
* ),
* });
*
* const formattedPrompt = await composedPrompt.format({
* person: "Elon Musk",
* example_q: `What's your favorite car?`,
* example_a: "Tesla",
* input: `What's your favorite social media site?`,
* });
* ```
*/
export class PipelinePromptTemplate<
PromptTemplateType extends BasePromptTemplate
> extends BasePromptTemplate {
static lc_name() {
return "PipelinePromptTemplate";
}
pipelinePrompts: PipelinePromptParams<PromptTemplateType>[];
finalPrompt: PromptTemplateType;
constructor(input: PipelinePromptTemplateInput<PromptTemplateType>) {
super({ ...input, inputVariables: [] });
this.pipelinePrompts = input.pipelinePrompts;
this.finalPrompt = input.finalPrompt;
this.inputVariables = this.computeInputValues();
}
/**
* Computes the input values required by the pipeline prompts.
* @returns Array of input values required by the pipeline prompts.
*/
protected computeInputValues() {
const intermediateValues = this.pipelinePrompts.map(
(pipelinePrompt) => pipelinePrompt.name
);
const inputValues = this.pipelinePrompts
.map((pipelinePrompt) =>
pipelinePrompt.prompt.inputVariables.filter(
(inputValue) => !intermediateValues.includes(inputValue)
)
)
.flat();
return [...new Set(inputValues)];
}
protected static extractRequiredInputValues(
allValues: InputValues,
requiredValueNames: string[]
) {
return requiredValueNames.reduce((requiredValues, valueName) => {
// eslint-disable-next-line no-param-reassign
requiredValues[valueName] = allValues[valueName];
return requiredValues;
}, {} as InputValues);
}
/**
* Formats the pipeline prompts based on the provided input values.
* @param values Input values to format the pipeline prompts.
* @returns Promise that resolves with the formatted input values.
*/
protected async formatPipelinePrompts(
values: InputValues
): Promise<InputValues> {
const allValues = await this.mergePartialAndUserVariables(values);
for (const { name: pipelinePromptName, prompt: pipelinePrompt } of this
.pipelinePrompts) {
const pipelinePromptInputValues =
PipelinePromptTemplate.extractRequiredInputValues(
allValues,
pipelinePrompt.inputVariables
);
// eslint-disable-next-line no-instanceof/no-instanceof
if (pipelinePrompt instanceof ChatPromptTemplate) {
allValues[pipelinePromptName] = await pipelinePrompt.formatMessages(
pipelinePromptInputValues
);
} else {
allValues[pipelinePromptName] = await pipelinePrompt.format(
pipelinePromptInputValues
);
}
}
return PipelinePromptTemplate.extractRequiredInputValues(
allValues,
this.finalPrompt.inputVariables
);
}
/**
* Formats the final prompt value based on the provided input values.
* @param values Input values to format the final prompt value.
* @returns Promise that resolves with the formatted final prompt value.
*/
async formatPromptValue(
values: InputValues
): Promise<PromptTemplateType["PromptValueReturnType"]> {
return this.finalPrompt.formatPromptValue(
await this.formatPipelinePrompts(values)
);
}
async format(values: InputValues): Promise<string> {
return this.finalPrompt.format(await this.formatPipelinePrompts(values));
}
/**
* Handles partial prompts, which are prompts that have been partially
* filled with input values.
* @param values Partial input values.
* @returns Promise that resolves with a new PipelinePromptTemplate instance with updated input variables.
*/
async partial(
values: PartialValues
): Promise<PipelinePromptTemplate<PromptTemplateType>> {
const promptDict = { ...this };
promptDict.inputVariables = this.inputVariables.filter(
(iv) => !(iv in values)
);
promptDict.partialVariables = {
...(this.partialVariables ?? {}),
...values,
};
return new PipelinePromptTemplate<PromptTemplateType>(promptDict);
}
serialize(): SerializedBasePromptTemplate {
throw new Error("Not implemented.");
}
_getPromptType(): string {
return "pipeline";
}
}