Skip to content

Instantly share code, notes, and snippets.

@NickDiakopoulos
Created February 26, 2023 12:02
Show Gist options
  • Save NickDiakopoulos/a3611956a0d280f96ef6518d53120a84 to your computer and use it in GitHub Desktop.
Save NickDiakopoulos/a3611956a0d280f96ef6518d53120a84 to your computer and use it in GitHub Desktop.
Generative AI for News Media.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyN88h9P7WPtfJ/ZiLNH1wRa",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/NickDiakopoulos/a3611956a0d280f96ef6518d53120a84/generative-ai-for-news-media.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Generative AI for News Media\n",
"\n",
"The purpose of this notebook is to provide an overview and some examples of how generative models such as GPT-3 can be used for productive purposes in the domain of journalism. It's important to get a handle on the capabilities and limitations of these models in order to use them responsibly. \n",
"\n",
"You can try out OpenAI's interactive \"playground\" (https://beta.openai.com/playground) and try variations of the tasks I walk through below. Before we get to tasks though, I'll talk about prompts and model limitations. \n"
],
"metadata": {
"id": "_6hg3-7BCj7M"
}
},
{
"cell_type": "markdown",
"source": [
"## Prompting\n",
"The main way you interact with these kinds of models is through *prompting* -- writing commands in natural language. **The prompt is the textual input to the model used to describe the task that the model should perform.** For example, you could prompt the model with \"Brainstorm three article ideas on the topic of generative AI in news media\" and it should generate a list of three ideas. \n",
"\n",
"Prompting is your main way of controlling the machine and providing context or data for the task. You need to communicate clearly and precisely what you want, and what you don't. When writing prompts think about the verbs and objects to describe what you want to happen, like \"<u>Summarize</u> this scientific <u>abstract</u> using lay terminology: \\<abstract text\\>\". \n",
"\n",
"Prompting is sometimes referred to as \"prompt design\" or \"prompt engineering\" since you often need to carefully craft and iterate or \"debug\" your prompts so that they accomplish what you want. This is similar to how we explain things to other people: if someone doesn't understand you, you try again and explain it a bit differently, trying to clarify. \n",
"\n",
"Here are a few tips for prompting: \n",
"\n",
"-- Use plain language, be concrete with verbs and objects\n",
"\n",
"-- Iterate on different prompts in the playground so you can quickly see feedback on whether it's working as expected. You might need to try different ways of expressing what you want the model to do.\n",
"\n",
"-- Start by being as descriptive as possible and then work backwards to remove extra words and see if performance stays consistent\n",
"\n",
"-- Play with the scope and complexity of task: “Write an article about GPT-3” vs. “List three specific advantages of GPT-3 for news media”"
],
"metadata": {
"id": "DGJibeJhhiBK"
}
},
{
"cell_type": "markdown",
"source": [
"## Model Limitations\n",
"Journalists need to be aware of the limitations of the AI systems they use. Generate AI models like GPT-3 generally come with a few caveats including:\n",
"\n",
"-- They don't \"reason\" well, especially for complex tasks with multiple steps that require planning. \n",
"\n",
"-- They can \"hallucinate\" information leading to factual inaccuracies. They generally cannot provide accurate references or links back to where information came from. \n",
"\n",
"-- They can exhibit societal biases based on the data they're trained on. Based on when the training data ends the model won't \"know\" anything about the world after that date (for GPT-3.5 this is June, 2021) \n",
"\n",
"-- They don't generally do well on math problems.\n",
"\n",
"-- There are length limitations (~3000 words for GPT-3) and generally the longer the output text, the less coherent.\n",
"\n",
"-- They may memorize text from their training that could result in copyright issues."
],
"metadata": {
"id": "4aj05wEQhlSm"
}
},
{
"cell_type": "markdown",
"source": [
"# Capabilities\n",
"\n",
"Next I'll walk through some basic use-cases for generative AI in news media, focusing on text output using the GPT-3 model. I'll demonstrate six basic tasks that should have some journalistic value: (1) Rewriting, (2) Summarization, (3) Brainstorming, (4) Classification, (5) Extraction, and (6) Data-to-text. There are other example tasks you can check out in the OpenAI documentation: https://beta.openai.com/examples as well as in the OpenAI Cookbook: https://github.com/openai/openai-cookbook. \n",
"\n",
"**To edit and run the code, create a copy of this Google Doc in your drive, then sign up on OpenAI for an API key and paste it in below where it says `openai.api_key`.** Or you can also copy-paste the prompts into the OpenAI playground to see the output there.\n",
"\n",
"If you're keen to build on these tasks or develop your own, see the Generative AI in the Newsroom Challenge: https://medium.com/@ndiakopoulos/the-generative-ai-in-the-newsroom-challenge-9fe2dc5fb2a7"
],
"metadata": {
"id": "nByxKMdPFZbR"
}
},
{
"cell_type": "code",
"source": [
"!pip install openai"
],
"metadata": {
"id": "Tw-jfXTlLxPj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"import openai\n",
"\n",
"# Copy your Open AI Secret Key here between the quotation marks\n",
"openai.api_key = \"COPY IN KEY HERE\"\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"from IPython.display import HTML, display\n",
"\n",
"def set_css():\n",
" display(HTML('''\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" '''))\n",
"get_ipython().events.register('pre_run_cell', set_css)\n"
],
"metadata": {
"id": "MuOp8j_nKRC6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 17
},
"outputId": "76dac68f-3abe-471a-c397-7c2d5ac0c2ba"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"source": [
"## 1. Rewriting\n",
"\n",
"These models tend to be good at paraphrasing text, which can be useful for removing jargon or simplifying the language of a text. \n",
"\n",
"**Use Case**: You're a science reporter trying to see if a recently published paper deals with a topic of interest. So you prompt the model to dejargonize a scientific abstract from a pre-print server. \n",
"\n",
"⚠ **WARNING** ⚠ : Output like this should only be used to get a quick sense for what the research is about and help you decide if you want to read the full study. \n",
"\n",
"\n"
],
"metadata": {
"id": "H2L8QVd1g-_K"
}
},
{
"cell_type": "code",
"source": [
"# Here is the prompt command\n",
"prompt = \"Rewrite the following abstract to avoid heavy scientific jargon and use simpler vocabulary: \"\n",
"\n",
"# Here is a jargony scientific abstract taken from bioarxiv: https://www.biorxiv.org/content/10.1101/581280v3 \n",
"abstract = \\\n",
"\"Current treatments for depression are limited by suboptimal efficacy, delayed response, \\\n",
"and frequent side effects. Intermittent theta-burst stimulation (iTBS) is a non-invasive brain stimulation treatment that is \\\n",
"FDA-approved for treatment-resistant depression (TRD). Recent methodological advancements suggest iTBS could be improved \\\n",
"through 1) treating with multiple sessions per day at optimally-spaced intervals, 2) applying a higher overall pulse-dose \\\n",
"of stimulation and 3) precision targeting of the left dorsolateral prefrontal cortex (L-DLPFC) to subgenual anterior cingulate \\\n",
"cortex (sgACC) circuit. We examined the feasibility, tolerability, and preliminary efficacy of an accelerated, high-dose, \\\n",
"resting-state functional connectivity MRI (fcMRI)-guided iTBS protocol for TRD termed \\\n",
"‘Stanford Accelerated Intelligent Neuromodulation Therapy (SAINT)’.\"\n",
"\n",
"response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + abstract, # Concatenate the prompt and abstract\n",
" temperature=0.0, # Set the temperature to zero to remove variation in the output (should always get the same result)\n",
" max_tokens=300,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
")\n",
"\n",
"# Output the response\n",
"print (response[\"choices\"][0][\"text\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 177
},
"id": "zI4zN4RMg8yq",
"outputId": "b47b5863-6684-41f1-e196-8b086a1778b1"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\n",
"Current treatments for depression can be limited by their lack of effectiveness, slow response time, and side effects. Intermittent theta-burst stimulation (iTBS) is a non-invasive brain stimulation treatment that has been approved by the FDA for treatment-resistant depression (TRD). Recent research suggests that iTBS could be improved by 1) having multiple sessions per day at the right intervals, 2) using a higher overall amount of stimulation, and 3) targeting the left dorsolateral prefrontal cortex (L-DLPFC) to subgenual anterior cingulate cortex (sgACC) circuit more precisely. We looked into the feasibility, tolerability, and preliminary effectiveness of a new iTBS protocol for TRD called 'Stanford Accelerated Intelligent Neuromodulation Therapy (SAINT)', which uses a higher dose of stimulation and is guided by resting-state functional connectivity MRI (fcMRI).\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"---\n",
"\n"
],
"metadata": {
"id": "hlXaqpe4Cr5Z"
}
},
{
"cell_type": "markdown",
"source": [
"## 2. Summarization\n",
"\n",
"These models are quite proficient at crunching a text down so you can scan it more quickly. \n",
"\n",
"**Use Case**: You're an AI reporter and want to keep tabs on state legislatures that may be introducing relevant bills. You want to set up an alert so that any bill mentioning \"AI\" gets summarized and sent to your email. \n",
"\n",
"⚠ **WARNING** ⚠ : Output like this should only be used to get a quick sense for what the bill is about and help you decide if you want to read the full bill in detail. \n",
"\n"
],
"metadata": {
"id": "MSzaSwBF8f_8"
}
},
{
"cell_type": "code",
"source": [
"# Import packages needed to get and parse the data\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n",
"# Here is the prompt command\n",
"prompt = \"Summarize what is journalistically newsworthy about the following proposed bill: \"\n",
"\n",
"# Download the text for a proposed Massachusetts bill\n",
"url = \"http://malegislature.gov/Bills/193/SD1827/Senate/Bill/Text\"\n",
"response = requests.get(url, verify=False)\n",
"soup = BeautifulSoup(response.text, 'html.parser')\n",
"bill_text = soup.get_text()\n",
"\n",
"# Uncomment this if you want to see the original bill text\n",
"#print (bill_text)\n",
"\n",
"response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + bill_text, # Concatenate the prompt and bill text\n",
" temperature=0.7, # Setting the temperature to the default which can result in some variability from run to run\n",
" max_tokens=250,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
")\n",
"\n",
"# Output the response\n",
"print (response[\"choices\"][0][\"text\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 142
},
"id": "Kq4-7Evf8w5u",
"outputId": "41e5cca6-a1cb-447d-b393-ffe46b459251"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'\\nThe proposed bill SD.1827 aims to regulate generative artificial intelligence models, such as ChatGPT, in order to protect the public’s safety, privacy and intellectual property rights. The bill would require companies operating large-scale generative artificial intelligence models to adhere to certain operating standards, such as not engaging in discrimination or bias, using authentication processes to prevent plagiarism and collecting informed consent from individuals before using their data. It would also require these companies to register with the attorney general and for the attorney general to maintain a public registry of all companies registered under this act. This proposed bill is newsworthy as it seeks to provide protections for individuals from potentially harmful AI models, as well as to provide a legal framework for the use of generative AI models.'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"---\n",
"\n"
],
"metadata": {
"id": "PB2SbFj-Kk60"
}
},
{
"cell_type": "markdown",
"source": [
"## 3. Brainstorming\n",
"\n",
"These models can be helpful for generating diverse ideas that can spark your creativity. \n",
"\n",
"**Use Case**: A press release just landed in your inbox from the NYC Mayor's office. You haven't covered the topic before and don't have much background so you use the model to jumpstart your thinking about possible story angles you could pursue. \n",
"\n",
"**Note**: The use of the `presence_penality` parameter here to nudge the model to produce distinct ideas.\n",
"\n",
"⚠ **WARNING** ⚠ : The more you know about a topic, the less the model will probably surprise you. Don't expect any truly creative thinking from the model, but it might offer the right spark for your own creativity.\n",
"\n"
],
"metadata": {
"id": "6Bu86ihQK4Mm"
}
},
{
"cell_type": "code",
"source": [
"# Import packages needed to get and parse the data\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n",
"# Here is the prompt command\n",
"prompt = \"Brainstorm three specific news article ideas by critically assessing the following press release, explaining each idea: \"\n",
"\n",
"# Download the text for the NYC Press Release\n",
"url = \"https://www.nyc.gov/office-of-the-mayor/news/077-23/mayor-adams-helps-new-yorkers-save-up-150-million-overdue-water-bills\"\n",
"response = requests.get(url, verify=False)\n",
"soup = BeautifulSoup(response.text, 'html.parser')\n",
"press_release_text = soup.select_one(\".col-content\").get_text()\n",
"\n",
"# Uncomment this if you want to see the original press release text\n",
"#print (bill_text)\n",
"\n",
"response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + press_release_text, # Concatenate the prompt and bill text\n",
" temperature=0.7, # Setting the temperature to the default which can result in some variability from run to run\n",
" max_tokens=500,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.8 # Setting the presence penality to a relatively high value should help the model brainstorm distinct ideas\n",
")\n",
"\n",
"# Output the response\n",
"print (response[\"choices\"][0][\"text\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 195
},
"id": "qfejQ2d7Kp2f",
"outputId": "0daf4e60-56df-4895-b1e2-fcf28336d987"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"1. \"Mayor Adams Offers Relief to Nearly 200,000 New Yorkers With Unpaid Water Bills\": This article would explore how Mayor Adams' amnesty program is offering relief to nearly 200,000 customers with overdue water bills by forgiving up to 100 percent of interest when customers pay a portion or all of their outstanding water bills. \n",
"\n",
"2. \"New Yorkers Could Save up to $150 Million With Temporary Water Bill Amnesty Program\": This article would focus on the potential savings for New Yorkers of up to $150 million that are available through Mayor Adams' temporary water bill amnesty program. \n",
"\n",
"3. \"NYC Water Infrastructure Threatened By $1.2 Billion Owed in Unpaid Water Bills\": This article would explain how the $1.2 billion owed in unpaid water bills poses a threat to NYC's water infrastructure and could lead to additional rate hikes if left unpaid.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"---\n",
"\n"
],
"metadata": {
"id": "DBzLMfWfTimU"
}
},
{
"cell_type": "markdown",
"source": [
"## 4. Classification\n",
"\n",
"Generative models like GPT3 can also be used for analytic tasks like classifying a text into different categories. \n",
"\n",
"**Use Case**: You're an engaged journalist doing a follow up story on recent coverage around the need to replace gas cooktops, such as https://www.washingtonpost.com/climate-solutions/2023/02/04/how-to-use-gas-stove-safely/. You want to run a quick survey with people who have switched to induction stoves and ask them why they switched. To find these people you decide to scan the more than 2,700 comments made on the last story. \n",
"\n",
"**Note**: The prompt here is designed to output structured data, which can be used easy in downstream processing, like filtering. \n",
"\n",
"⚠ **WARNING** ⚠ : Don't make assumptions about what your classifier might be missing. You might need to devise a small study to assess accuracy. "
],
"metadata": {
"id": "nSa6oZrTTXCZ"
}
},
{
"cell_type": "code",
"source": [
"import json\n",
"import time\n",
"from openai.error import RateLimitError\n",
"\n",
"# For purposes of the demo we just scan 6 comments, but this could easily be scaled up.\n",
"sample_comments = [\"We were considering converting to gas when replacing our stove. We went with induction instead. It’s amazing. Cooks food/boils water faster. Is 90% efficient vs. gas 30% efficacy. Equipped with an air fryer function. We had to purchase new cook wear that is required because of the magnification properties on the burners. You need pots that are made to adhere so to speak to burners while heating. But it’s worth it! Love our new pots and get dinner ready faster. Now if it would only clean -up.\",\n",
" \"When we first moved into our house (we weren't the first owner) it had a conventional electric cooktop. Conventional electric stoves just plain suck for a variety of reasons. But we were lucky, because the builder of our house included both a gas pipe and a 220 volt outlet in the island where the cooktop sat. We considered replacing the electric cooktop with an induction one but didn't want to spend the time, effort and money to replace the majority of our cookware with pots and pans that were magnetic. So in our case it was easy work to yank the electric cooktop and replace it with a gas one. It took the appliance store guys about 30 minutes to make the switch. A couple of years ago we remodeled our kitchen, and in the big scheme of cost and inconvenience, replacing our cookware was pretty trivial. So we finally got our induction cooktop. It combines the best features of electric (more efficient than gas, no air pollution) with the best features of gas (easy and quick to regulate temperature), and includes advantages neither one has (doesn't heat up the kitchen, cooktop doesn't stay burning hot for long when cooking is done), and it heats things faster than either electric or gas. Without a doubt induction cooking is the way of the future. And I'm sure prices will come down as more home owners who can't or don't want to spend on high-end appliances start to demand them.\",\n",
" \"We have a gas stove, and like it. Our range hood is vented to the outside. We will be getting an induction range within 2 to 4 years. We are older, and it is a safer option since there will not be an open flame. We took this decision before the \\\"controversy\\\" arose. Gas stoves are better than standard electric in some areas (where gas is cheaper), and work very well. It is also true that the products of combustion are a problem. Houseplants help, opening windows helps, range hoods help. Nothing works perfectly.\",\n",
" \"Unless you have a small home with little ventilation, the risk from gas stove emissions is quite small. After cooking on a induction stove top for a year in a rental we opted for gas when we put a new stove in our own house. It is well ventilated and this old house provides a lot of unplanned ventilation too.\",\n",
" \"And just so we're all clear: the suitability of a pan for induction cooking is not \\\"adhesion\\\". Testing with a magnet is a quick way to determine whether the pan has enough ferrous metal to respond to the magnetic field generate by the element.\",\n",
" \"The gas stove hullabaloo is absurd. The matter of the plastic straws is infinitely more serious because they stay out there essentially forever and affect other creatures. I have no problem banning them.\"\n",
" ]\n",
"\n",
"# Here is the prompt command\n",
"prompt = 'Does the following comment describe someone\\'s experience of switching to an induction cooktop? Respond with JSON structured data with a \"label\" field that is either \"yes\" or \"no\" and and \"explanation\" field that provides an explanation of why it\\'s labeled that way: '\n",
"\n",
"# This array will store our categorized data\n",
"output_data = []\n",
"\n",
"# Loop through our list of sample_comments\n",
"for ncomment, comment_text in enumerate(sample_comments):\n",
" print (\"Testing comment\", ncomment)\n",
"\n",
" # Construct the API request \n",
" try:\n",
" response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + comment_text, \n",
" temperature=0.0, # We don't want any variability in the responses \n",
" max_tokens=256,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
" )\n",
"\n",
" data = {}\n",
" data[\"response\"] = json.loads(response[\"choices\"][0][\"text\"])\n",
" data[\"comment\"] = comment_text\n",
" output_data.append(data)\n",
" #print (data)\n",
"\n",
" # You need to sleep for 3 seconds in between API calls to avoid being rate limited (the rate limit is 20 calls / minute for the free tier)\n",
" time.sleep(5)\n",
" except RateLimitError as e:\n",
" print (e)\n",
" time.sleep (10) # Wait some more\n",
"\n",
"# Outcome the final data\n",
"print (json.dumps(output_data, indent=2))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "PXua9gdcTs6T",
"outputId": "0301f87a-38e6-4792-edc7-81049022bfab"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Testing comment 0\n",
"Testing comment 1\n",
"Testing comment 2\n",
"Testing comment 3\n",
"Testing comment 4\n",
"Testing comment 5\n",
"[\n",
" {\n",
" \"response\": {\n",
" \"label\": \"yes\",\n",
" \"explanation\": \"The comment describes a positive experience with an induction cooktop, including its efficiency, air fryer function, and the need to purchase new cookware.\"\n",
" },\n",
" \"comment\": \"We were considering converting to gas when replacing our stove. We went with induction instead. It\\u2019s amazing. Cooks food/boils water faster. Is 90% efficient vs. gas 30% efficacy. Equipped with an air fryer function. We had to purchase new cook wear that is required because of the magnification properties on the burners. You need pots that are made to adhere so to speak to burners while heating. But it\\u2019s worth it! Love our new pots and get dinner ready faster. Now if it would only clean -up.\"\n",
" },\n",
" {\n",
" \"response\": {\n",
" \"label\": \"Yes\",\n",
" \"explanation\": \"The comment describes the experience of switching from an electric cooktop to an induction cooktop.\"\n",
" },\n",
" \"comment\": \"When we first moved into our house (we weren't the first owner) it had a conventional electric cooktop. Conventional electric stoves just plain suck for a variety of reasons. But we were lucky, because the builder of our house included both a gas pipe and a 220 volt outlet in the island where the cooktop sat. We considered replacing the electric cooktop with an induction one but didn't want to spend the time, effort and money to replace the majority of our cookware with pots and pans that were magnetic. So in our case it was easy work to yank the electric cooktop and replace it with a gas one. It took the appliance store guys about 30 minutes to make the switch. A couple of years ago we remodeled our kitchen, and in the big scheme of cost and inconvenience, replacing our cookware was pretty trivial. So we finally got our induction cooktop. It combines the best features of electric (more efficient than gas, no air pollution) with the best features of gas (easy and quick to regulate temperature), and includes advantages neither one has (doesn't heat up the kitchen, cooktop doesn't stay burning hot for long when cooking is done), and it heats things faster than either electric or gas. Without a doubt induction cooking is the way of the future. And I'm sure prices will come down as more home owners who can't or don't want to spend on high-end appliances start to demand them.\"\n",
" },\n",
" {\n",
" \"response\": {\n",
" \"label\": \"no\",\n",
" \"explanation\": \"This comment does not describe someone's experience of switching to an induction cooktop, as they have not yet made the switch.\"\n",
" },\n",
" \"comment\": \"We have a gas stove, and like it. Our range hood is vented to the outside. We will be getting an induction range within 2 to 4 years. We are older, and it is a safer option since there will not be an open flame. We took this decision before the \\\"controversy\\\" arose. Gas stoves are better than standard electric in some areas (where gas is cheaper), and work very well. It is also true that the products of combustion are a problem. Houseplants help, opening windows helps, range hoods help. Nothing works perfectly.\"\n",
" },\n",
" {\n",
" \"response\": {\n",
" \"label\": \"no\",\n",
" \"explanation\": \"The comment does not describe someone's experience of switching to an induction cooktop.\"\n",
" },\n",
" \"comment\": \"Unless you have a small home with little ventilation, the risk from gas stove emissions is quite small. After cooking on a induction stove top for a year in a rental we opted for gas when we put a new stove in our own house. It is well ventilated and this old house provides a lot of unplanned ventilation too.\"\n",
" },\n",
" {\n",
" \"response\": {\n",
" \"label\": \"no\",\n",
" \"explanation\": \"This comment does not describe someone's experience of switching to an induction cooktop.\"\n",
" },\n",
" \"comment\": \"And just so we're all clear: the suitability of a pan for induction cooking is not \\\"adhesion\\\". Testing with a magnet is a quick way to determine whether the pan has enough ferrous metal to respond to the magnetic field generate by the element.\"\n",
" },\n",
" {\n",
" \"response\": {\n",
" \"label\": \"no\",\n",
" \"explanation\": \"The comment does not describe someone's experience of switching to an induction cooktop.\"\n",
" },\n",
" \"comment\": \"The gas stove hullabaloo is absurd. The matter of the plastic straws is infinitely more serious because they stay out there essentially forever and affect other creatures. I have no problem banning them.\"\n",
" }\n",
"]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"---\n",
"\n"
],
"metadata": {
"id": "fpcL7XgPToNA"
}
},
{
"cell_type": "markdown",
"source": [
"## 5. Extraction\n",
"\n",
"Models can also take unstructured data like documents and structure it for further analysis. \n",
"\n",
"**Use Case**: Roberto Rocha posted an interesting use case here https://robertorocha.info/getting-tabular-data-from-unstructured-text-with-gpt-3-an-ongoing-experiment/ The idea is to extract structured data about who is lobbying government officials from the Candian federal lobbyist registry. This is quite a messy data source. \n",
"\n",
"⚠ **WARNING** ⚠ : It took a lot of prompt iteration for this one to get it to work as intended even just on the small sample of documents in the demo. Further testing would be required to confidently generalize it to a larger dataset. "
],
"metadata": {
"id": "AC5QCa-kTXKi"
}
},
{
"cell_type": "code",
"source": [
"import time\n",
"from openai.error import RateLimitError\n",
"\n",
"# A sample of 4 documents we might want to parse\n",
"sample_documents = [\"1996-1997 EXECUTIVE ASSISTANT MINISTER OF TRANSPORT\",\n",
"\"Special Assistant 1991 to 1993 Hon. Robert Kaplan\",\n",
"\"September 1984 to February 1988 Senior Policy Analyst - various assignments related to federal procurement and trade policy Department of Supply and Services\",\n",
"\"January 2002 to May 2002 Chief of Staff Office of the Minister of Public Works and Government Services Canada\"\n",
"]\n",
"\n",
"# Here is the prompt command\n",
"prompt = 'Extract two dates or years from the input data. Also extract a job description from each line of text in the input data. Create a three-column table with the first date, second date, and job description. If there is no date or job description, leave the column blank. \\\n",
"Use the following format: first date | second date | job description \\\n",
"input data: '\n",
"\n",
"# Loop through our list of sample_comments\n",
"for ndoc, document_text in enumerate(sample_documents):\n",
" # Construct the API request \n",
" try:\n",
" response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + document_text, \n",
" temperature=0.7, \n",
" max_tokens=256,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
" )\n",
"\n",
" print (response[\"choices\"][0][\"text\"])\n",
"\n",
" # You need to sleep for 3 seconds in between API calls to avoid being rate limited (the rate limit is 20 calls / minute for the free tier)\n",
" time.sleep(3)\n",
" except RateLimitError as e:\n",
" print (e)\n",
" time.sleep (10) # Wait some more\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 231
},
"id": "yu1gjvGTTtu7",
"outputId": "bb4e1a06-9ebf-45d7-a4aa-cc80557eb6e2"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\n",
"1996 | 1997 | Executive Assistant Minister of Transport\n",
"\n",
"\n",
"1991 | 1993 | Special Assistant Hon. Robert Kaplan\n",
"\n",
"\n",
"September 1984 | February 1988 | Senior Policy Analyst\n",
"\n",
"\n",
"January 2002 | May 2002 | Chief of Staff, Office of the Minister of Public Works and Government Services Canada\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"---\n",
"\n"
],
"metadata": {
"id": "oNKkaiXjTqYc"
}
},
{
"cell_type": "markdown",
"source": [
"## 6. Structured Data to Text\n",
"\n",
"These models can take structured data and output readable text to support various data journalism projects. \n",
"\n",
"**Use Case**: You want to create a quick blurb for your website that summarizes the CDC weekly Influenze Surveillance Data (https://www.cdc.gov/flu/weekly/index.htm) and personalizes it the location (e.g. state) of the user. \n",
"\n",
"⚠ **WARNING** ⚠ : When generating text, always be sure to double-check it for factual accuracy before publishing. "
],
"metadata": {
"id": "gIuCH1ryTXTw"
}
},
{
"cell_type": "code",
"source": [
"# Here is the prompt command\n",
"prompt = \"Write a short description of this week's influenza activity based on the following data: \"\n",
"\n",
"# And some sample data\n",
"data = \"State: Virginia \\\n",
"ILI Activity Level: Moderate \\\n",
"ILI Rate for visits to emergency departments or urgent care centers: 3.2%\"\n",
"\n",
"response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt= prompt + data, \n",
" temperature=0.7, \n",
" max_tokens=256,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
")\n",
"\n",
"# Output the response\n",
"print (response[\"choices\"][0][\"text\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 88
},
"id": "zfXnXPSvTfMC",
"outputId": "23ebcf7e-9c34-4cf6-e12e-99b618ec9d40"
},
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <style>\n",
" pre {\n",
" white-space: pre-wrap;\n",
" }\n",
" </style>\n",
" "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\n",
"This week, influenza activity in Virginia has been moderate. Visits to emergency departments or urgent care centers have reported an ILI rate of 3.2%.\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "qFxEeTliu0mQ"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment