shukdevdatta123 commited on
Commit
bcec1a0
·
verified ·
1 Parent(s): b5247e8

Delete v1.txt

Browse files
Files changed (1) hide show
  1. v1.txt +0 -556
v1.txt DELETED
@@ -1,556 +0,0 @@
1
- import os
2
- import json
3
- import time
4
- import gradio as gr
5
- from datetime import datetime
6
- from typing import List, Dict, Any, Optional, Union
7
- import threading
8
-
9
- # Import Groq
10
- from groq import Groq
11
-
12
- class CreativeAgenticAI:
13
- """
14
- Creative Agentic AI Chat Tool using Groq's compound models
15
- """
16
-
17
- def __init__(self, api_key: str, model: str = "compound-beta"):
18
- """
19
- Initialize the Creative Agentic AI system.
20
-
21
- Args:
22
- api_key: Groq API key
23
- model: Which Groq model to use ('compound-beta' or 'compound-beta-mini')
24
- """
25
- self.api_key = api_key
26
- if not self.api_key:
27
- raise ValueError("No API key provided")
28
-
29
- self.client = Groq(api_key=self.api_key)
30
- self.model = model
31
- self.conversation_history = []
32
-
33
- def chat(self, message: str,
34
- include_domains: List[str] = None,
35
- exclude_domains: List[str] = None,
36
- system_prompt: str = None,
37
- temperature: float = 0.7,
38
- max_tokens: int = 1024) -> Dict:
39
- """
40
- Send a message to the AI and get a response
41
-
42
- Args:
43
- message: User's message
44
- include_domains: List of domains to include for web search
45
- exclude_domains: List of domains to exclude from web search
46
- system_prompt: Custom system prompt
47
- temperature: Model temperature (0.0-2.0)
48
- max_tokens: Maximum tokens in response
49
-
50
- Returns:
51
- AI response with metadata
52
- """
53
- # Default system prompt if none provided
54
- if not system_prompt:
55
- system_prompt = """You are a creative and intelligent AI assistant with agentic capabilities.
56
- You can search the web, analyze information, and provide comprehensive responses.
57
- Be helpful, creative, and engaging while maintaining accuracy."""
58
-
59
- # Build messages
60
- messages = [{"role": "system", "content": system_prompt}]
61
-
62
- # Add conversation history (last 10 exchanges)
63
- messages.extend(self.conversation_history[-20:]) # Last 10 user-assistant pairs
64
-
65
- # Add current message
66
- messages.append({"role": "user", "content": message})
67
-
68
- # Set up API parameters
69
- params = {
70
- "messages": messages,
71
- "model": self.model,
72
- "temperature": temperature,
73
- "max_tokens": max_tokens
74
- }
75
-
76
- # Add domain filtering if specified
77
- if include_domains and include_domains[0].strip():
78
- params["include_domains"] = [domain.strip() for domain in include_domains if domain.strip()]
79
- if exclude_domains and exclude_domains[0].strip():
80
- params["exclude_domains"] = [domain.strip() for domain in exclude_domains if domain.strip()]
81
-
82
- try:
83
- # Make the API call
84
- response = self.client.chat.completions.create(**params)
85
- content = response.choices[0].message.content
86
-
87
- # Extract tool usage information
88
- tool_info = self._extract_tool_info(response)
89
-
90
- # Add to conversation history
91
- self.conversation_history.append({"role": "user", "content": message})
92
- self.conversation_history.append({"role": "assistant", "content": content})
93
-
94
- # Create response object
95
- response_data = {
96
- "content": content,
97
- "timestamp": datetime.now().isoformat(),
98
- "model": self.model,
99
- "tool_usage": tool_info,
100
- "parameters": {
101
- "temperature": temperature,
102
- "max_tokens": max_tokens,
103
- "include_domains": include_domains,
104
- "exclude_domains": exclude_domains
105
- }
106
- }
107
-
108
- return response_data
109
-
110
- except Exception as e:
111
- error_msg = f"Error: {str(e)}"
112
- self.conversation_history.append({"role": "user", "content": message})
113
- self.conversation_history.append({"role": "assistant", "content": error_msg})
114
-
115
- return {
116
- "content": error_msg,
117
- "timestamp": datetime.now().isoformat(),
118
- "model": self.model,
119
- "tool_usage": None,
120
- "error": str(e)
121
- }
122
-
123
- def _extract_tool_info(self, response) -> Dict:
124
- """Extract tool usage information in a JSON serializable format"""
125
- tool_info = None
126
- if hasattr(response.choices[0].message, 'executed_tools'):
127
- tools = response.choices[0].message.executed_tools
128
- if tools:
129
- tool_info = []
130
- for tool in tools:
131
- tool_dict = {
132
- "tool_type": getattr(tool, "type", "unknown"),
133
- "tool_name": getattr(tool, "name", "unknown"),
134
- }
135
- if hasattr(tool, "input"):
136
- tool_dict["input"] = str(tool.input)
137
- if hasattr(tool, "output"):
138
- tool_dict["output"] = str(tool.output)
139
- tool_info.append(tool_dict)
140
- return tool_info
141
-
142
- def clear_history(self):
143
- """Clear conversation history"""
144
- self.conversation_history = []
145
-
146
- def get_history_summary(self) -> str:
147
- """Get a summary of conversation history"""
148
- if not self.conversation_history:
149
- return "No conversation history"
150
-
151
- user_messages = [msg for msg in self.conversation_history if msg["role"] == "user"]
152
- assistant_messages = [msg for msg in self.conversation_history if msg["role"] == "assistant"]
153
-
154
- return f"Conversation: {len(user_messages)} user messages, {len(assistant_messages)} assistant responses"
155
-
156
- # Global variables
157
- ai_instance = None
158
- api_key_status = "Not Set"
159
-
160
- def validate_api_key(api_key: str, model: str) -> str:
161
- """Validate Groq API key and initialize AI instance"""
162
- global ai_instance, api_key_status
163
-
164
- if not api_key or len(api_key.strip()) < 10:
165
- api_key_status = "Invalid ❌"
166
- return "❌ Please enter a valid API key (should be longer than 10 characters)"
167
-
168
- try:
169
- # Test the API key
170
- client = Groq(api_key=api_key)
171
- # Try a simple request to validate
172
- test_response = client.chat.completions.create(
173
- messages=[{"role": "user", "content": "Hello"}],
174
- model=model,
175
- max_tokens=10
176
- )
177
-
178
- # Create AI instance
179
- ai_instance = CreativeAgenticAI(api_key=api_key, model=model)
180
- api_key_status = "Valid ✅"
181
-
182
- return f"✅ API Key Valid! Creative Agentic AI is ready.\n\n**Model:** {model}\n**Status:** Connected and ready for chat!"
183
-
184
- except Exception as e:
185
- api_key_status = "Invalid ❌"
186
- ai_instance = None
187
- return f"❌ Error validating API key: {str(e)}\n\nPlease check your API key and try again."
188
-
189
- def update_model(model: str) -> str:
190
- """Update the model selection"""
191
- global ai_instance
192
-
193
- if ai_instance:
194
- ai_instance.model = model
195
- return f"✅ Model updated to: **{model}**"
196
- else:
197
- return "⚠️ Please set your API key first"
198
-
199
- def chat_with_ai(message: str,
200
- include_domains: str,
201
- exclude_domains: str,
202
- system_prompt: str,
203
- temperature: float,
204
- max_tokens: int,
205
- history: List) -> tuple:
206
- """Main chat function"""
207
- global ai_instance
208
-
209
- if not ai_instance:
210
- error_msg = "⚠️ Please set your Groq API key first!"
211
- history.append([message, error_msg])
212
- return history, ""
213
-
214
- if not message.strip():
215
- return history, ""
216
-
217
- # Process domain lists
218
- include_list = [d.strip() for d in include_domains.split(",")] if include_domains.strip() else []
219
- exclude_list = [d.strip() for d in exclude_domains.split(",")] if exclude_domains.strip() else []
220
-
221
- try:
222
- # Get AI response
223
- response = ai_instance.chat(
224
- message=message,
225
- include_domains=include_list if include_list else None,
226
- exclude_domains=exclude_list if exclude_list else None,
227
- system_prompt=system_prompt if system_prompt.strip() else None,
228
- temperature=temperature,
229
- max_tokens=int(max_tokens)
230
- )
231
-
232
- # Format response
233
- ai_response = response["content"]
234
-
235
- # Add tool usage info if available
236
- if response.get("tool_usage"):
237
- ai_response += f"\n\n*🔧 Tools used: {len(response['tool_usage'])} tool(s)*"
238
-
239
- # Add to history
240
- history.append([message, ai_response])
241
-
242
- return history, ""
243
-
244
- except Exception as e:
245
- error_msg = f"❌ Error: {str(e)}"
246
- history.append([message, error_msg])
247
- return history, ""
248
-
249
- def clear_chat_history():
250
- """Clear the chat history"""
251
- global ai_instance
252
- if ai_instance:
253
- ai_instance.clear_history()
254
- return []
255
-
256
- def create_gradio_app():
257
- """Create the main Gradio application"""
258
-
259
- # Custom CSS for better styling
260
- css = """
261
- .container {
262
- max-width: 1200px;
263
- margin: 0 auto;
264
- }
265
- .header {
266
- text-align: center;
267
- background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
268
- color: white;
269
- padding: 20px;
270
- border-radius: 10px;
271
- margin-bottom: 20px;
272
- }
273
- .status-box {
274
- background-color: #f8f9fa;
275
- border: 1px solid #dee2e6;
276
- border-radius: 8px;
277
- padding: 15px;
278
- margin: 10px 0;
279
- }
280
- .example-box {
281
- background-color: #e8f4fd;
282
- border-left: 4px solid #007bff;
283
- padding: 15px;
284
- margin: 10px 0;
285
- border-radius: 0 8px 8px 0;
286
- }
287
- .domain-info {
288
- background-color: #fff3cd;
289
- border: 1px solid #ffeaa7;
290
- border-radius: 8px;
291
- padding: 15px;
292
- margin: 10px 0;
293
- }
294
- """
295
-
296
- with gr.Blocks(css=css, title="🤖 Creative Agentic AI Chat", theme=gr.themes.Soft()) as app:
297
-
298
- # Header
299
- gr.HTML("""
300
- <div class="header">
301
- <h1>🤖 Creative Agentic AI Chat Tool</h1>
302
- <p>Powered by Groq's Compound Models with Web Search & Agentic Capabilities</p>
303
- </div>
304
- """)
305
-
306
- # NeuroScope AI Section
307
- with gr.Accordion("🤖 NeuroScope AI", open=False):
308
- gr.Markdown("""
309
- - 🧠 Intelligence (Neuro)
310
- - 🔍 Domain filtering (Scope)
311
- - 🤖 AI capabilities (AI)
312
- - ⚡ Precision (Scope implies focused, targeted results)
313
- """)
314
-
315
- with gr.Accordion("📚 IMPORTANT !!!", open=False):
316
- gr.Markdown("""
317
- When **no domains are specified**, NeuroScope AI operates with unrestricted web search capabilities, allowing the compound models to autonomously search across the entire internet to find the most relevant and up-to-date information to answer your questions - essentially giving the AI complete freedom to use its agentic tools and search any website it deems helpful. When **domains are specified** (either include or exclude lists), you're essentially putting the AI on a leash - if you specify "include domains" like "arxiv.org, *.edu", the AI can ONLY search those academic sources, making it laser-focused on scholarly content, while "exclude domains" like "wikipedia.org, reddit.com" tells the AI to search everywhere EXCEPT those sites, filtering out potentially unreliable sources. The **🎭 Custom System Prompt** feature allows you to completely override the AI's default personality and behavior - instead of the default creative assistant, you could make it act like a professional business consultant, a coding mentor, a creative writer, or even a specific character, essentially giving you the power to reshape the AI's entire conversational style and expertise focus with a single prompt change.
318
- """)
319
-
320
- # API Key Section
321
- with gr.Row():
322
- with gr.Column(scale=3):
323
- api_key = gr.Textbox(
324
- label="🔑 Groq API Key",
325
- placeholder="Enter your Groq API key here...",
326
- type="password",
327
- info="Get your API key from: https://console.groq.com/"
328
- )
329
- with gr.Column(scale=2):
330
- model_selection = gr.Radio(
331
- choices=["compound-beta", "compound-beta-mini"],
332
- label="🧠 Model Selection",
333
- value="compound-beta",
334
- info="compound-beta: More powerful | compound-beta-mini: Faster"
335
- )
336
- with gr.Column(scale=1):
337
- connect_btn = gr.Button("🔗 Connect", variant="primary", size="lg")
338
-
339
- # Status display
340
- status_display = gr.Markdown("### 📊 Status: Not connected", elem_classes=["status-box"])
341
-
342
- # Connect button functionality
343
- connect_btn.click(
344
- fn=validate_api_key,
345
- inputs=[api_key, model_selection],
346
- outputs=[status_display]
347
- )
348
-
349
- model_selection.change(
350
- fn=update_model,
351
- inputs=[model_selection],
352
- outputs=[status_display]
353
- )
354
-
355
- # Main Chat Interface
356
- with gr.Tab("💬 Chat"):
357
- chatbot = gr.Chatbot(
358
- label="Creative AI Assistant",
359
- height=500,
360
- show_label=True,
361
- bubble_full_width=False,
362
- show_copy_button=True
363
- )
364
-
365
- with gr.Row():
366
- msg = gr.Textbox(
367
- label="Your Message",
368
- placeholder="Type your message here...",
369
- lines=3,
370
- scale=4
371
- )
372
- with gr.Column(scale=1):
373
- send_btn = gr.Button("📤 Send", variant="primary")
374
- clear_btn = gr.Button("🗑️ Clear", variant="secondary")
375
-
376
- # Advanced Settings
377
- with gr.Accordion("⚙️ Advanced Settings", open=False):
378
- with gr.Row():
379
- temperature = gr.Slider(
380
- minimum=0.0,
381
- maximum=2.0,
382
- value=0.7,
383
- step=0.1,
384
- label="🌡️ Temperature",
385
- info="Higher = more creative, Lower = more focused"
386
- )
387
- max_tokens = gr.Slider(
388
- minimum=100,
389
- maximum=4000,
390
- value=1024,
391
- step=100,
392
- label="📝 Max Tokens",
393
- info="Maximum length of response"
394
- )
395
-
396
- system_prompt = gr.Textbox(
397
- label="🎭 Custom System Prompt",
398
- placeholder="Override the default system prompt...",
399
- lines=2,
400
- info="Leave empty to use default creative assistant prompt"
401
- )
402
-
403
- # Domain Filtering Section
404
- with gr.Accordion("🌐 Domain Filtering (for Web Search)", open=False):
405
- gr.Markdown("""
406
- <div class="domain-info">
407
- <h4>🔍 Domain Filtering Guide</h4>
408
- <p>Control which websites the AI can search when answering your questions:</p>
409
- <ul>
410
- <li><strong>Include Domains:</strong> Only search these domains (comma-separated)</li>
411
- <li><strong>Exclude Domains:</strong> Never search these domains (comma-separated)</li>
412
- <li><strong>Examples:</strong> arxiv.org, *.edu, github.com, stackoverflow.com</li>
413
- <li><strong>Wildcards:</strong> Use *.edu for all educational domains</li>
414
- </ul>
415
- </div>
416
- """)
417
-
418
- with gr.Row():
419
- include_domains = gr.Textbox(
420
- label="✅ Include Domains (comma-separated)",
421
- placeholder="arxiv.org, *.edu, github.com, stackoverflow.com",
422
- info="Only search these domains"
423
- )
424
- exclude_domains = gr.Textbox(
425
- label="❌ Exclude Domains (comma-separated)",
426
- placeholder="wikipedia.org, reddit.com, twitter.com",
427
- info="Never search these domains"
428
- )
429
-
430
- with gr.Accordion("🔗 Common Domain Examples", open=False):
431
- gr.Markdown("""
432
- **Academic & Research:**
433
- - arxiv.org, *.edu, scholar.google.com, researchgate.net
434
-
435
- **Technology & Programming:**
436
- - github.com, stackoverflow.com, docs.python.org, developer.mozilla.org
437
-
438
- **News & Media:**
439
- - reuters.com, bbc.com, npr.org, apnews.com
440
-
441
- **Business & Finance:**
442
- - bloomberg.com, wsj.com, nasdaq.com, sec.gov
443
-
444
- **Science & Medicine:**
445
- - nature.com, science.org, pubmed.ncbi.nlm.nih.gov, who.int
446
- """)
447
-
448
- # How to Use Section
449
- with gr.Accordion("📖 How to Use This App", open=False):
450
- gr.Markdown("""
451
- ### 🚀 Getting Started
452
- 1. **Enter your Groq API Key** - Get one from [console.groq.com](https://console.groq.com/)
453
- 2. **Select a model** - Choose between compound-beta (powerful) or compound-beta-mini (fast)
454
- 3. **Click Connect** - Validate your key and connect to the AI
455
- 4. **Start chatting!** - Type your message and get intelligent responses
456
-
457
- ### 🎯 Key Features
458
- - **Agentic AI**: The AI can use tools and search the web autonomously
459
- - **Domain Filtering**: Control which websites the AI searches
460
- - **Creative Responses**: Optimized for engaging and helpful conversations
461
- - **Memory**: Maintains conversation context throughout the session
462
- - **Customizable**: Adjust temperature, tokens, and system prompts
463
-
464
- ### 💡 Tips for Best Results
465
- - Be specific in your questions for better responses
466
- - Use domain filtering for specialized research
467
- - Adjust temperature: higher for creativity, lower for precision
468
- - Try different system prompts for different conversation styles
469
- """)
470
-
471
- # Sample Examples Section
472
- with gr.Accordion("🎯 Sample Examples to Test", open=False):
473
- gr.Markdown("""
474
- <div class="example-box">
475
- <h4>🔬 Research & Analysis</h4>
476
- <ul>
477
- <li>"What are the latest breakthroughs in quantum computing?"</li>
478
- <li>"Compare different machine learning frameworks for beginners"</li>
479
- <li>"Analyze the current state of renewable energy technology"</li>
480
- </ul>
481
-
482
- <h4>💻 Programming & Tech</h4>
483
- <ul>
484
- <li>"Write a Python script to analyze CSV data and create visualizations"</li>
485
- <li>"Explain the differences between React and Vue.js with examples"</li>
486
- <li>"Help me debug this JavaScript code: [paste your code]"</li>
487
- </ul>
488
-
489
- <h4>🎨 Creative Tasks</h4>
490
- <ul>
491
- <li>"Write a short story about AI and humans working together"</li>
492
- <li>"Create a marketing plan for a sustainable fashion brand"</li>
493
- <li>"Generate ideas for a mobile app that helps with mental health"</li>
494
- </ul>
495
-
496
- <h4>📊 Business & Analysis</h4>
497
- <ul>
498
- <li>"What are the current trends in the cryptocurrency market?"</li>
499
- <li>"Analyze the pros and cons of remote work for startups"</li>
500
- <li>"Create a business plan outline for a food delivery service"</li>
501
- </ul>
502
-
503
- <h4>🌐 With Domain Filtering</h4>
504
- <ul>
505
- <li>Include "*.edu, arxiv.org": "Latest research on climate change solutions"</li>
506
- <li>Include "github.com, stackoverflow.com": "Best practices for API development"</li>
507
- <li>Exclude "wikipedia.org, reddit.com": "Professional analysis of market trends"</li>
508
- </ul>
509
- </div>
510
- """)
511
-
512
- # Event handlers
513
- send_btn.click(
514
- fn=chat_with_ai,
515
- inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot],
516
- outputs=[chatbot, msg]
517
- )
518
-
519
- msg.submit(
520
- fn=chat_with_ai,
521
- inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot],
522
- outputs=[chatbot, msg]
523
- )
524
-
525
- clear_btn.click(
526
- fn=clear_chat_history,
527
- outputs=[chatbot]
528
- )
529
-
530
- # Footer
531
- gr.Markdown("""
532
- ---
533
- ### 🚀 About This Tool
534
- This Creative Agentic AI Chat Tool leverages Groq's powerful compound models to provide intelligent,
535
- context-aware responses with web search capabilities. Perfect for research, programming help,
536
- creative writing, and complex problem-solving.
537
-
538
- **Features:**
539
- - 🔍 Web search with domain filtering
540
- - 🧠 Advanced AI reasoning with tool usage
541
- - 💬 Conversational memory and context
542
- - ⚙️ Customizable parameters and prompts
543
- - 🎨 Creative and analytical capabilities
544
- """)
545
-
546
- return app
547
-
548
- # Main execution
549
- if __name__ == "__main__":
550
- app = create_gradio_app()
551
- app.launch(
552
- server_name="0.0.0.0",
553
- server_port=7860,
554
- share=False,
555
- show_error=True
556
- )