Spaces:
Sleeping
Sleeping
Create gpt_analyzer.py
Browse files- gpt_analyzer.py +53 -0
gpt_analyzer.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# gpt_analyzer.py
|
2 |
+
import json
|
3 |
+
from openai import OpenAI
|
4 |
+
from typing import Dict, 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}"
|
14 |
+
|
15 |
+
Consider these available data sources in hospital Web Data system:
|
16 |
+
{json.dumps(available_categories, indent=2, ensure_ascii=False)}
|
17 |
+
|
18 |
+
Return JSON with this structure:
|
19 |
+
{{
|
20 |
+
"required_reports": [
|
21 |
+
{{
|
22 |
+
"category": "Which category (OPD/IPD/PCT/etc)",
|
23 |
+
"report_type": "Specific report name needed",
|
24 |
+
"fields_needed": ["List of required fields"],
|
25 |
+
"filters": {{
|
26 |
+
"date_range": "Required date range if specified",
|
27 |
+
"other_filters": ["Other filters needed"]
|
28 |
+
}}
|
29 |
+
}}
|
30 |
+
],
|
31 |
+
"interpretation": "Brief explanation of what data is needed",
|
32 |
+
"confidence_score": "HIGH/MEDIUM/LOW"
|
33 |
+
}}
|
34 |
+
"""
|
35 |
+
|
36 |
+
try:
|
37 |
+
response = self.client.chat.completions.create(
|
38 |
+
messages=[
|
39 |
+
{
|
40 |
+
"role": "system",
|
41 |
+
"content": "You are a healthcare data analyst expert who understands hospital information systems."
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"role": "user",
|
45 |
+
"content": prompt
|
46 |
+
}
|
47 |
+
],
|
48 |
+
model="gpt-4",
|
49 |
+
response_format={ "type": "json_object" }
|
50 |
+
)
|
51 |
+
return json.loads(response.choices[0].message.content)
|
52 |
+
except Exception as e:
|
53 |
+
return {"error": str(e)}
|