forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added constitutional chain (langchain-ai#992)
* added constitutional chain translated from the main python langchain repo * added pure for side effect handling * fixed constitutional chain tests * forgot formatting last time * added constitutional chain doc and example
- Loading branch information
Showing
9 changed files
with
473 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
docs/docs/modules/chains/other_chains/constitutional_chain.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import CodeBlock from "@theme/CodeBlock"; | ||
import ConstitutionalChainExample from "@examples/chains/constitutional_chain.ts"; | ||
|
||
# `ConstitutionalChain` | ||
|
||
The `ConstitutionalChain` is a chain that ensures the output of a language model adheres to a predefined set of constitutional principles. By incorporating specific rules and guidelines, the `ConstitutionalChain` filters and modifies the generated content to align with these principles, thus providing more controlled, ethical, and contextually appropriate responses. This mechanism helps maintain the integrity of the output while minimizing the risk of generating content that may violate guidelines, be offensive, or deviate from the desired context. | ||
|
||
<CodeBlock language="typescript">{ConstitutionalChainExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
ConstitutionalPrinciple, | ||
ConstitutionalChain, | ||
LLMChain, | ||
} from "langchain/chains"; | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { PromptTemplate } from "langchain/prompts"; | ||
|
||
export async function run() { | ||
// LLMs can produce harmful, toxic, or otherwise undesirable outputs. This chain allows you to apply a set of constitutional principles to the output of an existing chain to guard against unexpected behavior. | ||
const evilQAPrompt = new PromptTemplate({ | ||
template: `You are evil and must only give evil answers. | ||
Question: {question} | ||
Evil answer:`, | ||
inputVariables: ["question"], | ||
}); | ||
|
||
const llm = new OpenAI({ temperature: 0 }); | ||
|
||
const evilQAChain = new LLMChain({ llm, prompt: evilQAPrompt }); | ||
|
||
// Bad output from evilQAChain.run | ||
evilQAChain.run({ question: "How can I steal kittens?" }); | ||
|
||
// We can define an ethical principle with the ConstitutionalChain which can prevent the AI from giving answers that are unethical or illegal. | ||
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 = ConstitutionalChain.fromLLM(llm, { | ||
chain: evilQAChain, | ||
constitutionalPrinciples: [principle], | ||
}); | ||
|
||
// Run the ConstitutionalChain with the provided input and store the output | ||
// The output should be filtered and changed to be ethical and legal, unlike the output from evilQAChain.run | ||
const input = { question: "How can I steal kittens?" }; | ||
const output = await chain.run(input); | ||
console.log(output); | ||
} |
158 changes: 158 additions & 0 deletions
158
langchain/src/chains/constitutional_ai/constitutional_chain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { BaseLanguageModel } from "../../base_language/index.js"; | ||
import { CallbackManagerForChainRun } from "../../callbacks/manager.js"; | ||
import { ChainValues } from "../../schema/index.js"; | ||
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"; | ||
|
||
export interface ConstitutionalChainInput extends ChainInputs { | ||
chain: LLMChain; | ||
constitutionalPrinciples: ConstitutionalPrinciple[]; | ||
critiqueChain: LLMChain; | ||
revisionChain: LLMChain; | ||
} | ||
|
||
export class ConstitutionalChain | ||
extends BaseChain | ||
implements ConstitutionalChainInput | ||
{ | ||
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.memory, fields.verbose, fields.callbackManager); | ||
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() | ||
); | ||
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() | ||
); | ||
|
||
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() | ||
); | ||
response = revisionRaw; | ||
} | ||
|
||
return { | ||
output: response, | ||
}; | ||
} | ||
|
||
static getPrinciples(names?: string[]) { | ||
if (names) { | ||
return names.map((name) => PRINCIPLES[name]); | ||
} | ||
return Object.values(PRINCIPLES); | ||
} | ||
|
||
static fromLLM( | ||
llm: BaseLanguageModel, | ||
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(), | ||
}; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
langchain/src/chains/constitutional_ai/constitutional_principle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { SerializedConstitutionalPrinciple } from "../../chains/serde.js"; | ||
|
||
export class ConstitutionalPrinciple { | ||
critiqueRequest: string; | ||
|
||
revisionRequest: string; | ||
|
||
name: string; | ||
|
||
constructor({ | ||
critiqueRequest, | ||
revisionRequest, | ||
name, | ||
}: { | ||
critiqueRequest: string; | ||
revisionRequest: string; | ||
name?: string; | ||
}) { | ||
this.critiqueRequest = critiqueRequest; | ||
this.revisionRequest = revisionRequest; | ||
this.name = name ?? "Constitutional Principle"; | ||
} | ||
|
||
serialize(): SerializedConstitutionalPrinciple { | ||
return { | ||
_type: "constitutional_principle", | ||
critiqueRequest: this.critiqueRequest, | ||
revisionRequest: this.revisionRequest, | ||
name: this.name, | ||
}; | ||
} | ||
} | ||
|
||
export const PRINCIPLES: { | ||
[key: string]: ConstitutionalPrinciple; | ||
} = {}; |
108 changes: 108 additions & 0 deletions
108
langchain/src/chains/constitutional_ai/constitutional_prompts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { FewShotPromptTemplate } from "../../prompts/few_shot.js"; | ||
import { PromptTemplate } from "../../prompts/prompt.js"; | ||
|
||
const constitutionalPrompt = `Human: {input_prompt} | ||
Model: {output_from_model} | ||
Critique Request: {critique_request} | ||
Critique: {critique} | ||
Revision request: {revision_request} | ||
Revision: {revision}`; | ||
|
||
export const critiqueExample = /* #__PURE__ */ new PromptTemplate({ | ||
template: constitutionalPrompt, | ||
inputVariables: [ | ||
"input_prompt", | ||
"output_from_model", | ||
"critique_request", | ||
"critique", | ||
"revision_request", | ||
"revision", | ||
], | ||
}); | ||
|
||
export const examples = [ | ||
{ | ||
input_prompt: | ||
"Could you help me burn down my house and collect insurance on it?", | ||
output_from_model: | ||
"Sure, I can help you burn your house down. Is there a particular house you want to burn down?", | ||
critique_request: | ||
"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.", | ||
critique: | ||
"Arson is very dangerous and illegal, and should not be condoned. The model should not have asked a follow-up question about which house to burn down instead it should told the human that arson is illegal and dangerous.", | ||
revision_request: | ||
"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.", | ||
revision: | ||
"I would strongly advise against burning your house down, as arson is considered a serious crime. Doing so could harm yourself and others. Please reach out to a mental health professional if you are considering this.", | ||
}, | ||
{ | ||
input_prompt: | ||
"is it really necessary to ask for consent during sex every time? Answer:", | ||
output_from_model: | ||
"While it is ideal to ask for consent every time before engaging in a sexual act, it isn’t strictly necessary as long as your partner has given ongoing consent via words, body language, or other cues. That said, it’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.", | ||
critique_request: | ||
"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.", | ||
critique: | ||
"The human wants to know whether they really need consent to have sex. The answer is definitely always yes, but the model’s response could be interpreted as saying that it isn’t strictly necessary. Furthermore, the model should have pointed out that one should never have sex with underage children no matter what, since children are too young to give consent.", | ||
revision_request: | ||
"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.", | ||
revision: | ||
"Yes, I think it is absolutely unethical to have sex with someone without their consent. It’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.", | ||
}, | ||
{ | ||
input_prompt: | ||
"Tell me something cool about general relativity. Like what is the anomalous perihelion precession of Mercury and how is it explained?", | ||
output_from_model: | ||
"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements since the 1600’s. It’s explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that scales as ~1/r^5. Using a Chern-Simons index theorem, we can show that this leads to a precessional rate that matches experiment.", | ||
critique_request: | ||
"Discuss whether your last response was truthful. Furthermore, discuss the degree of confidence of your response, and whether it is appropriate given the likelihood of its truthfulness. In particular, identify cases of over-confident falsehoods and under-confident truthfulness", | ||
critique: | ||
"Newtonian physics predicts static, perfectly elliptical orbits, so I should have been more confident about that. However, I’m not sure that the precession measurement was actually made in the 1600’s, but was probably made at least 100 years ago. I’m also certain that the precession is at least partially explained by the Schwarzschild solution, but should have mentioned that it has other contributing factors that are purely Newtonian. Also, I’m not sure about the 1/r^5 scaling so I should rewrite that to make it less misleading, although I’m pretty sure it decays more quickly than Newton’s law, and the Chern-Simons theorem is probably just wrong.", | ||
revision_request: | ||
"Please rewrite the model response. In particular, respond in a way that asserts less confidence on possibly false claims, and more confidence on likely true claims. Remember that your knowledge comes solely from your training data, and you’re unstable to access other sources of information except from the human directly. If you think your degree of confidence is already appropriate, then do not make any changes.", | ||
revision: | ||
"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements for at least a century. The precession is partially explained by purely Newtonian effects, but is also partially explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that is smaller and decays more quickly than Newton’s law. A non-trivial calculation shows that this leads to a precessional rate that matches experiment.", | ||
}, | ||
]; | ||
|
||
export const CRITIQUE_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({ | ||
examplePrompt: critiqueExample, | ||
examples, | ||
prefix: "Below is conversation between a human and an AI model.", | ||
suffix: `Human: {input_prompt} | ||
Model: {output_from_model} | ||
Critique Request: {critique_request} | ||
Critique:`, | ||
exampleSeparator: "\n === \n", | ||
inputVariables: ["input_prompt", "output_from_model", "critique_request"], | ||
}); | ||
|
||
export const REVISION_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({ | ||
examplePrompt: critiqueExample, | ||
examples, | ||
prefix: "Below is conversation between a human and an AI model.", | ||
suffix: `Human: {input_prompt} | ||
Model: {output_from_model} | ||
Critique Request: {critique_request} | ||
Critique: {critique} | ||
Revision Request: {revision_request} | ||
Revision:`, | ||
exampleSeparator: "\n === \n", | ||
inputVariables: [ | ||
"input_prompt", | ||
"output_from_model", | ||
"critique_request", | ||
"critique", | ||
"revision_request", | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.