yashAI007 commited on
Commit
6b470db
ยท
1 Parent(s): 3628833

update README.md with claud

Browse files
Files changed (2) hide show
  1. README.md +117 -418
  2. README1.md +509 -0
README.md CHANGED
@@ -12,498 +12,197 @@ short_description: Chatbot
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
 
15
- # Multi-User Memory Chatbot with LangChain, Tools, and Gradio
16
 
17
- A conversational AI chatbot built with **LangChain**, **Gradio**, and multiple custom tools.
18
- This project supports:
19
-
20
- - **multi-user chat sessions**
21
- - **multiple threads per user**
22
- - **memory-aware conversations**
23
- - **tool calling**
24
- - optional **tool activity display in the UI**
25
-
26
- It is useful for experimenting with AI agents that can search, calculate, scrape websites, fetch weather, read files, and more.
27
 
28
  ---
29
 
30
- ## Features
31
-
32
- - ๐Ÿ‘ค **Multiple users**
33
- - ๐Ÿงต **Multiple chat threads per user**
34
- - ๐Ÿง  **Conversation memory using `thread_id` and `user_id`**
35
- - ๐Ÿ›  **Tool-enabled chatbot**
36
- - ๐ŸŒ **Web search using Tavily**
37
- - ๐Ÿ“š **Wikipedia search**
38
- - โ˜๏ธ **Weather lookup**
39
- - ๐Ÿงฎ **Calculator**
40
- - ๐Ÿ **Python code execution**
41
- - ๐Ÿ“„ **Read local files**
42
- - ๐Ÿ”Ž **Scrape webpage text**
43
- - ๐Ÿงพ **Pretty-print JSON**
44
- - ๐Ÿ’พ **Save user preferences**
45
- - ๐Ÿ–ฅ **System info tool**
46
- - ๐ŸŽจ **Gradio web interface**
47
- - โœ… Optional **tool activity panel** to show which tools were called
48
 
49
- ---
 
 
 
 
 
 
 
 
50
 
51
- ## Project Structure
52
 
53
- A recommended project structure:
54
 
55
- ```text
56
- project/
57
- โ”‚
58
- โ”œโ”€โ”€ tools.py # All tool definitions
59
- โ”œโ”€โ”€ memory_chatbot.py # Chatbot / LangChain agent backend
60
- โ”œโ”€โ”€ app.py # Gradio UI
61
- โ”œโ”€โ”€ .env # Environment variables
62
- โ”œโ”€โ”€ requirements.txt # Python dependencies
63
- โ””โ”€โ”€ README.md
64
  ```
65
-
66
- > **Important:**
67
- > If your UI file imports `chatbot` like this:
68
-
69
- ```python
70
- from memory_chatbot import chatbot
71
  ```
72
 
73
- then your UI file should **not** also be named `memory_chatbot.py`, otherwise you may get a **circular import error**.
74
- Use a separate file name such as `app.py` for the Gradio interface.
75
-
76
  ---
77
 
78
- ## Tools Included
79
-
80
- The project currently includes the following tools:
81
-
82
- ### 1. `time_date`
83
- Returns todayโ€™s date in `yyyy-mm-dd` format.
84
-
85
- ### 2. `calculator`
86
- Evaluates mathematical expressions.
87
-
88
- ### 3. `python_exec`
89
- Executes Python code and returns local variables.
90
-
91
- ### 4. `get_weather`
92
- Fetches current weather for a city using `wttr.in`.
93
-
94
- ### 5. `wikipedia_search`
95
- Searches Wikipedia and returns a short summary.
96
-
97
- ### 6. `scrape_website`
98
- Extracts text content from a webpage.
99
-
100
- ### 7. `read_file`
101
- Reads the contents of a local file.
102
-
103
- ### 8. `format_json`
104
- Formats and prettifies a JSON string.
105
-
106
- ### 9. `generate_sql`
107
- Converts natural language into a placeholder SQL query.
108
 
109
- ### 10. `system_info`
110
- Returns system/platform information.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
- ### 11. `save_user_preference`
113
- Stores user preference text (currently mocked).
114
 
115
- ### 12. `tool_tavily`
116
- Performs web search using Tavily.
117
 
118
  ---
119
 
120
- ## Installation
121
-
122
- ## 1. Clone the repository
123
 
124
- ```bash
125
- git clone https://github.com/your-username/your-repo-name.git
126
- cd your-repo-name
127
- ```
128
-
129
- ## 2. Create a virtual environment
130
-
131
- ### Windows
132
-
133
- ```bash
134
- python -m venv venv
135
- venv\Scripts\activate
136
- ```
 
