{ "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 }