File size: 6,553 Bytes
433146a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2d0476
433146a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91afd29
433146a
 
 
 
 
 
d2d0476
5110d54
433146a
 
 
 
 
 
 
 
 
b0540b3
433146a
 
 
 
 
 
 
b0540b3
433146a
b0540b3
433146a
a75702e
433146a
b0540b3
433146a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---

title: Langgraph  
emoji: πŸ“ˆ  
colorFrom: yellow  
colorTo: red  
sdk: streamlit  
sdk_version: 1.41.1  
app_file: app.py  
pinned: false  
license: apache-2.0  
short_description: Langgraph  

---

# πŸš€ LangGraph: Building Stateful AI Agents as Graphs  

**Official Links**  
- [GitHub Repository](https://github.com/langchain-ai/langgraph)  
- [Documentation](https://langchain-ai.github.io/langgraph/)  
- [LangChain Academy Course](https://github.com/gayashan4lk/langchain-academy-lang-graph-intro) (Free Intro)  
- [Prebuilt Agents & Templates](https://langchain-ai.github.io/langgraph/concepts/template_applications/)  

---

## πŸ“Œ Overview  
LangGraph is a Python library for creating **stateful, multi-step AI workflows** using Large Language Models (LLMs). It enables dynamic decision-making through graph-based architectures, allowing developers to design agents that loop, branch, and interact with tools/APIs. Inspired by Pregel and Apache Beam, it’s widely used by companies like LinkedIn, Uber, and Elastic.  

---

## πŸ”‘ Key Features  
1. **Graph-Based Workflows**  
   - Define workflows as **nodes** (tasks) and **edges** (connections).  
   - Supports **conditional edges** (e.g., "If tool call needed, route to tools node").  

2. **State Management**  
   - Persist conversation history, tool outputs, or custom data across interactions using checkpoints.  

3. **Multi-Actor Support**  
   - Build collaborative agents (e.g., one agent retrieves data, another generates responses).  

4. **Prebuilt Agents**  
   - Use templates like `create_react_agent` for ReAct-style agents or customize workflows.  

5. **LangGraph Platform** (Commercial)  
   - Deploy agents at scale with streaming, background runs, and monitoring.  

---

## πŸ› οΈ Getting Started  

### Installation  
```bash
pip install -U langgraph  # Core library 
pip install langchain_groq streamlit  # For Groq models and Streamlit UI
```

### Basic Example: ReAct Agent  
```python
from langgraph.prebuilt import create_react_agent
from langchain_groq import ChatGroq  # Changed import
import os

# Define a search tool (remains the same)
@tool
def search(query: str):
    if "san francisco" in query.lower():
        return "60Β°F, foggy"
    return "90Β°F, sunny"

# Initialize agent with Groq
model = ChatGroq(
    temperature=0,
    model_name="mixtral-8x7b-32768"  # Groq model name
)
checkpointer = MemorySaver()  # Persists state
app = create_react_agent(model, [search], checkpointer=checkpointer)

# Invoke with thread_id for state retention (same)
response = app.invoke(
    {"messages": [{"role": "user", "content": "Weather in SF?"}]},
    config={"configurable": {"thread_id": 42}}
)
print(response["messages"][-1].content)  # Outputs weather details
```

Key:  
1. Uses `langchain_groq.ChatGroq`  
2. Specifies Groq model name (`mixtral-8x7b-32768` or `llama2-70b-4096`)  
3. Requires `GROQ_API_KEY` environment variable  

Set your API key:  
```python
os.environ["GROQ_API_KEY"] = "your-key-here"
```

---

## πŸš€ Running the Streamlit App  

This project includes a Streamlit-based web interface for interacting with the Groq ReAct Agent. Follow the steps below to set up and run the app.  

### Prerequisites  
1. **Python 3.8+**: Ensure Python is installed on your system.  
2. **Groq API Key**: Obtain your API key from the [Groq Cloud Console](https://console.groq.com/).  

### Steps to Run  
1. Clone the repository:  
   ```bash
   git clone https://github.com/aitiwari/langgraph.git
   cd langgraph
   ```

2. Install the required dependencies:  
   ```bash
   pip install -r requirements.txt
   ```

3. Set your Groq API key as an environment variable:  
   ```bash
   export GROQ_API_KEY="your-api-key-here"
   ```

4. Start the Streamlit app:  
   ```bash
   streamlit run app.py
   ```

5. Open your browser and navigate to the URL provided in the terminal (usually `http://localhost:8501`).  

### Using the App  
- **Input Field**: Type your question or query in the chat input box at the bottom of the screen.  
- **Chat History**: The conversation history will be displayed above the input field.  
- **API Key**: If you didn't set the `GROQ_API_KEY` environment variable, you'll be prompted to enter it in the app.  

### Example Queries  
- "What's the weather in San Francisco?"  
- "Tell me about the weather in New York."  

### Stopping the App  
To stop the Streamlit app, press `Ctrl+C` in the terminal where the app is running.  

---

## 🌟 Use Cases  

### 1. Basic Chatbot  
**Prompt**: What is generative AI?  
**Reference**:  
![alt text](basic_chatbot.png)  

### 2. Chatbot with Tool  
**Prompt**: Latest news from the India vs Australia match?  
**Reference**:  
![alt text](chatbot_with_search_tool.png)  

### 3. Appointment Receptionist  
**Prompt 1**: Book an appointment for priti.  
**Prompt 2**: Yes or No.  
**Reference**:  
![alt text](appointment_receptionist.png)  
![alt text](state_graph_ar.png)

### 4. Customer Support  
**Reference**:  
![alt text](cs_flow.png)  
![alt text](customer_support.png)  

---

## πŸ“š Templates & Community  
Explore prebuilt templates for chatbots, retrieval agents, and more:  
```bash
langgraph new  # CLI to create apps from templates 
```  
| Template          | Description                          | Link                                   |  
|-------------------|--------------------------------------|----------------------------------------|  
| ReAct Agent       | Tool-calling agent with memory       | [GitHub](https://github.com/langchain-ai/langgraph/tree/main/examples) |  
| Retrieval Agent   | RAG with dynamic context fetching    | [Docs](https://langchain-ai.github.io/langgraph/concepts/template_applications/) |  

---

## πŸ“– References  
1. [Official Documentation](https://langchain-ai.github.io/langgraph/)  
2. [GitHub Examples](https://github.com/langchain-ai/langgraph/tree/main/examples)  
3. [Agentic RAG Guide](https://medium.com/@wendell_89912/building-an-agentic-rag-with-langgraph-a-step-by-step-guide-009c5f0cce0a)  
4. [Production Case Studies](https://blog.langchain.dev/top-5-langgraph-agents-in-production-2024/)  

---

For advanced use cases (multi-agent systems, human-in-the-loop), refer to the [LangGraph Platform](https://www.langchain.com/langgraph) or explore the [LangChain Academy](https://github.com/gayashan4lk/langchain-academy-lang-graph-intro).  

--- 

This version integrates the Streamlit app instructions seamlessly into the existing structure while maintaining the original tone and style.