File size: 1,779 Bytes
030876e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import dspy
import json
from typing import Literal

# --- 1. LLM Configuration ---
def setup_dspy_classifier(save_path, api_key_path):
    with open(api_key_path, "r") as f:
        api_keys = json.load(f)

    # Configure the LM
    # Note: 'gpt-5-mini' is used per your configuration; ensure this matches your provider
    openai_model = dspy.LM(model='gpt-5-mini', api_key=api_keys["openai"])
    dspy.configure(lm=openai_model)

    class HealthLiteracySignature(dspy.Signature):
        """
        Judge the health literacy level of a generated medical summary.
        Identify if the language is suitable for a layperson (low) or requires medical expertise (proficient).
        """
        summary_text: str = dspy.InputField(desc="The generated medical summary to be analyzed.")
        reasoning: str = dspy.OutputField(desc="Analysis of jargon, acronyms, and sentence complexity.")
        label: Literal["low_health_literacy", "intermediate_health_literacy", "proficient_health_literacy"] = dspy.OutputField()

    class HealthLiteracyClassifier(dspy.Module):
        def __init__(self):
            super().__init__()
            self.predictor = dspy.ChainOfThought(HealthLiteracySignature)

        def forward(self, summary_text):
            return self.predictor(summary_text=summary_text)

    # Initialize and load weights
    classifier_instance = HealthLiteracyClassifier()
    classifier_instance.load(save_path)
    return classifier_instance

# Global instantiation (optional, or you can call setup in your main script)
API_FILE = "/home/mshahidul/api_new.json"
SAVE_PATH = "/home/mshahidul/readctrl/data/new_exp/optimized_health_classifier_gpt5-mini_v2.json"

# Create the instance to be imported
classifier = setup_dspy_classifier(SAVE_PATH, API_FILE)