File size: 2,925 Bytes
824f395
 
 
8cb093a
824f395
 
 
 
 
 
 
 
 
ae51ba3
824f395
 
ae51ba3
 
 
824f395
 
 
0bdb060
 
 
 
 
 
 
 
 
 
 
 
824f395
0bdb060
 
824f395
 
 
 
 
 
ae51ba3
0bdb060
 
 
ae51ba3
824f395
ae51ba3
824f395
 
 
 
 
ae51ba3
824f395
 
 
 
 
 
ae51ba3
824f395
ae51ba3
 
 
 
 
 
824f395
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# gpt_analyzer.py
import json
from openai import OpenAI
from typing import Dict, List, Any

class GPTAnalyzer:
    def __init__(self, api_key: str):
        self.client = OpenAI(api_key=api_key)
    
    def analyze_request(self, request_text: str, available_categories: List[str]) -> Dict[str, Any]:
        prompt = f"""
        As a hospital data analyst, analyze this data request:
        "{request_text}"

        Consider these available data sources in hospital Web Data system:
        {json.dumps(available_categories, indent=2, ensure_ascii=False)}

        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:

        {{
            "required_reports": [
                {{
                    "category": "OPD",
                    "report_type": "diagnosis",
                    "fields_needed": ["HN", "Patient_Name", "VisitDate", "Doctor_Name", "ICD_Code", "ICD_Name"],
                    "filters": {{
                        "date_range": "January 2024",
                        "other_filters": []
                    }}
                }},
                {{
                    "category": "OPD",
                    "report_type": "waiting_times",
                    "fields_needed": ["HN", "Wait_Time_Statistics"],
                    "filters": {{
                        "date_range": "January 2024",
                        "other_filters": []
                    }}
                }}
            ],
            "interpretation": "Brief explanation of what data is needed",
            "confidence_score": "HIGH/MEDIUM/LOW"
        }}

        IMPORTANT:
        - report_type must match exactly one of the available report names
        - fields_needed must match exactly the field names in the reports
        - Return only the JSON, no other text
        """

        try:
            response = self.client.chat.completions.create(
                messages=[
                    {
                        "role": "system",
                        "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."
                    },
                    {
                        "role": "user",
                        "content": prompt
                    }
                ],
                model="gpt-4o-mini",
            )
            # Extract the JSON string from the response
            response_text = response.choices[0].message.content.strip()
            # Parse the JSON string
            return json.loads(response_text)
        except json.JSONDecodeError as e:
            return {"error": f"Failed to parse GPT response as JSON: {str(e)}"}
        except Exception as e:
            return {"error": str(e)}