File size: 11,944 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question Generation and Policy Comparison\n",
"This Notebook generates questions from the gold standard policy, and uses them to query both the gold standard and company policies, producing a .csv file of the queries, responses, and sources."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"from llama_index import SimpleDirectoryReader, ServiceContext, VectorStoreIndex\n",
"from llama_index.evaluation import DatasetGenerator\n",
"import nest_asyncio\n",
"import pandas as pd\n",
"nest_asyncio.apply()"
]
},
{
"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": [
"## Load in the gold standard and company policies"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"gold_standard = SimpleDirectoryReader(input_files=['data/meta.pdf']).load_data()\n",
"company = SimpleDirectoryReader(input_files=['data/usenix.pdf']).load_data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate questions from the gold standard policy\n",
"Here 50 questions are generated"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data_generator = DatasetGenerator.from_documents(gold_standard)\n",
"eval_questions = data_generator.generate_questions_from_nodes(num=50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set chunk information for when forming queries about the documents"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"chunk_size = 128\n",
"chunk_overlap = 20\n",
"similarity_top_k = 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the question-asking function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def ask_questions(docs):\n",
" service_context = ServiceContext.from_defaults(chunk_size=chunk_size, chunk_overlap=20)\n",
" index = VectorStoreIndex.from_documents(docs, service_context=service_context)\n",
" query_engine = index.as_query_engine(similarity_top_k=similarity_top_k)\n",
" responses = []\n",
" sources = []\n",
" for question in eval_questions:\n",
" response = query_engine.query(\"Do not lie when answering the following question. Only use information from the sources given. \" + question)\n",
" source = \"\"\n",
" for i in range(similarity_top_k):\n",
" source += response.source_nodes[i].node.get_content(metadata_mode=\"all\") + \"\\n\\n-----\\n\"\n",
" responses.append(response.response)\n",
" sources.append(source)\n",
" return responses, sources\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Query the two documents\n",
"The results are added to a dataframe"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What is the file name of the document?\n",
"When was the document last modified?\n",
"What is the file path of the document?\n",
"When was the document last accessed?\n",
"What is the creation date of the document?\n",
"What is the effective date mentioned in the document?\n",
"How many pages does the document have?\n",
"What is the label of the first page?\n",
"What is the label of the last page?\n",
"What type of document is it?\n",
"What is the purpose of the Privacy Policy mentioned in the context information?\n",
"Which products are covered by the Privacy Policy?\n",
"How does Meta encourage users to understand their privacy rights?\n",
"What information does Meta collect, use, and share according to the Privacy Policy?\n",
"How long does Meta keep users' information?\n",
"How does Meta ensure the safety of transferred information?\n",
"What rights do users have according to the Privacy Policy?\n",
"Where can users manage their information in the Meta Products?\n",
"What additional resources are provided to learn more about privacy topics?\n",
"What is the significance of the Privacy Centre mentioned in the context information?\n",
"What are some examples of Meta Products mentioned in the policy?\n",
"What is the file name of the document?\n",
"When was the last modified date of the document?\n",
"What are some examples of Meta Platforms Technologies Products?\n",
"What is the creation date of the document?\n",
"What are some examples of Meta Business Tools?\n",
"What is the file path of the document?\n",
"What is the last accessed date of the document?\n",
"What are some examples of Facebook products mentioned in the policy?\n",
"Are there any supplemental privacy policies mentioned in the document?\n",
"What are some examples of information that Facebook collects when you sign up for their products?\n",
"How does Facebook handle end-to-end encrypted messaging?\n",
"What types of information does Facebook collect about your friends or followers?\n",
"What kind of information does Facebook collect from the devices you use their products on?\n",
"How does Facebook collect information from partners about your online activities?\n",
"What factors determine the type of information Facebook collects and processes about you?\n",
"Can Facebook collect information about you even if you don't have an account?\n",
"What information does Facebook collect if you sell furniture on Marketplace?\n",
"What information does Facebook collect if you post a reel on Instagram?\n",
"How does Facebook ensure the privacy and security of the information they collect?\n",
"What are some of the activities that can be done on our products?\n",
"How does Meta collect information about user activity?\n",
"What types of content does Meta collect from users?\n",
"Can Meta see the content of end-to-end encrypted messages?\n",
"What types of metadata does Meta collect about content and messages?\n",
"How does Meta use information about user interactions with ads?\n",
"What types of transactions does Meta collect information about?\n",
"How does Meta use information about views and interactions with Facebook Pages?\n",
"What information might users choose to provide about themselves?\n",
"How does Meta handle the joint processing of Page Insights with Page admins?\n",
"What is the file name of the document?\n",
"When was the document last modified?\n",
"What is the file path of the document?\n",
"When was the document last accessed?\n",
"What is the creation date of the document?\n",
"What is the effective date mentioned in the document?\n",
"How many pages does the document have?\n",
"What is the label of the first page?\n",
"What is the label of the last page?\n",
"What type of document is it?\n",
"What is the purpose of the Privacy Policy mentioned in the context information?\n",
"Which products are covered by the Privacy Policy?\n",
"How does Meta encourage users to understand their privacy rights?\n",
"What information does Meta collect, use, and share according to the Privacy Policy?\n",
"How long does Meta keep users' information?\n",
"How does Meta ensure the safety of transferred information?\n",
"What rights do users have according to the Privacy Policy?\n",
"Where can users manage their information in the Meta Products?\n",
"What additional resources are provided to learn more about privacy topics?\n",
"What is the significance of the Privacy Centre mentioned in the context information?\n",
"What are some examples of Meta Products mentioned in the policy?\n",
"What is the file name of the document?\n",
"When was the last modified date of the document?\n",
"What are some examples of Meta Platforms Technologies Products?\n",
"What is the creation date of the document?\n",
"What are some examples of Meta Business Tools?\n",
"What is the file path of the document?\n",
"What is the last accessed date of the document?\n",
"What are some examples of Facebook products mentioned in the policy?\n",
"Are there any supplemental privacy policies mentioned in the document?\n",
"What are some examples of information that Facebook collects when you sign up for their products?\n",
"How does Facebook handle end-to-end encrypted messaging?\n",
"What types of information does Facebook collect about your friends or followers?\n",
"What kind of information does Facebook collect from the devices you use their products on?\n",
"How does Facebook collect information from partners about your online activities?\n",
"What factors determine the type of information Facebook collects and processes about you?\n",
"Can Facebook collect information about you even if you don't have an account?\n",
"What information does Facebook collect if you sell furniture on Marketplace?\n",
"What information does Facebook collect if you post a reel on Instagram?\n",
"How does Facebook ensure the privacy and security of the information they collect?\n",
"What are some of the activities that can be done on our products?\n",
"How does Meta collect information about user activity?\n",
"What types of content does Meta collect from users?\n",
"Can Meta see the content of end-to-end encrypted messages?\n",
"What types of metadata does Meta collect about content and messages?\n",
"How does Meta use information about user interactions with ads?\n",
"What types of transactions does Meta collect information about?\n",
"How does Meta use information about views and interactions with Facebook Pages?\n",
"What information might users choose to provide about themselves?\n",
"How does Meta handle the joint processing of Page Insights with Page admins?\n"
]
}
],
"source": [
"Data = pd.DataFrame(index=eval_questions)\n",
"Data[\"Meta\"], Data[\"Meta Sources\"] = ask_questions(gold_standard)\n",
"Data\n",
"Data[\"Company\"], Data[\"Company Sources\"] = ask_questions(company)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write the results to a .csv file"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"Data.to_csv(\"./Results/Compare.csv\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "docu_compare",
"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
}
|