I am trying to create an OpenAI and Pandas integrated program that takes values and a user input to generate a chat response and loads the responses into the second column on the excel. Not sure what's up, but the program isn't working. It runs for like 10 minutes before hitting a bad gateway error. Can someone explain to me what I may be doing wrong w my code? EDIT: It is working now, but now it's not storing my Column Values
There's less than 100 rows in the column for the test.
I expected my code to store the values in the second row of my excel file. Here is my code:
import pandas as pd
import openai
# Set up your OpenAI API credentials
openai.api_key = "MYKEY"
def store_column_values_to_excel(file_path, sheet_name, column_name):
# Read the Excel file into a DataFrame
df = pd.read_excel(file_path, sheet_name=sheet_name)
# Get the values from the specified column
column_values = df[column_name].tolist()
return column_values
def generate_chat_prompt(column_values, user_input):
prompt = "User: " + user_input + "\nAI: "
for value in column_values:
prompt += "\nUser: " + user_input + value + "\nAI: "
return prompt
def store_responses_to_excel(file_path, sheet_name, column_name, responses):
# Read the Excel file into a DataFrame
df = pd.read_excel(file_path, sheet_name=sheet_name)
# Add a new column for storing responses
df["AI Response"] = responses
# Save the updated DataFrame to the Excel file
df.to_excel(file_path, sheet_name=sheet_name, index=False)
# Specify the Excel file details
file_path = "MYFILELOCATION"
sheet_name = "Sheet1"
column_name = "URLs"
# User input for ChatGPT
user_input = input("User: ")
# Store column values from Excel
column_values = store_column_values_to_excel(file_path, sheet_name, column_name)
# Generate the chat prompt
chat_prompt = generate_chat_prompt(column_values, user_input)
# Specify the parameters for the ChatGPT API call
chat_params = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": user_input},
],
"max_tokens": 100,
"temperature": 0.7,
}
# Interact with ChatGPT
response = openai.ChatCompletion.create(**chat_params)
ai_response = response.choices[0].message["content"]
print("AI:", ai_response)
# Store AI responses in the second column of the Excel file
store_responses_to_excel(file_path, sheet_name, "AI Response", [ai_response])
Here's what I'm getting: openai.error.APIError: Bad gateway. {"error":{"code":502,"message":"Bad gateway.","param":null,"type":"cf_bad_gateway"}} 502 {'error': {'code': 502, 'message': 'Bad gateway.', 'param': None, 'type': 'cf_bad_gateway'}} ....
The bad gateway issue is fixed, but now it is not storing the column values: User: Given the following URL, does this company... AI: I'm sorry, but you haven't provided a URL for me to review. Please provide the URL so I can assist you better.