Skip to main content

Self-critique chain with constitutional AI

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.

npm install @langchain/openai
import {
ConstitutionalPrinciple,
ConstitutionalChain,
LLMChain,
} from "langchain/chains";
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";

// 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);

API Reference:


Help us out by providing feedback on this documentation page: