Raichat integrates WhatsApp Web with AI-powered customer service functionality using LangChain and Google Generative AI. Below is a breakdown of the main components:
figure 1. raichat answer base on its embedding text
- dotenv is used to load environment variables from a
.env
file, such asAPI_KEY
for Google Generative AI. - The Google service account key file (
key.json
) is set toGOOGLE_APPLICATION_CREDENTIALS
.
we dont use vertex AI because of some payment issue
whatsapp-web.js
:- Manages the interaction with WhatsApp via the web interface.
Client
andLocalAuth
are used to create and manage the authentication session.- QR Code Login: Displays a QR code to log in to WhatsApp.
- Event Listeners:
on("qr")
: Generates and logs the QR code for user login.on("authenticated")
: Confirms successful authentication.on("ready")
: Indicates the WhatsApp client is ready to send/receive messages.on("message")
: Handles incoming messages and sends automated replies.
- LangChain: Provides a framework for working with language models and document retrieval.
- Google Generative AI:
- Chat model (
gemini-1.5-flash
) is used for generating human-like responses. - Embeddings model (
text-embedding-004
) is used to create vector representations of text data for retrieval tasks.
- Chat model (
- MemoryVectorStore:
- Stores preprocessed data (
synthetic_data.txt
) as embeddings. - Acts as a retriever for relevant information.
- Stores preprocessed data (
- Prompt Template:
- Specifies the system behavior, user interaction style, and task description.
- Includes context, such as responding politely in Indonesian and adhering to predefined constraints.
- Question Answering Chain:
- Processes questions and generates answers based on the AI model.
- Retrieval Augmented Generation (RAG) Chain:
- Combines document retrieval with the AI model to provide contextually accurate responses.
- The
handleChat
function processes incoming WhatsApp messages:- Integrates the prompt template with the user's question.
- Uses the RAG chain to retrieve relevant data.
- Generates a response using the AI model.
- Message Types:
!ping
: Replies with "pong" (test message).!echo <text>
: Echoes the input text back to the user.!q <question>
: Processes the question using the AI model and returns a generated response.- Other: Responds with a default message if no specific command is detected.
- The
handleError
function logs errors and ensures graceful handling of issues like authentication failures or disconnections.
- Puppeteer: Configured as the underlying browser engine for
whatsapp-web.js
, ensuring headless operation for automation.
- The
main
function initializes the WhatsApp client:- Authenticates using the local strategy.
- Sets up event listeners for logging in, receiving messages, and handling disconnections.
- Begins processing incoming messages once the client is ready.
-
WhatsApp Login:
- User logs in via QR code.
- Client becomes ready to handle messages.
-
AI Processing:
- User sends a command (
!q <question>
). - Question is processed using RAG and LangChain.
- AI generates a polite, contextually relevant response.
- User sends a command (
-
Response Delivery:
- Generated response is sent back to the user via WhatsApp.
-
Error Handling:
- Logs errors and ensures the bot restarts cleanly in case of disconnections.
- Automated customer service.
- AI-enhanced question answering.
- Integration of LangChain for advanced document retrieval.
- Real-time WhatsApp messaging with dynamic responses.
Prerequisites
- git
- pnpm
Clone and install
git clone https://github.com/vatoer/pu-chatbot.git
cd pu-chatbot
pnpm install
configure your .env
file
GOOGLE_API_KEY=${API_KEY}
API_KEY=${API_KEY}
Run chatbot
prepare your whatsapp
pnpm nodemon chat6.js
atau
pnpm start
it will show you a qr code, scan with your whatsapp
asking chatbot, chat to your whatsapp from other number in bahasa indonesia
!q siapa kamu
!q siapa stargan?
pnpm add whatsapp-web.js qrcode-terminal puppeteer dotenv fs nodemon @google/generative-ai @langchain/textsplitters langchain @langchain/google-genai @langchain/core
To install Git, follow these steps based on your operating system:
-
Download the Git Installer:
- Visit the official Git website: https://git-scm.com.
- Download the latest Git version for Windows.
-
Run the Installer:
- Open the downloaded
.exe
file. - Follow the installation steps. You can use the default settings unless you have specific preferences.
- Open the downloaded
-
Configure Git:
-
Open Git Bash or Command Prompt.
-
Set your name and email:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
-
-
Verify Installation:
-
Open Git Bash or Command Prompt and run:
git --version
-
You should see the installed Git version.
-
-
Using Homebrew (Recommended):
-
Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install Git using Homebrew:
brew install git
-
-
Alternative: Install via Xcode Command Line Tools:
-
Open Terminal and run:
xcode-select --install
-
Follow the on-screen prompts to install Git.
-
-
Verify Installation:
-
Open Terminal and run:
git --version
-
You should see the installed Git version.
-
-
Using Package Managers:
-
For Debian/Ubuntu:
sudo apt update sudo apt install git
-
For Fedora:
sudo dnf install git
-
For CentOS/RHEL:
sudo yum install git
-
For Arch Linux:
sudo pacman -S git
-
-
Verify Installation:
-
Open a terminal and run:
git --version
-
-
Configure Git User:
-
Run these commands to set your name and email globally:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
-
-
Optional: Set a Default Editor:
-
To set
vim
,nano
, or another editor for Git:git config --global core.editor "nano"
-
-
Check Configuration:
-
View your Git configuration:
git config --list
-
To install pnpm
, you can follow these steps:
If you already have Node.js and npm
installed, you can install pnpm
globally with the following command:
npm install -g pnpm
If you're using Node.js v16.13 or higher, Corepack
comes pre-installed, which manages package managers like pnpm
. You can enable Corepack
and then use it to install pnpm
:
corepack enable
corepack prepare pnpm@latest --activate
If you prefer to install it directly without npm
, use the following shell script:
curl -fsSL https://get.pnpm.io/install.sh | sh -
This script downloads and installs pnpm
.
After installation, verify that pnpm
is installed correctly by checking its version:
pnpm --version
If you're on macOS or Linux and use Homebrew, you can install pnpm
like this:
brew install pnpm
-
If you're using Windows, you can follow the first or second method, or use Scoop:
scoop install pnpm