137
 
138
- ### macOS / Linux
139
 
140
- ```bash
141
- python3 -m venv venv
142
- source venv/bin/activate
143
- ```
144
 
145
- ## 3. Install dependencies
146
 
147
  ```bash
 
 
148
  pip install -r requirements.txt
149
  ```
150
 
151
- If you do not already have a `requirements.txt`, you can use something like this:
152
 
153
- ```txt
154
- gradio
155
  langchain
156
- langchain-core
157
  langchain-tavily
158
- python-dotenv
159
- requests
160
  wikipedia
161
  beautifulsoup4
 
 
162
  ```
163
 
164
- Depending on your chatbot backend, you may also need your model provider package, such as:
165
-
166
- - `langchain-openai`
167
- - `langchain-google-genai`
168
- - `langchain-groq`
169
- - or another LLM integration
170
-
171
- ---
172
-
173
- ## Environment Variables
174
-
175
- Create a `.env` file in the root directory.
176
 
177
- Example:
178
 
179
  ```env
 
180
  TAVILY_API_KEY=your_tavily_api_key
181
- OPENAI_API_KEY=your_openai_api_key
182
  ```
183
 
184
- If you are using another model provider, replace `OPENAI_API_KEY` with the correct key for your setup.
185
-
186
- ---
187
-
188
- ## Running the App
189
 
190
- Start the Gradio interface with:
191
 
192
  ```bash
193
  python app.py
194
  ```
195
 
196
- Then open the local Gradio URL shown in the terminal, usually:
197
-
198
- ```text
199
- http://127.0.0.1:7860
200
- ```
201
 
202
  ---
203
 
204
- ## How It Works
205
-
206
- ### User and Thread Management
207
-
208
- Each conversation is identified using:
209
-
210
- - `user_id`
211
- - `thread_id`
212
-
213
- This allows the chatbot to maintain separate memory for:
214
-
215
- - different users
216
- - different chat sessions for the same user
217
-
218
- ### Chat Flow
219
-
220
- When the user sends a message:
221
 
222
- 1. the message is wrapped as a `HumanMessage`
223
- 2. the chatbot is invoked with `thread_id` and `user_id`
224
- 3. the agent may call one or more tools
225
- 4. the final AI response is displayed in the chat UI
226
- 5. the conversation is stored under that user/thread pair
227
-
228
- ---
229
-
230
- ## Gradio UI Features
231
-
232
- The Gradio app includes:
233
-
234
- - **User selector**
235
- - **New user button**
236
- - **Thread selector**
237
- - **New chat button**
238
- - **Chat area**
239
- - **Message input box**
240
-
241
- This makes it easy to simulate multiple users and sessions in one interface.
242
-
243
- ---
244
-
245
- ## Showing Tool Calls in the UI
246
-
247
- Yes โ€” this project can be extended to show:
248
-
249
- - which tool was called
250
- - tool arguments
251
- - tool output
252
-
253
- For example, if the chatbot uses `get_weather("Delhi")`, the UI can display something like:
254
-
255
- ```text
256
- Calling tool: get_weather
257
- Arguments: {"city": "Delhi"}
258
- Output: Delhi: +34ยฐC, Sunny
259
  ```
260
-
261
- This is useful for:
262
-
263
- - debugging
264
- - understanding agent behavior
265
- - teaching/demo purposes
266
-
267
- A good way to display this is by adding a `gr.Markdown()` or `gr.Textbox()` panel in the UI and parsing:
268
-
269
- - `AIMessage.tool_calls`
270
- - `ToolMessage.content`
271
-
272
- ---
273
-
274
- ## Example Use Cases
275
-
276
- - Ask todayโ€™s date
277
- - Calculate expressions like `25 * 12`
278
- - Execute small Python snippets
279
- - Look up weather in a city
280
- - Search Wikipedia topics
281
- - Scrape a webpage summary
282
- - Read a local file
283
- - Format JSON data
284
- - Save user preferences
285
-
286
- ---
287
-
288
- ## Example Prompts
289
-
290
- Try asking:
291
-
292
- ```text
293
- What is today's date?
294
  ```
295
 
296
- ```text
297
- Calculate 125 * 48
298
- ```
299
-
300
- ```text
301
- What is the weather in London?
302
- ```
303
-
304
- ```text
305
- Search Wikipedia for Artificial Intelligence
306
- ```
307
-
308
- ```text
309
- Read the contents of sample.txt
310
- ```
311
-
312
- ```text
313
- Format this JSON: {"name":"John","age":25}
314
- ```
315
 
316
  ---
317
 
