{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%reset -sf","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:40.976004Z","iopub.execute_input":"2022-07-14T02:08:40.976746Z","iopub.status.idle":"2022-07-14T02:08:41.485857Z","shell.execute_reply.started":"2022-07-14T02:08:40.976673Z","shell.execute_reply":"2022-07-14T02:08:41.482977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip install openai","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:42.610706Z","iopub.execute_input":"2022-07-14T02:08:42.611174Z","iopub.status.idle":"2022-07-14T02:08:55.474031Z","shell.execute_reply.started":"2022-07-14T02:08:42.611138Z","shell.execute_reply":"2022-07-14T02:08:55.472141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.set_option('display.max_colwidth', 0)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:55.476779Z","iopub.execute_input":"2022-07-14T02:08:55.477285Z","iopub.status.idle":"2022-07-14T02:08:55.486045Z","shell.execute_reply.started":"2022-07-14T02:08:55.477242Z","shell.execute_reply":"2022-07-14T02:08:55.484281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# you need an openai key\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nOPENAI_API_KEY = user_secrets.get_secret(\"openai\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:55.488640Z","iopub.execute_input":"2022-07-14T02:08:55.490313Z","iopub.status.idle":"2022-07-14T02:08:55.726739Z","shell.execute_reply.started":"2022-07-14T02:08:55.490239Z","shell.execute_reply":"2022-07-14T02:08:55.724944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:55.730661Z","iopub.execute_input":"2022-07-14T02:08:55.731286Z","iopub.status.idle":"2022-07-14T02:08:59.385324Z","shell.execute_reply.started":"2022-07-14T02:08:55.731230Z","shell.execute_reply":"2022-07-14T02:08:59.383083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample = pd.concat([df[df[\"target\"] == 1].sample(5), df[df[\"target\"] == 0].sample(5)]).sample(frac=1).drop(\"qid\", axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:59.387179Z","iopub.execute_input":"2022-07-14T02:08:59.387608Z","iopub.status.idle":"2022-07-14T02:08:59.585240Z","shell.execute_reply.started":"2022-07-14T02:08:59.387575Z","shell.execute_reply":"2022-07-14T02:08:59.583795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def query_model(question_text, prompt):\n    import os\n    import openai\n\n    openai.api_key = OPENAI_API_KEY\n\n    response = openai.Completion.create(\n        model=\"text-davinci-002\",\n        prompt=f\"\\\"{question_text}\\\"\\n\\n{prompt}\\n\",\n        temperature=0.7,\n        max_tokens=256,\n        top_p=1,\n        frequency_penalty=0,\n        presence_penalty=0\n    )\n\n    return response[\"choices\"][0][\"text\"].strip()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:59.587079Z","iopub.execute_input":"2022-07-14T02:08:59.588399Z","iopub.status.idle":"2022-07-14T02:08:59.596533Z","shell.execute_reply.started":"2022-07-14T02:08:59.588346Z","shell.execute_reply":"2022-07-14T02:08:59.595049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prompts = [\n    \"Does this question contain assumptions?\",\n    \"Is this question insincere?\",\n    \"Is this question discriminatory?\",\n]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:59.599062Z","iopub.execute_input":"2022-07-14T02:08:59.599768Z","iopub.status.idle":"2022-07-14T02:08:59.611145Z","shell.execute_reply.started":"2022-07-14T02:08:59.599714Z","shell.execute_reply":"2022-07-14T02:08:59.608998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for prompt in prompts:\n    df_sample[prompt] = [query_model(question_text, prompt) for question_text in df_sample[\"question_text\"]]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:08:59.613404Z","iopub.execute_input":"2022-07-14T02:08:59.618567Z","iopub.status.idle":"2022-07-14T02:09:44.490541Z","shell.execute_reply.started":"2022-07-14T02:08:59.618500Z","shell.execute_reply":"2022-07-14T02:09:44.489120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample","metadata":{"execution":{"iopub.status.busy":"2022-07-14T02:09:44.492775Z","iopub.execute_input":"2022-07-14T02:09:44.493586Z","iopub.status.idle":"2022-07-14T02:09:44.514352Z","shell.execute_reply.started":"2022-07-14T02:09:44.493539Z","shell.execute_reply":"2022-07-14T02:09:44.512858Z"},"trusted":true},"execution_count":null,"outputs":[]}]}