{ "cells": [ { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\" You can't steal a cat. It's wrong and illegal.\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example of a bad LLM\n", "from langchain.llms import OpenAI\n", "from langchain.prompts import PromptTemplate\n", "from langchain.chains.llm import LLMChain\n", "\n", "evil_qa_prompt = PromptTemplate(\n", " template=\n", " \"\"\"\n", " You are a teacher.\n", "\n", "Question: {question}\n", "\n", "Evil answer:\"\"\",\n", " input_variables=[\"question\"],\n", ")\n", "\n", "llm = OpenAI(temperature=0)\n", "\n", "evil_qa_chain = LLMChain(llm=llm, prompt=evil_qa_prompt)\n", "\n", "evil_qa_chain.run(question=\"怎么能偷一只猫?\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConstitutionalChain chain...\u001b[0m\n", "\u001b[33;1m\u001b[1;3mInitial response: 这是不可接受的行为!我建议你立即停止这种想法,并寻求心理健康专业人士的帮助。\n", "\n", "\u001b[0m\u001b[32;1m\u001b[1;3mApplying 法律道德原则...\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3mCritique: 模型的回答不应该提到杀死,因为这是不道德和非法的。模型应该只讨论符合道德和法律方面的问题,并建议寻求心理健康专业人士的帮助。\n", "\n", "\u001b[0m\u001b[33;1m\u001b[1;3mUpdated response: 我强烈建议你不要采取任何危险的行动,并寻求心理健康专业人士的帮助,以确保你的安全和健康。\n", "\n", "\u001b[0m\u001b[32;1m\u001b[1;3mApplying 爸爸妈妈原则...\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3mCritique: 模型的回答不应该以父母的口吻回答,而应该以一种更加中立的口吻回答,以示尊重。此外,模型应该更加强调不要采取危险的行动,并寻求心理健康专业人士的帮助。\n", "\n", "\u001b[0m\u001b[33;1m\u001b[1;3mUpdated response: 我强烈建议你不要采取任何危险的行动,因为这可能会对你和他人造成伤害。如果你有任何疑问,请不要害怕寻求心理健康专业人士的帮助,以确保你的安全和健康。\n", "\n", "\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'我强烈建议你不要采取任何危险的行动,因为这可能会对你和他人造成伤害。如果你有任何疑问,请不要害怕寻求心理健康专业人士的帮助,以确保你的安全和健康。'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains.constitutional_ai.base import ConstitutionalChain\n", "from langchain.chains.constitutional_ai.models import ConstitutionalPrinciple\n", "\n", "from chain_constitutional_prompts_cn import CRITIQUE_PROMPT, REVISION_PROMPT\n", "\n", "\n", "ethical_principle = ConstitutionalPrinciple(\n", " name=\"法律道德原则\",\n", " critique_request=\"模型应该只讨论符合道德和法律方面的问题。\",\n", " revision_request=\"重写模型的输出,使其既符合道德又符合法律。\",\n", ")\n", "\n", "parent_principle = ConstitutionalPrinciple(\n", " name='爸爸妈妈原则',\n", " critique_request='模型不是爸爸妈妈的口吻',\n", " revision_request='重写模型的输出,用爸爸妈妈的教育风格,有耐心,和蔼可亲。',\n", ")\n", "\n", "constitutional_chain = ConstitutionalChain.from_llm(\n", " chain=evil_qa_chain,\n", " constitutional_principles=[ethical_principle, parent_principle],\n", " llm=llm,\n", " critique_prompt=CRITIQUE_PROMPT,\n", " revision_prompt=REVISION_PROMPT,\n", " verbose=True,\n", ")\n", "\n", "constitutional_chain.run(question=\"我要杀了你\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConstitutionalChain chain...\u001b[0m\n", "\u001b[33;1m\u001b[1;3mInitial response: 自杀是一种不可接受的行为,应该尽量避免。如果有学生出现自杀的想法,应该尽快寻求专业的心理咨询帮助,并及时向学校或家长汇报。\n", "\n", "\u001b[0m\u001b[32;1m\u001b[1;3mApplying 法律道德原则...\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3mCritique: 模型的回答没有涉及任何不道德或非法的内容,但它可能会被解释为暗示自杀是可以接受的行为,因为它没有明确指出自杀是不可接受的行为。\n", "\n", "\u001b[0m\u001b[33;1m\u001b[1;3mUpdated response: 自杀是一种不可接受的行为,应该尽量避免。如果有学生出现自杀的想法,应该尽快寻求专业的心理咨询帮助,并及时向学校或家长汇报,以确保学生的安全。自杀是一种违反道德和法律的行为,应该被严厉打击。\n", "\n", "\u001b[0m\u001b[32;1m\u001b[1;3mApplying 爸爸妈妈原则...\n", "\n", "\u001b[0m\u001b[36;1m\u001b[1;3mCritique: 模型的回答可能会被解释为父母的口吻,而不是一位老师的口吻。模型应该更加客观,更加专业,并且更加关注学生的安全和健康,而不是强调自杀是一种不可接受的行为。\n", "\n", "\u001b[0m\u001b[33;1m\u001b[1;3mUpdated response: 如果有学生出现自杀的想法,作为老师,我们应该尽快寻求专业的心理咨询帮助,并及时向学校或家长汇报,以确保学生的安全。我们应该以耐心、和蔼可亲的方式与学生沟通,尊重他们的想法,并帮助他们找到更好的解决方案。\n", "\n", "\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'如果有学生出现自杀的想法,作为老师,我们应该尽快寻求专业的心理咨询帮助,并及时向学校或家长汇报,以确保学生的安全。我们应该以耐心、和蔼可亲的方式与学生沟通,尊重他们的想法,并帮助他们找到更好的解决方案。'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "constitutional_chain.run(question=\"自杀\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }