# 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)} Return JSON with this structure: {{ "required_reports": [ {{ "category": "Which category (OPD/IPD/PCT/etc)", "report_type": "Specific report name needed", "fields_needed": ["List of required fields"], "filters": {{ "date_range": "Required date range if specified", "other_filters": ["Other filters needed"] }} }} ], "interpretation": "Brief explanation of what data is needed", "confidence_score": "HIGH/MEDIUM/LOW" }} """ try: response = self.client.chat.completions.create( messages=[ { "role": "system", "content": "You are a healthcare data analyst expert who understands hospital information systems." }, { "role": "user", "content": prompt } ], model="gpt-4o-mini", response_format={ "type": "json_object" } ) return json.loads(response.choices[0].message.content) except Exception as e: return {"error": str(e)}