Spaces:
Sleeping
Sleeping
Delete Building_a_Safety_Agent.ipynb
Browse files- Building_a_Safety_Agent.ipynb +0 -316
Building_a_Safety_Agent.ipynb
DELETED
@@ -1,316 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"nbformat": 4,
|
3 |
-
"nbformat_minor": 0,
|
4 |
-
"metadata": {
|
5 |
-
"colab": {
|
6 |
-
"provenance": []
|
7 |
-
},
|
8 |
-
"kernelspec": {
|
9 |
-
"name": "python3",
|
10 |
-
"display_name": "Python 3"
|
11 |
-
},
|
12 |
-
"language_info": {
|
13 |
-
"name": "python"
|
14 |
-
}
|
15 |
-
},
|
16 |
-
"cells": [
|
17 |
-
{
|
18 |
-
"cell_type": "code",
|
19 |
-
"execution_count": null,
|
20 |
-
"metadata": {
|
21 |
-
"id": "bdp9fSdWKBhp",
|
22 |
-
"collapsed": true
|
23 |
-
},
|
24 |
-
"outputs": [],
|
25 |
-
"source": [
|
26 |
-
"# Install the Libraries used in this notebook.\n",
|
27 |
-
"\n",
|
28 |
-
"!pip install -qU langchain openai transformers selfcheckgpt profanityfilter\n",
|
29 |
-
"! python -m spacy download en"
|
30 |
-
]
|
31 |
-
},
|
32 |
-
{
|
33 |
-
"cell_type": "markdown",
|
34 |
-
"source": [
|
35 |
-
"##Safety Agent"
|
36 |
-
],
|
37 |
-
"metadata": {
|
38 |
-
"id": "6wj7wxo9aTe5"
|
39 |
-
}
|
40 |
-
},
|
41 |
-
{
|
42 |
-
"cell_type": "code",
|
43 |
-
"source": [
|
44 |
-
"from langchain.chat_models import ChatOpenAI\n",
|
45 |
-
"from langchain.chains.conversation.memory import ConversationBufferWindowMemory\n",
|
46 |
-
"import openai\n",
|
47 |
-
"import os\n",
|
48 |
-
"os.environ['OPENAI_API_KEY'] = openai.api_key= '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' #platform.openai.com api key\n",
|
49 |
-
"\n",
|
50 |
-
"# initialize LLM\n",
|
51 |
-
"llm = ChatOpenAI(\n",
|
52 |
-
" temperature=0,\n",
|
53 |
-
" model_name='gpt-3.5-turbo'\n",
|
54 |
-
")\n",
|
55 |
-
"\n",
|
56 |
-
"# initialize conversational memory\n",
|
57 |
-
"conversational_memory = ConversationBufferWindowMemory(\n",
|
58 |
-
" memory_key='chat_history',\n",
|
59 |
-
" k=5,\n",
|
60 |
-
" return_messages=True\n",
|
61 |
-
")"
|
62 |
-
],
|
63 |
-
"metadata": {
|
64 |
-
"id": "OIKitpN-fPSF"
|
65 |
-
},
|
66 |
-
"execution_count": null,
|
67 |
-
"outputs": []
|
68 |
-
},
|
69 |
-
{
|
70 |
-
"cell_type": "markdown",
|
71 |
-
"source": [
|
72 |
-
"### Profanity Detection Tool"
|
73 |
-
],
|
74 |
-
"metadata": {
|
75 |
-
"id": "O6BLtKefgSxZ"
|
76 |
-
}
|
77 |
-
},
|
78 |
-
{
|
79 |
-
"cell_type": "code",
|
80 |
-
"source": [
|
81 |
-
"from profanityfilter import ProfanityFilter\n",
|
82 |
-
"import spacy\n",
|
83 |
-
"from langchain.tools import BaseTool\n",
|
84 |
-
"from typing import Optional\n",
|
85 |
-
"\n",
|
86 |
-
"class Profanity_Check(BaseTool):\n",
|
87 |
-
" name = \"Profanity_Checker\"\n",
|
88 |
-
" description = (\n",
|
89 |
-
" \"use this tool when you need to check for profanity in given text\"\n",
|
90 |
-
" )\n",
|
91 |
-
" def _run(\n",
|
92 |
-
" self,\n",
|
93 |
-
" sentence1: Optional[str] = None\n",
|
94 |
-
" ):\n",
|
95 |
-
" pf = ProfanityFilter()\n",
|
96 |
-
" flag = pf.is_profane(sentence1)\n",
|
97 |
-
" if flag: return 'Profanity Detected'\n",
|
98 |
-
" else: return 'No Profanity found'\n",
|
99 |
-
"\n",
|
100 |
-
"\n",
|
101 |
-
" def _arun(self, sentence1, sentence2):\n",
|
102 |
-
" raise NotImplementedError(\"This tool does not support async runs.\")\n"
|
103 |
-
],
|
104 |
-
"metadata": {
|
105 |
-
"id": "_SJa13N5i1kY"
|
106 |
-
},
|
107 |
-
"execution_count": null,
|
108 |
-
"outputs": []
|
109 |
-
},
|
110 |
-
{
|
111 |
-
"cell_type": "markdown",
|
112 |
-
"source": [
|
113 |
-
"### Hallucination Detection Tool"
|
114 |
-
],
|
115 |
-
"metadata": {
|
116 |
-
"id": "YicTP78lgXlT"
|
117 |
-
}
|
118 |
-
},
|
119 |
-
{
|
120 |
-
"cell_type": "code",
|
121 |
-
"source": [
|
122 |
-
"from selfcheckgpt.modeling_selfcheck import SelfCheckBERTScore\n",
|
123 |
-
"\n",
|
124 |
-
"class Hallucination_Scorer(BaseTool):\n",
|
125 |
-
" name = \"Hallucination_Scorer\"\n",
|
126 |
-
" description = (\n",
|
127 |
-
" \"use this tool when a you need to give hallucination scores\"\n",
|
128 |
-
" )\n",
|
129 |
-
" def _run(\n",
|
130 |
-
" self,\n",
|
131 |
-
" sentence1: Optional[str] = None\n",
|
132 |
-
" ):\n",
|
133 |
-
" selfcheck_bertscore = SelfCheckBERTScore()\n",
|
134 |
-
" nlp = spacy.load('en_core_web_sm')\n",
|
135 |
-
" passage = sentence1\n",
|
136 |
-
" sentences = [sent.text.strip() for sent in nlp(passage).sents]\n",
|
137 |
-
"\n",
|
138 |
-
" chat_completion = openai.ChatCompletion.create(model=\"gpt-3.5-turbo\", messages=[{\"role\": \"user\", \"content\": sentence1}])\n",
|
139 |
-
" sample1 = chat_completion.choices[0].message.content\n",
|
140 |
-
" chat_completion = openai.ChatCompletion.create(model=\"gpt-3.5-turbo\", messages=[{\"role\": \"user\", \"content\": sentence1}])\n",
|
141 |
-
" sample2 = chat_completion.choices[0].message.content\n",
|
142 |
-
" chat_completion = openai.ChatCompletion.create(model=\"gpt-3.5-turbo\", messages=[{\"role\": \"user\", \"content\": sentence1}])\n",
|
143 |
-
" sample3 = chat_completion.choices[0].message.content\n",
|
144 |
-
"# SelfCheck-BERTScore: Score for each sentence where value is in [0.0, 1.0] and high value means non-factual\n",
|
145 |
-
" sent_scores_bertscore = selfcheck_bertscore.predict(\n",
|
146 |
-
" sentences = sentences, # list of sentences\n",
|
147 |
-
" sampled_passages = [sample1, sample2, sample3], # list of sampled passages\n",
|
148 |
-
" )\n",
|
149 |
-
" return sent_scores_bertscore\n",
|
150 |
-
"\n",
|
151 |
-
"\n",
|
152 |
-
" def _arun(self, sentence1, sentence2):\n",
|
153 |
-
" raise NotImplementedError(\"This tool does not support async runs.\")\n"
|
154 |
-
],
|
155 |
-
"metadata": {
|
156 |
-
"id": "1CA0tBsWYC6K"
|
157 |
-
},
|
158 |
-
"execution_count": null,
|
159 |
-
"outputs": []
|
160 |
-
},
|
161 |
-
{
|
162 |
-
"cell_type": "markdown",
|
163 |
-
"source": [
|
164 |
-
"### Initializing Safety Agent\n"
|
165 |
-
],
|
166 |
-
"metadata": {
|
167 |
-
"id": "6wqgeEvKgex7"
|
168 |
-
}
|
169 |
-
},
|
170 |
-
{
|
171 |
-
"cell_type": "code",
|
172 |
-
"source": [
|
173 |
-
"from langchain.agents import initialize_agent\n",
|
174 |
-
"\n",
|
175 |
-
"# Pass the tools\n",
|
176 |
-
"tools = [Hallucination_Scorer(),Profanity_Check()]\n",
|
177 |
-
"\n",
|
178 |
-
"# initialize agent with tools\n",
|
179 |
-
"agent = initialize_agent(\n",
|
180 |
-
" agent='chat-conversational-react-description',\n",
|
181 |
-
" tools=tools, # Point each smaller sized agent towards the test we use\n",
|
182 |
-
" llm=llm, # Can be buiult over any LLM\n",
|
183 |
-
" verbose=True, ## Temperature for responses is set to zero for determinsitc test score// change to 1 when generating reports.\n",
|
184 |
-
" max_iterations=3, # Avoid Looping\n",
|
185 |
-
" early_stopping_method='generate', # Stop and generate a score\n",
|
186 |
-
" memory=conversational_memory # Chat Memory\n",
|
187 |
-
"\n",
|
188 |
-
")"
|
189 |
-
],
|
190 |
-
"metadata": {
|
191 |
-
"id": "4hI5kL3I10sM"
|
192 |
-
},
|
193 |
-
"execution_count": null,
|
194 |
-
"outputs": []
|
195 |
-
},
|
196 |
-
{
|
197 |
-
"cell_type": "markdown",
|
198 |
-
"source": [
|
199 |
-
"##### Loading generated policy"
|
200 |
-
],
|
201 |
-
"metadata": {
|
202 |
-
"id": "RNxBUVOsjXpr"
|
203 |
-
}
|
204 |
-
},
|
205 |
-
{
|
206 |
-
"cell_type": "code",
|
207 |
-
"source": [
|
208 |
-
"with open(\"/content/Generated_Policy.txt\", \"r\") as file1:\n",
|
209 |
-
" text = file1.read()\n",
|
210 |
-
" generated_policy = ' '.join(text.split('\\n'))\n",
|
211 |
-
"file1.close()"
|
212 |
-
],
|
213 |
-
"metadata": {
|
214 |
-
"id": "exp-mK78gbUG",
|
215 |
-
"collapsed": true
|
216 |
-
},
|
217 |
-
"execution_count": null,
|
218 |
-
"outputs": []
|
219 |
-
},
|
220 |
-
{
|
221 |
-
"cell_type": "code",
|
222 |
-
"source": [
|
223 |
-
"generated_policy"
|
224 |
-
],
|
225 |
-
"metadata": {
|
226 |
-
"id": "KsGtYtrkgpey"
|
227 |
-
},
|
228 |
-
"execution_count": null,
|
229 |
-
"outputs": []
|
230 |
-
},
|
231 |
-
{
|
232 |
-
"cell_type": "markdown",
|
233 |
-
"source": [
|
234 |
-
"### Hallucination Scoring"
|
235 |
-
],
|
236 |
-
"metadata": {
|
237 |
-
"id": "2_32mHKGjeqt"
|
238 |
-
}
|
239 |
-
},
|
240 |
-
{
|
241 |
-
"cell_type": "code",
|
242 |
-
"source": [
|
243 |
-
"prompt1 = 'Hallucination score for :'+generated_policy"
|
244 |
-
],
|
245 |
-
"metadata": {
|
246 |
-
"id": "VWH3VDo03oHr"
|
247 |
-
},
|
248 |
-
"execution_count": null,
|
249 |
-
"outputs": []
|
250 |
-
},
|
251 |
-
{
|
252 |
-
"cell_type": "code",
|
253 |
-
"source": [
|
254 |
-
"var1 = agent(prompt1)"
|
255 |
-
],
|
256 |
-
"metadata": {
|
257 |
-
"id": "8aCm3m79hppI"
|
258 |
-
},
|
259 |
-
"execution_count": null,
|
260 |
-
"outputs": []
|
261 |
-
},
|
262 |
-
{
|
263 |
-
"cell_type": "code",
|
264 |
-
"source": [
|
265 |
-
"var1['output']"
|
266 |
-
],
|
267 |
-
"metadata": {
|
268 |
-
"id": "gum7V3J9QFBv"
|
269 |
-
},
|
270 |
-
"execution_count": null,
|
271 |
-
"outputs": []
|
272 |
-
},
|
273 |
-
{
|
274 |
-
"cell_type": "markdown",
|
275 |
-
"source": [
|
276 |
-
"### Profanity Detection"
|
277 |
-
],
|
278 |
-
"metadata": {
|
279 |
-
"id": "yjyYQJj0jlxm"
|
280 |
-
}
|
281 |
-
},
|
282 |
-
{
|
283 |
-
"cell_type": "code",
|
284 |
-
"source": [
|
285 |
-
"prompt2 = 'Check for profanity in '+generated_policy\n"
|
286 |
-
],
|
287 |
-
"metadata": {
|
288 |
-
"id": "xoc4nCbr4Im5"
|
289 |
-
},
|
290 |
-
"execution_count": null,
|
291 |
-
"outputs": []
|
292 |
-
},
|
293 |
-
{
|
294 |
-
"cell_type": "code",
|
295 |
-
"source": [
|
296 |
-
"var2 = agent(prompt2)"
|
297 |
-
],
|
298 |
-
"metadata": {
|
299 |
-
"id": "fBsWajM4lOyo"
|
300 |
-
},
|
301 |
-
"execution_count": null,
|
302 |
-
"outputs": []
|
303 |
-
},
|
304 |
-
{
|
305 |
-
"cell_type": "code",
|
306 |
-
"source": [
|
307 |
-
"var2['output']"
|
308 |
-
],
|
309 |
-
"metadata": {
|
310 |
-
"id": "Nc6aYJ1eYnpf"
|
311 |
-
},
|
312 |
-
"execution_count": null,
|
313 |
-
"outputs": []
|
314 |
-
}
|
315 |
-
]
|
316 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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