Download src/llm.py from sarra2002/RareDx: direct link, hf CLI and curl.
- Browser
- Download file 6.15 kB
-
https://huggingface.co/spaces/sarra2002/RareDx/resolve/main/src/llm.py
- Command line
-
hf download hf://spaces/sarra2002/RareDx/src/llm.py
-
curl -L -o llm.py https://huggingface.co/spaces/sarra2002/RareDx/resolve/main/src/llm.py
6.15 kB
| """LLM branch: Groq calls with cache and quota handling, symptom extraction and 10 diagnoses.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import re | |
| import time | |
| from pathlib import Path | |
| from typing import Callable | |
| from .observability import Tracer | |
| EXTRACTION_PROMPT = """You are an expert in the Human Phenotype Ontology (HPO). | |
| Task: convert a clinical note (French or English) into the list of HPO term labels it describes. | |
| Rules: | |
| 1. One clinical sign per item. Split combined findings ("cleft lip and palate" -> two items). | |
| 2. Write each item as an HPO label: medical English, singular, capitalised first word, no sentence. | |
| 3. Be anatomically precise (duct vs gland, eye vs skin, absent vs partly missing). | |
| 4. Include abnormal lab results as signs (e.g. "lactates très élevés" -> "Lactic acidosis"). | |
| 5. Ignore age, sex, family names and anything that is not a clinical sign. | |
| 6. Never name a disease, syndrome or diagnosis. | |
| 7. Answer ONLY with a JSON list of strings. | |
| Examples: | |
| - "mou depuis la naissance" -> "Hypotonia" | |
| - "crises convulsives" -> "Seizure" | |
| - "very long, thin fingers" -> "Arachnodactyly" | |
| - "glycémie basse au bilan" -> "Hypoglycemia" | |
| """ | |
| DIAGNOSIS_PROMPT = ( | |
| "You are an expert in rare genetic and metabolic diseases. Given a patient's clinical features " | |
| "(HPO terms), list the 10 most likely rare disease diagnoses, most likely first. Use precise " | |
| "disease names, with the type or subtype when relevant. " | |
| 'Answer ONLY with JSON: {"diagnoses": ["name 1", "name 2", ..., "name 10"]}') | |
| class QuotaExceeded(Exception): | |
| """The daily quota of the free API is reached (the cache is kept).""" | |
| def read_json(text: str, default): | |
| m = re.search(r"\{.*\}|\[.*\]", text or "", re.S) | |
| try: | |
| return json.loads(m.group(0)) if m else default | |
| except json.JSONDecodeError: | |
| return default | |
| class LLMClient: | |
| """Temperature 0, every answer cached on disk (same cache as the notebook).""" | |
| def __init__(self, model: str, api_key: str | None = None, cache_path: Path | None = None, | |
| completion_fn: Callable[[str, str], tuple[str, dict]] | None = None, | |
| tracer: Tracer | None = None): | |
| self.model = model | |
| self.cache_path = Path(cache_path) if cache_path else None | |
| self.cache: dict[str, str] = (json.load(open(self.cache_path, encoding="utf-8")) | |
| if self.cache_path and self.cache_path.exists() else {}) | |
| self.tracer = tracer or Tracer(enabled=False) | |
| self._api_key = api_key | |
| self._completion_fn = completion_fn # injectable for tests: (system, content) -> (text, usage) | |
| self._client = None | |
| self.call = self.tracer.wrap(self.call, "appel LLM", as_type="generation") | |
| def _groq_completion(self, system: str, content: str) -> tuple[str, dict]: | |
| if self._client is None: | |
| from groq import Groq | |
| if not self._api_key: | |
| raise RuntimeError("GROQ_API_KEY is missing (set it as an environment variable).") | |
| self._client = Groq(api_key=self._api_key) | |
| rep = self._client.chat.completions.create( | |
| model=self.model, temperature=0, max_tokens=1500, reasoning_effort="low", | |
| messages=[{"role": "system", "content": system}, {"role": "user", "content": content}]) | |
| u = getattr(rep, "usage", None) | |
| return rep.choices[0].message.content or "", { | |
| "input": getattr(u, "prompt_tokens", 0), "output": getattr(u, "completion_tokens", 0)} | |
| def _key(self, system: str, content: str) -> str: | |
| return hashlib.sha256(f"{self.model}|{system}|{content}".encode()).hexdigest()[:20] | |
| def call(self, system: str, content: str, attempts: int = 6) -> str: | |
| key = self._key(system, content) | |
| self.tracer.update_generation(model=self.model, input={"consigne": system[:300], "contenu": content}) | |
| if key in self.cache: | |
| self.tracer.update_generation(output=self.cache[key], metadata={"cache": True}) | |
| return self.cache[key] | |
| completion = self._completion_fn or self._groq_completion | |
| for _ in range(attempts): | |
| try: | |
| text, usage = completion(system, content) | |
| self.tracer.update_generation(output=text, metadata={"cache": False}, usage_details=usage) | |
| if text: | |
| self.cache[key] = text | |
| if self.cache_path: | |
| json.dump(self.cache, open(self.cache_path, "w", encoding="utf-8"), ensure_ascii=False) | |
| return text | |
| except Exception as e: # noqa: BLE001 - errors from the provider are strings | |
| msg = str(e) | |
| if "429" in msg or "rate limit" in msg.lower(): | |
| if "per day" in msg.lower() or "TPD" in msg or "RPD" in msg: | |
| self.tracer.update_generation(level="ERROR", status_message="daily quota") | |
| raise QuotaExceeded("Daily Groq quota reached: try again later (the cache is kept).") | |
| time.sleep(60) | |
| elif "503" in msg or "502" in msg or "over capacity" in msg.lower(): | |
| time.sleep(30) | |
| else: | |
| self.tracer.update_generation(level="ERROR", status_message=msg[:200]) | |
| raise | |
| raise QuotaExceeded("Groq does not answer.") | |
| def extract_symptoms(self, note: str) -> list[str]: | |
| """Free-text note (French or English) -> list of HPO labels in English.""" | |
| res = read_json(self.call(EXTRACTION_PROMPT, note), []) | |
| return [str(x) for x in res] if isinstance(res, list) else [] | |
| def diagnoses(self, symptom_names: list[str]) -> list[str]: | |
| """Symptom names -> 10 disease names, most likely first.""" | |
| content = "Patient clinical features:\n" + "\n".join(f"- {n}" for n in symptom_names) | |
| res = read_json(self.call(DIAGNOSIS_PROMPT, content), {}) | |
| if not isinstance(res, dict): | |
| return [] | |
| return [str(x).strip() for x in res.get("diagnoses", []) if str(x).strip()][:10] | |