318
- ## Security Warning
319
-
320
- This project includes some tools that are **unsafe for public deployment** without restrictions.
321
-
322
- ### Risky tools
323
 
324
- - `calculator` uses `eval()`
325
- - `python_exec` uses `exec()`
326
- - `read_file` can access local files
327
- - `scrape_website` can fetch arbitrary URLs
328
 
329
- ### Why this matters
330
-
331
- If exposed publicly, users may be able to:
332
-
333
- - run arbitrary Python code
334
- - read sensitive local files
335
- - access internal resources
336
- - misuse the server
337
-
338
- ### Recommendation
339
-
340
- Use this project:
341
-
342
- - for **local development**
343
- - for **learning**
344
- - for **internal testing only**
345
-
346
- Before deploying publicly, you should:
347
-
348
- - remove `eval()` and `exec()`
349
- - restrict file access
350
- - validate URLs
351
- - sandbox execution
352
- - add authentication and rate limiting
353
-
354
- Tiny chatbot, big chaos potential. Better to keep the chaos leashed.
355
-
356
- ---
357
-
358
- ## Known Notes
359
-
360
- ### 1. Circular Import Risk
361
-
362
- If your UI file is named `memory_chatbot.py` and also contains:
363
-
364
- ```python
365
- from memory_chatbot import chatbot
366
  ```
367
-
368
- you will likely get an import issue.
369
-
370
- ### Fix
371
-
372
- Rename the Gradio UI file to something like:
373
-
374
- ```text
375
- app.py
376
  ```
377
 
378
- and keep the chatbot backend in:
379
-
380
- ```text
381
- memory_chatbot.py
382
- ```
383
-
384
- ---
385
-
386
- ### 2. Tool Results May Differ
387
-
388
- Some tools depend on external services:
389
-
390
- - `wttr.in`
391
- - `Wikipedia`
392
- - `Tavily`
393
- - websites being scraped
394
-
395
- If those services are unavailable, the tool may fail or return unexpected results.
396
-
397
- ---
398
-
399
- ## Future Improvements
400
-
401
- Possible enhancements for this project:
402
-
403
- - real memory store integration
404
- - persistent database for chat history
405
- - streaming responses
406
- - live tool-call updates in the UI
407
- - better error handling
408
- - authentication
409
- - safer code execution sandbox
410
- - SQL generation using LLM
411
- - file upload support
412
- - improved styling for the chat interface
413
-
414
- ---
415
-
416
- ## Sample `tools.py`
417
-
418
- This file contains all your custom tools used by the chatbot agent.
419
-
420
- It includes tools for:
421
-
422
- - date/time
423
- - calculator
424
- - Python execution
425
- - weather
426
- - Wikipedia
427
- - web scraping
428
- - file reading
429
- - JSON formatting
430
- - SQL generation
431
- - system information
432
- - user preference saving
433
- - Tavily search
434
-
435
- ---
436
-
437
- ## Sample Workflow
438
-
439
- 1. Start app
440
- 2. Select or create a user
441
- 3. Create a new thread
442
- 4. Ask a question
443
- 5. Agent decides whether to use tools
444
- 6. Tool result is returned
445
- 7. Final AI answer is shown
446
- 8. Conversation history is stored per user/thread
447
-
448
- ---
449
-
450
- ## Requirements Summary
451
-
452
- Main libraries used:
453
-
454
- - `gradio`
455
- - `langchain`
456
- - `langchain-core`
457
- - `langchain-tavily`
458
- - `requests`
459
- - `wikipedia`
460
- - `beautifulsoup4`
461
- - `python-dotenv`
462
-
463
  ---
464
 
465
- ## License
466
 
467
- This project is for educational and development use.
468
- Add your preferred license here, for example:
469
-
470
- ```text
471
- MIT License
472
- ```
473
 
474
  ---
475
 
476
- ## Author
477
-
478
- Built with LangChain, Gradio, and a healthy amount of agent curiosity.
479
 
480
- If you want, you can customize this section with your name, GitHub profile, or portfolio link.
 
 
481
 
482
  ---
483
 
484
- ## Contributing
485
 
486
- Contributions are welcome.
487
 
488
- You can improve this project by:
489
 
490
- - adding safer tool implementations
491
- - improving UI design
492
- - adding persistent memory
493
- - improving tool logging
494
- - integrating streaming
495
- - adding tests
496
 
497
  ---
498
-
499
- ## Final Notes
500
-
501
- This project is a great starting point if you want to learn:
502
-
503
- - LangChain tools
504
- - agent-based workflows
505
- - memory-aware chat apps
506
- - multi-thread chat management
507
- - Gradio interfaces
508
-
509
- If you are planning to deploy it publicly, please review the security concerns carefully before doing so.
 
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
 
