# LangGraph Basic Chatbot A basic chatbot built with LangGraph following the official tutorial. This chatbot uses OpenAI's GPT-4 model to provide conversational AI capabilities. ## Features - 🤖 Basic conversational AI using LangGraph - 🔄 State management with message history - 🚀 Built with uv for fast dependency management - 🔐 Secure API key handling ## Prerequisites - Python 3.8 or higher - [uv](https://docs.astral.sh/uv/) package manager - OpenAI API key ## Setup ### 1. Clone and Navigate to Project ```bash cd /Users/ghsioux/tmp/langgraph-pard0x ``` ### 2. Install Dependencies Dependencies are already installed with uv. The virtual environment is automatically managed. ### 3. Set up OpenAI API Key You have several options: #### Option A: Use the setup script ```bash uv run setup_env.py ``` #### Option B: Set environment variable manually ```bash export OPENAI_API_KEY='your-api-key-here' ``` #### Option C: Create a .env file Create a `.env` file in the project root: ``` OPENAI_API_KEY=your-api-key-here ``` ### 4. Run the Chatbot ```bash uv run main.py ``` ## Usage 1. Start the chatbot with `uv run main.py` 2. Type your messages and press Enter 3. The bot will respond using GPT-4 4. Type `quit`, `exit`, or `q` to end the conversation ### Example Conversation ``` 🤖 LangGraph Basic Chatbot Type 'quit', 'exit', or 'q' to exit the chat. ---------------------------------------- ✅ Chatbot initialized successfully! User: Hello! What is LangGraph? Assistant: LangGraph is a library designed to help build stateful multi-agent applications using language models... User: quit 👋 Goodbye! ``` ## Project Structure ``` langgraph-pard0x/ ├── main.py # Main chatbot implementation ├── setup_env.py # Environment setup helper ├── pyproject.toml # Project configuration and dependencies ├── README.md # This file └── .venv/ # Virtual environment (auto-managed by uv) ``` ## Dependencies - **langgraph**: Graph-based workflow library for LLM applications - **langchain**: Framework for developing applications with LLMs - **langchain-openai**: OpenAI integration for LangChain - **langsmith**: Observability and debugging for LangChain applications ## How It Works The chatbot uses LangGraph's `StateGraph` to manage conversation state: 1. **State Definition**: Uses a `TypedDict` with `messages` field that appends new messages 2. **Graph Creation**: Creates a state graph with a single "chatbot" node 3. **LLM Integration**: Uses `init_chat_model` to initialize OpenAI's GPT-4 4. **Message Processing**: The chatbot node processes incoming messages and returns responses 5. **Streaming**: Responses are streamed back to the user in real-time ## Next Steps This is the foundation for more advanced LangGraph features: - Add web search tools - Implement memory persistence - Add human-in-the-loop capabilities - Create multi-agent workflows Check out the [LangGraph tutorials](https://langchain-ai.github.io/langgraph/tutorials/) for more advanced features! ## Troubleshooting ### "Please set your OPENAI_API_KEY environment variable" - Make sure you've set your OpenAI API key using one of the methods above - Verify your API key is valid and active ### "Error initializing chatbot" - Check your internet connection - Verify your OpenAI API key has sufficient credits - Make sure all dependencies are installed correctly ### Import Errors - Run `uv sync` to ensure all dependencies are properly installed - Check that you're using the virtual environment created by uv