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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # API Key Section
307
+ with gr.Row():
308
+ with gr.Column(scale=3):
309
+ api_key = gr.Textbox(
310
+ label="πŸ”‘ Groq API Key",
311
+ placeholder="Enter your Groq API key here...",
312
+ type="password",
313
+ info="Get your API key from: https://console.groq.com/"
314
+ )
315
+ with gr.Column(scale=2):
316
+ model_selection = gr.Radio(
317
+ choices=["compound-beta", "compound-beta-mini"],
318
+ label="🧠 Model Selection",
319
+ value="compound-beta",
320
+ info="compound-beta: More powerful | compound-beta-mini: Faster"
321
+ )
322
+ with gr.Column(scale=1):
323
+ connect_btn = gr.Button("πŸ”— Connect", variant="primary", size="lg")
324
+
325
+ # Status display
326
+ status_display = gr.Markdown("### πŸ“Š Status: Not connected", elem_classes=["status-box"])
327
+
328
+ # Connect button functionality
329
+ connect_btn.click(
330
+ fn=validate_api_key,
331
+ inputs=[api_key, model_selection],
332
+ outputs=[status_display]
333
+ )
334
+
335
+ model_selection.change(
336
+ fn=update_model,
337
+ inputs=[model_selection],
338
+ outputs=[status_display]
339
+ )
340
+
341
+ # Main Chat Interface
342
+ with gr.Tab("πŸ’¬ Chat"):
343
+ chatbot = gr.Chatbot(
344
+ label="Creative AI Assistant",
345
+ height=500,
346
+ show_label=True,
347
+ bubble_full_width=False
348
+ )
349
+
350
+ with gr.Row():
351
+ msg = gr.Textbox(
352
+ label="Your Message",
353
+ placeholder="Type your message here...",
354
+ lines=3,
355
+ scale=4
356
+ )
357
+ with gr.Column(scale=1):
358
+ send_btn = gr.Button("πŸ“€ Send", variant="primary")
359
+ clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
360
+
361
+ # Advanced Settings
362
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
363
+ with gr.Row():
364
+ temperature = gr.Slider(
365
+ minimum=0.0,
366
+ maximum=2.0,
367
+ value=0.7,
368
+ step=0.1,
369
+ label="🌑️ Temperature",
370
+ info="Higher = more creative, Lower = more focused"
371
+ )
372
+ max_tokens = gr.Slider(
373
+ minimum=100,
374
+ maximum=4000,
375
+ value=1024,
376
+ step=100,
377
+ label="πŸ“ Max Tokens",
378
+ info="Maximum length of response"
379
+ )
380
+
381
+ system_prompt = gr.Textbox(
382
+ label="🎭 Custom System Prompt",
383
+ placeholder="Override the default system prompt...",
384
+ lines=2,
385
+ info="Leave empty to use default creative assistant prompt"
386
+ )
387
+
388
+ # Domain Filtering Section
389
+ with gr.Accordion("🌐 Domain Filtering (for Web Search)", open=False):
390
+ gr.Markdown("""
391
+ <div class="domain-info">
392
+ <h4>πŸ” Domain Filtering Guide</h4>
393
+ <p>Control which websites the AI can search when answering your questions:</p>
394
+ <ul>
395
+ <li><strong>Include Domains:</strong> Only search these domains (comma-separated)</li>
396
+ <li><strong>Exclude Domains:</strong> Never search these domains (comma-separated)</li>
397
+ <li><strong>Examples:</strong> arxiv.org, *.edu, github.com, stackoverflow.com</li>
398
+ <li><strong>Wildcards:</strong> Use *.edu for all educational domains</li>
399
+ </ul>
400
+ </div>
401
+ """)
402
+
403
+ with gr.Row():
404
+ include_domains = gr.Textbox(
405
+ label="βœ… Include Domains (comma-separated)",
406
+ placeholder="arxiv.org, *.edu, github.com, stackoverflow.com",
407
+ info="Only search these domains"
408
+ )
409
+ exclude_domains = gr.Textbox(
410
+ label="❌ Exclude Domains (comma-separated)",
411
+ placeholder="wikipedia.org, reddit.com, twitter.com",
412
+ info="Never search these domains"
413
+ )
414
+
415
+ with gr.Accordion("πŸ“š Common Domain Examples", open=False):
416
+ gr.Markdown("""
417
+ **Academic & Research:**
418
+ - arxiv.org, *.edu, scholar.google.com, researchgate.net
419
+
420
+ **Technology & Programming:**
421
+ - github.com, stackoverflow.com, docs.python.org, developer.mozilla.org
422
+
423
+ **News & Media:**
424
+ - reuters.com, bbc.com, npr.org, apnews.com
425
+
426
+ **Business & Finance:**
427
+ - bloomberg.com, wsj.com, nasdaq.com, sec.gov
428
+
429
+ **Science & Medicine:**
430
+ - nature.com, science.org, pubmed.ncbi.nlm.nih.gov, who.int
431
+ """)
432
+
433
+ # How to Use Section
434
+ with gr.Accordion("πŸ“– How to Use This App", open=False):
435
+ gr.Markdown("""
436
+ ### πŸš€ Getting Started
437
+ 1. **Enter your Groq API Key** - Get one from [console.groq.com](https://console.groq.com/)
438
+ 2. **Select a model** - Choose between compound-beta (powerful) or compound-beta-mini (fast)
439
+ 3. **Click Connect** - Validate your key and connect to the AI
440
+ 4. **Start chatting!** - Type your message and get intelligent responses
441
+
442
+ ### 🎯 Key Features
443
+ - **Agentic AI**: The AI can use tools and search the web autonomously
444
+ - **Domain Filtering**: Control which websites the AI searches
445
+ - **Creative Responses**: Optimized for engaging and helpful conversations
446
+ - **Memory**: Maintains conversation context throughout the session
447
+ - **Customizable**: Adjust temperature, tokens, and system prompts
448
+
449
+ ### πŸ’‘ Tips for Best Results
450
+ - Be specific in your questions for better responses
451
+ - Use domain filtering for specialized research
452
+ - Adjust temperature: higher for creativity, lower for precision
453
+ - Try different system prompts for different conversation styles
454
+ """)
455
+
456
+ # Sample Examples Section
457
+ with gr.Accordion("🎯 Sample Examples to Test", open=False):
458
+ gr.Markdown("""
459
+ <div class="example-box">
460
+ <h4>πŸ”¬ Research & Analysis</h4>
461
+ <ul>
462
+ <li>"What are the latest breakthroughs in quantum computing?"</li>
463
+ <li>"Compare different machine learning frameworks for beginners"</li>
464
+ <li>"Analyze the current state of renewable energy technology"</li>
465
+ </ul>
466
+
467
+ <h4>πŸ’» Programming & Tech</h4>
468
+ <ul>
469
+ <li>"Write a Python script to analyze CSV data and create visualizations"</li>
470
+ <li>"Explain the differences between React and Vue.js with examples"</li>
471
+ <li>"Help me debug this JavaScript code: [paste your code]"</li>
472
+ </ul>
473
+
474
+ <h4>🎨 Creative Tasks</h4>
475
+ <ul>
476
+ <li>"Write a short story about AI and humans working together"</li>
477
+ <li>"Create a marketing plan for a sustainable fashion brand"</li>
478
+ <li>"Generate ideas for a mobile app that helps with mental health"</li>
479
+ </ul>
480
+
481
+ <h4>πŸ“Š Business & Analysis</h4>
482
+ <ul>
483
+ <li>"What are the current trends in the cryptocurrency market?"</li>
484
+ <li>"Analyze the pros and cons of remote work for startups"</li>
485
+ <li>"Create a business plan outline for a food delivery service"</li>
486
+ </ul>
487
+
488
+ <h4>🌐 With Domain Filtering</h4>
489
+ <ul>
490
+ <li>Include "*.edu, arxiv.org": "Latest research on climate change solutions"</li>
491
+ <li>Include "github.com, stackoverflow.com": "Best practices for API development"</li>
492
+ <li>Exclude "wikipedia.org, reddit.com": "Professional analysis of market trends"</li>
493
+ </ul>
494
+ </div>
495
+ """)
496
+
497
+ # Event handlers
498
+ send_btn.click(
499
+ fn=chat_with_ai,
500
+ inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot],
501
+ outputs=[chatbot, msg]
502
+ )
503
+
504
+ msg.submit(
505
+ fn=chat_with_ai,
506
+ inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot],
507
+ outputs=[chatbot, msg]
508
+ )
509
+
510
+ clear_btn.click(
511
+ fn=clear_chat_history,
512
+ outputs=[chatbot]
513
+ )
514
+
515
+ # Footer
516
+ gr.Markdown("""
517
+ ---
518
+ ### πŸš€ About This Tool
519
+ This Creative Agentic AI Chat Tool leverages Groq's powerful compound models to provide intelligent,
520
+ context-aware responses with web search capabilities. Perfect for research, programming help,
521
+ creative writing, and complex problem-solving.
522
+
523
+ **Features:**
524
+ - πŸ” Web search with domain filtering
525
+ - 🧠 Advanced AI reasoning with tool usage
526
+ - πŸ’¬ Conversational memory and context
527
+ - βš™οΈ Customizable parameters and prompts
528
+ - 🎨 Creative and analytical capabilities
529
+ """)
530
+
531
+ return app
532
+
533
+ # Main execution
534
+ if __name__ == "__main__":
535
+ app = create_gradio_app()
536
+ app.launch(
537
+ server_name="0.0.0.0",
538
+ server_port=7860,
539
+ share=False,
540
+ show_error=True
541
+ )