File size: 7,457 Bytes
2ef1d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dccc64a
 
 
 
 
 
 
 
2ef1d57
dccc64a
 
 
 
 
 
 
 
 
 
2ef1d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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
}