Navya-Sree commited on
Commit
c54e455
Β·
verified Β·
1 Parent(s): ed60c1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +375 -49
app.py CHANGED
@@ -1,58 +1,384 @@
1
  import streamlit as st
2
- import os
 
 
 
 
 
 
3
 
4
- # Add this at the top of your app.py
5
- st.set_page_config(page_title="Multi-Agent Code Assistant", page_icon="πŸ€–")
 
 
 
 
6
 
7
- # API Key Setup
8
- if 'api_key' not in st.session_state:
9
- st.session_state.api_key = None
 
 
 
10
 
11
- # Sidebar for API key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with st.sidebar:
13
- st.header("πŸ”‘ API Configuration")
14
 
15
- # Option 1: Use environment variable
16
- env_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
17
 
18
- if env_key:
19
- st.session_state.api_key = env_key
20
- st.success("βœ… API key loaded from environment")
21
- else:
22
- # Option 2: User input
23
- user_key = st.text_input(
24
- "Enter OpenAI API Key:",
25
- type="password",
26
- help="Get your key from https://platform.openai.com/api-keys"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  )
28
 
29
- if user_key:
30
- st.session_state.api_key = user_key
31
- st.success("βœ… API key set!")
32
-
33
- st.markdown("""
34
- ### How to get an API key:
35
- 1. Go to [OpenAI Platform](https://platform.openai.com/api-keys)
36
- 2. Click "Create new secret key"
37
- 3. Copy and paste it here
38
- 4. Your key is stored only in your browser session
39
- """)
40
-
41
- # Cost warning
42
- st.warning("""
43
- ⚠️ **Cost Warning:**
44
- - You're using YOUR OpenAI credits
45
- - GPT-3.5-turbo: ~$0.0015 per 1K tokens
46
- - Approx. 100 requests = $0.15
47
- """)
48
-
49
- # Check if API key is available
50
- if not st.session_state.api_key:
51
- st.error("πŸ” Please enter your OpenAI API key in the sidebar to continue.")
52
- st.stop()
53
-
54
- # Now initialize your agents with the API key
55
- import openai
56
- openai.api_key = st.session_state.api_key
57
-
58
- # Rest of your app continues...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import time
3
+ import json
4
+ from typing import Dict, Any
5
+ import plotly.graph_objects as go
6
+ import plotly.express as px
7
+ import pandas as pd
8
+ from datetime import datetime
9
 
10
+ # Import our agents
11
+ from agents.coder_agent import CoderAgent
12
+ from agents.reviewer_agent import ReviewerAgent
13
+ from agents.tester_agent import TesterAgent
14
+ from utils.rag_system import RAGSystem
15
+ from utils.monitoring import MonitoringSystem
16
 
17
+ # Set page config
18
+ st.set_page_config(
19
+ page_title="Multi-Agent Code Assistant",
20
+ page_icon="πŸ€–",
21
+ layout="wide"
22
+ )
23
 
24
+ # Initialize agents (with caching for performance)
25
+ @st.cache_resource
26
+ def init_agents():
27
+ """Initialize all agents once."""
28
+ return {
29
+ "coder": CoderAgent(),
30
+ "reviewer": ReviewerAgent(),
31
+ "tester": TesterAgent(),
32
+ "rag": RAGSystem(),
33
+ "monitor": MonitoringSystem()
34
+ }
35
+
36
+ # Initialize session state
37
+ if 'conversation' not in st.session_state:
38
+ st.session_state.conversation = []
39
+ if 'metrics' not in st.session_state:
40
+ st.session_state.metrics = {
41
+ "total_requests": 0,
42
+ "successful_generations": 0,
43
+ "average_test_score": 0,
44
+ "average_review_score": 0
45
+ }
46
+
47
+ # Title and description
48
+ st.title("πŸ€– Multi-Agent Code Assistant")
49
+ st.markdown("""
50
+ This system uses multiple AI agents working together:
51
+ 1. **Coder Agent**: Writes code based on your requirements
52
+ 2. **Reviewer Agent**: Checks code quality and suggests improvements
53
+ 3. **Tester Agent**: Creates and runs tests to verify functionality
54
+ 4. **RAG System**: Provides relevant documentation context
55
+ """)
56
+
57
+ # Sidebar for configuration
58
  with st.sidebar:
59
+ st.header("βš™οΈ Configuration")
60
 