15
+ # ๐Ÿค– Memory Chatbot
16
 
17
+ 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.
 
 
 
 
 
 
 
 
 
18
 
19
  ---
20
 
21
+ ## โœจ Features
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ | Feature | Description |
24
+ |---|---|
25
+ | ๐Ÿง  Short-term memory | Remembers conversation context within a thread using `InMemorySaver` |
26
+ | ๐Ÿ’พ Long-term memory | Stores user preferences, profile, interests, and project info using `InMemoryStore` |
27
+ | ๐Ÿ‘ฅ Multi-user support | Each user has isolated memory and conversation threads |
28
+ | ๐Ÿงต Multi-thread support | Each user can have multiple independent chat sessions |
29
+ | ๐Ÿ”ง Tool use | 12 built-in tools (web search, weather, calculator, Python exec, and more) |
30
+ | ๐Ÿท๏ธ Tool call badges | The UI shows which tools were called for each AI response |
31
+ | โšก Fast inference | Powered by Groq's ultra-fast LLM API |
32
 
33
+ ---
34
 
35
+ ## ๐Ÿ—‚๏ธ Project Structure
36
 
 
 
 
 
 
 
 
 
 
37
  ```
38
+ .
39
+ โ”œโ”€โ”€ memory_chatbot.py # LangGraph graph: memory nodes + agent chat node
40
+ โ”œโ”€โ”€ app.py # Gradio UI with multi-user / multi-thread support
41
+ โ”œโ”€โ”€ tool.py # All tool definitions (12 tools)
42
+ โ”œโ”€โ”€ .env # API keys (not committed)
43
+ โ””โ”€โ”€ README.md
44
  ```
45
 
 
 
 
46
  ---
47
 
48
+ ## ๐Ÿงฉ Architecture
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ ```
51
+ User Message
52
+ โ”‚
53
+ โ–ผ
54
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
55
+ โ”‚ memory_analyzer โ”‚ โ† Detects if message contains info worth storing long-term
56
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
57
+ โ”‚
58
+ โ–ผ
59
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
60
+ โ”‚ memory_retrieval โ”‚ โ† Searches long-term store and injects relevant memories
61
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
62
+ โ”‚
63
+ โ–ผ
64
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
65
+ โ”‚ chat_node โ”‚ โ† LangChain agent with 12 tools; returns answer + tool log
66
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
67
+ โ”‚
68
+ โ–ผ
69
+ AI Response
70
+ + Tool Call Badges
71
+ ```
72
 
73
+ ### Memory Types
 
74
 
75
+ - **Short-term (checkpointer):** Full conversation history per `thread_id`, managed automatically by LangGraph's `InMemorySaver`.
76
+ - **Long-term (store):** Key-value memories namespaced by `(user_id, category)`. Categories: `preferences`, `profile`, `interests`, `project`.
77
 
78
  ---
79
 
80
+ ## ๐Ÿ”ง Tools
 
 
81
 
82
+ | Tool | Description |
83
+ |---|---|
84
+ | `tool_tavily` | Web search via Tavily API |
85
+ | `time_date` | Returns today's date |
86
+ | `calculator` | Evaluates math expressions |
87
+ | `python_exec` | Executes arbitrary Python code |
88
+ | `get_weather` | Current weather for any city |
89
+ | `wikipedia_search` | Wikipedia summary search |
90
+ | `scrape_website` | Extracts text from a URL |
91
+ | `read_file` | Reads a local file |
92
+ | `format_json` | Pretty-prints JSON |
93
+ | `generate_sql` | Converts natural language to SQL |
94
+ | `system_info` | Returns OS/platform info |
95
+ | `save_user_preference` | Saves a user preference to memory |
96
 
97
+ ---
98
 
99
+ ## ๐Ÿš€ Setup
 
 
 
100
 
101
+ ### 1. Clone & install dependencies
102
 
103
  ```bash
104
+ git clone <your-repo-url>
105
+ cd memory-chatbot
106
  pip install -r requirements.txt
107
  ```
108
 
109
+ **Key dependencies:**
110
 
111
+ ```
112
+ langgraph
113
  langchain
114
+ langchain-groq
115
  langchain-tavily
116
+ gradio
 
117
  wikipedia
118
  beautifulsoup4
119
+ requests
120
+ python-dotenv
121
  ```
122
 
123
+ ### 2. Configure API keys
 
 
 
 
 
 
 
 
 
 
 
124
 
125
+ Create a `.env` file in the project root:
126
 
