Spaces:
Sleeping
Sleeping
File size: 2,856 Bytes
824f395 8cb093a 824f395 95c7c2d 824f395 95c7c2d 824f395 ae51ba3 824f395 ae51ba3 824f395 0bdb060 824f395 ae51ba3 0bdb060 ae51ba3 824f395 ae51ba3 824f395 95c7c2d 824f395 ae51ba3 824f395 ae51ba3 824f395 95c7c2d ae51ba3 95c7c2d ae51ba3 95c7c2d ae51ba3 95c7c2d ae51ba3 824f395 95c7c2d 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 |
# gpt_analyzer.py
import json
from openai import OpenAI
from typing import Dict, List, Any
class GPTAnalyzer:
def __init__(self, api_key: str):
print("Initializing GPT Analyzer")
self.client = OpenAI(api_key=api_key)
def analyze_request(self, request_text: str, available_categories: List[str]) -> Dict[str, Any]:
print(f"Analyzing request with available categories: {available_categories}")
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": []
}}
}}
],
"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:
print("Sending request to GPT")
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",
)
response_text = response.choices[0].message.content.strip()
print(f"GPT Response received: {response_text}")
return json.loads(response_text)
except json.JSONDecodeError as e:
print(f"JSON Parse Error: {str(e)}")
return {"error": f"Failed to parse GPT response as JSON: {str(e)}"}
except Exception as e:
print(f"General Error: {str(e)}")
return {"error": str(e)} |