| import dspy |
| import json |
| from typing import Literal |
|
|
| |
| def setup_dspy_classifier(save_path, api_key_path): |
| with open(api_key_path, "r") as f: |
| api_keys = json.load(f) |
|
|
| |
| |
| 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) |
|
|
| |
| classifier_instance = HealthLiteracyClassifier() |
| classifier_instance.load(save_path) |
| return classifier_instance |
|
|
| |
| API_FILE = "/home/mshahidul/api_new.json" |
| SAVE_PATH = "/home/mshahidul/readctrl/data/new_exp/optimized_health_classifier_gpt5-mini_v2.json" |
|
|
| |
| classifier = setup_dspy_classifier(SAVE_PATH, API_FILE) |