File size: 6,946 Bytes
3628833 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 6b470db cc85559 | 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | ---
title: Chatbot
emoji: π»
colorFrom: yellow
colorTo: green
sdk: gradio
sdk_version: 6.6.0
app_file: app.py
pinned: false
short_description: Chatbot
---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
# π€ Memory Chatbot
A multi-user, multi-thread AI chatbot with **short-term and long-term memory**, powered by LangGraph, LangChain, Groq (Qwen3-32B), and a Gradio UI. The chatbot can call a variety of tools and displays which tools were used directly in the chat interface.
---
## β¨ Features
| Feature | Description |
|---|---|
| π§ Short-term memory | Remembers conversation context within a thread using `InMemorySaver` |
| πΎ Long-term memory | Stores user preferences, profile, interests, and project info using `InMemoryStore` |
| π₯ Multi-user support | Each user has isolated memory and conversation threads |
| π§΅ Multi-thread support | Each user can have multiple independent chat sessions |
| π§ Tool use | 12 built-in tools (web search, weather, calculator, Python exec, and more) |
| π·οΈ Tool call badges | The UI shows which tools were called for each AI response |
| β‘ Fast inference | Powered by Groq's ultra-fast LLM API |
---
## ποΈ Project Structure
```
.
βββ memory_chatbot.py # LangGraph graph: memory nodes + agent chat node
βββ app.py # Gradio UI with multi-user / multi-thread support
βββ tool.py # All tool definitions (12 tools)
βββ .env # API keys (not committed)
βββ README.md
```
---
## π§© Architecture
```
User Message
β
βΌ
βββββββββββββββββββ
β memory_analyzer β β Detects if message contains info worth storing long-term
ββββββββββ¬βββββββββ
β
βΌ
ββββββββββββββββββββ
β memory_retrieval β β Searches long-term store and injects relevant memories
ββββββββββ¬ββββββββββ
β
βΌ
βββββββββββββββββ
β chat_node β β LangChain agent with 12 tools; returns answer + tool log
βββββββββββββββββ
β
βΌ
AI Response
+ Tool Call Badges
```
### Memory Types
- **Short-term (checkpointer):** Full conversation history per `thread_id`, managed automatically by LangGraph's `InMemorySaver`.
- **Long-term (store):** Key-value memories namespaced by `(user_id, category)`. Categories: `preferences`, `profile`, `interests`, `project`.
---
## π§ Tools
| Tool | Description |
|---|---|
| `tool_tavily` | Web search via Tavily API |
| `time_date` | Returns today's date |
| `calculator` | Evaluates math expressions |
| `python_exec` | Executes arbitrary Python code |
| `get_weather` | Current weather for any city |
| `wikipedia_search` | Wikipedia summary search |
| `scrape_website` | Extracts text from a URL |
| `read_file` | Reads a local file |
| `format_json` | Pretty-prints JSON |
| `generate_sql` | Converts natural language to SQL |
| `system_info` | Returns OS/platform info |
| `save_user_preference` | Saves a user preference to memory |
---
## π Setup
### 1. Clone & install dependencies
```bash
git clone <your-repo-url>
cd memory-chatbot
pip install -r requirements.txt
```
**Key dependencies:**
```
langgraph
langchain
langchain-groq
langchain-tavily
gradio
wikipedia
beautifulsoup4
requests
python-dotenv
```
### 2. Configure API keys
Create a `.env` file in the project root:
```env
GROQ_API_KEY=your_groq_api_key
TAVILY_API_KEY=your_tavily_api_key
```
Get your keys:
- Groq: https://console.groq.com
- Tavily: https://app.tavily.com
### 3. Run
```bash
python app.py
```
Then open http://localhost:7860 in your browser.
---
## π₯οΈ UI Guide
```
βββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β π€ Users β β
β ββββββββββββ β [Chat history appears here] β
β β user_abc β β β
β ββββββββββββ β User: What's the weather in Paris? β
β [β New User] β β
β β AI: It's 18Β°C and sunny in Paris. β
β π§΅ Threads β π§ get_weather β
β β thread_123 β β
β β thread_456 β β
β [β New Chat] β βββββββββββββββββββββββββββββββ β
β β β Type message and press Enter β β
βββββββββββββββββββ΄ββββ΄ββββββββββββββββββββββββββββββ΄βββββ
```
- **New User** β creates an isolated user with fresh memory and a new thread
- **New Chat** β starts a new thread for the current user (long-term memory is preserved)
- **Tool badges** β appear below each AI reply showing which tools were called
---
## π How Tool Display Works
When the agent calls tools, `chat_node` inspects the returned messages for `tool_calls` and `ToolMessage` entries and builds a `tool_log` list. The Gradio UI renders these as colored HTML badges inline in the chat (Gradio's `Chatbot` renders HTML by default).
Example badge output:
```
π§ get_weather π§ wikipedia_search
```
---
## π Configuration
| Parameter | Location | Default | Description |
|---|---|---|---|
| `model` | `memory_chatbot.py` | `qwen/qwen3-32b` | Groq model to use |
| `temperature` | `memory_chatbot.py` | `0` | LLM temperature |
| `max_results` | `tool.py` (Tavily) | `5` | Web search result count |
| `memory categories` | `memory_chatbot.py` | preferences, profile, interests, project | Long-term memory namespaces |
---
## β οΈ Limitations
- Memory is **in-process only** β all memory resets when the server restarts. For persistence, replace `InMemorySaver` and `InMemoryStore` with database-backed alternatives (e.g., `PostgresSaver`, `RedisStore`).
- `python_exec` tool executes arbitrary code with no sandboxing β use with caution in production.
- `scrape_website` does not handle JavaScript-rendered pages.
---
## π οΈ Extending
**Add a new tool:** Define it with `@tool` in `tool.py`, then add it to the `tools` list in `memory_chatbot.py`.
**Persist memory to a database:** Replace `InMemorySaver()` with a LangGraph-compatible checkpointer and `InMemoryStore()` with a persistent store.
**Change the LLM:** Swap `ChatGroq` for any LangChain-compatible chat model (OpenAI, Anthropic, etc.).
---
|