Spaces:
Running
Running
Update eb_agent_module.py
Browse files- eb_agent_module.py +13 -7
eb_agent_module.py
CHANGED
@@ -580,6 +580,8 @@ class EmployerBrandingAgent:
|
|
580 |
else:
|
581 |
return "general"
|
582 |
|
|
|
|
|
583 |
async def _generate_pandas_response(self, query: str) -> tuple[str, bool]:
|
584 |
"""Generate response using PandasAI for data queries"""
|
585 |
if not self.pandas_agent or not hasattr(self, 'pandas_dfs'):
|
@@ -647,7 +649,7 @@ class EmployerBrandingAgent:
|
|
647 |
# Try to provide a more helpful error message
|
648 |
if "Invalid output" in str(e) and "plot save path" in str(e):
|
649 |
return "I tried to create a visualization but encountered a formatting issue. Please try rephrasing your request or ask for specific data without requesting a chart.", False
|
650 |
-
|
651 |
|
652 |
async def _generate_enhanced_response(self, query: str, pandas_result: str = "", query_type: str = "general") -> str:
|
653 |
"""Generate enhanced response combining PandasAI results with RAG context"""
|
@@ -767,7 +769,6 @@ class EmployerBrandingAgent:
|
|
767 |
logging.error(f"Error in _generate_enhanced_response: {e}", exc_info=True)
|
768 |
return f"I encountered an error while processing your request: {str(e)}"
|
769 |
|
770 |
-
|
771 |
def _validate_query(self, query: str) -> bool:
|
772 |
"""Validate user query input"""
|
773 |
if not query or not isinstance(query, str) or len(query.strip()) < 3:
|
@@ -803,10 +804,18 @@ class EmployerBrandingAgent:
|
|
803 |
# For data-related queries, try PandasAI first
|
804 |
if query_type in ["data", "hybrid"] and self.pandas_agent:
|
805 |
logging.info("Attempting PandasAI analysis...")
|
806 |
-
pandas_text_output,
|
807 |
|
808 |
if pandas_success:
|
809 |
-
logging.info(f"PandasAI analysis successful. Text: '{str(pandas_text_output)[:100]}...'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
810 |
else:
|
811 |
# pandas_text_output might contain the error message from PandasAI
|
812 |
logging.warning(f"PandasAI analysis failed or returned no specific result. Message from PandasAI: {pandas_text_output}")
|
@@ -820,7 +829,6 @@ class EmployerBrandingAgent:
|
|
820 |
elif query_type in ["data", "hybrid"] and not self.pandas_agent:
|
821 |
llm_context_from_pandas += "Note: The data analysis tool is currently unavailable.\n"
|
822 |
|
823 |
-
|
824 |
# Always call the LLM to formulate the final response
|
825 |
final_llm_response = await self._generate_enhanced_response(
|
826 |
query=user_query,
|
@@ -837,7 +845,6 @@ class EmployerBrandingAgent:
|
|
837 |
logging.error(f"Critical error in process_query: {e}", exc_info=True)
|
838 |
return {"text": f"I encountered a critical error while processing your request: {type(e).__name__}. Please check the logs.", "image_path": None}
|
839 |
|
840 |
-
|
841 |
def update_dataframes(self, new_dataframes: Dict[str, pd.DataFrame]):
|
842 |
"""Updates the agent's DataFrames and reinitializes PandasAI agent"""
|
843 |
self.all_dataframes = {k: v.copy() for k, v in new_dataframes.items()}
|
@@ -877,7 +884,6 @@ class EmployerBrandingAgent:
|
|
877 |
"charts_save_path_pandasai": pai.config.save_charts_path if pai.config.llm else "PandasAI not configured"
|
878 |
}
|
879 |
|
880 |
-
|
881 |
def get_available_analyses(self) -> List[str]:
|
882 |
"""Returns list of suggested analyses based on available data"""
|
883 |
if not self.all_dataframes:
|
|
|
580 |
else:
|
581 |
return "general"
|
582 |
|
583 |
+
# Replace the _generate_pandas_response method and everything after it with this properly indented code:
|
584 |
+
|
585 |
async def _generate_pandas_response(self, query: str) -> tuple[str, bool]:
|
586 |
"""Generate response using PandasAI for data queries"""
|
587 |
if not self.pandas_agent or not hasattr(self, 'pandas_dfs'):
|
|
|
649 |
# Try to provide a more helpful error message
|
650 |
if "Invalid output" in str(e) and "plot save path" in str(e):
|
651 |
return "I tried to create a visualization but encountered a formatting issue. Please try rephrasing your request or ask for specific data without requesting a chart.", False
|
652 |
+
return f"Error processing data query: {str(e)}", False
|
653 |
|
654 |
async def _generate_enhanced_response(self, query: str, pandas_result: str = "", query_type: str = "general") -> str:
|
655 |
"""Generate enhanced response combining PandasAI results with RAG context"""
|
|
|
769 |
logging.error(f"Error in _generate_enhanced_response: {e}", exc_info=True)
|
770 |
return f"I encountered an error while processing your request: {str(e)}"
|
771 |
|
|
|
772 |
def _validate_query(self, query: str) -> bool:
|
773 |
"""Validate user query input"""
|
774 |
if not query or not isinstance(query, str) or len(query.strip()) < 3:
|
|
|
804 |
# For data-related queries, try PandasAI first
|
805 |
if query_type in ["data", "hybrid"] and self.pandas_agent:
|
806 |
logging.info("Attempting PandasAI analysis...")
|
807 |
+
pandas_text_output, pandas_success = await self._generate_pandas_response(user_query)
|
808 |
|
809 |
if pandas_success:
|
810 |
+
logging.info(f"PandasAI analysis successful. Text: '{str(pandas_text_output)[:100]}...'")
|
811 |
+
# Check for chart generation in response
|
812 |
+
if "Chart Generated" in pandas_text_output:
|
813 |
+
# Extract chart path from response if present
|
814 |
+
lines = pandas_text_output.split('\n')
|
815 |
+
for line in lines:
|
816 |
+
if "Chart saved at:" in line:
|
817 |
+
pandas_chart_path = line.split("Chart saved at: ")[1].strip()
|
818 |
+
break
|
819 |
else:
|
820 |
# pandas_text_output might contain the error message from PandasAI
|
821 |
logging.warning(f"PandasAI analysis failed or returned no specific result. Message from PandasAI: {pandas_text_output}")
|
|
|
829 |
elif query_type in ["data", "hybrid"] and not self.pandas_agent:
|
830 |
llm_context_from_pandas += "Note: The data analysis tool is currently unavailable.\n"
|
831 |
|
|
|
832 |
# Always call the LLM to formulate the final response
|
833 |
final_llm_response = await self._generate_enhanced_response(
|
834 |
query=user_query,
|
|
|
845 |
logging.error(f"Critical error in process_query: {e}", exc_info=True)
|
846 |
return {"text": f"I encountered a critical error while processing your request: {type(e).__name__}. Please check the logs.", "image_path": None}
|
847 |
|
|
|
848 |
def update_dataframes(self, new_dataframes: Dict[str, pd.DataFrame]):
|
849 |
"""Updates the agent's DataFrames and reinitializes PandasAI agent"""
|
850 |
self.all_dataframes = {k: v.copy() for k, v in new_dataframes.items()}
|
|
|
884 |
"charts_save_path_pandasai": pai.config.save_charts_path if pai.config.llm else "PandasAI not configured"
|
885 |
}
|
886 |
|
|
|
887 |
def get_available_analyses(self) -> List[str]:
|
888 |
"""Returns list of suggested analyses based on available data"""
|
889 |
if not self.all_dataframes:
|