--- 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 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.). ---