Update agent/rica_agent.py
Browse files- agent/rica_agent.py +44 -34
agent/rica_agent.py
CHANGED
|
@@ -10,6 +10,11 @@ from agent_tools.ml_tools import predict_customer_churn_hf, get_model_status
|
|
| 10 |
def create_rica_agent_hf():
|
| 11 |
"""Create RICA agent optimized for HF Spaces"""
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# HF Spaces optimized tools
|
| 14 |
hf_tools = [
|
| 15 |
predict_customer_churn_hf,
|
|
@@ -19,7 +24,7 @@ def create_rica_agent_hf():
|
|
| 19 |
try:
|
| 20 |
agent = CodeAgent(
|
| 21 |
tools=hf_tools,
|
| 22 |
-
model=OpenAI(api_key=
|
| 23 |
add_base_tools=False,
|
| 24 |
max_iterations=3 # Reduced for HF Spaces performance
|
| 25 |
)
|
|
@@ -30,43 +35,48 @@ def create_rica_agent_hf():
|
|
| 30 |
def execute_rica_analysis_hf(analysis_type: str, parameters: dict = None):
|
| 31 |
"""Execute RICA analysis optimized for HF Spaces"""
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"comprehensive": f"""
|
| 38 |
-
Execute business intelligence analysis:
|
| 39 |
-
1) Check model status with get_model_status()
|
| 40 |
-
2) Predict customer churn with predict_customer_churn_hf()
|
| 41 |
-
3) Provide executive summary with key insights and recommendations
|
| 42 |
-
|
| 43 |
-
Focus on actionable insights for business decision-making.
|
| 44 |
-
Parameters: {parameters}
|
| 45 |
-
""",
|
| 46 |
-
|
| 47 |
-
"churn_focus": f"""
|
| 48 |
-
Focus on customer churn analysis:
|
| 49 |
-
1) Predict customer churn with predict_customer_churn_hf(risk_threshold={parameters.get('risk_threshold', 0.6)})
|
| 50 |
-
2) Identify high-risk customers requiring immediate attention
|
| 51 |
-
3) Provide specific intervention strategies
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
Provide quick business insights:
|
| 58 |
-
1) Get model status with get_model_status()
|
| 59 |
-
2) Run limited churn analysis with predict_customer_churn_hf()
|
| 60 |
-
3) Summarize top 3 business priorities
|
| 61 |
|
| 62 |
-
Parameters: {parameters}
|
| 63 |
-
"""
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
goal = hf_goals.get(analysis_type, hf_goals["comprehensive"])
|
| 67 |
-
|
| 68 |
-
try:
|
| 69 |
result = agent.run(goal)
|
| 70 |
return result
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
return f"Analysis failed: {str(e)}. Please check your API key and
|
|
|
|
| 10 |
def create_rica_agent_hf():
|
| 11 |
"""Create RICA agent optimized for HF Spaces"""
|
| 12 |
|
| 13 |
+
# Check API key availability
|
| 14 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
if not api_key:
|
| 16 |
+
raise ValueError("OpenAI API key not configured")
|
| 17 |
+
|
| 18 |
# HF Spaces optimized tools
|
| 19 |
hf_tools = [
|
| 20 |
predict_customer_churn_hf,
|
|
|
|
| 24 |
try:
|
| 25 |
agent = CodeAgent(
|
| 26 |
tools=hf_tools,
|
| 27 |
+
model=OpenAI(api_key=api_key),
|
| 28 |
add_base_tools=False,
|
| 29 |
max_iterations=3 # Reduced for HF Spaces performance
|
| 30 |
)
|
|
|
|
| 35 |
def execute_rica_analysis_hf(analysis_type: str, parameters: dict = None):
|
| 36 |
"""Execute RICA analysis optimized for HF Spaces"""
|
| 37 |
|
| 38 |
+
# Check API key first
|
| 39 |
+
if not os.getenv("OPENAI_API_KEY"):
|
| 40 |
+
return "Error: OpenAI API key not configured. Please set your API key in the sidebar."
|
| 41 |
|
| 42 |
+
try:
|
| 43 |
+
agent = create_rica_agent_hf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Simplified goals for HF Spaces
|
| 46 |
+
hf_goals = {
|
| 47 |
+
"comprehensive": f"""
|
| 48 |
+
Execute business intelligence analysis:
|
| 49 |
+
1) Check model status with get_model_status()
|
| 50 |
+
2) Predict customer churn with predict_customer_churn_hf()
|
| 51 |
+
3) Provide executive summary with key insights and recommendations
|
| 52 |
+
|
| 53 |
+
Focus on actionable insights for business decision-making.
|
| 54 |
+
Parameters: {parameters}
|
| 55 |
+
""",
|
| 56 |
+
|
| 57 |
+
"churn_focus": f"""
|
| 58 |
+
Focus on customer churn analysis:
|
| 59 |
+
1) Predict customer churn with predict_customer_churn_hf(risk_threshold={parameters.get('risk_threshold', 0.6)})
|
| 60 |
+
2) Identify high-risk customers requiring immediate attention
|
| 61 |
+
3) Provide specific intervention strategies
|
| 62 |
+
|
| 63 |
+
Parameters: {parameters}
|
| 64 |
+
""",
|
| 65 |
+
|
| 66 |
+
"quick_insights": f"""
|
| 67 |
+
Provide quick business insights:
|
| 68 |
+
1) Get model status with get_model_status()
|
| 69 |
+
2) Run limited churn analysis with predict_customer_churn_hf()
|
| 70 |
+
3) Summarize top 3 business priorities
|
| 71 |
+
|
| 72 |
+
Parameters: {parameters}
|
| 73 |
+
"""
|
| 74 |
+
}
|
| 75 |
|
| 76 |
+
goal = hf_goals.get(analysis_type, hf_goals["comprehensive"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
result = agent.run(goal)
|
| 79 |
return result
|
| 80 |
+
|
| 81 |
except Exception as e:
|
| 82 |
+
return f"Analysis failed: {str(e)}. Please check your API key and model status."
|