Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Olshansk/029d77c1d2bceda2d2d42a03a106019d to your computer and use it in GitHub Desktop.
Save Olshansk/029d77c1d2bceda2d2d42a03a106019d to your computer and use it in GitHub Desktop.
Example of OpenAI function calling API to extract data from LAPD newsroom articles.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import json\n",
"import requests\n",
"from bs4 import BeautifulSoup"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"url = 'https://www.lapdonline.org/newsroom/officer-involved-shooting-in-hollywood-area-nrf059-18ma/'\n",
"html = requests.get(url).content\n",
"soup = BeautifulSoup(html).find('div', class_='detail-cms-content')\n",
"text = soup.text.strip()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"date\": \"October 29, 2018\",\n",
" \"violent\": true,\n",
" \"fatal\": true,\n",
" \"in_custody\": false,\n",
" \"unintentional_discharge\": false,\n",
" \"injured\": [\"Officer Edward Agdeppa\"],\n",
" \"deceased\": [\"Albert Ramon Dorsey\"],\n",
" \"serials\": [41000]\n",
"}\n"
]
}
],
"source": [
"functions = [\n",
" {\n",
" \"name\": \"extract_data\",\n",
" \"description\": \"Add the summary of a newsroom article to the database.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"date\": {\n",
" \"type\": \"string\",\n",
" \"format\": \"date\"\n",
" },\n",
" \"violent\": {\n",
" \"type\": \"boolean\",\n",
" \"description\": \"Does this describe a violent incident?\"\n",
" },\n",
" \"fatal\": {\n",
" \"type\": \"boolean\",\n",
" \"description\": \"Does this describe a fatal incident?\"\n",
" },\n",
" \"in_custody\": {\n",
" \"type\": \"boolean\",\n",
" \"description\": \"Did this happen in custody?\"\n",
" },\n",
" \"unintentional_discharge\": {\n",
" \"type\": \"boolean\",\n",
" \"description\": \"Was this an unintentional discharge?\"\n",
" },\n",
" \"injured\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"What are the names of the people who were injured, if any?\"\n",
" },\n",
" \"deceased\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"string\"\n",
" },\n",
" \"description\": \"What are the names of the people who are deceased, if any?\"\n",
" },\n",
" \"serials\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"number\"\n",
" },\n",
" \"description\": \"What are the serial numbers of the officers involved?\"\n",
" }\n",
" },\n",
" \"required\": [\"date\", \"violent\", \"fatal\", \"in_custody\", \"unintentional_discharge\", \"injured\", \"deceased\", \"serials\"],\n",
" },\n",
" }\n",
"]\n",
"\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant that extracts summaries of LAPD newsroom articles as JSON for a database.\"},\n",
" {\"role\": \"user\", \"content\": 'Extract a summary from the following article: ' + text}\n",
"]\n",
"\n",
"response = openai.ChatCompletion.create(\n",
" model='gpt-3.5-turbo-0613', functions=functions, messages=messages)\n",
"\n",
"print(response.choices[0]['message']['function_call']['arguments'])\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "question-answering",
"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.8.16"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment