yashAI007 commited on
Commit
cc85559
·
1 Parent(s): b8c0485

update README.md

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