61
+ # Model selection
62
+ model_choice = st.selectbox(
63
+ "Select Model",
64
+ ["gpt-3.5-turbo", "gpt-4"],
65
+ help="GPT-4 is more accurate but slower and more expensive"
66
+ )
67
 
68
+ # Temperature slider
69
+ temperature = st.slider(
70
+ "Creativity (Temperature)",
71
+ min_value=0.0,
72
+ max_value=1.0,
73
+ value=0.7,
74
+ help="Higher values make output more creative, lower values more deterministic"
75
+ )
76
+
77
+ # Advanced options
78
+ with st.expander("Advanced Options"):
79
+ use_rag = st.checkbox("Use RAG (Documentation Context)", value=True)
80
+ auto_test = st.checkbox("Auto-run Tests", value=True)
81
+ show_raw = st.checkbox("Show Raw Responses", value=False)
82
+
83
+ st.divider()
84
+
85
+ # System metrics
86
+ st.header("πŸ“Š System Metrics")
87
+ col1, col2 = st.columns(2)
88
+ with col1:
89
+ st.metric("Total Requests", st.session_state.metrics["total_requests"])
90
+ with col2:
91
+ st.metric("Success Rate",
92
+ f"{(st.session_state.metrics['successful_generations'] / max(st.session_state.metrics['total_requests'], 1)) * 100:.1f}%")
93
+
94
+ # Add custom documentation
95
+ st.divider()
96
+ st.header("πŸ“š Add Documentation")
97
+ custom_doc = st.text_area("Add custom documentation for RAG system:")
98
+ if st.button("Add to Knowledge Base") and custom_doc:
99
+ agents = init_agents()
100
+ agents["rag"].add_document(custom_doc, "user")
101
+ st.success("Documentation added successfully!")
102
+
103
+ # Main input area
104
+ st.subheader("πŸ’‘ What code would you like to generate?")
105
+
106
+ # Example prompts
107
+ example_prompts = [
108
+ "Write a function to reverse a string",
109
+ "Create a function that calculates factorial",
110
+ "Write a function to check if a number is prime",
111
+ "Create a function that finds the Fibonacci number at position n",
112
+ "Write a function to sort a list of integers"
113
+ ]
114
+
115
+ # Create columns for example buttons
116
+ cols = st.columns(3)
117
+ for i, prompt in enumerate(example_prompts):
118
+ with cols[i % 3]:
119
+ if st.button(prompt, key=f"example_{i}"):
120
+ st.session_state.user_prompt = prompt
121
+
122
+ # Text input for custom prompt
123
+ user_prompt = st.text_input(
124
+ "Or enter your own prompt:",
125
+ key="user_prompt",
126
+ placeholder="e.g., Write a Python function to validate email addresses"
127
+ )
128
+
129
+ # Initialize agents
130
+ agents = init_agents()
131
+
132
+ # Update coder agent parameters
133
+ agents["coder"].model = model_choice
134
+ agents["coder"].temperature = temperature
135
+
136
+ if st.button("πŸš€ Generate Code", type="primary") and user_prompt:
137
+ # Update metrics
138
+ st.session_state.metrics["total_requests"] += 1
139
+
140
+ # Create progress tracking
141
+ progress_bar = st.progress(0)
142
+ status_text = st.empty()
143
+
144
+ # Step 1: Get RAG context
145
+ status_text.text("πŸ” Retrieving relevant documentation...")
146
+ context = agents["rag"].get_context(user_prompt) if use_rag else ""
147
+ progress_bar.progress(20)
148
+
149
+ # Step 2: Generate code
150
+ status_text.text("πŸ’» Generating code with Coder Agent...")
151
+ code_result = agents["coder"].generate_code(user_prompt, context)
152
+ progress_bar.progress(40)
153
+
154
+ if code_result["status"] == "success":
155
+ # Update success metrics
156
+ st.session_state.metrics["successful_generations"] += 1
157
+
158
+ # Display generated code
159
+ st.subheader("βœ… Generated Code")
160
+ st.code(code_result["code"], language="python")
161
+
162
+ # Show raw response if enabled
163
+ if show_raw:
164
+ with st.expander("View Raw Response"):
165
+ st.text(code_result["raw_response"])
166
+
167
+ # Step 3: Review code
168
+ status_text.text("πŸ” Reviewing code with Reviewer Agent...")
169
+ review_result = agents["reviewer"].comprehensive_review(code_result["code"])
170
+ progress_bar.progress(60)
171
+
172
+ # Display review results
173
+ st.subheader("πŸ“ Code Review Results")
174
+
175
+ # Create tabs for different review aspects
176
+ review_tab1, review_tab2, review_tab3 = st.tabs([
177
+ "Overall Score",
178
+ "Static Analysis",
179
+ "LLM Suggestions"
180
+ ])
181
+
182
+ with review_tab1:
183
+ score = review_result["overall_score"]
184
+ st.metric("Overall Quality Score", f"{score:.1f}/10.0")
185
+
186
+ # Visual gauge
187
+ fig = go.Figure(go.Indicator(
188
+ mode="gauge+number",
189
+ value=score,
190
+ domain={'x': [0, 1], 'y': [0, 1]},
191
+ title={'text': "Code Quality"},
192
+ gauge={
193
+ 'axis': {'range': [0, 10]},
194
+ 'bar': {'color': "darkblue"},
195
+ 'steps': [
196
+ {'range': [0, 3], 'color': "red"},
197
+ {'range': [3, 7], 'color': "yellow"},
198
+ {'range': [7, 10], 'color': "green"}
199
+ ],
200
+ 'threshold': {
201
+ 'line': {'color': "black", 'width': 4},
202
+ 'thickness': 0.75,
203
+ 'value': score
204
+ }
205
+ }
206
+ ))
207
+ fig.update_layout(height=200)
208
+ st.plotly_chart(fig, use_container_width=True)
209
+
210
+ with review_tab2:
211
+ static = review_result["static_analysis"]
212
+ if static["status"] == "success":
213
+ st.write(f"**Pylint Score:** {static.get('score', 0):.1f}/10.0")
214
+ if static.get("issues"):
215
+ st.write("**Issues Found:**")
216
+ for issue in static["issues"][:5]: # Show first 5 issues
217
+ st.write(f"- {issue.get('message', 'Unknown issue')}")
218
+ else:
219
+ st.success("No issues found in static analysis!")
220
+ else:
221
+ st.error(f"Static analysis failed: {static.get('error')}")
222
+
223
+ with review_tab3:
224
+ llm = review_result["llm_review"]
225
+ if llm["status"] == "success":
226
+ st.write(llm["review"])
227
+ if llm.get("suggestions"):
228
+ st.write("**Key Suggestions:**")
229
+ for suggestion in llm["suggestions"]:
230
+ st.write(f"β€’ {suggestion}")
231
+ else:
232
+ st.error(f"LLM review failed: {llm.get('error')}")
233
+
234
+ # Step 4: Test code if enabled
235
+ if auto_test:
236
+ status_text.text("πŸ§ͺ Testing code with Tester Agent...")
237
+ test_result = agents["tester"].test_code(code_result["code"], user_prompt)
238
+ progress_bar.progress(80)
239
+
240
+ # Display test results
241
+ st.subheader("πŸ§ͺ Test Results")
242
+
243
+ # Test metrics
244
+ if test_result["test_results"]["status"] == "success":
245
+ metrics = test_result["test_results"]["metrics"]
246
+
247
+ col1, col2, col3 = st.columns(3)
248
+ with col1:
249
+ st.metric("Total Tests", metrics["total_tests"])
250
+ with col2:
251
+ st.metric("Passed Tests", metrics["passed_tests"])
252
+ with col3:
253
+ st.metric("Pass Rate", f"{metrics['pass_rate']:.1f}%")
254
+
255
+ # Test details
256
+ with st.expander("View Test Details"):
257
+ for i, test_case in enumerate(test_result["test_cases"]):
258
+ st.write(f"**Test {i+1}:**")
259
+ st.write(f"- Input: `{test_case['input']}`")
260
+ st.write(f"- Expected: `{test_case['expected']}`")
261
+
262
+ # Update average score
263
+ current_avg = st.session_state.metrics["average_test_score"]
264
+ total_req = max(1, st.session_state.metrics["successful_generations"])
265
+ st.session_state.metrics["average_test_score"] = (
266
+ (current_avg * (total_req - 1) + metrics["pass_rate"]) / total_req
267
+ )
268
+ else:
269
+ st.error(f"Testing failed: {test_result['test_results'].get('error')}")
270
+
271
+ # Step 5: Log to monitoring
272
+ status_text.text("πŸ“Š Logging results to monitoring system...")
273
+ agents["monitor"].log_generation({
274
+ "timestamp": datetime.now().isoformat(),
275
+ "prompt": user_prompt,
276
+ "code": code_result["code"],
277
+ "model": model_choice,
278
+ "temperature": temperature,
279
+ "review_score": review_result["overall_score"],
280
+ "test_score": test_result.get("test_results", {}).get("metrics", {}).get("pass_rate", 0) if auto_test else None,
281
+ "tokens_used": code_result.get("tokens_used", 0)
282
+ })
283
+
284
+ # Update conversation history
285
+ st.session_state.conversation.append({
286
+ "role": "user",
287
+ "content": user_prompt,
288
+ "timestamp": datetime.now().strftime("%H:%M:%S")
289
+ })
290
+ st.session_state.conversation.append({
291
+ "role": "assistant",
292
+ "content": f"Generated code with score {review_result['overall_score']:.1f}/10.0",
293
+ "timestamp": datetime.now().strftime("%H:%M:%S")
294
+ })
295
+
296
+ progress_bar.progress(100)
297
+ status_text.text("βœ… Process completed!")
298
+
299
+ # Update review score average
300
+ current_avg = st.session_state.metrics["average_review_score"]
301
+ total_req = max(1, st.session_state.metrics["successful_generations"])
302
+ st.session_state.metrics["average_review_score"] = (
303
+ (current_avg * (total_req - 1) + review_result["overall_score"]) / total_req
304
  )