127
  ```env
128
+ GROQ_API_KEY=your_groq_api_key
129
  TAVILY_API_KEY=your_tavily_api_key
 
130
  ```
131
 
132
+ Get your keys:
133
+ - Groq: https://console.groq.com
134
+ - Tavily: https://app.tavily.com
 
 
135
 
136
+ ### 3. Run
137
 
138
  ```bash
139
  python app.py
140
  ```
141
 
142
+ Then open http://localhost:7860 in your browser.
 
 
 
 
143
 
144
  ---
145
 
146
+ ## ๐Ÿ–ฅ๏ธ UI Guide
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  ```
149
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
150
+ โ”‚ ๐Ÿ‘ค Users โ”‚ โ”‚
151
+ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ [Chat history appears here] โ”‚
152
+ โ”‚ โ”‚ user_abc โ”‚ โ”‚ โ”‚
153
+ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ User: What's the weather in Paris? โ”‚
154
+ โ”‚ [โž• New User] โ”‚ โ”‚
155
+ โ”‚ โ”‚ AI: It's 18ยฐC and sunny in Paris. โ”‚
156
+ โ”‚ ๐Ÿงต Threads โ”‚ ๐Ÿ”ง get_weather โ”‚
157
+ โ”‚ โ—‹ thread_123 โ”‚ โ”‚
158
+ โ”‚ โ— thread_456 โ”‚ โ”‚
159
+ โ”‚ [โž• New Chat] โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
160
+ โ”‚ โ”‚ โ”‚ Type message and press Enter โ”‚ โ”‚
161
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”˜
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  ```
163
 
164
+ - **New User** โ€” creates an isolated user with fresh memory and a new thread
165
+ - **New Chat** โ€” starts a new thread for the current user (long-term memory is preserved)
166
+ - **Tool badges** โ€” appear below each AI reply showing which tools were called
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  ---
169
 
170
+ ## ๐Ÿ” How Tool Display Works
 
 
 
 
171
 
172
+ 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).
 
 
 
173
 
174
+ Example badge output:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  ```
176
+ ๐Ÿ”ง get_weather ๐Ÿ”ง wikipedia_search
 
 
 
 
 
 
 
 
177
  ```
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  ---
180
 
181
+ ## ๐Ÿ“ Configuration
182
 
183
+ | Parameter | Location | Default | Description |
184
+ |---|---|---|---|
185
+ | `model` | `memory_chatbot.py` | `qwen/qwen3-32b` | Groq model to use |
186
+ | `temperature` | `memory_chatbot.py` | `0` | LLM temperature |
187
+ | `max_results` | `tool.py` (Tavily) | `5` | Web search result count |
188
+ | `memory categories` | `memory_chatbot.py` | preferences, profile, interests, project | Long-term memory namespaces |
189
 
190
  ---
191
 
192
+ ## โš ๏ธ Limitations
 
 
193
 
