Spaces:
Sleeping
Sleeping
File size: 1,944 Bytes
824f395 8cb093a 824f395 ac60599 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 |
# 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)} |