A somewhat technical project to set up, but it offers a good overview of the construction to adopt.
We know that the AI can be the victim of hallucinations. It can also rely on general knowledge, which is not always reliable and relevant. Creating a personalized chatbot with RAG (retrieval-augmented generation) solves these two major problems. This allows the AI to scrutinize the documents provided (reports, FAQs, technical manuals, internal policies, etc.), understand them and integrate them into its conversational model. This provides a smooth and intuitive user experience, with quick access to the information you are looking for. The applications are numerous. For example, an industrial equipment manufacturer can provide automatic assistance based on its technical manuals.
To create a custom chatbot with RAG, we will create virtual environment, a project folder, install dependencies and launch the application. We go through a slightly technical method, but which must adapt to a large number of machines.
1. Create a Python environment for the chatbot project
We download Python 3.11.8 for Windows (64-bit), to adapt according to your machine. In Advanced Options, we check the boxes Create shortcuts for installed applications and Add Python to environment variables;.
To verify that Python and pip are installed, we open a new PowerShell or Command Prompt window and type the commands:
# Vérifier Python python --version # Vérifier pip pip --version # Résultat attendu : # Python 3.11.8 # pip 23.0.1 from C:Users...AppDataLocalProgramsPythonPython311libsite-packagespip
This should show the installed versions without errors.
We then install the C++ Build Tools. Indeed, some Python libraries are written in C++ and need compilation tools to install correctly on Windows. Without these tools we will receive an error when installing dependencies. We go to: https://visualstudio.microsoft.com/fr/visual-cpp-build-tools/, click on “Download Build Tools”. We launch the installer, on the “Workloads” tab, we check the “Desktop development in C++” box. Then we go to “Install” at the bottom right. We check the installation on PowerShell by typing: cl.exe
We then create the project and the virtual environment. In Powershell we write:
# Allez à la racine de votre disque C: (ou un autre emplacement de votre choix) cd C: # Créez le dossier du projet mkdir chatbot-rag-local # Entrez dans le dossier du projet. C'est une étape cruciale. cd chatbot-rag-local
Your command prompt should now display PS C:chatbot-rag-local>.
Now that we are inside the project folder, we will create the virtual environment. This is a subfolder named venv that will contain a copy of Python and all of our project’s dependencies. We execute the command:
python -m venv venv
This creates a venv folder in C:chatbot-rag-local. It contains a structure (Include, Lib, Scripts) which completely isolates the project. Libraries installed later will go into venvLib and will not affect the overall Python installation.
Before installing anything, we “activate” this environment. It tells PowerShell to use the Python and pip located in the venv folder.
.venvScriptsActivate.ps1
After running this command, the start of our command line displays (venv):
(venv) PS C:chatbot-rag-local>
Note that if you get a security error, run the following command only once to allow scripts, then try enabling again.
Plain Text Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Answer Y (Yes) to the question.
2. Creating a project folder
Now that your Python environment is ready, we will launch the chatbot project directly on our machine. In the chatbot-rag-local project folder, we will set up a data folder, .env, requirements.txt, app.py files and a templates folder.
A. Document.txt
In the data folder, we create a document.txt. It is recommended to use plain text, without exotic characters of reasonable length and encode in Latin-1. For our RAG chabot, we take a simple example of a text on the Eiffel Tower. You can then replace it with whatever you want. We copy and paste the text:
“The Eiffel Tower is a 330 meter high iron tower located in Paris, at the northwest end of the Champ-de-Mars park on the banks of the Seine in the 7th arrondissement. Its official address is 5, avenue Anatole-France. Built in two years by Gustave Eiffel and his collaborators for the 1889 Paris Universal Exhibition, it has become the symbol of the French capital and a leading tourist site. It “It is the fourth most visited paid French cultural site in 2023, with 6.3 million visitors.”
B. .env file
The .env file is used to store the secret API key. To take it, we connect to our OpenAI account at https://platform.openai.com/. We click on the cogwheel at the top right, select “API keys” on the left and “Create new secret key”, which we copy into env:
@" OPENAI_API_KEY=VOTRE_API_KEY "@ | Out-File -Encoding UTF8NoBOM .env
C.Requirements.txt
Still in the chatbot-rag-local folder, we create a “requirements.txt” file:
We mention the list of Python dependencies there:
flask langchain langchain-community langchain-openai faiss-cpu python-dotenv
D. app.py
Again in the “chatbot-rag-local” folder, we create an app.py file. This is the heart of the application. It contains the code to create a RAG chatbot. It is divided into 3 main parts. It loads the dependencies and initializes the system. It creates the vector database. Then it sets up the Flask server to communicate with the user. The code is:
# -*- coding: utf-8 -*-
import os
from flask import Flask, request, render_template, jsonify
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
# Lire la clé API du fichier .env
api_key = None
try:
with open('.env', 'r', encoding='utf-8') as f:
content = f.read()
for line in content.split('n'):
line = line.strip()
if line.startswith('OPENAI_API_KEY='):
api_key = line.replace('OPENAI_API_KEY=', '').strip(
break
except Exception as e:
print(f"Erreur lors de la lecture du fichier .env: {e}")
if not api_key:
raise ValueError("Clé API OpenAI non trouvée dans le fichier .env")
print("Clé API chargée avec succès")
print("Chargement du document...")
try:
loader = TextLoader('./data/document.txt', encoding='latin-1')
documents = loader.load()
except UnicodeDecodeError:
loader = TextLoader('./data/document.txt', encoding='latin-1')
documents = loader.load()
print("Division du document en morceaux...")
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
print("Création de la base de données vectorielle...")
embeddings = OpenAIEmbeddings(api_key=api_key)
vectorstore = FAISS.from_documents(docs, embeddings)
print("Création du modèle LLM...")
llm = ChatOpenAI(model_, api_key=api_key)
print("Application démarrée !")
app = Flask(__name__, template_folder='templates')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/ask', methods=('POST'))
def ask():
try:
data = request.get_json()
question = data.get('question', '')
if not question:
return jsonify({"result": "Veuillez poser une question"})
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
relevant_docs = retriever.invoke(question)
context = "n".join((doc.page_content for doc in relevant_docs))
prompt = f"Contexte:n{context}nnQuestion: {question}nnRéponse:"
response = llm.invoke(prompt)
return jsonify({"result": response.content})
except Exception as e:
return jsonify({"result": f"Erreur: {str(e)}"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Note that, for more robust code, you can replace the part:
api_key = None try: with open('.env', 'r', encoding='utf-8') as f: content = f.read() for line in content.split('n'): line = line.strip() if line.startswith('OPENAI_API_KEY='): api_key = line.replace('OPENAI_API_KEY=', '').strip() break
By :
from dotenv import load_dotenv load_dotenv() api_key = os.getenv('OPENAI_API_KEY') if not api_key: raise ValueError("Clé API OpenAI non trouvée dans le fichier .env")
You can also replace gpt-3.5-turbo with gpt-4-mini in the code.
E.index.html
In the “templates” folder located at the root of the project, we create the index.html file. He creates the web interface of the chatbot. It contains HTML for structure, CSS for styling, and JavaScript for logic (what happens when you click). We copy and paste this code:
Chatbot
3. Installing dependencies
In our PowerShell terminal, with (Friv) activated at the beginning of the command line, we type:
(venv) PS C:Usersadmin> cd C:chatbot-rag-local
(venv) PS C:chatbot-rag-local> pip install -r requirements.txt
(venv) PS C:chatbot-rag-local> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
This installs Flask, LangChain, and other necessary dependencies.
The final structure of the project is:
chatbot-rag-local/
├── venv/ (created automatically)
│ ├── Scripts/
│ ├── Lib/
│ └── …
├── data/
│ └── document.txt
├── templates/
│ └── index.html
├── app.py
├── requirements.txt
└── .env
Still in the same terminal, we launch the chatbot with the command:
We see this message appear:
To view the chatbot, we go to the specified address, for example, http://127.0.0.1:5000 in this case.
On the chatbot page, we ask questions about the Eiffel Tower in the appropriate area at the bottom of the interface.
The chatbot’s response appears in gray. It corresponds well to the information given in the document.txt. To stop the chatbot, we press Ctrl + C in PowerShell.
4. Customize and deploy the chatbot
To have a text other than that of the Eiffel Tower, you must modify the document.txt file in the data folder. For example :
powershell @" VOTRE_NOUVEAU_TEXTE_ICI "@ | Out-File -Encoding Default datadocument.txt
After changing the text, restart the application:
powershell # Arrêtez l'application actuelle (Ctrl+C) # Puis relancez-la python app.py
To provide multiple separate files, you need to modify the app.py code. Create several files in the data folder:
powershell # Document 1 @" La France est un pays situe en Europe occidentale. Sa capitale est Paris. "@ | Out-File -Encoding Default datafrance.txt # Document 2 @" Python est un langage de programmation cree en 1991 par Guido van Rossum. "@ | Out-File -Encoding Default datapython.txt
Then open app.py in Notepad++ and replace this section:
Python
print("Chargement du document...")
try:
loader = TextLoader('./data/document.txt', encoding='latin-1')
documents = loader.load()
except UnicodeDecodeError:
loader = TextLoader('./data/document.txt', encoding='latin-1')
documents = loader.load()
By :
Python
print("Chargement des documents...")
import os
from pathlib import Path
documents = ()
data_folder = Path('./data')
# Charger tous les fichiers .txt du dossier data
for file_path in data_folder.glob('*.txt'):
try:
loader = TextLoader(str(file_path), encoding='latin-1')
docs = loader.load()
documents.extend(docs)
print(f"Charge: {file_path.name}")
except Exception as e:
print(f"Erreur en chargeant {file_path.name}: {e}")
if not documents:
raise ValueError("Aucun document trouve dans le dossier data/")
Save and relaunch the application.
You can deploy the chatbot on the internet with a Heroku account through https://www.pythonanywhere.com/. You can deploy it on AWS, Google Cloud or Azure. These services offer more power but are more complex to configure. Check out their documentation for Flask apps. On an existing website, it is possible to use an iframe or a personalized widget.