194
+ - 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`).
195
+ - `python_exec` tool executes arbitrary code with no sandboxing โ€” use with caution in production.
196
+ - `scrape_website` does not handle JavaScript-rendered pages.
197
 
198
  ---
199
 
200
+ ## ๐Ÿ› ๏ธ Extending
201
 
202
+ **Add a new tool:** Define it with `@tool` in `tool.py`, then add it to the `tools` list in `memory_chatbot.py`.
203
 
204
+ **Persist memory to a database:** Replace `InMemorySaver()` with a LangGraph-compatible checkpointer and `InMemoryStore()` with a persistent store.
205
 
206
+ **Change the LLM:** Swap `ChatGroq` for any LangChain-compatible chat model (OpenAI, Anthropic, etc.).
 
 
 
 
 
207
 
208
  ---
 
 
 
 
 
 
 
 
 
 
 
 
README1.md ADDED
@@ -0,0 +1,509 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chatbot
3
+ emoji: ๐Ÿ’ป
4
+ colorFrom: yellow
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 6.6.0
8
+ app_file: app.py
9
+ pinned: false
10
+ short_description: Chatbot
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ # Multi-User Memory Chatbot with LangChain, Tools, and Gradio
16
+
17
+ A conversational AI chatbot built with **LangChain**, **Gradio**, and multiple custom tools.
18
+ This project supports:
19
+
20
+ - **multi-user chat sessions**
21
+ - **multiple threads per user**
22
+ - **memory-aware conversations**
23
+ - **tool calling**
24
+ - optional **tool activity display in the UI**
25
+
26
+ It is useful for experimenting with AI agents that can search, calculate, scrape websites, fetch weather, read files, and more.
27
+
28
+ ---
29
+
30
+ ## Features
31
+
32
+ - ๐Ÿ‘ค **Multiple users**
33
+ - ๐Ÿงต **Multiple chat threads per user**
34
+ - ๐Ÿง  **Conversation memory using `thread_id` and `user_id`**
35
+ - ๐Ÿ›  **Tool-enabled chatbot**
36
+ - ๐ŸŒ **Web search using Tavily**
37
+ - ๐Ÿ“š **Wikipedia search**
38
+ - โ˜๏ธ **Weather lookup**
39
+ - ๐Ÿงฎ **Calculator**
40
+ - ๐Ÿ **Python code execution**
41
+ - ๐Ÿ“„ **Read local files**
42
+ - ๐Ÿ”Ž **Scrape webpage text**
43
+ - ๐Ÿงพ **Pretty-print JSON**
44
+ - ๐Ÿ’พ **Save user preferences**
45
+ - ๐Ÿ–ฅ **System info tool**
46
+ - ๐ŸŽจ **Gradio web interface**
47
+ - โœ… Optional **tool activity panel** to show which tools were called
48
+
49
+ ---
50
+
51
+ ## Project Structure
52
+
53
+ A recommended project structure:
54
+
55
+ ```text
56
+ project/
57
+ โ”‚
58
+ โ”œโ”€โ”€ tools.py # All tool definitions
59
+ โ”œโ”€โ”€ memory_chatbot.py # Chatbot / LangChain agent backend
60
+ โ”œโ”€โ”€ app.py # Gradio UI
61
+ โ”œโ”€โ”€ .env # Environment variables
62
+ โ”œโ”€โ”€ requirements.txt # Python dependencies
63
+ โ””โ”€โ”€ README.md
64
+ ```
65
+
66
+ > **Important:**
67
+ > If your UI file imports `chatbot` like this:
68
+
69
+ ```python
70
+ from memory_chatbot import chatbot
71
+ ```
72
+
73
+ then your UI file should **not** also be named `memory_chatbot.py`, otherwise you may get a **circular import error**.
74
+ Use a separate file name such as `app.py` for the Gradio interface.
75
+
76
+ ---
77
+
78
+ ## Tools Included
79
+
80
+ The project currently includes the following tools:
81
+
82
+ ### 1. `time_date`
83
+ Returns todayโ€™s date in `yyyy-mm-dd` format.
84
+
85
+ ### 2. `calculator`
86
+ Evaluates mathematical expressions.
87
+
88
+ ### 3. `python_exec`
89
+ Executes Python code and returns local variables.
90
+
91
+ ### 4. `get_weather`
92
+ Fetches current weather for a city using `wttr.in`.
93
+
94
+ ### 5. `wikipedia_search`
95
+ Searches Wikipedia and returns a short summary.
96
+
97
+ ### 6. `scrape_website`
98
+ Extracts text content from a webpage.
99
+
100
+ ### 7. `read_file`
101
+ Reads the contents of a local file.
102
+
103
+ ### 8. `format_json`
104
+ Formats and prettifies a JSON string.
105
+
106
+ ### 9. `generate_sql`
107
+ Converts natural language into a placeholder SQL query.
108
+
109
+ ### 10. `system_info`
110
+ Returns system/platform information.
111
+
112
+ ### 11. `save_user_preference`
113
+ Stores user preference text (currently mocked).
114
+
115
+ ### 12. `tool_tavily`
116
+ Performs web search using Tavily.
117
+
118
+ ---
119
+
120
+ ## Installation
121
+
122
+ ## 1. Clone the repository
123
+
124
+ ```bash
125
+ git clone https://github.com/your-username/your-repo-name.git
126
+ cd your-repo-name
127
+ ```
128
+
129
+ ## 2. Create a virtual environment
130
+
131
+ ### Windows
132
+
133
+ ```bash
134
+ python -m venv venv
135
+ venv\Scripts\activate
136
+ ```
137
+
138
+ ### macOS / Linux
139
+
140
+ ```bash
141
+ python3 -m venv venv
142
+ source venv/bin/activate
143
+ ```
144
+
145
+ ## 3. Install dependencies
146
+
147
+ ```bash
148
+ pip install -r requirements.txt
149
+ ```
150
+
151
+ If you do not already have a `requirements.txt`, you can use something like this:
152
+
153
+ ```txt
154
+ gradio
155
+ langchain
156
+ langchain-core
157
+ langchain-tavily
158
+ python-dotenv
159
+ requests
160
+ wikipedia
161
+ beautifulsoup4
162
+ ```
163
+
164
+ Depending on your chatbot backend, you may also need your model provider package, such as:
165
+
166
+ - `langchain-openai`
167
+ - `langchain-google-genai`
168
+ - `langchain-groq`
169
+ - or another LLM integration
170
+
171
+ ---
172
+
173
+ ## Environment Variables
174
+
175
+ Create a `.env` file in the root directory.
176
+
177
+ Example:
178
+
179
+ ```env
180
+ TAVILY_API_KEY=your_tavily_api_key
181
+ OPENAI_API_KEY=your_openai_api_key
182
+ ```
183
+
184
+ If you are using another model provider, replace `OPENAI_API_KEY` with the correct key for your setup.
185
+
186
+ ---
187
+
188
+ ## Running the App
189
+
190
+ Start the Gradio interface with:
191
+
192
+ ```bash
193
+ python app.py
194
+ ```
195
+
196
+ Then open the local Gradio URL shown in the terminal, usually:
197
+
198
+ ```text
199
+ http://127.0.0.1:7860
200
+ ```
201
+
202
+ ---
203
+
204
+ ## How It Works
205
+
206
+ ### User and Thread Management
207
+
208
+ Each conversation is identified using:
209
+
210
+ - `user_id`
211
+ - `thread_id`
212
+
213
+ This allows the chatbot to maintain separate memory for:
214
+
215
+ - different users
216
+ - different chat sessions for the same user
217
+
218
+ ### Chat Flow
219
+
220
+ When the user sends a message:
221
+
222
+ 1. the message is wrapped as a `HumanMessage`
223
+ 2. the chatbot is invoked with `thread_id` and `user_id`
224
+ 3. the agent may call one or more tools
225
+ 4. the final AI response is displayed in the chat UI
226
+ 5. the conversation is stored under that user/thread pair
227
+
228
+ ---
229
+
230
+ ## Gradio UI Features
231
+
232
+ The Gradio app includes:
233
+
234
+ - **User selector**
235
+ - **New user button**
236
+ - **Thread selector**
237
+ - **New chat button**
238
+ - **Chat area**
239
+ - **Message input box**
240
+
241
+ This makes it easy to simulate multiple users and sessions in one interface.
242
+
243
+ ---
244
+
245
+ ## Showing Tool Calls in the UI
246
+
247
+ Yes โ€” this project can be extended to show:
248
+
249
+ - which tool was called
250
+ - tool arguments
251
+ - tool output
252
+
253
+ For example, if the chatbot uses `get_weather("Delhi")`, the UI can display something like:
254
+
255
+ ```text
256
+ Calling tool: get_weather
257
+ Arguments: {"city": "Delhi"}
258
+ Output: Delhi: +34ยฐC, Sunny
259
+ ```
260
+
261
+ This is useful for:
262
+
263
+ - debugging
264
+ - understanding agent behavior
265
+ - teaching/demo purposes
266
+
267
+ A good way to display this is by adding a `gr.Markdown()` or `gr.Textbox()` panel in the UI and parsing:
268
+
269
+ - `AIMessage.tool_calls`
270
+ - `ToolMessage.content`
271
+
272
+ ---
273
+
274
+ ## Example Use Cases
275
+
276
+ - Ask todayโ€™s date
277
+ - Calculate expressions like `25 * 12`
278
+ - Execute small Python snippets
279
+ - Look up weather in a city
280
+ - Search Wikipedia topics
281
+ - Scrape a webpage summary
282
+ - Read a local file
283
+ - Format JSON data
284
+ - Save user preferences
285
+
286
+ ---
287
+
288
+ ## Example Prompts
289
+
290
+ Try asking:
291
+
292
+ ```text
293
+ What is today's date?
294
+ ```
295
+
296
+ ```text
297
+ Calculate 125 * 48
298
+ ```
299
+
300
+ ```text
301
+ What is the weather in London?
302
+ ```
303
+
304
+ ```text
305
+ Search Wikipedia for Artificial Intelligence
306
+ ```
307
+
308
+ ```text
309
+ Read the contents of sample.txt
310
+ ```
311
+
312
+ ```text
313
+ Format this JSON: {"name":"John","age":25}
314
+ ```
315
+
316
+ ---
317
+
318
+ ## Security Warning
319
+
320
+ This project includes some tools that are **unsafe for public deployment** without restrictions.
321
+
322
+ ### Risky tools
323
+
324
+ - `calculator` uses `eval()`
325
+ - `python_exec` uses `exec()`
326
+ - `read_file` can access local files
327
+ - `scrape_website` can fetch arbitrary URLs
328
+
329
+ ### Why this matters
330
+
331
+ If exposed publicly, users may be able to:
332
+
333
+ - run arbitrary Python code
334
+ - read sensitive local files
335
+ - access internal resources
336
+ - misuse the server
337
+
338
+ ### Recommendation
339
+
340
+ Use this project:
341
+
342
+ - for **local development**
343
+ - for **learning**
344
+ - for **internal testing only**
345
+
346
+ Before deploying publicly, you should:
347
+
348
+ - remove `eval()` and `exec()`
349
+ - restrict file access
350
+ - validate URLs
351
+ - sandbox execution
352
+ - add authentication and rate limiting
353
+
354
+ Tiny chatbot, big chaos potential. Better to keep the chaos leashed.
355
+
356
+ ---
357
+
358
+ ## Known Notes
359
+
360
+ ### 1. Circular Import Risk
361
+
362
+ If your UI file is named `memory_chatbot.py` and also contains:
363
+
364
+ ```python
365
+ from memory_chatbot import chatbot
366
+ ```
367
+
368
+ you will likely get an import issue.
369
+
370
+ ### Fix
371
+
372
+ Rename the Gradio UI file to something like:
373
+
374
+ ```text
375
+ app.py
376
+ ```
377
+
378
+ and keep the chatbot backend in:
379
+
380
+ ```text
381
+ memory_chatbot.py
382
+ ```
383
+
384
+ ---
385
+
386
+ ### 2. Tool Results May Differ
387
+
388
+ Some tools depend on external services:
389
+
390
+ - `wttr.in`
391
+ - `Wikipedia`
392
+ - `Tavily`
393
+ - websites being scraped
394
+
395
+ If those services are unavailable, the tool may fail or return unexpected results.
396
+
397
+ ---
398
+
399
+ ## Future Improvements
400
+
401
+ Possible enhancements for this project:
402
+
403
+ - real memory store integration
404
+ - persistent database for chat history
405
+ - streaming responses
406
+ - live tool-call updates in the UI
407
+ - better error handling
408
+ - authentication
409
+ - safer code execution sandbox
410
+ - SQL generation using LLM
411
+ - file upload support
412
+ - improved styling for the chat interface
413
+
414
+ ---
415
+
416
+ ## Sample `tools.py`
417
+
418
+ This file contains all your custom tools used by the chatbot agent.
419
+
420
+ It includes tools for:
421
+
422
+ - date/time
423
+ - calculator
424
+ - Python execution
425
+ - weather
426
+ - Wikipedia
427
+ - web scraping
428
+ - file reading
429
+ - JSON formatting
430
+ - SQL generation
431
+ - system information
432
+ - user preference saving
433
+ - Tavily search
434
+
435
+ ---
436
+
437
+ ## Sample Workflow
438
+
439
+ 1. Start app
440
+ 2. Select or create a user
441
+ 3. Create a new thread
442
+ 4. Ask a question
443
+ 5. Agent decides whether to use tools
444
+ 6. Tool result is returned
445
+ 7. Final AI answer is shown
446
+ 8. Conversation history is stored per user/thread
447
+
448
+ ---
449
+
450
+ ## Requirements Summary
451
+
452
+ Main libraries used:
453
+
454
+ - `gradio`
455
+ - `langchain`
456
+ - `langchain-core`
457
+ - `langchain-tavily`
458
+ - `requests`
459
+ - `wikipedia`
460
+ - `beautifulsoup4`
461
+ - `python-dotenv`
462
+
463
+ ---
464
+
465
+ ## License
466
+
467
+ This project is for educational and development use.
468
+ Add your preferred license here, for example:
469
+
470
+ ```text
471
+ MIT License
472
+ ```
473
+
474
+ ---
475
+
476
+ ## Author
477
+
478
+ Built with LangChain, Gradio, and a healthy amount of agent curiosity.
479
+
480
+ If you want, you can customize this section with your name, GitHub profile, or portfolio link.
481
+
482
+ ---
483
+
484
+ ## Contributing
485
+
486
+ Contributions are welcome.
487
+
488
+ You can improve this project by:
489
+
490
+ - adding safer tool implementations
491
+ - improving UI design
492
+ - adding persistent memory
493
+ - improving tool logging
494
+ - integrating streaming
495
+ - adding tests
496
+
497
+ ---
498
+
499
+ ## Final Notes
500
+
501
+ This project is a great starting point if you want to learn:
502
+
503
+ - LangChain tools
504
+ - agent-based workflows
505
+ - memory-aware chat apps
506
+ - multi-thread chat management
507
+ - Gradio interfaces
508
+
509
+ If you are planning to deploy it publicly, please review the security concerns carefully before doing so.