Spaces:
Sleeping
Sleeping
Update gpt_analyzer.py
Browse files- gpt_analyzer.py +10 -11
gpt_analyzer.py
CHANGED
@@ -5,9 +5,12 @@ from typing import Dict, List, Any
|
|
5 |
|
6 |
class GPTAnalyzer:
|
7 |
def __init__(self, api_key: str):
|
|
|
8 |
self.client = OpenAI(api_key=api_key)
|
9 |
|
10 |
def analyze_request(self, request_text: str, available_categories: List[str]) -> Dict[str, Any]:
|
|
|
|
|
11 |
prompt = f"""
|
12 |
As a hospital data analyst, analyze this data request:
|
13 |
"{request_text}"
|
@@ -27,15 +30,6 @@ class GPTAnalyzer:
|
|
27 |
"date_range": "January 2024",
|
28 |
"other_filters": []
|
29 |
}}
|
30 |
-
}},
|
31 |
-
{{
|
32 |
-
"category": "OPD",
|
33 |
-
"report_type": "waiting_times",
|
34 |
-
"fields_needed": ["HN", "Wait_Time_Statistics"],
|
35 |
-
"filters": {{
|
36 |
-
"date_range": "January 2024",
|
37 |
-
"other_filters": []
|
38 |
-
}}
|
39 |
}}
|
40 |
],
|
41 |
"interpretation": "Brief explanation of what data is needed",
|
@@ -49,6 +43,7 @@ class GPTAnalyzer:
|
|
49 |
"""
|
50 |
|
51 |
try:
|
|
|
52 |
response = self.client.chat.completions.create(
|
53 |
messages=[
|
54 |
{
|
@@ -62,11 +57,15 @@ class GPTAnalyzer:
|
|
62 |
],
|
63 |
model="gpt-4o-mini",
|
64 |
)
|
65 |
-
|
66 |
response_text = response.choices[0].message.content.strip()
|
67 |
-
|
|
|
68 |
return json.loads(response_text)
|
|
|
69 |
except json.JSONDecodeError as e:
|
|
|
70 |
return {"error": f"Failed to parse GPT response as JSON: {str(e)}"}
|
71 |
except Exception as e:
|
|
|
72 |
return {"error": str(e)}
|
|
|
5 |
|
6 |
class GPTAnalyzer:
|
7 |
def __init__(self, api_key: str):
|
8 |
+
print("Initializing GPT Analyzer")
|
9 |
self.client = OpenAI(api_key=api_key)
|
10 |
|
11 |
def analyze_request(self, request_text: str, available_categories: List[str]) -> Dict[str, Any]:
|
12 |
+
print(f"Analyzing request with available categories: {available_categories}")
|
13 |
+
|
14 |
prompt = f"""
|
15 |
As a hospital data analyst, analyze this data request:
|
16 |
"{request_text}"
|
|
|
30 |
"date_range": "January 2024",
|
31 |
"other_filters": []
|
32 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
}}
|
34 |
],
|
35 |
"interpretation": "Brief explanation of what data is needed",
|
|
|
43 |
"""
|
44 |
|
45 |
try:
|
46 |
+
print("Sending request to GPT")
|
47 |
response = self.client.chat.completions.create(
|
48 |
messages=[
|
49 |
{
|
|
|
57 |
],
|
58 |
model="gpt-4o-mini",
|
59 |
)
|
60 |
+
|
61 |
response_text = response.choices[0].message.content.strip()
|
62 |
+
print(f"GPT Response received: {response_text}")
|
63 |
+
|
64 |
return json.loads(response_text)
|
65 |
+
|
66 |
except json.JSONDecodeError as e:
|
67 |
+
print(f"JSON Parse Error: {str(e)}")
|
68 |
return {"error": f"Failed to parse GPT response as JSON: {str(e)}"}
|
69 |
except Exception as e:
|
70 |
+
print(f"General Error: {str(e)}")
|
71 |
return {"error": str(e)}
|