This repository contains the source code for Retail-GPT, a open-source RAG-based chatbot designed to guide users through product recommendations and assist with cart operations, aiming to enhance user engagement with retail e-commerce and serve as a virtual sales agent. The goal of this system is to test the viability of such an assistant and provide an adaptable framework for implementing sales chatbots across different retail businesses.
For demonstration and test purposes, the chatbot implemented in this repository is contextualized to work as a sales agent for a fictional Foo convenience store. Nevertheless, the implementation is not dependant of this domain and could be adapted to work with other type of businesses.
The project paper can be read at retailGPT PDF
The complete project is composed of 4 different applications that can be run separately:
- Chat interface: Front-end application for demonstration purposes built primarily with
Streamlit
. This application is optional, as the chatbot can be executed directly from the terminal. - Rasa chatbot:
Rasa
application that serves as a base for all the chatbot. Responsible for processing chit-chat, extracting entities and delegating tasks to the RAG subsystem - Rasa actions server:
Rasa
's custom actions server, in which validations and the RAG system is implemented - Redis database: Database for storing conversation histories and users' carts.
For powering the Rag system implemented in the actions server, Open AI's gpt-4o
model was used through the Open AI API
for its response quality and hability to perform function calls. Function calls are the primary feature used for integrating the model with external tools due to its simplicity of use and efectiveness, but it would be possible to achieve similar results by replacing them with techniques such as ReAct prompting.
To install and run the chatbot demo interface, follow the steps below:
- Clone the repository
- Run
cd chat_interface
to go to the interface directory - Install poetry for dependency management
- Install the dependencies with
poetry install
in your current active environment (using a separate python environment is advisable) - Go to the app's folder with
cd src
- Run the interface application with
streamlit run app.py
The chatbot demo interface will be available at http://localhost:8501. You can interact with the chatbot by typing messages in the input box and pressing Enter.
To install the dependencies and execute the chatbot:
- Run
cd retailGPT/rasa_chatbot
to go to the Rasa chatbot directory - Run
poetry install
to install the dependencies in your current active environment (using a separate python environment is advisable) - Run
python -m spacy download en_core_web_lg
to download thespacy
model used - Run
rasa train
for training the model - Run
rasa run
if you want to run the application as an API.
Alternatively, instead of running rasa run
, you can run rasa shell
(can add --debug
option to get more logs) for running the application and starting a chat directly in the terminal. This option can be useful if you don't want to run the separate interface application.
To install the dependencies and execute the actions server:
- Run
cd retailGPT/actions_server
to go to the Rasa chatbot directory - Run
poetry install
to install the dependencies in your current active environment (using a separate python environment is advisable) - Make sure you have an environment variable
OPENAI_API_KEY
set with your API key value- If you prefer using Open AI through Microsoft Azure, make sure you edit the boolean use_azure in llm_handler to True and set the following environment variables:
AZURE_OPENAI_API_KEY
: key for azure Open AIAZURE_RESOURCE
: Azure resource in which the model is deployedAZURE_API_VERSION
: Api version of the deployment
- If you prefer using Open AI through Microsoft Azure, make sure you edit the boolean use_azure in llm_handler to True and set the following environment variables:
- Run
python -m rasa_sdk --actions actions
for running the server
The database is run as a Redis container. For running the database:
- Make sure you have Docker installed in your machine
- Run
docker-compose up database
The database will run at port 6379 and will be ready to use by the other applications
You can also run everything through Docker using the compose file at the root of the project. Be aware that, in order for the containers to communicate between each other in the Docker network, you will also need to update the endpoints in the project. In this, sense, do:
In chat_interface/src/chatbot.py
: Change "localhost" to "rasa" in the Rasa chatbot URLs and "localhost" to "database" in the single Redis-related URL.
In retailGPT/rasa_chatbot/endpoints.tml
: Change "localhost" to "actions"
In retailGPT/actions_server/src/LLMChatbot/services/chatbot.py
: Change "localhost" to "database"
To deploy the bot in an Virtual Machine, you can use a Nginx server as a reverse proxy to route the requests to the correct container and validate the SSL certificate. The Nginx configuration file is located at /etc/nginx/sites-available/default
. In this file, we map the default port 80 to the Streamlit app running on port 8501. Besides that, we also map the Streamlit app /_stcore/stream
endpoint to the corresponding location on the machine. This is necessary for the Streamlit app to work properly.
For the generation and renewal of the SSL certificate, we recommend using Certbot.
If this is your first time working on this repository, you will probably need to get familiar with the files and directories listed below. Please note that not all the project's files are listed, only the most important ones.
chat_interface
: Interface applicationretailGPT
: Contains all the chatbot's logicrasa_chatbot
:Rasa
projectendpoints.yml
: Contains the endpoints for the bot, such as the action server and the tracker store.domain.yml
: Contains the domain of the bot, including intents, entities, actions, and responses.config.yml
: NLP pipeline used by Rasadata
nlu.yml
: Contains the NLU training data.stories.yml
: Contains the stories for the bot.rules.yml
: Contains the rules for the bot.
models
: Contains the trained models for classifing intents and extracting slots.
datasets
: fictional datasets that are used for testing and providing the available e-commerce productsactions_server
:Rasa
's custom actions serversrc
LLMChatbot
: implements the RAG-based subsystemchatbot.py
: Contains the code for the LLM-based chatbot.prompts.py
: Contains the prompts for the LLM-based chatbot.services
database.py
: Contains the code for the database service.product_handler.py
: Contains the code for product-related features.memory_handler.py
: Contains the code for memory-related features.cart_handler.py
: Contains the code for cart-related features.llm_handler.py
: Contains the code for LLM-related featuresguardrails
: Contains guardrails implementation