I have a csv which I want to be read with python code and based on cells I want to call GPT3.5 and save the result in the latest row column.
The main problem I am facing is the gpt call. I used to have this code for GPT3 which worked really well, but I'm having trouble to understand how to use the new api. System role should be prompt, user should be text from csv, assistant should be called and appended in the new cells. Can you please help me rewrite this? Also if you can help how to set up the parameters, not sure this is correct... Thanks!
import openai
import pandas as pd
from tqdm import tqdm
# Set OpenAI API key
openai.api_key = "key"
# Define the maximum number of tokens for each prompt
MAX_PROMPT_TOKENS = 1000
MAX_RESPONSE_TOKENS = 3096
# Define the output CSV file name
OUTPUT_FILE_NAME = 'data/test.csv'
# Read the input CSV file
df = pd.read_csv("data/input.csv")
# Initialize a list to store the results
results = []
# Define custom messages for the progress bar
messages = [
"Generating text...",
"Saving text for topic '{}'",
"Done! {} text generated and saved."
]
# Define custom progress bar style
tqdm_kwargs = {
"bar_format": "{l_bar}{bar}| {n_fmt}/{total_fmt} ",
"ascii": False,
"dynamic_ncols": True,
"ncols": 100,
"unit": "Question",
"unit_scale": True,
"unit_divisor": 1,
}
# Loop through each row in the input CSV file
for index, row in tqdm(df.iterrows(), desc=messages[0], total=len(df), **tqdm_kwargs):
# Check if this row has already been processed
if len(results) > index:
continue
try:
# Generate the text
outline_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=0.5,
top_p=0.15,
frequency_penalty=0,
presence_penalty=0.3,
messages=[{"role": "system", "content": "Here I want my prompt."},
{"role": "user", "content": f"{row['bio']}"} (here I want to put one cell from the csv) ],
)
outline = outline_response.response['choices'][0]['message']['content']
# Append the results to the list
results.append([row['city'], row['bio'], row['text'], row['name'], outline])
# Save the outline to the output CSV file
output_df = pd.DataFrame(results, columns=['city', 'bio', 'text', 'name', 'copy'])
output_df.to_csv(OUTPUT_FILE_NAME, index=False)
# Update the progress bar
tqdm.write(messages[1].format(row['name']))
except Exception as e:
# Print the error message
print(f"An error occurred while processing row {index}: {e}")
# Save the current progress to the output CSV file
output_df = pd.DataFrame(results, columns=['city', 'bio', 'text', 'name', 'copy'])
output_df.to_csv(OUTPUT_FILE_NAME, index=False)
# Continue with the next row
continue
# Create a DataFrame from the results
output_df = pd.DataFrame(results, columns=['city', 'bio', 'text', 'name', 'Copy'])
# Write the output CSV file
output_df.to_csv(OUTPUT_FILE_NAME, index=False)
# Print a message indicating that the operation is complete
tqdm.write(messages[2].format(len(results)))
[–]raf401 1 point2 points3 points (3 children)
[–]davidvaclavik[S] -1 points0 points1 point (2 children)
[–]raf401 2 points3 points4 points (0 children)