Integrate Generative AI into a WhatsApp chatbot with LangGraph AI agent
Add GenAI functionality to your chatbot to enhance the chatbot's intelligence. The chatbot uses GenAI to get additional, current information to generate responses.
This tutorial shows how to integrate Generative AI (GenAI) into a WhatsApp chatbot by using the LangGraph AI agent. Customize the example to meet your use case and requirements.
Products and channels [#products-channels]
Prerequisites [#prerequisites]
-
Infobip account. Select WhatsApp as the channel on the welcome screen.
-
Python 3.11 or higher. Make sure that your environment is set up.
-
WhatsApp is connected to the selected device (phone or PC through the web app).
-
Do one of the following.
-
An understanding of LLM functions and tool calling.
Implementation steps [#implementation-steps]
Configure the Infobip web interface
Get data from the Infobip web interface
- Log on to the Infobip web interface.
- Copy the API Key and API Base URL.
- Go to Channels and Numbers > Channels > WhatsApp > Senders tab.
- Copy the default Infobip test sender number 447860099299.
Configure the test sender
-
Select the three dots next to the Infobip test sender and select Edit configuration.
-
In the Inbound configuration > Keywords section, select the three dots next to the keyword that matches your username and select Edit.
-
Configure the forwarding action for inbound messages.
-
In the Forwarding action section, select Forward to HTTP.
-
Enter the URL of your endpoint. Example: Tunnel URL to localhost using pinggy.
ssh -p 443 -L4300:localhost:4300 -o StrictHostKeyChecking=no -o ServerAliveInterval=30 -R0:localhost:8000 a.pinggy.ios -
In the Renderer field, select MO_OTT_CONTACT.
-
-
If your Infobip account is in the trial period, send a keyword to the registered number. Example: Send the keyword WA123.
Set up the Python environment
Set up the environment
-
Create a new Python virtual environment.
-
Add the following code to
requirements.txt.1fastapi==0.115.92uvicorn==0.343langgraph==0.3.14python-dotenv==1.0.15langchain\_openai==0.3.76langchain\_community==0.3.187duckduckgo\_search==7.5.0 -
Use the following code to install the required libraries.
pip install -r requirements.txt
Set up secrets
Create a .env file to store API keys and sensitive configuration. Use either Azure credentials or OPENAI_API_KEY.
Configure the code
The code structure contains the following.
- Main: API endpoint.
- Message sender: Sends messages to WhatsApp through Infobip.
- Core agent: Manages GenAI processing.
Configure the API endpoint: main.py
Use FastAPI library to make the service available online.
-
Import the required modules and create a function to extract inbound messages.
python1from fastapi import FastAPI, Request2from send_message import message_sender3from core_agent import InboundMessage, message_handler4import uvicorn56app = FastAPI()78def extract_response_data(response: dict) -> InboundMessage:9 try:10 text_field = response['results'][0]['message']['text']11 contact_name = response['results'][0]['contact']['name']12 sender_number = response['results'][0]['from']13 constructed_message = InboundMessage(message_text=text_field, contact=contact_name, from_number=sender_number)14 return constructed_message15 except KeyError as e:16 return {"KeyError": str(e)} -
Configure the FastAPI endpoint to manage incoming messages.
python1@app.post("/incoming")2async def receive_event(request: Request):3 try:4 response = await request.json()5 inbound_data = extract_response_data(response)6 outbound_message = message_handler(inbound_data)7 return(message_sender(inbound_data.from_number, outbound_message))8 except Exception as e:9 return {"error": str(e)} -
Run the server.
python1if __name__ == "__main__":2 uvicorn.run(app, host="0.0.0.0", port=8000)
Configure the message sender: send_message.py
The message sender sends outbound messages by using the WhatsApp sender number and Infobip credentials.
Integrate LangGraph: core_agent.py
This section shows how to integrate LangGraph logic into your chatbot.
LangGraph is a framework for building GenAI agent solutions. It does the following.
- Controls the integration process.
- Helps get an overview of the process.
- Enables integration of advanced concepts such as human-in-the loop and tool calling.
In this tutorial, the GenAI agent uses DuckDuckGo Search tool to search the web to support its responses. The GenAI agent uses the GPT-4o model from OpenAI.
-
Manage imports for LangGraph and LangChain. These are used to call the Search tool function and Azure OpenAI. Instead of Azure OpenAI, you can use either direct calls to OpenAI or one of the chat model providers.
python1from dotenv import load_dotenv2import os3from langchain_core.messages import BaseMessage4from langchain_openai import AzureChatOpenAI5from typing import Annotated6from typing_extensions import TypedDict7from langgraph.checkpoint.memory import MemorySaver8from langgraph.graph import StateGraph, START9from langgraph.graph.message import add_messages10from langgraph.prebuilt import ToolNode, tools_condition11from langchain_community.tools import DuckDuckGoSearchResults12from pydantic import BaseModel1314load_dotenv() -
Define the message class with Pydantic validation.
python1class InboundMessage(BaseModel):2 message_text: str3 contact: str4 from_number: str -
Define the state of the graph.
python1class State(TypedDict):2 messages: Annotated[list, add_messages] -
Define the LLM and memory store for the conversation and tool bindings. Instead of DuckDuckGoSearchResults, you can write custom functions.
python1llm = AzureChatOpenAI(azure_deployment=os.getenv("DEPLOYMENT_NAME"), streaming=False,2 max_tokens=700)34memory = MemorySaver()56search_tool = DuckDuckGoSearchResults(output_format="json")7tools = [search_tool]8llm_with_tools = llm.bind_tools(tools)910def chatbot(state: State):11 return {"messages": [llm_with_tools.invoke(state["messages"])]} -
Define the function to build the graph and manage messages.
The following image shows the graph that is built by the Message handler function.
The message handler function listens for events in the graph stream and looks for the messages. It builds a graph and calls it when end users send messages to the chatbot. This triggers a request to a GenAI model that generates the output.
The system prompt "imitate C3PO" shows the impact of system prompts. Modify this part according to your use case by using prompt engineering techniques.
Launch
Launch main.py and start a conversation with your sender over WhatsApp. If you develop the system locally, start a tunnel for localhost access.
The following example shows a Pinggy tunnel that receives messages from the Infobip platform and sends them to the LangGraph AI agent.
Using GPT-4o enhances the chatbot's intelligence. Also, the ability to search the web makes sure that the chatbot has additional, current information.
The example in the following image shows that the GenAI chatbot knows about the latest Infobip news. This shows its ability to search the web to support its responses.