geteff
1
Hi! I am trying to build a workflow that iterates over contents from an Excel file, which uses assistant api retrieval. It is always processing the first row and I am not sure what is wrong. Thanks to anyone who helps.
import os
import pandas as pd
import openai
import time
from openai import OpenAI
from dotenv import load_dotenv
# Load the OpenAI API key
load_dotenv('your_env_file.env')
api_key = os.getenv('YOUR_ENV_VARIABLE_FOR_API_KEY')
openai.api_key = api_key # Make sure to set the API key for the openai package
# Read contents from an Excel file
excel_path = 'path_to_your_excel_file.xlsx'
df = pd.read_excel(excel_path)
content_column = 'your_content_column_name' # Replace with the name of your column
# Prepare a column in the DataFrame to store responses
df['Assistant_Responses'] = ''
# Upload a file with an "assistants" purpose
client = OpenAI()
file = client.files.create(
file=open("path_to_your_file.json", "rb"),
purpose='assistants'
)
print(file.id)
# Iterate over each content in the DataFrame
for index, row in df.iterrows():
user_input = row[content_column]
print(f"Processing row {index + 1}/{len(df)}...")
# Create a thread
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=user_input,
file_ids=[file.id]
)
run = openai.beta.threads.runs.create(
thread_id=thread.id,
assistant_id="your_assistant_id"
)
while run.status != 'requires_action':
time.sleep(1)
run = openai.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
try:
messages = openai.beta.threads.messages.list(
thread_id=thread.id
)
# Assuming the last message is the response
if messages.data:
response = messages.data[-1].content
df.at[index, 'Assistant_Responses'] = response
except Exception as e:
print(f"Error processing row {index + 1}: {e}")
# Save the updated DataFrame back to the Excel file
df.to_excel(excel_path, index=False)
print("Processing complete.")