RAG-HPO
Extract Human Phenotype Ontology (HPO) terms from clinical notes with a large language model and retrieval-augmented generation.
rag_hpo is a pip-installable Python package version of
RAG-HPO, the method by
Brandon T. Garcia and colleagues (Posey Lab, Baylor College of Medicine). The original
was a Jupyter notebook. This package turns it into a library you can import and call
from scripts or pipelines. It adds a pluggable embedding backend, an LLM client for any
OpenAI-compatible endpoint, and checkpointing so long runs can resume.
RAG-HPO is not an LLM itself. It drives an LLM that you provide (local or hosted). The model finds phenotype phrases in free text, and a vector index of the HPO grounds each phrase to a real HPO ID.
How it works
clinical note ──► LLM: extract phrases ──► keep "Abnormal" ──► FAISS: top-15 HPO candidates
│
┌────────────── local match? (bag-of-words / exact / fuzzy ≥ 80)
│ yes │ no
▼ ▼
HPO ID ◄────────────────────────────── LLM: pick one candidate
(local fallback if it fails)
- Extract: the LLM reads each note and returns phenotype phrases, each tagged
Abnormal,Normal,Family History,OtherorSuspected. OnlyAbnormalphrases go on to the next steps. - Retrieve: each phrase is embedded and matched against a FAISS index of every HPO label and synonym. The 15 most similar distinct terms become its candidates.
- Match locally: phrases that match a candidate closely enough get an HPO ID without another LLM call.
- Map with the LLM: for the remaining phrases, the LLM picks the best candidate, using the sentence the phrase came from as context.
- Compile: results are grouped per patient.
You need
- The HPO ontology in OBO format (
hp.obo), from the HPO release page orhttps://purl.obolibrary.org/obo/hp.obo. - An LLM endpoint that speaks the OpenAI chat-completions API. The default is a local Ollama server. Groq, OpenAI, vLLM and LM Studio also work.
Quickstart
from pathlib import Path
import pronto
from rag_hpo.embedding_model import SentenceTransformerModel
from rag_hpo.llm_client import LLMClient
from rag_hpo.ontology import Ontology
from rag_hpo.rag_hpo import RAGHPO
from rag_hpo.hit import Hit
embedding_model = SentenceTransformerModel() # SapBERT-based biomedical model
llm = LLMClient() # Ollama at localhost:11434, model qwen3.5:4b
# One-time: build the HPO vector database (repeat when HPO releases a new version)
ontology = pronto.Ontology("hp.obo")
rag = RAGHPO.inti_and_create_embeddings(
ontology,
embedding_model,
llm,
hp_meta_file=Path("hpo_meta.json"),
emb_file=Path("hpo_embedded.npz"),
)
# Later runs: load the existing database
rag = RAGHPO(embedding_model, llm, "hpo_meta.json", "hpo_embedded.npz")
results: dict[str, list[Hit]] = rag.extract(
[
"The patient has recurrent seizures and global developmental delay. Hearing is normal."
]
)
print(results["0"].hpo_id)
To process a CSV of notes, pass a DataFrame with a clinical_note column and,
optionally, a patient_id column:
import pandas as pd
notes = pd.read_csv("notes.csv") # columns: patient_id, clinical_note
results = rag.extract(notes, checkpoint_dir="checkpoints")
Using a hosted LLM (Groq)
import os
llm = LLMClient.groq(api_key=os.environ["GROQ_API_KEY"]) # llama-3.3-70b-versatile
LLMClient.groq() applies Groq's free-tier limits: 30 requests per minute and 500,000
tokens per day.
Output
extract() returns a dict[str, list[Hit]] where the key represents a patient.
{
"1": [
{
"phrase": "poor eye contact",
"category": "Abnormal",
"hpo_id": "HP:0000817"
},
{
"phrase": "marked autistic behaviors",
"category": "Abnormal",
"hpo_id": "No Candidate Fit"
}
]
}
hpo_id is an HPO ID such as HP:0001250, or the string "No Candidate Fit" if
nothing could be mapped. Patients with no abnormal findings are left out.
Citation
If you use RAG-HPO in your work, please cite the original publication:
Garcia BT, et al. Improving Automated Deep Phenotyping Through Large Language Models Using Retrieval-Augmented Generation. Genome Medicine (2025). doi:10.1186/s13073-025-01521-w
License
MIT. See LICENSE. The original RAG-HPO is © 2024 Jennifer Posey Lab, Baylor College of Medicine.
Metadata
Release files for RAG-HPO 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| rag_hpo-0.1.2.tar.gz | 109.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rag_hpo-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 216.5 kB
Release files / rag_hpo-0.1.2.tar.gz
| Download URL | rag_hpo-0.1.2.tar.gz |
|---|---|
| Size | 109.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e0e6cda91025128fa5e186c9da14bc73ecd815fe23176ec9439b34e9abd81473
|
|
BLAKE2b-256 checksum How to use checksums |
3d29927b96f0150f66fc657b6a33d9bfaf8de5f47a26b0bcbc537c93737f5ed3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logRelease files / rag_hpo-0.1.2-py3-none-any.whl
| Download URL | rag_hpo-0.1.2-py3-none-any.whl |
|---|---|
| Size | 107.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f29a582ee095c43415316d44e27efeddf59ee5bdc55f98d1dc45d2412f4c65c7
|
|
BLAKE2b-256 checksum How to use checksums |
6df04ae84aadc89341ac9430282ba50978b31176bf7a18fd93d81d6a9dab2284
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logSecurity for RAG-HPO 0.1.2
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for rag_hpo-0.1.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logA 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.