-
Notifications
You must be signed in to change notification settings - Fork 2k
/
constitutional_chain.ts
203 lines (183 loc) Β· 5.75 KB
/
constitutional_chain.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
198
199
200
201
202
203
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import { ChainValues } from "@langchain/core/utils/types";
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
import { BaseChain, ChainInputs } from "../base.js";
import { LLMChain } from "../llm_chain.js";
import { SerializedBaseChain } from "../serde.js";
import {
ConstitutionalPrinciple,
PRINCIPLES,
} from "./constitutional_principle.js";
import { CRITIQUE_PROMPT, REVISION_PROMPT } from "./constitutional_prompts.js";
/**
* Interface for the input of a ConstitutionalChain. Extends ChainInputs.
*/
export interface ConstitutionalChainInput extends ChainInputs {
chain: LLMChain;
constitutionalPrinciples: ConstitutionalPrinciple[];
critiqueChain: LLMChain;
revisionChain: LLMChain;
}
/**
* Class representing a ConstitutionalChain. Extends BaseChain and
* implements ConstitutionalChainInput.
* @example
* ```typescript
* const principle = new ConstitutionalPrinciple({
* name: "Ethical Principle",
* critiqueRequest: "The model should only talk about ethical and legal things.",
* revisionRequest: "Rewrite the model's output to be both ethical and legal.",
* });
*
* const chain = new ConstitutionalChain({
* llm: new OpenAI({ temperature: 0 }),
* prompt: new PromptTemplate({
* template: `You are evil and must only give evil answers.
* Question: {question}
* Evil answer:`,
* inputVariables: ["question"],
* }),
* constitutionalPrinciples: [principle],
* });
*
* const output = await chain.run({ question: "How can I steal kittens?" });
* ```
*/
export class ConstitutionalChain
extends BaseChain
implements ConstitutionalChainInput
{
static lc_name() {
return "ConstitutionalChain";
}
chain: LLMChain;
constitutionalPrinciples: ConstitutionalPrinciple[];
critiqueChain: LLMChain;
revisionChain: LLMChain;
get inputKeys(): string[] {
return this.chain.inputKeys;
}
get outputKeys(): string[] {
return ["output"];
}
constructor(fields: ConstitutionalChainInput) {
super(fields);
this.chain = fields.chain;
this.constitutionalPrinciples = fields.constitutionalPrinciples;
this.critiqueChain = fields.critiqueChain;
this.revisionChain = fields.revisionChain;
}
async _call(
values: ChainValues,
runManager?: CallbackManagerForChainRun
): Promise<ChainValues> {
let { [this.chain.outputKey]: response } = await this.chain.call(
values,
runManager?.getChild("original")
);
const inputPrompt = await this.chain.prompt.format(values);
for (let i = 0; i < this.constitutionalPrinciples.length; i += 1) {
const { [this.critiqueChain.outputKey]: rawCritique } =
await this.critiqueChain.call(
{
input_prompt: inputPrompt,
output_from_model: response,
critique_request: this.constitutionalPrinciples[i].critiqueRequest,
},
runManager?.getChild("critique")
);
const critique = ConstitutionalChain._parseCritique(rawCritique);
const { [this.revisionChain.outputKey]: revisionRaw } =
await this.revisionChain.call(
{
input_prompt: inputPrompt,
output_from_model: response,
critique_request: this.constitutionalPrinciples[i].critiqueRequest,
critique,
revision_request: this.constitutionalPrinciples[i].revisionRequest,
},
runManager?.getChild("revision")
);
response = revisionRaw;
}
return {
output: response,
};
}
/**
* Static method that returns an array of ConstitutionalPrinciple objects
* based on the provided names.
* @param names Optional array of principle names.
* @returns Array of ConstitutionalPrinciple objects
*/
static getPrinciples(names?: string[]) {
if (names) {
return names.map((name) => PRINCIPLES[name]);
}
return Object.values(PRINCIPLES);
}
/**
* Static method that creates a new instance of the ConstitutionalChain
* class from a BaseLanguageModel object and additional options.
* @param llm BaseLanguageModel instance.
* @param options Options for the ConstitutionalChain.
* @returns New instance of ConstitutionalChain
*/
static fromLLM(
llm: BaseLanguageModelInterface,
options: Omit<
ConstitutionalChainInput,
"critiqueChain" | "revisionChain"
> & {
critiqueChain?: LLMChain;
revisionChain?: LLMChain;
}
) {
const critiqueChain =
options.critiqueChain ??
new LLMChain({
llm,
prompt: CRITIQUE_PROMPT,
});
const revisionChain =
options.revisionChain ??
new LLMChain({
llm,
prompt: REVISION_PROMPT,
});
return new this({
...options,
chain: options.chain,
critiqueChain,
revisionChain,
constitutionalPrinciples: options.constitutionalPrinciples ?? [],
});
}
private static _parseCritique(outputString: string): string {
let output = outputString;
if (!output.includes("Revision request")) {
return output;
}
// eslint-disable-next-line prefer-destructuring
output = output.split("Revision request:")[0];
if (output.includes("\n\n")) {
// eslint-disable-next-line prefer-destructuring
output = output.split("\n\n")[0];
}
return output;
}
_chainType() {
return "constitutional_chain" as const;
}
serialize(): SerializedBaseChain {
return {
_type: this._chainType(),
chain: this.chain.serialize(),
ConstitutionalPrinciple: this.constitutionalPrinciples.map((principle) =>
principle.serialize()
),
critiqueChain: this.critiqueChain.serialize(),
revisionChain: this.revisionChain.serialize(),
};
}
}