cperazza/RFM_Segmentation
This is a basic workflow with CrewAI agents working with sales transactions to draw business insights and marketing recommendations. The agents will work on everything from the execution plan to the business insights report. It works with local LLM via Ollama (I'm using llama3:8B but you can easily change it).
RFM Segmentation Tool
Project Goals
The RFM Segmentation Tool is designed to help businesses analyze their sales transaction data and segment their customers based on Recency, Frequency, and Monetary (RFM) metrics. This tool utilizes multiple AI agents to manage the project, load and preprocess data, calculate RFM segments, and provide detailed insights and recommendations.
Project Structure
The project is organized into the following directories:
config/: Contains YAML configuration files for agents and tasks.src/: Contains the main scripts and agent scripts.agents/: Contains the individual agent scripts.tools/: Contains custom tools if necessary.
data/: Contains the output files generated by the agents.venv/: Virtual environment for the project.
Agents and Their Roles
- Project Manager: Manages the project scope and execution plan.
- Data Loader: Loads and preprocesses sales transaction data.
- RFM Calculator: Calculates RFM metrics and segments customers.
- Insight Provider: Analyzes RFM segments and provides detailed insights and recommendations.
Installation and Setup
Prerequisites
- Python 3.11 (or a compatible version)
- Pip (Python package installer)
- Git (optional, for cloning the repository)
- Jupyter Notebook (optional, for running the notebook version)
- VS Code or PyCharm (optional, for running the script version)
- Ollama with
llama3:8Bmodel installed (or any local model of your preference)
Steps
-
Clone the Repository
git clone <https://github.com/cperazza/CrewAI_RFM_Segmentation> cd RFM_Tool
-
Create and Activate Virtual Environment
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Required Packages
pip install -r requirements.txt
-
Configure Local LLM via Ollama
Set the following environment variables in your terminal or include them in a.envfile.export OPENAI_API_BASE='http://localhost:11434/v1' export OPENAI_MODEL_NAME='openhermes' # Adjust based on available model export OPENAI_API_KEY=''
-
Run the Main Script
python src/main.py
Detailed Interaction Guide
Project Manager Agent
- Goal: Define and manage the project scope and execution plan.
- Interaction:
- The agent will ask for detailed project guidelines. Provide a description of your project data.
- If you have reference files (CSV), provide the file path.
- The agent will analyze the data and provide an execution plan.
- Confirm if you are happy with the plan. If not, provide feedback, and the agent will adjust the plan accordingly.
- The agent will save the execution plan as a PDF in the
data/folder.
Data Loader Agent
- Goal: Load and preprocess sales transaction data.
- Interaction:
- The agent will ask for the path to the project data file (CSV).
- The agent will load and analyze the data, performing exploratory data analysis (EDA).
- Confirm if you have any questions about the data. The agent will use LLM to provide answers.
- The agent will calculate RFM, AOV, and AOI metrics and save the processed data as
processed_data.csvin thedata/folder. - Confirm if you are happy with the processed data. If not, provide feedback, and the agent will adjust the data accordingly.
RFM Calculator Agent
- Goal: Calculate RFM metrics and segment customers.
- Interaction:
- The agent will ask for the path to the processed data file (processed_data.csv).
- The agent will calculate RFM segments using quartiles, quintiles, and deciles.
- The agent will save the segmented data as
rfm_segmented_data.csvin thedata/folder. - Confirm if you are happy with the segmented data. If not, provide feedback, and the agent will adjust the segmentation accordingly.
Insight Provider Agent
- Goal: Provide detailed insights and recommendations based on RFM analysis.
- Interaction:
- The agent will ask for the path to the segmented data file (rfm_segmented_data.csv).
- The agent will analyze the RFM segments and generate insights and visualizations.
- The agent will save the final report as a PDF and the chart code as a Python file in the
data/folder. - Confirm if you are happy with the final report and insights. If not, provide feedback, and the agent will adjust the analysis accordingly.
Testing with Real-life Data
To ensure the functionality and performance of the system, load real-life sales transaction data following the standard template:
customer_idorder_iddaterevenue
Follow the interaction guide to run the agents sequentially and verify the outputs at each stage.
Conclusion
This documentation provides a comprehensive guide to set up and run the RFM Segmentation Tool using AI agents. Follow the steps carefully to ensure a smooth setup and execution. If you encounter any issues, refer to the troubleshooting section or reach out for support.
Troubleshooting
- Virtual Environment Issues: Ensure the virtual environment is activated before running any scripts.
- Package Installation Errors: Check the
requirements.txtfile for any missing or conflicting packages. - LLM Configuration: Ensure the environment variables for Ollama are correctly set.
Contact
For further assistance, please contact [calperazza@gmail.com].