File size: 7,560 Bytes
af779a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Policy Compare\n",
    "This Jupyter Notebook aims to compare how well your policy document aligns with that of the RAI Gold-Standard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.node_parser import SimpleNodeParser\n",
    "from llama_index import (\n",
    "    ServiceContext,\n",
    "    SimpleDirectoryReader)\n",
    "from llama_index import VectorStoreIndex\n",
    "import openai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OpenAI API Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "openai.api_key = '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'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chunk Information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "chunk_size = 128\n",
    "similarity_top_k = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load document as query engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = SimpleDirectoryReader(input_files=['data/usenix.pdf']).load_data()\n",
    "service_context = ServiceContext.from_defaults(chunk_size=chunk_size)\n",
    "index = VectorStoreIndex.from_documents(documents, service_context=service_context)\n",
    "\n",
    "query_engine = index.as_query_engine(similarity_top_k=similarity_top_k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Form query and get response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This privacy policy collects personal information such as name, email address, job title, company, mailing address, phone number, industry, job function, and student status.\n"
     ]
    }
   ],
   "source": [
    "query = \"Which specific personal information does this privacy policy collect?\"\n",
    "response = query_engine.query(query)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get Sources Used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "page_label: 3\n",
      "file_name: usenix.pdf\n",
      "\n",
      "We will ask for your consent before using information for a purpose other than those that are set out in this Privacy Policy .Website Cookies and Logs\n",
      "Like many websites, we use cookies and similar tracking technology (collectively \"Cookies\") to collect and use personal\n",
      "information about you.For further information about the types of Cookies we use, please see our Cookie Statement\n",
      "(https://www .usenix.org/cookies) .Log Data\n",
      "We collect the information that your browser sends whenever you visit our website.\n",
      "\n",
      "-----\n",
      "\n",
      "page_label: 4\n",
      "file_name: usenix.pdf\n",
      "\n",
      "If you have questions about or need further information concerning the legal basis on which we collect and use your\n",
      "personal information, please contact us at [email protected]  (mailto:[email protected]) .Data Retention\n",
      "We retain your personal information and a record of membership, purchases, event attendance, volunteer service, and\n",
      "related data.\n",
      "\n",
      "-----\n",
      "\n",
      "page_label: 4\n",
      "file_name: usenix.pdf\n",
      "\n",
      "Legal Basis for Processing Your Personal\n",
      "Information\n",
      "We collect personal information from you where the processing is in our legitimate interests.As described above, the\n",
      "data is collected to provide services to our members, conference attendees, those who wish to publish papers or present\n",
      "at our conferences, and other interested parties.We send communications announcing upcoming events, submission deadlines, and other issues of interest to our\n",
      "membership and constituents.Y ou may opt out of these communications at any time.\n",
      "\n",
      "-----\n",
      "\n",
      "page_label: 4\n",
      "file_name: usenix.pdf\n",
      "\n",
      "Where required by law or regulation, court order , or other judicial authorization; in response to lawful requests by\n",
      "public authorities, including for the purposes of meeting national security and law enforcement requirements; to\n",
      "protect or defend our rights, interests, or property , or that of third parties; to investigate any wrongdoing in\n",
      "connection with our products and services; and to protect the vital interests of an individual.To any other person with your consent.We may disclose aggregate, non-identifying information about our members and constituents based on anonymized\n",
      "data.\n",
      "\n",
      "-----\n",
      "\n",
      "page_label: 1\n",
      "file_name: usenix.pdf\n",
      "\n",
      "Personal Information You Provide to Us\n",
      "and Our Data Processors\n",
      "USENIX Membership and Account Creation\n",
      "When you create an account on the usenix.org website, you provide your name, email address, job title, company ,\n",
      "mailing address, phone number , industry , job function, and student status.\n",
      "\n",
      "-----\n",
      "\n",
      "page_label: 3\n",
      "file_name: usenix.pdf\n",
      "\n",
      "These providers have limited access to your information to perform these tasks on our behalf and are\n",
      "contractually bound to protect and to use it only for the purposes for which it was disclosed and consistent with\n",
      "this Privacy Policy .\n",
      "\n",
      "-----\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(similarity_top_k):\n",
    "    print(response.source_nodes[i].node.get_content(metadata_mode=\"all\") + \"\\n\\n-----\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write all to text file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"Results/Sources\" + \"_\" + \"_\".join(query.split()[:3]) + \".txt\", \"w\") as fp:\n",
    "    fp.write(\"---------- Query ----------\\n\" + query + \"\\n\\n\")\n",
    "    fp.write(\"-------- Response ---------\\n\" + str(response) + \"\\n\\n\")\n",
    "    fp.write(\"------ Sources used -------\\n\")\n",
    "    for i in range(similarity_top_k):\n",
    "        fp.write(response.source_nodes[i].node.get_content(metadata_mode=\"all\") + \"\\n\\n-----\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TW",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}