File size: 1,880 Bytes
89c6379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
models.py — Pydantic request / response models for the RareDx diagnosis API.
"""

from pydantic import BaseModel, Field
from typing import Optional


class DiagnoseRequest(BaseModel):
    note: str = Field(
        ...,
        min_length=10,
        description="Plain-text clinical note describing the patient's presentation.",
        examples=["18 year old male, extremely tall, displaced lens, heart murmur, scoliosis"],
    )
    top_n: int = Field(default=10, ge=1, le=50, description="Max candidates to return.")
    threshold: float = Field(
        default=0.55, ge=0.3, le=0.95,
        description="Minimum BioLORD cosine similarity to accept an HPO match.",
    )


class HPOMatch(BaseModel):
    phrase:  str
    hpo_id:  str
    term:    str
    score:   float


class Candidate(BaseModel):
    rank:               int
    orpha_code:         str
    name:               str
    definition:         Optional[str]
    rrf_score:          float
    graph_rank:         Optional[int]
    chroma_rank:        Optional[int]
    graph_matches:      Optional[int]    # number of HPO terms matched in graph
    chroma_sim:         Optional[float]  # cosine similarity from ChromaDB
    matched_hpo:        list[dict] = []  # HPO terms matched via graph
    hallucination_flag: bool = False
    flag_reason:        Optional[str] = None
    evidence_score:     float = 0.0


class DiagnoseResponse(BaseModel):
    note:               str
    phrases_extracted:  list[str]
    hpo_matches:        list[HPOMatch]
    hpo_ids_used:       list[str]
    candidates:         list[Candidate]  # all candidates (flag fields attached)
    passed_candidates:  list[Candidate]  # guard passed
    flagged_candidates: list[Candidate]  # guard flagged
    top_diagnosis:      Optional[Candidate]
    graph_backend:      str
    chroma_backend:     str
    elapsed_seconds:    float