GuglielmoTor commited on
Commit
afb06b4
·
verified ·
1 Parent(s): f3ff19a

Update eb_agent_module.py

Browse files
Files changed (1) hide show
  1. eb_agent_module.py +42 -15
eb_agent_module.py CHANGED
@@ -338,7 +338,10 @@ class EmployerBrandingAgent:
338
 
339
  self.chat_history: List[Dict[str, str]] = []
340
  self.is_ready = False
341
-
 
 
 
342
 
343
  # Initialize PandasAI Agent
344
  self.pandas_agent = None
@@ -363,10 +366,14 @@ class EmployerBrandingAgent:
363
 
364
  # Set PandasAI configuration
365
  pai.config.set({
366
- "llm": llm,
367
- "temperature": 0.7,
368
- "verbose": True,
369
- "enable_cache": True
 
 
 
 
370
  })
371
 
372
  # Store dataframes for chat queries (we'll use them directly)
@@ -490,7 +497,7 @@ class EmployerBrandingAgent:
490
  ## Your Enhanced Capabilities:
491
  You now have advanced data analysis capabilities through PandasAI integration, allowing you to:
492
  - Directly query and analyze DataFrames with natural language
493
- - Generate charts and visualizations automatically
494
  - Perform complex statistical analysis on LinkedIn employer branding data
495
  - Handle multi-DataFrame queries and joins seamlessly
496
 
@@ -587,15 +594,35 @@ class EmployerBrandingAgent:
587
  dfs = list(self.pandas_dfs.values())
588
  pandas_response = pai.chat(query, *dfs)
589
 
590
- # Check if response is meaningful
591
- if pandas_response and str(pandas_response).strip():
592
- return str(pandas_response), True
593
- else:
594
- return "I couldn't generate a meaningful analysis for this query.", False
595
-
596
- except Exception as e:
597
- logging.error(f"Error in PandasAI processing: {e}", exc_info=True)
598
- return f"I encountered an error while analyzing the data: {str(e)}", False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
 
600
  async def _generate_enhanced_response(self, query: str, pandas_result: str = "", query_type: str = "general") -> str:
601
  """Generate enhanced response combining PandasAI results with RAG context"""
 
338
 
339
  self.chat_history: List[Dict[str, str]] = []
340
  self.is_ready = False
341
+
342
+ # Create charts directory
343
+ self.charts_dir = "./charts"
344
+ os.makedirs(self.charts_dir, exist_ok=True)
345
 
346
  # Initialize PandasAI Agent
347
  self.pandas_agent = None
 
366
 
367
  # Set PandasAI configuration
368
  pai.config.set({
369
+ "llm": llm,
370
+ "temperature": 0.7,
371
+ "verbose": True,
372
+ "enable_cache": True,
373
+ "save_charts": True, # Enable chart saving
374
+ "save_charts_path": "./charts", # Directory to save charts
375
+ "open_charts": False, # Don't auto-open charts in browser
376
+ "custom_whitelisted_dependencies": ["matplotlib", "seaborn", "plotly"] # Allow plotting libraries
377
  })
378
 
379
  # Store dataframes for chat queries (we'll use them directly)
 
497
  ## Your Enhanced Capabilities:
498
  You now have advanced data analysis capabilities through PandasAI integration, allowing you to:
499
  - Directly query and analyze DataFrames with natural language
500
+ - Generate charts and visualizations automatically (ALWAYS create charts when data visualization would be helpful)
501
  - Perform complex statistical analysis on LinkedIn employer branding data
502
  - Handle multi-DataFrame queries and joins seamlessly
503
 
 
594
  dfs = list(self.pandas_dfs.values())
595
  pandas_response = pai.chat(query, *dfs)
596
 
597
+ # Handle different response types (text, charts, etc.)
598
+ response_text = ""
599
+ chart_info = ""
600
+
601
+ # Check if a chart was generated
602
+ import os
603
+ charts_dir = "./charts"
604
+ if os.path.exists(charts_dir):
605
+ chart_files = [f for f in os.listdir(charts_dir) if f.endswith(('.png', '.jpg', '.jpeg', '.svg'))]
606
+ if chart_files:
607
+ # Get the most recent chart file
608
+ chart_files.sort(key=lambda x: os.path.getmtime(os.path.join(charts_dir, x)), reverse=True)
609
+ latest_chart = chart_files[0]
610
+ chart_path = os.path.join(charts_dir, latest_chart)
611
+ chart_info = f"\n\n📊 **Chart Generated**: {latest_chart}\nChart saved at: {chart_path}"
612
+ logging.info(f"Chart generated: {chart_path}")
613
+
614
+ # Combine text response with chart info
615
+ if pandas_response and str(pandas_response).strip():
616
+ response_text = str(pandas_response).strip()
617
+ else:
618
+ response_text = "Analysis completed"
619
+
620
+ final_response = response_text + chart_info
621
+ return final_response, True
622
+
623
+ except Exception as e:
624
+ logging.error(f"Error in PandasAI processing: {e}", exc_info=True)
625
+ return f"I encountered an error while analyzing the data: {str(e)}", False
626
 
627
  async def _generate_enhanced_response(self, query: str, pandas_result: str = "", query_type: str = "general") -> str:
628
  """Generate enhanced response combining PandasAI results with RAG context"""