305
 
306
+ else:
307
+ st.error(f"Code generation failed: {code_result.get('error')}")
308
+ progress_bar.progress(100)
309
+ status_text.text("❌ Process failed!")
310
+
311
+ # Conversation history
312
+ if st.session_state.conversation:
313
+ st.divider()
314
+ st.subheader("πŸ“œ Conversation History")
315
+
316
+ for message in reversed(st.session_state.conversation[-10:]): # Show last 10 messages
317
+ if message["role"] == "user":
318
+ st.markdown(f"**πŸ‘€ You** ({message['timestamp']}):")
319
+ st.info(message["content"])
320
+ else:
321
+ st.markdown(f"**πŸ€– Assistant** ({message['timestamp']}):")
322
+ st.success(message["content"])
323
+
324
+ # Monitoring dashboard (collapsible)
325
+ with st.expander("πŸ“ˆ View Monitoring Dashboard"):
326
+ if st.button("Refresh Dashboard"):
327
+ st.rerun()
328
+
329
+ metrics_data = agents["monitor"].get_metrics()
330
+
331
+ if metrics_data:
332
+ # Convert to DataFrame for visualization
333
+ df = pd.DataFrame(metrics_data)
334
+
335
+ # Create metrics visualization
336
+ col1, col2 = st.columns(2)
337
+
338
+ with col1:
339
+ if len(df) > 1:
340
+ # Score trend
341
+ fig = px.line(
342
+ df,
343
+ x='timestamp',
344
+ y='review_score',
345
+ title='Code Quality Score Trend',
346
+ markers=True
347
+ )
348
+ st.plotly_chart(fig, use_container_width=True)
349
+
350
+ with col2:
351
+ if 'tokens_used' in df.columns:
352
+ # Token usage
353
+ fig = px.bar(
354
+ df,
355
+ x='timestamp',
356
+ y='tokens_used',
357
+ title='Token Usage per Request'
358
+ )
359
+ st.plotly_chart(fig, use_container_width=True)
360
+
361
+ # Export data
362
+ st.download_button(
363
+ label="πŸ“₯ Export Metrics Data",
364
+ data=df.to_csv(index=False),
365
+ file_name=f"code_assistant_metrics_{datetime.now().strftime('%Y%m%d')}.csv",
366
+ mime="text/csv"
367
+ )
368
+ else:
369
+ st.info("No metrics data available yet. Generate some code first!")
370
+
371
+ # Footer
372
+ st.divider()
373
+ st.markdown("""
374
+ ---
375
+ ### 🎯 **How This System Works:**
376
+
377
+ 1. **Multi-Agent Architecture**: Each agent specializes in one task (coding, reviewing, testing)
378
+ 2. **Agent Communication**: Agents pass structured data between each other
379
+ 3. **RAG Integration**: Provides context from documentation to reduce hallucinations
380
+ 4. **Evaluation Pipeline**: Continuous monitoring of code quality and test performance
381
+ 5. **Incremental Improvement**: Start simple and add features gradually
382
+
383
+ **Built with**: OpenAI GPT, LangChain, ChromaDB, Streamlit
384
+ """)