Spaces:
Runtime error
Runtime error
Update simplified_planning_agent.py
Browse files- simplified_planning_agent.py +49 -29
simplified_planning_agent.py
CHANGED
|
@@ -35,11 +35,11 @@ class PlanningAgent:
|
|
| 35 |
print("Planning Agent initialized successfully")
|
| 36 |
|
| 37 |
def create_analysis_plan(self, alert_description: str) -> Tuple[AnalysisPlan, Dict]:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
Your task is to create a detailed analysis plan to investigate sales anomalies.
|
| 44 |
|
| 45 |
For pharmaceutical sales analysis:
|
|
@@ -84,32 +84,52 @@ Your output should be a complete JSON-formatted analysis plan following this str
|
|
| 84 |
|
| 85 |
Be thorough but focus on creating a practical analysis workflow.
|
| 86 |
"""
|
| 87 |
-
|
| 88 |
-
user_message = f"Create an analysis plan for the following alert: {alert_description}"
|
| 89 |
-
|
| 90 |
-
# Make direct API call to Claude
|
| 91 |
-
try:
|
| 92 |
-
import anthropic
|
| 93 |
-
client = anthropic.Anthropic(api_key=self.api_key)
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
try:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
def extract_json_from_text(self, text: str) -> Dict:
|
| 115 |
"""Extract JSON from text that might contain additional content"""
|
|
|
|
| 35 |
print("Planning Agent initialized successfully")
|
| 36 |
|
| 37 |
def create_analysis_plan(self, alert_description: str) -> Tuple[AnalysisPlan, Dict]:
|
| 38 |
+
"""Generate an analysis plan based on the alert description"""
|
| 39 |
+
print("Planning Agent: Creating analysis plan...")
|
| 40 |
+
|
| 41 |
+
# Create the system prompt and user message
|
| 42 |
+
system_prompt = """You are an expert pharmaceutical analytics planning agent.
|
| 43 |
Your task is to create a detailed analysis plan to investigate sales anomalies.
|
| 44 |
|
| 45 |
For pharmaceutical sales analysis:
|
|
|
|
| 84 |
|
| 85 |
Be thorough but focus on creating a practical analysis workflow.
|
| 86 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
user_message = f"Create an analysis plan for the following alert: {alert_description}"
|
| 89 |
+
|
| 90 |
+
# Make direct API call to Claude
|
| 91 |
try:
|
| 92 |
+
import anthropic
|
| 93 |
+
client = anthropic.Anthropic(api_key=self.api_key)
|
| 94 |
+
|
| 95 |
+
# Use the correct API structure based on the Anthropic Python SDK version
|
| 96 |
+
try:
|
| 97 |
+
# For newer versions of the Anthropic SDK
|
| 98 |
+
response = client.messages.create(
|
| 99 |
+
model="claude-3-haiku-20240307",
|
| 100 |
+
max_tokens=2000,
|
| 101 |
+
temperature=0.2,
|
| 102 |
+
system=system_prompt,
|
| 103 |
+
messages=[
|
| 104 |
+
{"role": "user", "content": user_message}
|
| 105 |
+
]
|
| 106 |
+
)
|
| 107 |
+
except TypeError:
|
| 108 |
+
# Fallback for older versions of the Anthropic SDK
|
| 109 |
+
response = client.messages.create(
|
| 110 |
+
model="claude-3-haiku-20240307",
|
| 111 |
+
max_tokens=2000,
|
| 112 |
+
temperature=0.2,
|
| 113 |
+
messages=[
|
| 114 |
+
{"role": "system", "content": system_prompt},
|
| 115 |
+
{"role": "user", "content": user_message}
|
| 116 |
+
]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Extract response content
|
| 120 |
+
response_text = response.content[0].text
|
| 121 |
+
|
| 122 |
+
# Extract JSON from the response
|
| 123 |
+
plan_dict = self.extract_json_from_text(response_text)
|
| 124 |
+
|
| 125 |
+
# Convert to Pydantic model for validation
|
| 126 |
+
analysis_plan = AnalysisPlan.model_validate(plan_dict)
|
| 127 |
+
|
| 128 |
+
return analysis_plan, plan_dict
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"Error creating analysis plan: {e}")
|
| 132 |
+
raise
|
| 133 |
|
| 134 |
def extract_json_from_text(self, text: str) -> Dict:
|
| 135 |
"""Extract JSON from text that might contain additional content"""
|