This code reads a CSV file containing a column of keywords and uses the OpenAI API to categorize each keyword into an intention and a stage. It then creates a Pandas DataFrame with the categorized keywords and saves it as a CSV file.
- CSV file containing a column of keywords
- OpenAI API key
- Read the CSV file and store it in a Pandas DataFrame.
- Set the OpenAI API key.
- Initialize variables for counting keywords and batch indexing.
- Calculate the total number of keywords.
- Iterate over batches of 100 keywords until all keywords are processed.
- Convert each batch of keywords to a string.
- Create a prompt for the OpenAI API using the batch of keywords.
- Call the OpenAI API to categorize the keywords.
- Process the categorized keywords and split them into substrings.
- Assign each substring to temporary variables and append them to corresponding lists.
- Update the counters.
- Create a Pandas DataFrame from the lists of categorized keywords.
- Save the DataFrame as a CSV file.
- Categorized keywords saved as a CSV file.
- To run the script need
cp .env.local .env
- Place a ChatGPT Openai key in the .env file for the script to function properly.
- Create a virtual environment and run the command and install all the dependencies
pip install -r requirements.txt
- Activate the virtual environment and run the script in the shell.
python search_intent_to_csv.py
- Or the command to install all the tools with poetry install, the auto executes everything alone.
make install