Read list of example addresses to a dataframe¶

In [1]:
import os
import openai
import pandas as pd

df = pd.read_csv("Full.csv",usecols = ['Company'])
df
Out[1]:
Company
0 L.B. Thompson Inc., Tampa, FL 33610
1 Presiliana Zamora, Conroe, TX 77304
2 Cesar Tapia, Miami, FL 33166
3 Knobel Refrigeration, Inc., North Platte, NE 6...
4 Greensburg, PA 15601
5 Lighthouse Electric, Boulder, CO 80301
6 Priority Energy Services, LLC, Raleigh, MS 39153
7 KM Davis Contracting Co., Inc., Marietta, GA 3...
8 Jenkins Electric, Post, TX 79356
9 Lariat Communications, De Kalb, TX 75559
10 Larry Walty Roofing and Guttering, Inc,, Wichi...
11 Newohmer Brothers Feed Lot, Elgin, NE 68636
12 Art Iron Works, Inc., Onconta, AL 35121
13 Fort Smith Garage Doors, Kibler, AR 72921
14 SES Welding, LLC, Holly Bill, FL 32117
15 Allens Inc,, Siloam Springs, AR 72761
16 Howie's Tree Services, Merrick, NY 11566
17 Martin Product Sales, LLC, Beaumont, TX 77705
18 Green Contractors of NY, Merrick NY 11566
19 Alvin Wynn Electric Company, Alapaha, GA 31622
20 Lakeridge Winery, Clermont, FL 34711
21 Mandy Electric, Inc., Tampa, FL 33610
22 Arthur Vincent & Sons Construction Inc, Armonk...

Pass csv data to OpenAI API line with prompt and read results into a dataframe and csv¶

In [3]:
openai.api_key = "sk-uCplXmEuLWmyxxxxxxxxxxxxxxxxxxxxhgWBbLaBj"
results = []

for index, row in df.iterrows():

  response = openai.Completion.create(
  model="text-ada-001",
  prompt=f"I am address parsing robot. Give me an imput string and i will parse out the city, state, and zip code\\n\n\
  Input: L.B. Thompson Inc., Tampa, FL 33610\ncity: Tampa\nstate: FL\nzip: 33610\n\nInput: Priority Energy Services, LLC, Raleigh, MS 39153\ncity: Raleigh\nstate: MS\nzip: 39153\n\n\
  Input: Larry Walty Roofing and Guttering, Inc,, Wichita, KS 67208\ncity: Wichita\nstate: KS\nzip: 67208\n\n\
  Input: Alvin Wynn Electric Company, Alapaha, GA 31622\ncity: Alapaha\nstate: GA\nzip: 31622\n\n\
  Input: Jensen Construction Company, Pine Bluff, AR  71601\ncity: Pine Bluff\nstate: AR\nzip: 71601\n\n\
  Input: Veolia Environmental Services Pinellas, Inc., St. Petersburg, FL 33716-2002\ncity: St. Petersburg\nstate: FL\nzip: 33716\n\n\
  Input: Allens Inc,, Siloam Springs, AR 72761\ncity: Siloam Springs\nstate: AR\nzip: 72761\n\n\
  Input: Hardrives Asphalt Company, Deerfield Beach, FL\ncity: Deerfield Beach\nstate: FL\nzip: N/A\n\n\
  Input: Greensburg, PA 15601\ncity: Greensburg\nstate: PA\nzip: 15601\n\n\
  Input: Cesar Tapia, Miami, FL 33166\ncity: Miami\nstate: FL\nzip: 33166\n\n\
  Input: SES Welding, LLC, Holly Bill, FL 32117\ncity: Holly Bill\nstate: FL\nzip: 32117\n\n\
  Input: {row['Company']}",
  temperature=0.7,
  max_tokens=256,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0)

  output = response.choices[0].text.replace('city: ','').replace('state: ','').replace('zip: ','').split('\n')
  city = output[1]
  state = output[2]
  zip = output[3]
  results.append([row['Company'], city, state, zip])
  output_df = pd.DataFrame(results, columns = ['Input', 'city', 'state', 'zip'])
  output_df.to_csv('output.csv', index=False)
    
pd.DataFrame(results)
Out[3]:
0 1 2 3
0 L.B. Thompson Inc., Tampa, FL 33610 Tampa FL 33610
1 Presiliana Zamora, Conroe, TX 77304 Conroe TX 77304
2 Cesar Tapia, Miami, FL 33166 Miami FL 33166
3 Knobel Refrigeration, Inc., North Platte, NE 6... North Platte NE 69103
4 Greensburg, PA 15601 Greensburg PA 15601
5 Lighthouse Electric, Boulder, CO 80301 Boulder CO 80301
6 Priority Energy Services, LLC, Raleigh, MS 39153 Raleigh MS 39153
7 KM Davis Contracting Co., Inc., Marietta, GA 3... Marietta GA 30008
8 Jenkins Electric, Post, TX 79356 Post TX 79356
9 Lariat Communications, De Kalb, TX 75559 De Kalb TX 75559
10 Larry Walty Roofing and Guttering, Inc,, Wichi... Wichita KS 67208
11 Newohmer Brothers Feed Lot, Elgin, NE 68636 Elgin NE 68636
12 Art Iron Works, Inc., Onconta, AL 35121 Ononta AL 35121
13 Fort Smith Garage Doors, Kibler, AR 72921 Kibler AR 72921
14 SES Welding, LLC, Holly Bill, FL 32117 Holly Bill FL 32117
15 Allens Inc,, Siloam Springs, AR 72761 Siloam Springs AR 72761
16 Howie's Tree Services, Merrick, NY 11566 city:Merrick NY 11566
17 Martin Product Sales, LLC, Beaumont, TX 77705 Beaumont TX 77705
18 Green Contractors of NY, Merrick NY 11566 Merrick NY NY 11566
19 Alvin Wynn Electric Company, Alapaha, GA 31622 Alapaha GA 31622
20 Lakeridge Winery, Clermont, FL 34711 Clermont FL 34711
21 Mandy Electric, Inc., Tampa, FL 33610 Tampa FL 33610
22 Arthur Vincent & Sons Construction Inc, Armonk... Armonk NY 10504