Skip to content

Instantly share code, notes, and snippets.

@kmad
Created June 24, 2023 19:41
Show Gist options
  • Save kmad/80093f76686abdf7358225647cda0b4c to your computer and use it in GitHub Desktop.
Save kmad/80093f76686abdf7358225647cda0b4c to your computer and use it in GitHub Desktop.
Example of using OpenAISchema and Function Calls
import openai
from openai_function_call import OpenAISchema
from pydantic import Field
from typing import List, Any
import requests
from bs4 import BeautifulSoup
def getPage(url):
html = requests.get(url).content
soup = BeautifulSoup(html, features='lxml') # Can customize based on need; e.g., .find('div', class_='detail-cms-content')
return soup.text.strip().replace("\n", " ")
# Based on examples provided in https://github.com/jxnl/openai_function_call/
def getCompletion(text: str, schema: OpenAISchema):
schemaName = schema.__name__
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
functions=[schema.openai_schema],
messages=[
{"role": "system", "content": f"I'm going to ask for details about the following text. Use {schemaName} to parse this data."},
{"role": "user", "content": f"{text}"},
],
)
return schema.from_response(completion)
#### Example 1: Extracting details from a CFTC enforcement action ####
class EnforcementDetails(OpenAISchema):
"""Extract information regarding CFTC enforcement action(s)"""
date: str = Field(..., description="Date of the action")
regulator: str = Field(..., description="Name of the regulator bringing the action")
enforcement_type: str = Field(..., description="Type of enforcement action: warning letter, civil fines, suspension/revocation of license, civil lawsuit, criminal lawsuit")
impacted_entities: List[str] = Field(..., description="Names of entities impacted by the enforcement action")
related_case: str = Field(..., description="Case number(s) of related actions")
defendants: List[str] = Field(..., description="Names of individuals or entities named as defendants")
alleged_crimes: List[str] = Field(..., description="Specific crimes alleged")
text = getPage('https://www.cftc.gov/PressRoom/PressReleases/8726-23')
enforcement_details = getCompletion(text, EnforcementDetails)
print(enforcement_details)
#### Example 2: LAPD Press Release Example ####
## Adapted from https://twitter.com/kcimc/status/1668789461780668416
class LAPDEventDetails(OpenAISchema):
"""User Details"""
date: str = Field(..., description="Date of event")
injured: str = Field(..., description="Who was injured in the event, if any")
serial: int = Field(..., description="Officer badge or serial number, if applicable")
deceased: str = Field(..., description="Names of those who are deceased, if any")
text = getPage('https://www.lapdonline.org/newsroom/officer-involved-shooting-in-hollywood-area-nrf059-18ma/')
event_details = getCompletion(text, LAPDEventDetails)
print(event_details)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment