| --- |
| 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 |
|
|
| # Multi-User Memory Chatbot with LangChain, Tools, and Gradio |
|
|
| A conversational AI chatbot built with **LangChain**, **Gradio**, and multiple custom tools. |
| This project supports: |
|
|
| - **multi-user chat sessions** |
| - **multiple threads per user** |
| - **memory-aware conversations** |
| - **tool calling** |
| - optional **tool activity display in the UI** |
|
|
| It is useful for experimenting with AI agents that can search, calculate, scrape websites, fetch weather, read files, and more. |
|
|
| --- |
|
|
| ## Features |
|
|
| - ๐ค **Multiple users** |
| - ๐งต **Multiple chat threads per user** |
| - ๐ง **Conversation memory using `thread_id` and `user_id`** |
| - ๐ **Tool-enabled chatbot** |
| - ๐ **Web search using Tavily** |
| - ๐ **Wikipedia search** |
| - โ๏ธ **Weather lookup** |
| - ๐งฎ **Calculator** |
| - ๐ **Python code execution** |
| - ๐ **Read local files** |
| - ๐ **Scrape webpage text** |
| - ๐งพ **Pretty-print JSON** |
| - ๐พ **Save user preferences** |
| - ๐ฅ **System info tool** |
| - ๐จ **Gradio web interface** |
| - โ
Optional **tool activity panel** to show which tools were called |
|
|
| --- |
|
|
| ## Project Structure |
|
|
| A recommended project structure: |
|
|
| ```text |
| project/ |
| โ |
| โโโ tools.py # All tool definitions |
| โโโ memory_chatbot.py # Chatbot / LangChain agent backend |
| โโโ app.py # Gradio UI |
| โโโ .env # Environment variables |
| โโโ requirements.txt # Python dependencies |
| โโโ README.md |
| ``` |
|
|
| > **Important:** |
| > If your UI file imports `chatbot` like this: |
|
|
| ```python |
| from memory_chatbot import chatbot |
| ``` |
|
|
| then your UI file should **not** also be named `memory_chatbot.py`, otherwise you may get a **circular import error**. |
| Use a separate file name such as `app.py` for the Gradio interface. |
|
|
| --- |
|
|
| ## Tools Included |
|
|
| The project currently includes the following tools: |
|
|
| ### 1. `time_date` |
| Returns todayโs date in `yyyy-mm-dd` format. |
| |
| ### 2. `calculator` |
| Evaluates mathematical expressions. |
| |
| ### 3. `python_exec` |
| Executes Python code and returns local variables. |
|
|
| ### 4. `get_weather` |
| Fetches current weather for a city using `wttr.in`. |
| |
| ### 5. `wikipedia_search` |
| Searches Wikipedia and returns a short summary. |
|
|
| ### 6. `scrape_website` |
| Extracts text content from a webpage. |
| |
| ### 7. `read_file` |
| Reads the contents of a local file. |
|
|
| ### 8. `format_json` |
| Formats and prettifies a JSON string. |
| |
| ### 9. `generate_sql` |
| Converts natural language into a placeholder SQL query. |
|
|
| ### 10. `system_info` |
| Returns system/platform information. |
| |
| ### 11. `save_user_preference` |
| Stores user preference text (currently mocked). |
| |
| ### 12. `tool_tavily` |
| Performs web search using Tavily. |
|
|
| --- |
|
|
| ## Installation |
|
|
| ## 1. Clone the repository |
|
|
| ```bash |
| git clone https://github.com/your-username/your-repo-name.git |
| cd your-repo-name |
| ``` |
|
|
| ## 2. Create a virtual environment |
|
|
| ### Windows |
|
|
| ```bash |
| python -m venv venv |
| venv\Scripts\activate |
| ``` |
|
|
| ### macOS / Linux |
|
|
| ```bash |
| python3 -m venv venv |
| source venv/bin/activate |
| ``` |
|
|
| ## 3. Install dependencies |
|
|
| ```bash |
| pip install -r requirements.txt |
| ``` |
|
|
| If you do not already have a `requirements.txt`, you can use something like this: |
|
|
| ```txt |
| gradio |
| langchain |
| langchain-core |
| langchain-tavily |
| python-dotenv |
| requests |
| wikipedia |
| beautifulsoup4 |
| ``` |
|
|
| Depending on your chatbot backend, you may also need your model provider package, such as: |
|
|
| - `langchain-openai` |
| - `langchain-google-genai` |
| - `langchain-groq` |
| - or another LLM integration |
|
|
| --- |
|
|
| ## Environment Variables |
|
|
| Create a `.env` file in the root directory. |
|
|
| Example: |
|
|
| ```env |
| TAVILY_API_KEY=your_tavily_api_key |
| OPENAI_API_KEY=your_openai_api_key |
| ``` |
|
|
| If you are using another model provider, replace `OPENAI_API_KEY` with the correct key for your setup. |
|
|
| --- |
|
|
| ## Running the App |
|
|
| Start the Gradio interface with: |
|
|
| ```bash |
| python app.py |
| ``` |
|
|
| Then open the local Gradio URL shown in the terminal, usually: |
|
|
| ```text |
| http://127.0.0.1:7860 |
| ``` |
|
|
| --- |
|
|
| ## How It Works |
|
|
| ### User and Thread Management |
|
|
| Each conversation is identified using: |
|
|
| - `user_id` |
| - `thread_id` |
|
|
| This allows the chatbot to maintain separate memory for: |
|
|
| - different users |
| - different chat sessions for the same user |
|
|
| ### Chat Flow |
|
|
| When the user sends a message: |
|
|
| 1. the message is wrapped as a `HumanMessage` |
| 2. the chatbot is invoked with `thread_id` and `user_id` |
| 3. the agent may call one or more tools |
| 4. the final AI response is displayed in the chat UI |
| 5. the conversation is stored under that user/thread pair |
|
|
| --- |
|
|
| ## Gradio UI Features |
|
|
| The Gradio app includes: |
|
|
| - **User selector** |
| - **New user button** |
| - **Thread selector** |
| - **New chat button** |
| - **Chat area** |
| - **Message input box** |
|
|
| This makes it easy to simulate multiple users and sessions in one interface. |
|
|
| --- |
|
|
| ## Showing Tool Calls in the UI |
|
|
| Yes โ this project can be extended to show: |
|
|
| - which tool was called |
| - tool arguments |
| - tool output |
|
|
| For example, if the chatbot uses `get_weather("Delhi")`, the UI can display something like: |
|
|
| ```text |
| Calling tool: get_weather |
| Arguments: {"city": "Delhi"} |
| Output: Delhi: +34ยฐC, Sunny |
| ``` |
|
|
| This is useful for: |
|
|
| - debugging |
| - understanding agent behavior |
| - teaching/demo purposes |
|
|
| A good way to display this is by adding a `gr.Markdown()` or `gr.Textbox()` panel in the UI and parsing: |
|
|
| - `AIMessage.tool_calls` |
| - `ToolMessage.content` |
|
|
| --- |
|
|
| ## Example Use Cases |
|
|
| - Ask todayโs date |
| - Calculate expressions like `25 * 12` |
| - Execute small Python snippets |
| - Look up weather in a city |
| - Search Wikipedia topics |
| - Scrape a webpage summary |
| - Read a local file |
| - Format JSON data |
| - Save user preferences |
|
|
| --- |
|
|
| ## Example Prompts |
|
|
| Try asking: |
|
|
| ```text |
| What is today's date? |
| ``` |
|
|
| ```text |
| Calculate 125 * 48 |
| ``` |
|
|
| ```text |
| What is the weather in London? |
| ``` |
|
|
| ```text |
| Search Wikipedia for Artificial Intelligence |
| ``` |
|
|
| ```text |
| Read the contents of sample.txt |
| ``` |
|
|
| ```text |
| Format this JSON: {"name":"John","age":25} |
| ``` |
|
|
| --- |
|
|
| ## Security Warning |
|
|
| This project includes some tools that are **unsafe for public deployment** without restrictions. |
|
|
| ### Risky tools |
|
|
| - `calculator` uses `eval()` |
| - `python_exec` uses `exec()` |
| - `read_file` can access local files |
| - `scrape_website` can fetch arbitrary URLs |
|
|
| ### Why this matters |
|
|
| If exposed publicly, users may be able to: |
|
|
| - run arbitrary Python code |
| - read sensitive local files |
| - access internal resources |
| - misuse the server |
|
|
| ### Recommendation |
|
|
| Use this project: |
|
|
| - for **local development** |
| - for **learning** |
| - for **internal testing only** |
|
|
| Before deploying publicly, you should: |
|
|
| - remove `eval()` and `exec()` |
| - restrict file access |
| - validate URLs |
| - sandbox execution |
| - add authentication and rate limiting |
|
|
| Tiny chatbot, big chaos potential. Better to keep the chaos leashed. |
|
|
| --- |
|
|
| ## Known Notes |
|
|
| ### 1. Circular Import Risk |
|
|
| If your UI file is named `memory_chatbot.py` and also contains: |
|
|
| ```python |
| from memory_chatbot import chatbot |
| ``` |
|
|
| you will likely get an import issue. |
|
|
| ### Fix |
|
|
| Rename the Gradio UI file to something like: |
|
|
| ```text |
| app.py |
| ``` |
|
|
| and keep the chatbot backend in: |
|
|
| ```text |
| memory_chatbot.py |
| ``` |
|
|
| --- |
|
|
| ### 2. Tool Results May Differ |
|
|
| Some tools depend on external services: |
|
|
| - `wttr.in` |
| - `Wikipedia` |
| - `Tavily` |
| - websites being scraped |
|
|
| If those services are unavailable, the tool may fail or return unexpected results. |
|
|
| --- |
|
|
| ## Future Improvements |
|
|
| Possible enhancements for this project: |
|
|
| - real memory store integration |
| - persistent database for chat history |
| - streaming responses |
| - live tool-call updates in the UI |
| - better error handling |
| - authentication |
| - safer code execution sandbox |
| - SQL generation using LLM |
| - file upload support |
| - improved styling for the chat interface |
|
|
| --- |
|
|
| ## Sample `tools.py` |
|
|
| This file contains all your custom tools used by the chatbot agent. |
|
|
| It includes tools for: |
|
|
| - date/time |
| - calculator |
| - Python execution |
| - weather |
| - Wikipedia |
| - web scraping |
| - file reading |
| - JSON formatting |
| - SQL generation |
| - system information |
| - user preference saving |
| - Tavily search |
|
|
| --- |
|
|
| ## Sample Workflow |
|
|
| 1. Start app |
| 2. Select or create a user |
| 3. Create a new thread |
| 4. Ask a question |
| 5. Agent decides whether to use tools |
| 6. Tool result is returned |
| 7. Final AI answer is shown |
| 8. Conversation history is stored per user/thread |
|
|
| --- |
|
|
| ## Requirements Summary |
|
|
| Main libraries used: |
|
|
| - `gradio` |
| - `langchain` |
| - `langchain-core` |
| - `langchain-tavily` |
| - `requests` |
| - `wikipedia` |
| - `beautifulsoup4` |
| - `python-dotenv` |
|
|
| --- |
|
|
| ## License |
|
|
| This project is for educational and development use. |
| Add your preferred license here, for example: |
|
|
| ```text |
| MIT License |
| ``` |
|
|
| --- |
|
|
| ## Author |
|
|
| Built with LangChain, Gradio, and a healthy amount of agent curiosity. |
|
|
| If you want, you can customize this section with your name, GitHub profile, or portfolio link. |
|
|
| --- |
|
|
| ## Contributing |
|
|
| Contributions are welcome. |
|
|
| You can improve this project by: |
|
|
| - adding safer tool implementations |
| - improving UI design |
| - adding persistent memory |
| - improving tool logging |
| - integrating streaming |
| - adding tests |
|
|
| --- |
|
|
| ## Final Notes |
|
|
| This project is a great starting point if you want to learn: |
|
|
| - LangChain tools |
| - agent-based workflows |
| - memory-aware chat apps |
| - multi-thread chat management |
| - Gradio interfaces |
|
|
| If you are planning to deploy it publicly, please review the security concerns carefully before doing so. |