Spaces:
Sleeping
Sleeping
Update gpt_analyzer.py
Browse files- gpt_analyzer.py +15 -10
gpt_analyzer.py
CHANGED
@@ -11,11 +11,12 @@ class GPTAnalyzer:
|
|
11 |
prompt = f"""
|
12 |
As a hospital data analyst, analyze this data request:
|
13 |
"{request_text}"
|
14 |
-
|
15 |
Consider these available data sources in hospital Web Data system:
|
16 |
{json.dumps(available_categories, indent=2, ensure_ascii=False)}
|
17 |
-
|
18 |
-
|
|
|
19 |
{{
|
20 |
"required_reports": [
|
21 |
{{
|
@@ -40,28 +41,32 @@ class GPTAnalyzer:
|
|
40 |
"interpretation": "Brief explanation of what data is needed",
|
41 |
"confidence_score": "HIGH/MEDIUM/LOW"
|
42 |
}}
|
43 |
-
|
44 |
IMPORTANT:
|
45 |
- report_type must match exactly one of the available report names
|
46 |
- fields_needed must match exactly the field names in the reports
|
47 |
-
-
|
48 |
"""
|
49 |
-
|
50 |
try:
|
51 |
response = self.client.chat.completions.create(
|
52 |
messages=[
|
53 |
{
|
54 |
"role": "system",
|
55 |
-
"content": "You are a healthcare data analyst expert who understands hospital information systems. Always return exact matching report names and field names."
|
56 |
},
|
57 |
{
|
58 |
"role": "user",
|
59 |
"content": prompt
|
60 |
}
|
61 |
],
|
62 |
-
model="gpt-
|
63 |
-
response_format={ "type": "json_object" }
|
64 |
)
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
except Exception as e:
|
67 |
return {"error": str(e)}
|
|
|
11 |
prompt = f"""
|
12 |
As a hospital data analyst, analyze this data request:
|
13 |
"{request_text}"
|
14 |
+
|
15 |
Consider these available data sources in hospital Web Data system:
|
16 |
{json.dumps(available_categories, indent=2, ensure_ascii=False)}
|
17 |
+
|
18 |
+
You must return a response that exactly matches this JSON structure. Fields must exactly match the available report fields. DO NOT include any other text in your response except the JSON:
|
19 |
+
|
20 |
{{
|
21 |
"required_reports": [
|
22 |
{{
|
|
|
41 |
"interpretation": "Brief explanation of what data is needed",
|
42 |
"confidence_score": "HIGH/MEDIUM/LOW"
|
43 |
}}
|
44 |
+
|
45 |
IMPORTANT:
|
46 |
- report_type must match exactly one of the available report names
|
47 |
- fields_needed must match exactly the field names in the reports
|
48 |
+
- Return only the JSON, no other text
|
49 |
"""
|
50 |
+
|
51 |
try:
|
52 |
response = self.client.chat.completions.create(
|
53 |
messages=[
|
54 |
{
|
55 |
"role": "system",
|
56 |
+
"content": "You are a healthcare data analyst expert who understands hospital information systems. Always return exact matching report names and field names. Return only JSON, no other text."
|
57 |
},
|
58 |
{
|
59 |
"role": "user",
|
60 |
"content": prompt
|
61 |
}
|
62 |
],
|
63 |
+
model="gpt-4o-mini",
|
|
|
64 |
)
|
65 |
+
# Extract the JSON string from the response
|
66 |
+
response_text = response.choices[0].message.content.strip()
|
67 |
+
# Parse the JSON string
|
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)}
|