Skip to main content

medeval-framework

PyPI version License: Apache 2.0 Python Support CI Pipeline

A rigorous, open-source Python evaluation framework designed to benchmark medical Large Language Models (LLMs) for clinical accuracy, hallucination rates, model calibration, and safety.


📖 Table of Contents


🌟 Key Features

  • Multi-Dataset Benchmarks: Out-of-the-box loaders for standardized medical datasets (MedQA, PubMedQA).
  • Dual Clinical Safety Audit:
    • Deterministic Checker: Fast regex scanning for explicit clinical contraindications in Sickle Cell Disease and Cardiology.
    • Semantic Safety Net: NLI-based hazard verification (SemanticSafetyChecker) to flag context-dependent medical hazards.
  • Unified Model & PEFT Connectors: Modular drivers to query API models (OpenAI) or execute local PyTorch/Transformers weights (Hugging Face) and PEFT/LoRA adapters smoothly.
  • NLP & Semantic Accuracy Engines: Exact Match comparison and BERTScore semantic similarity scoring (SemanticSimilarityScorer).
  • NLI Hallucination Detection: Cross-encoder Natural Language Inference (NLIHallucinationDetector) evaluating predictions (hypothesis) against authoritative clinical facts (ground_truth).
  • Advanced Calibration Suite: Vectorized calculation of Expected Calibration Error (ECE), Maximum Calibration Error (MCE), and Brier Score.
  • CLI & Report Generator: Command-line medeval interface and structured JSON report exporter for auditability.

⚙️ Installation

1. Install via Pip (Recommended)

medeval-framework is available on PyPI. You can install the core framework or include optional ML/NLP extras:

# Core installation (numpy-only, lightweight)
pip install medeval-framework

# Full ML & NLP stack (Transformers, PyTorch, evaluate, datasets, bert_score, peft)
pip install medeval-framework[nlp]

# Complete installation including development and testing tools
pip install medeval-framework[all]

2. Install from Source

For development or contributing:

# Clone the repository
git clone https://github.com/TeslaInch/medeval-framework.git
cd medeval-framework

# Install editable package with all extras
pip install -e ".[all]"

🚀 Quickstart

1. Command Line Interface (CLI)

Run evaluations directly from your terminal using the medeval command:

# Get full usage help and flag options
medeval --help

# Run evaluation on MedQA using OpenAI GPT-4o with Sickle Cell safety audit (default)
export OPENAI_API_KEY="your-api-key"
medeval --model gpt-4o --dataset medqa --limit 20 --output report.json

# Run evaluation on Hugging Face model or PEFT adapter with explicit safety selection
# Options for --safety: sickle_cell (default), none
medeval \
  --model "microsoft/Phi-3.5-mini-instruct" \
  --dataset medqa \
  --safety sickle_cell \
  --device "cuda:0" \
  --limit 50 \
  --output base_model_report.json

CLI Options Reference

Flag Description Default
--model (required) Model identifier (HuggingFace repo, PEFT adapter, gpt-4o, openai:..., or mock-...). -
--dataset (required) Benchmark dataset: medqa or pubmedqa. -
--output (required) File path where JSON report is saved. -
--safety Safety checker to run: sickle_cell (SCD contraindications) or none. sickle_cell
--device PyTorch device index (cpu, cuda:0, etc.). cpu
--limit Maximum number of samples to process. All
--no-hallucination Disable NLI hallucination check to increase speed. Enabled
--no-semantic-similarity Disable BERTScore semantic scoring to increase speed. Enabled
--trust-remote-code Enable trust_remote_code=True for Hugging Face models. False

2. Python Orchestration API

Create customized evaluation pipelines tailored to specific clinical domains. You can configure individual safety checkers (SickleCellSafetyChecker, CardiologySafetyChecker, SemanticSafetyChecker) or combine them into a composite SafetySuite:

from medeval.benchmark import BenchmarkLoader
from medeval.models.huggingface import HuggingFaceConnector
from medeval.runner import BenchmarkRunner
from medeval.safety import (
    SickleCellSafetyChecker,
    CardiologySafetyChecker,
    SemanticSafetyChecker,
    SafetySuite,
)
from medeval.report import export_report_to_json

# 1. Load benchmark dataset
loader = BenchmarkLoader(split="test", max_samples=10)
samples = loader.load_medqa()

# 2. Instantiate Model Connector (Hugging Face base or PEFT adapter)
model = HuggingFaceConnector(model_name="microsoft/Phi-3.5-mini-instruct", device="cuda:0")

# 3. Configure Safety Checker
# Option A: Domain-specific checker (e.g. Sickle Cell Disease only)
safety_checker = SickleCellSafetyChecker()

# Option B: Multi-domain SafetySuite (Sickle Cell + Cardiology + NLI Semantic Net)
# safety_suite = SafetySuite([
#     SickleCellSafetyChecker(),
#     CardiologySafetyChecker(),
#     SemanticSafetyChecker(device=0)
# ])

# 4. Initialize and execute BenchmarkRunner
runner = BenchmarkRunner(model=model, safety_checker=safety_checker, ignore_errors=True)
report = runner.run(samples)

# 5. Export structured JSON report
export_report_to_json(report, "evaluation_report.json")

📁 Repository Structure

medeval/
├── medeval/
│   ├── models/               # Model Connectors (Base, HF, PEFT, OpenAI, Mock)
│   ├── safety/               # Safety Checkers (SickleCell, Cardiology, Semantic, SafetySuite)
│   ├── accuracy.py           # Scorers (Exact Match, BERTScore F1)
│   ├── benchmark.py          # Benchmark Loaders (MedQA, PubMedQA)
│   ├── calibration.py        # Calibration Suite (ECE, MCE, Brier Score)
│   ├── hallucination.py      # NLI Cross-Encoder Hallucination Engine
│   ├── report.py             # Metric aggregation & JSON serialization
│   ├── runner.py             # BenchmarkRunner pipeline orchestrator
│   └── structures.py         # Data contracts (MedicalEvalSample & EvaluationReport)
├── tests/                    # 169 Unit & Integration Tests
├── pyproject.toml            # Ruff & Mypy configurations
├── setup.py                  # PyPI Packaging configuration
└── requirements.txt          # Package dependencies

🧪 Development & Testing

Ensure style alignment and type safety before submitting pull requests:

# Run full pytest test suite
pytest

# Run Ruff style & linting check
ruff check .

# Run Mypy static type verification
mypy medeval/

📄 License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Metadata

Release files for medeval-framework 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for medeval-framework 0.1.6
File Interpreter ABI Platform
medeval_framework-0.1.6-py3-none-any.whl Python 3 none any Details

Release files / medeval_framework-0.1.6-py3-none-any.whl

Download URL medeval_framework-0.1.6-py3-none-any.whl
Size 56.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fc3f53d88972847c3eb7657b252ffeba1a988498a83d2a00a8e28efa28c6994
BLAKE2b-256 checksum
How to use checksums
3a81bd21218ed50679860f67c1f3c67028a2e4d76da24879b05c72b34dc0d9f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Security for medeval-framework 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page