In this tutorial, you'll learn how to build a powerful chatbot capable of accessing real-time information from the internet. We'll leverage the OpenAI GPT-4 model via the LangChain OpenAI integration, combine it with the LangGraph framework, and integrate internet search capabilities using the DuckDuckGo search tool.
Prerequisites
Before you begin, ensure you have:
- Python 3.9 or newer installed on your system.
- OpenAI API Key: Obtain from OpenAI Platform.
Step 1: Project Setup
Create a new directory for your project and navigate into it:
mkdir langgraph-chatbot
cd langgraph-chatbot
Step 2: Create a Virtual Environment
It's recommended to use a virtual environment to manage your dependencies:
python3 -m venv venv
source venv/bin/activate # on Windows use venv\Scripts\activate
Step 3: Install Dependencies
Run the following command to install the required libraries:
pip install python-dotenv langgraph langchain langchain-openai langchain-community
Step 4: Configure Your API Key
Create a file named .env
inside your project directory and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here
Replace your_openai_api_key_here
with your actual OpenAI API key.
Step 5: Create Your Chatbot Script
Create a new Python file named langgraph_chatbot.py
and copy the following script into it:
#!/usr/bin/env python3
import os
from dotenv import load_dotenv
from typing import Annotated, TypedDict
# Load environment variables from .env file
load_dotenv()
# Use the OpenAI LLM from langchain_openai
from langchain_openai import ChatOpenAI
# Import DuckDuckGo search tool
from langchain_community.tools.ddg_search import DuckDuckGoSearchRun
# Import LangGraph components
from langgraph.graph import StateGraph, START
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
def main():
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("Please set your OPENAI_API_KEY in the .env file.")
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
# Initialize DuckDuckGo search tool
tool = DuckDuckGoSearchRun(max_results=2)
tools = [tool]
# Set up the LLM using GPT-4
llm = ChatOpenAI(model="gpt-4")
llm_with_tools = llm.bind_tools(tools)
# Define chatbot node
def chatbot(state: State):
return {"messages": [llm_with_tools.invoke(state["messages"])]}
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_node("tools", ToolNode(tools))
graph_builder.add_conditional_edges("chatbot", tools_condition)
graph_builder.add_edge("tools", "chatbot")
graph_builder.add_edge(START, "chatbot")
graph = graph_builder.compile()
# Function to handle input and responses
def stream_graph_updates(user_input: str):
for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}):
for value in event.values():
print("Assistant:", value["messages"][-1].content)
# REPL for user interaction
print("Welcome to the LangGraph chatbot! Type 'quit', 'exit', or 'q' to stop.")
while True:
try:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
stream_graph_updates(user_input)
except KeyboardInterrupt:
print("\nExiting.")
break
if __name__ == "__main__":
main()
Step 6: Run Your Chatbot
Execute your chatbot script:
python langgraph_chatbot.py
You should see:
Welcome to the LangGraph chatbot! Type 'quit', 'exit', or 'q' to stop.
User:
Try interacting with your chatbot. Ask it questions that require real-time internet search, for example:
User: What's the latest news about AI?
The chatbot will fetch live data using DuckDuckGo and respond intelligently using GPT-4.
Conclusion
You've successfully built a powerful LangGraph-based chatbot capable of fetching real-time data from the internet. This chatbot can now intelligently assist users by accessing updated information through the DuckDuckGo search engine and responding using OpenAI's GPT-4.
Feel free to expand its capabilities by integrating additional tools or enhancing the state management for more complex use-cases.