Skip to main content

宪法链

宪法链是一种链式结构,它确保语言模型的输出遵循预定义的宪法原则。通过纳入特定的规则和指南,宪法链可以过滤和修改生成的内容以符合这些原则,从而提供更加受控、道德和上下文恰当的响应。这种机制有助于保持输出的完整性,同时最大程度地减少生成可能违反指南、具有冒犯性或偏离所需上下文的内容的风险。

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