In this example I am going to use python to read a CSV with 1 column headed ‘country’ and then for each country in the file it will write a new CSV file with the information returned from ChatGPT3 to create a country and description CSV file.
file1.csv should be kept in to same folder as your python script. file2.csv will be created when the script is executed.
To install openai library you will need to use the following commands to install the python packages.
pip install openai
import csv import openai openai.api_key = 'paste your API key from chatgpt3 here' # Open the CSV file with open('file1.csv', 'r') as file: # Create a CSV reader reader = csv.reader(file) # Open the output CSV file with open('file2.csv', 'w', newline='') as outfile: # Create a CSV writer writer = csv.writer(outfile) # Loop through each row in the input CSV file for row in reader: # Access the 'country' column (index 0) in each row country = row[0] body = 'body' if country != "country": response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role" : "user", "content" : "Assume you are a tourguide specialising in eco holidays for travellers visiting countries around the world. Write an introductory page about " + country+ " telling the reader that the information on this page is a guide to the recommended citys and towns for travellers from the UK. Write 2 to 3 paragraphs only and use UK english in your spelling. Each paragraph must enclosed in HTML opening and closing p tags." } ], temperature=1, max_tokens=512, top_p=1, frequency_penalty=0, presence_penalty=0 ) body = response["choices"][0]["message"]["content"] print(country) #print(response["choices"][0]["message"]["content"]) #exit() # Write the row to the output CSV file with the additional 'body' column writer.writerow([country, body])